23 #ifndef HIP_SRC_HIP_HCC_INTERNAL_H 24 #define HIP_SRC_HIP_HCC_INTERNAL_H 28 #include <unordered_map> 31 #include "hsa/hsa_ext_amd.h" 32 #include "hip/hip_runtime.h" 37 #if (__hcc_workweek__ < 16354) 38 #error("This version of HIP requires a newer version of HCC."); 43 #if defined(__HCC_HAS_EXTENDED_AM_MEMTRACKER_UPDATE) and \ 44 (__HCC_HAS_EXTENDED_AM_MEMTRACKER_UPDATE != 0) 45 #define USE_APP_PTR_FOR_CTX 1 57 extern const int release;
61 extern int HIP_LAUNCH_BLOCKING;
62 extern int HIP_API_BLOCKING;
64 extern int HIP_PRINT_ENV;
65 extern int HIP_PROFILE_API;
69 extern int HIP_STAGING_SIZE;
70 extern int HIP_STREAM_SIGNALS;
71 extern int HIP_VISIBLE_DEVICES;
72 extern int HIP_FORCE_P2P_HOST;
74 extern int HIP_HOST_COHERENT;
76 extern int HIP_HIDDEN_FREE_MEM;
79 extern int HIP_SYNC_HOST_ALLOC;
80 extern int HIP_SYNC_STREAM_WAIT;
82 extern int HIP_SYNC_NULL_STREAM;
83 extern int HIP_INIT_ALLOC;
84 extern int HIP_FORCE_NULL_STREAM;
86 extern int HIP_SYNC_FREE;
88 extern int HIP_DUMP_CODE_OBJECT;
91 extern int HCC_OPT_FLUSH;
98 int tid()
const {
return _shortTid; };
99 pid_t pid()
const {
return _pid; };
100 uint64_t incApiSeqNum() {
return ++_apiSeqNum; };
101 uint64_t apiSeqNum()
const {
return _apiSeqNum; };
112 static const uint64_t MAX_TRIGGER = std::numeric_limits<uint64_t>::max();
114 void print(
int tid) {
115 std::cout <<
"Enabling tracing for ";
116 for (
auto iter = _profTrigger.begin(); iter != _profTrigger.end(); iter++) {
117 std::cout <<
"tid:" << tid <<
"." << *iter <<
",";
122 uint64_t nextTrigger() {
return _profTrigger.empty() ? MAX_TRIGGER : _profTrigger.back(); };
123 void add(uint64_t trigger) { _profTrigger.push_back(trigger); };
124 void sort() { std::sort(_profTrigger.begin(), _profTrigger.end(), std::greater<int>()); };
127 std::vector<uint64_t> _profTrigger;
133 extern thread_local hipError_t tls_lastHipError;
134 extern thread_local
TidInfo tls_tidInfo;
135 extern thread_local
bool tls_getPrimaryCtx;
137 extern std::vector<ProfTrigger> g_dbStartTriggers;
138 extern std::vector<ProfTrigger> g_dbStopTriggers;
148 #define KNRM "\x1B[0m" 149 #define KRED "\x1B[31m" 150 #define KGRN "\x1B[32m" 151 #define KYEL "\x1B[33m" 152 #define KBLU "\x1B[34m" 153 #define KMAG "\x1B[35m" 154 #define KCYN "\x1B[36m" 155 #define KWHT "\x1B[37m" 157 extern const char* API_COLOR;
158 extern const char* API_COLOR_END;
164 #define EVENT_THREAD_SAFE 1 166 #define STREAM_THREAD_SAFE 1 168 #define CTX_THREAD_SAFE 1 170 #define DEVICE_THREAD_SAFE 1 175 #define COMPILE_HIP_DB 1 183 #define COMPILE_HIP_TRACE_API 0x3 188 #ifndef COMPILE_HIP_ATP_MARKER 189 #define COMPILE_HIP_ATP_MARKER 0 198 #if COMPILE_HIP_ATP_MARKER 199 #include "CXLActivityLogger.h" 200 #define MARKER_BEGIN(markerName, group) amdtBeginMarker(markerName, group, nullptr); 201 #define MARKER_END() amdtEndMarker(); 202 #define RESUME_PROFILING amdtResumeProfiling(AMDT_ALL_PROFILING); 203 #define STOP_PROFILING amdtStopProfiling(AMDT_ALL_PROFILING); 206 #define MARKER_BEGIN(markerName, group) 208 #define RESUME_PROFILING 209 #define STOP_PROFILING 215 #define TRACE_ALL 0 // 0x01 216 #define TRACE_KCMD 1 // 0x02, kernel command 217 #define TRACE_MCMD 2 // 0x04, memory command 218 #define TRACE_MEM 3 // 0x08, memory allocation or deallocation. 219 #define TRACE_SYNC 4 // 0x10, synchronization (host or hipStreamWaitEvent) 220 #define TRACE_QUERY 5 // 0x20, hipEventRecord, hipEventQuery, hipStreamQuery 231 #define DB_MAX_FLAG 6 238 const char* _shortName;
242 static const DbName dbName[] = {
244 {KYEL,
"sync"}, {KCYN,
"mem"}, {KMAG,
"copy"}, {KRED,
"warn"},
250 #define tprintf(trace_level, ...) \ 252 if (HIP_DB & (1 << (trace_level))) { \ 254 snprintf(msgStr, sizeof(msgStr), __VA_ARGS__); \ 255 fprintf(stderr, " %ship-%s pid:%d tid:%d:%s%s", dbName[trace_level]._color, \ 256 dbName[trace_level]._shortName, tls_tidInfo.pid(), tls_tidInfo.tid(), msgStr, KNRM); \ 261 #define tprintf(trace_level, ...) 265 static inline uint64_t getTicks() {
return hc::get_system_ticks(); }
268 extern uint64_t recordApiTrace(std::string* fullStr,
const std::string& apiStr);
270 #if COMPILE_HIP_ATP_MARKER || (COMPILE_HIP_TRACE_API & 0x1) 271 #define API_TRACE(forceTrace, ...) \ 272 uint64_t hipApiStartTick = 0; \ 274 tls_tidInfo.incApiSeqNum(); \ 276 (HIP_PROFILE_API || (COMPILE_HIP_DB && (HIP_TRACE_API & (1 << TRACE_ALL))))) { \ 277 std::string apiStr = std::string(__func__) + " (" + ToString(__VA_ARGS__) + ')'; \ 278 std::string fullStr; \ 279 hipApiStartTick = recordApiTrace(&fullStr, apiStr); \ 280 if (HIP_PROFILE_API == 0x1) { \ 281 MARKER_BEGIN(__func__, "HIP") \ 282 } else if (HIP_PROFILE_API == 0x2) { \ 283 MARKER_BEGIN(fullStr.c_str(), "HIP"); \ 290 #define API_TRACE(IS_CMD, ...) tls_tidInfo.incApiSeqNum(); 293 #define HIP_SET_DEVICE() ihipDeviceSetState(); 298 #define HIP_INIT_API(cid, ...) \ 299 hip_impl::hip_init(); \ 300 API_TRACE(0, __VA_ARGS__); \ 301 HIP_CB_SPAWNER_OBJECT(cid); 307 #define HIP_INIT_SPECIAL_API(cid, tbit, ...) \ 308 hip_impl::hip_init(); \ 309 API_TRACE((HIP_TRACE_API & (1 << tbit)), __VA_ARGS__); \ 310 HIP_CB_SPAWNER_OBJECT(cid); 316 #define ihipLogStatus(hipStatus) \ 318 hipError_t localHipStatus = hipStatus; \ 319 tls_lastHipError = localHipStatus; \ 321 if ((COMPILE_HIP_TRACE_API & 0x2) && HIP_TRACE_API & (1 << TRACE_ALL)) { \ 322 auto ticks = getTicks() - hipApiStartTick; \ 323 fprintf(stderr, " %ship-api pid:%d tid:%d.%lu %-30s ret=%2d (%s)>> +%lu ns%s\n", \ 324 (localHipStatus == 0) ? API_COLOR : KRED, tls_tidInfo.pid(), tls_tidInfo.tid(), \ 325 tls_tidInfo.apiSeqNum(), __func__, localHipStatus, \ 326 ihipErrorString(localHipStatus), ticks, API_COLOR_END); \ 328 if (HIP_PROFILE_API) { \ 358 #define HIP_IPC_RESERVED_SIZE 24 365 char reserved[HIP_IPC_RESERVED_SIZE];
370 std::string fileName;
371 hsa_executable_t executable = {};
372 hsa_code_object_reader_t coReader = {};
376 if (executable.handle) hsa_executable_destroy(executable);
377 if (coReader.handle) hsa_code_object_reader_destroy(coReader);
387 bool try_lock() {
return true; }
391 #if EVENT_THREAD_SAFE 392 typedef std::mutex EventMutex;
394 #warning "Stream thread-safe disabled" 398 #if STREAM_THREAD_SAFE 399 typedef std::mutex StreamMutex;
401 #warning "Stream thread-safe disabled" 407 typedef std::mutex CtxMutex;
410 #warning "Ctx thread-safe disabled" 413 #if DEVICE_THREAD_SAFE 414 typedef std::mutex DeviceMutex;
417 #warning "Device thread-safe disabled" 424 template <
typename T>
428 : _criticalData(&criticalData),
429 _autoUnlock(autoUnlock)
432 tprintf(DB_SYNC,
"locking criticalData=%p for %s..\n", _criticalData,
433 ToString(_criticalData->_parent).c_str());
434 _criticalData->_mutex.lock();
439 tprintf(DB_SYNC,
"auto-unlocking criticalData=%p for %s...\n", _criticalData,
440 ToString(_criticalData->_parent).c_str());
441 _criticalData->_mutex.unlock();
446 tprintf(DB_SYNC,
"unlocking criticalData=%p for %s...\n", _criticalData,
447 ToString(_criticalData->_parent).c_str());
448 _criticalData->_mutex.unlock();
452 T* operator->() {
return _criticalData; };
460 template <
typename MUTEX_TYPE>
464 void lock() { _mutex.lock(); }
465 void unlock() { _mutex.unlock(); }
466 bool try_lock() {
return _mutex.try_lock(); }
472 template <
typename MUTEX_TYPE>
476 : _kernelCnt(0), _av(av), _parent(parentStream){};
486 tprintf(DB_SYNC,
"munlocking criticalData=%p for %s...\n",
this,
487 ToString(this->_parent).c_str());
493 tprintf(DB_SYNC,
"mtry_locking=%d criticalData=%p for %s...\n", gotLock,
this,
494 ToString(this->_parent).c_str());
495 return gotLock ?
this :
nullptr;
502 hc::accelerator_view _av;
523 enum ScheduleMode { Auto, Spin, Yield };
524 typedef uint64_t SeqNum_t;
531 void locked_copySync(
void* dst,
const void* src,
size_t sizeBytes,
unsigned kind,
532 bool resolveOn =
true);
534 bool locked_copy2DSync(
void* dst,
const void* src,
size_t width,
size_t height,
size_t srcPitch,
size_t dstPitch,
unsigned kind,
535 bool resolveOn =
true);
537 void locked_copyAsync(
void* dst,
const void* src,
size_t sizeBytes,
unsigned kind);
539 bool locked_copy2DAsync(
void* dst,
const void* src,
size_t width,
size_t height,
size_t srcPitch,
size_t dstPitch,
unsigned kind);
541 void lockedSymbolCopySync(hc::accelerator& acc,
void* dst,
void* src,
size_t sizeBytes,
542 size_t offset,
unsigned kind);
543 void lockedSymbolCopyAsync(hc::accelerator& acc,
void* dst,
void* src,
size_t sizeBytes,
544 size_t offset,
unsigned kind);
550 void lockclose_postKernelCommand(
const char* kernelName, hc::accelerator_view* av);
555 hc::accelerator_view* locked_getAv() {
561 hc::completion_future locked_recordEvent(
hipEvent_t event);
564 void locked_eventWaitComplete(hc::completion_future& marker, hc::hcWaitMode waitMode);
569 hc::hcWaitMode waitMode()
const;
574 void launchModuleKernel(hc::accelerator_view av, hsa_signal_t signal, uint32_t blockDimX,
575 uint32_t blockDimY, uint32_t blockDimZ, uint32_t gridDimX,
576 uint32_t gridDimY, uint32_t gridDimZ, uint32_t groupSegmentSize,
577 uint32_t sharedMemBytes,
void* kernarg,
size_t kernSize,
588 bool isDefaultStream()
const {
return _id == 0; };
599 unsigned resolveMemcpyDirection(
bool srcInDeviceMem,
bool dstInDeviceMem);
600 void resolveHcMemcpyDirection(
unsigned hipMemKind,
const hc::AmPointerInfo* dstPtrInfo,
601 const hc::AmPointerInfo* srcPtrInfo, hc::hcCommandKind* hcCopyDir,
602 ihipCtx_t** copyDevice,
bool* forceUnpinnedCopy);
604 bool canSeeMemory(
const ihipCtx_t* thisCtx,
const hc::AmPointerInfo* dstInfo,
605 const hc::AmPointerInfo* srcInfo);
607 void addSymbolPtrToTracker(hc::accelerator& acc,
void* ptr,
size_t sizeBytes);
615 std::mutex _hasQueueLock;
620 friend std::ostream& operator<<(std::ostream& os,
const ihipStream_t& s);
623 ScheduleMode _scheduleMode;
632 : _stream(stream), _callback(callback), _userData(userData) {
642 enum hipEventStatus_t {
643 hipEventStatusUnitialized = 0,
644 hipEventStatusCreated = 1,
645 hipEventStatusRecording = 2,
646 hipEventStatusComplete = 3,
650 enum ihipEventType_t {
651 hipEventTypeIndependent,
652 hipEventTypeStartCommand,
653 hipEventTypeStopCommand,
659 _state = hipEventStatusCreated;
662 _type = hipEventTypeIndependent;
665 void marker(
const hc::completion_future& marker) { _marker = marker; };
666 hc::completion_future& marker() {
return _marker; }
667 uint64_t timestamp()
const {
return _timestamp; };
668 ihipEventType_t type()
const {
return _type; };
670 ihipEventType_t _type;
671 hipEventStatus_t _state;
676 hc::completion_future _marker;
682 template <
typename MUTEX_TYPE>
705 void attachToCompletionFuture(
const hc::completion_future* cf,
hipStream_t stream,
706 ihipEventType_t eventType);
707 std::pair<hipEventStatus_t, uint64_t> refreshEventStatus();
713 return _criticalData._eventData;
730 template <
typename MUTEX_TYPE>
734 : _parent(parentDevice), _ctxCount(0){};
741 std::list<ihipCtx_t*>& ctxs() {
return _ctxs; };
742 const std::list<ihipCtx_t*>& const_ctxs()
const {
return _ctxs; };
743 int getcount() {
return _ctxCount; };
750 std::list<ihipCtx_t*> _ctxs;
764 ihipDevice_t(
unsigned deviceId,
unsigned deviceCnt, hc::accelerator& acc);
768 ihipCtx_t* getPrimaryCtx()
const {
return _primaryCtx; };
776 hc::accelerator _acc;
777 hsa_agent_t _hsaAgent;
806 std::vector<char> _arguments;
811 template <
typename MUTEX_TYPE>
815 : _parent(parentCtx), _peerCnt(0) {
816 _peerAgents =
new hsa_agent_t[deviceCnt];
820 if (_peerAgents !=
nullptr) {
822 _peerAgents =
nullptr;
829 std::list<ihipStream_t*>& streams() {
return _streams; };
830 const std::list<ihipStream_t*>& const_streams()
const {
return _streams; };
834 bool isPeerWatcher(
const ihipCtx_t* peer);
838 void resetPeerWatchers(
ihipCtx_t* thisDevice);
839 void printPeerWatchers(FILE* f)
const;
841 uint32_t peerCnt()
const {
return _peerCnt; };
842 hsa_agent_t* peerAgents()
const {
return _peerAgents; };
846 std::list<ihipCtx_t*> _peers;
848 std::stack<ihipExec_t> _execStack;
856 std::list<ihipStream_t*> _streams;
864 hsa_agent_t* _peerAgents;
866 void recomputePeerAgents();
897 void locked_waitAllStreams();
898 void locked_syncDefaultStream(
bool waitOnSelf,
bool syncHost);
902 const ihipDevice_t* getDevice()
const {
return _device; };
903 int getDeviceNum()
const {
return _device->_deviceId; };
906 ihipDevice_t* getWriteableDevice()
const {
return _device; };
908 std::string toString()
const;
932 extern unsigned g_deviceCnt;
933 extern hsa_agent_t g_cpu_agent;
934 extern hsa_agent_t* g_allAgents;
938 extern void ihipInit();
939 extern const char* ihipErrorString(hipError_t);
940 extern ihipCtx_t* ihipGetTlsDefaultCtx();
941 extern void ihipSetTlsDefaultCtx(
ihipCtx_t* ctx);
942 extern hipError_t ihipSynchronize(
void);
943 extern void ihipCtxStackUpdate();
944 extern hipError_t ihipDeviceSetState();
947 ihipCtx_t* ihipGetPrimaryCtx(
unsigned deviceIndex);
949 const char* name, hsa_agent_t *agent);
953 hipError_t ihipStreamSynchronize(
hipStream_t stream);
957 inline std::ostream& operator<<(std::ostream& os,
const ihipStream_t& s) {
959 os << s.getDevice()->_deviceId;
966 inline std::ostream& operator<<(std::ostream& os,
const dim3& s) {
977 inline std::ostream& operator<<(std::ostream& os,
const gl_dim3& s) {
989 inline std::ostream& operator<<(std::ostream& os,
const hipEvent_t& e) {
990 os <<
"event:" << std::hex << static_cast<void*>(e);
994 inline std::ostream& operator<<(std::ostream& os,
const ihipCtx_t* c) {
995 os <<
"ctx:" <<
static_cast<const void*
>(c) <<
".dev:" << c->getDevice()->_deviceId;
1002 hipError_t memcpyAsync(
void* dst,
const void* src,
size_t sizeBytes, hipMemcpyKind kind,
Definition: hip_hcc_internal.h:236
Definition: hip_hcc_internal.h:762
Definition: hip_hcc_internal.h:461
Definition: hip_hcc_internal.h:111
Definition: hip_hcc_internal.h:384
Definition: hip_hcc_internal.h:359
uint32_t x
x
Definition: hip_runtime_api.h:266
Definition: grid_launch.h:17
Definition: hip_module.cpp:89
Definition: hip_hcc_internal.h:801
Definition: hip_hcc_internal.h:882
Definition: hip_runtime_api.h:265
uint32_t y
y
Definition: hip_runtime_api.h:267
void(* hipStreamCallback_t)(hipStream_t stream, hipError_t status, void *userData)
Definition: hip_runtime_api.h:816
Definition: hip_hcc_internal.h:629
Definition: hip_hcc_internal.h:731
unsigned _computeUnits
Number of compute units supported by the device:
Definition: hip_hcc_internal.h:780
uint32_t z
z
Definition: hip_runtime_api.h:268
Definition: hip_runtime_api.h:83
Definition: hip_hcc_internal.h:657
Definition: hip_hcc_internal.h:683
Definition: hip_hcc_internal.h:1001
Definition: hip_hcc_internal.h:335
Definition: hip_hcc_internal.h:702
Definition: hip_hcc_internal.h:812
Definition: hip_hcc_internal.h:521
Definition: hip_hcc_internal.h:473
hipError_t hipEventRecord(hipEvent_t event, hipStream_t stream)
Record an event in the specified stream.
Definition: hip_event.cpp:110
hsa_amd_ipc_memory_t ipc_handle
ipc memory handle on ROCr
Definition: hip_hcc_internal.h:362
Definition: hip_hcc_internal.h:425
Definition: hip_hcc_internal.h:369
hipError_t hipStreamQuery(hipStream_t stream)
Return #hipSuccess if all of the operations in the specified stream have completed, or #hipErrorNotReady if not.
Definition: hip_stream.cpp:157
Definition: hip_hcc_internal.h:94