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_DUMP_CODE_OBJECT;
89 extern int HCC_OPT_FLUSH;
96 int tid()
const {
return _shortTid; };
97 pid_t pid()
const {
return _pid; };
98 uint64_t incApiSeqNum() {
return ++_apiSeqNum; };
99 uint64_t apiSeqNum()
const {
return _apiSeqNum; };
110 static const uint64_t MAX_TRIGGER = std::numeric_limits<uint64_t>::max();
112 void print(
int tid) {
113 std::cout <<
"Enabling tracing for ";
114 for (
auto iter = _profTrigger.begin(); iter != _profTrigger.end(); iter++) {
115 std::cout <<
"tid:" << tid <<
"." << *iter <<
",";
120 uint64_t nextTrigger() {
return _profTrigger.empty() ? MAX_TRIGGER : _profTrigger.back(); };
121 void add(uint64_t trigger) { _profTrigger.push_back(trigger); };
122 void sort() { std::sort(_profTrigger.begin(), _profTrigger.end(), std::greater<int>()); };
125 std::vector<uint64_t> _profTrigger;
131 extern thread_local hipError_t tls_lastHipError;
132 extern thread_local
TidInfo tls_tidInfo;
133 extern thread_local
bool tls_getPrimaryCtx;
135 extern std::vector<ProfTrigger> g_dbStartTriggers;
136 extern std::vector<ProfTrigger> g_dbStopTriggers;
146 #define KNRM "\x1B[0m" 147 #define KRED "\x1B[31m" 148 #define KGRN "\x1B[32m" 149 #define KYEL "\x1B[33m" 150 #define KBLU "\x1B[34m" 151 #define KMAG "\x1B[35m" 152 #define KCYN "\x1B[36m" 153 #define KWHT "\x1B[37m" 155 extern const char* API_COLOR;
156 extern const char* API_COLOR_END;
162 #define EVENT_THREAD_SAFE 1 164 #define STREAM_THREAD_SAFE 1 166 #define CTX_THREAD_SAFE 1 168 #define DEVICE_THREAD_SAFE 1 173 #define COMPILE_HIP_DB 1 181 #define COMPILE_HIP_TRACE_API 0x3 186 #ifndef COMPILE_HIP_ATP_MARKER 187 #define COMPILE_HIP_ATP_MARKER 0 196 #if COMPILE_HIP_ATP_MARKER 197 #include "CXLActivityLogger.h" 198 #define MARKER_BEGIN(markerName, group) amdtBeginMarker(markerName, group, nullptr); 199 #define MARKER_END() amdtEndMarker(); 200 #define RESUME_PROFILING amdtResumeProfiling(AMDT_ALL_PROFILING); 201 #define STOP_PROFILING amdtStopProfiling(AMDT_ALL_PROFILING); 204 #define MARKER_BEGIN(markerName, group) 206 #define RESUME_PROFILING 207 #define STOP_PROFILING 213 #define TRACE_ALL 0 // 0x01 214 #define TRACE_KCMD 1 // 0x02, kernel command 215 #define TRACE_MCMD 2 // 0x04, memory command 216 #define TRACE_MEM 3 // 0x08, memory allocation or deallocation. 217 #define TRACE_SYNC 4 // 0x10, synchronization (host or hipStreamWaitEvent) 218 #define TRACE_QUERY 5 // 0x20, hipEventRecord, hipEventQuery, hipStreamQuery 229 #define DB_MAX_FLAG 6 236 const char* _shortName;
240 static const DbName dbName[] = {
242 {KYEL,
"sync"}, {KCYN,
"mem"}, {KMAG,
"copy"}, {KRED,
"warn"},
248 #define tprintf(trace_level, ...) \ 250 if (HIP_DB & (1 << (trace_level))) { \ 252 snprintf(msgStr, sizeof(msgStr), __VA_ARGS__); \ 253 fprintf(stderr, " %ship-%s pid:%d tid:%d:%s%s", dbName[trace_level]._color, \ 254 dbName[trace_level]._shortName, tls_tidInfo.pid(), tls_tidInfo.tid(), msgStr, KNRM); \ 259 #define tprintf(trace_level, ...) 263 static inline uint64_t getTicks() {
return hc::get_system_ticks(); }
266 extern uint64_t recordApiTrace(std::string* fullStr,
const std::string& apiStr);
268 #if COMPILE_HIP_ATP_MARKER || (COMPILE_HIP_TRACE_API & 0x1) 269 #define API_TRACE(forceTrace, ...) \ 270 uint64_t hipApiStartTick = 0; \ 272 tls_tidInfo.incApiSeqNum(); \ 274 (HIP_PROFILE_API || (COMPILE_HIP_DB && (HIP_TRACE_API & (1 << TRACE_ALL))))) { \ 275 std::string apiStr = std::string(__func__) + " (" + ToString(__VA_ARGS__) + ')'; \ 276 std::string fullStr; \ 277 hipApiStartTick = recordApiTrace(&fullStr, apiStr); \ 278 if (HIP_PROFILE_API == 0x1) { \ 279 MARKER_BEGIN(__func__, "HIP") \ 280 } else if (HIP_PROFILE_API == 0x2) { \ 281 MARKER_BEGIN(fullStr.c_str(), "HIP"); \ 288 #define API_TRACE(IS_CMD, ...) tls_tidInfo.incApiSeqNum(); 291 #define HIP_SET_DEVICE() ihipDeviceSetState(); 296 #define HIP_INIT_API(cid, ...) \ 297 hip_impl::hip_init(); \ 298 API_TRACE(0, __VA_ARGS__); \ 299 HIP_CB_SPAWNER_OBJECT(cid); 305 #define HIP_INIT_SPECIAL_API(cid, tbit, ...) \ 306 hip_impl::hip_init(); \ 307 API_TRACE((HIP_TRACE_API & (1 << tbit)), __VA_ARGS__); \ 308 HIP_CB_SPAWNER_OBJECT(cid); 314 #define ihipLogStatus(hipStatus) \ 316 hipError_t localHipStatus = hipStatus; \ 317 tls_lastHipError = localHipStatus; \ 319 if ((COMPILE_HIP_TRACE_API & 0x2) && HIP_TRACE_API & (1 << TRACE_ALL)) { \ 320 auto ticks = getTicks() - hipApiStartTick; \ 321 fprintf(stderr, " %ship-api pid:%d tid:%d.%lu %-30s ret=%2d (%s)>> +%lu ns%s\n", \ 322 (localHipStatus == 0) ? API_COLOR : KRED, tls_tidInfo.pid(), tls_tidInfo.tid(), \ 323 tls_tidInfo.apiSeqNum(), __func__, localHipStatus, \ 324 ihipErrorString(localHipStatus), ticks, API_COLOR_END); \ 326 if (HIP_PROFILE_API) { \ 356 #define HIP_IPC_RESERVED_SIZE 24 363 char reserved[HIP_IPC_RESERVED_SIZE];
368 std::string fileName;
369 hsa_executable_t executable = {};
370 hsa_code_object_reader_t coReader = {};
374 if (executable.handle) hsa_executable_destroy(executable);
375 if (coReader.handle) hsa_code_object_reader_destroy(coReader);
385 bool try_lock() {
return true; }
389 #if EVENT_THREAD_SAFE 390 typedef std::mutex EventMutex;
392 #warning "Stream thread-safe disabled" 396 #if STREAM_THREAD_SAFE 397 typedef std::mutex StreamMutex;
399 #warning "Stream thread-safe disabled" 405 typedef std::mutex CtxMutex;
408 #warning "Ctx thread-safe disabled" 411 #if DEVICE_THREAD_SAFE 412 typedef std::mutex DeviceMutex;
415 #warning "Device thread-safe disabled" 422 template <
typename T>
426 : _criticalData(&criticalData),
427 _autoUnlock(autoUnlock)
430 tprintf(DB_SYNC,
"locking criticalData=%p for %s..\n", _criticalData,
431 ToString(_criticalData->_parent).c_str());
432 _criticalData->_mutex.lock();
437 tprintf(DB_SYNC,
"auto-unlocking criticalData=%p for %s...\n", _criticalData,
438 ToString(_criticalData->_parent).c_str());
439 _criticalData->_mutex.unlock();
444 tprintf(DB_SYNC,
"unlocking criticalData=%p for %s...\n", _criticalData,
445 ToString(_criticalData->_parent).c_str());
446 _criticalData->_mutex.unlock();
450 T* operator->() {
return _criticalData; };
458 template <
typename MUTEX_TYPE>
462 void lock() { _mutex.lock(); }
463 void unlock() { _mutex.unlock(); }
464 bool try_lock() {
return _mutex.try_lock(); }
470 template <
typename MUTEX_TYPE>
474 : _kernelCnt(0), _av(av), _parent(parentStream){};
484 tprintf(DB_SYNC,
"munlocking criticalData=%p for %s...\n",
this,
485 ToString(this->_parent).c_str());
491 tprintf(DB_SYNC,
"mtry_locking=%d criticalData=%p for %s...\n", gotLock,
this,
492 ToString(this->_parent).c_str());
493 return gotLock ?
this :
nullptr;
500 hc::accelerator_view _av;
521 enum ScheduleMode { Auto, Spin, Yield };
522 typedef uint64_t SeqNum_t;
529 void locked_copySync(
void* dst,
const void* src,
size_t sizeBytes,
unsigned kind,
530 bool resolveOn =
true);
532 bool locked_copy2DSync(
void* dst,
const void* src,
size_t width,
size_t height,
size_t srcPitch,
size_t dstPitch,
unsigned kind,
533 bool resolveOn =
true);
535 void locked_copyAsync(
void* dst,
const void* src,
size_t sizeBytes,
unsigned kind);
537 bool locked_copy2DAsync(
void* dst,
const void* src,
size_t width,
size_t height,
size_t srcPitch,
size_t dstPitch,
unsigned kind);
539 void lockedSymbolCopySync(hc::accelerator& acc,
void* dst,
void* src,
size_t sizeBytes,
540 size_t offset,
unsigned kind);
541 void lockedSymbolCopyAsync(hc::accelerator& acc,
void* dst,
void* src,
size_t sizeBytes,
542 size_t offset,
unsigned kind);
548 void lockclose_postKernelCommand(
const char* kernelName, hc::accelerator_view* av);
553 hc::accelerator_view* locked_getAv() {
559 hc::completion_future locked_recordEvent(
hipEvent_t event);
562 void locked_eventWaitComplete(hc::completion_future& marker, hc::hcWaitMode waitMode);
567 hc::hcWaitMode waitMode()
const;
572 void launchModuleKernel(hc::accelerator_view av, hsa_signal_t signal, uint32_t blockDimX,
573 uint32_t blockDimY, uint32_t blockDimZ, uint32_t gridDimX,
574 uint32_t gridDimY, uint32_t gridDimZ, uint32_t groupSegmentSize,
575 uint32_t sharedMemBytes,
void* kernarg,
size_t kernSize,
586 bool isDefaultStream()
const {
return _id == 0; };
597 unsigned resolveMemcpyDirection(
bool srcInDeviceMem,
bool dstInDeviceMem);
598 void resolveHcMemcpyDirection(
unsigned hipMemKind,
const hc::AmPointerInfo* dstPtrInfo,
599 const hc::AmPointerInfo* srcPtrInfo, hc::hcCommandKind* hcCopyDir,
600 ihipCtx_t** copyDevice,
bool* forceUnpinnedCopy);
602 bool canSeeMemory(
const ihipCtx_t* thisCtx,
const hc::AmPointerInfo* dstInfo,
603 const hc::AmPointerInfo* srcInfo);
605 void addSymbolPtrToTracker(hc::accelerator& acc,
void* ptr,
size_t sizeBytes);
613 std::mutex _hasQueueLock;
618 friend std::ostream& operator<<(std::ostream& os,
const ihipStream_t& s);
621 ScheduleMode _scheduleMode;
630 : _stream(stream), _callback(callback), _userData(userData) {
640 enum hipEventStatus_t {
641 hipEventStatusUnitialized = 0,
642 hipEventStatusCreated = 1,
643 hipEventStatusRecording = 2,
644 hipEventStatusComplete = 3,
648 enum ihipEventType_t {
649 hipEventTypeIndependent,
650 hipEventTypeStartCommand,
651 hipEventTypeStopCommand,
657 _state = hipEventStatusCreated;
660 _type = hipEventTypeIndependent;
663 void marker(
const hc::completion_future& marker) { _marker = marker; };
664 hc::completion_future& marker() {
return _marker; }
665 uint64_t timestamp()
const {
return _timestamp; };
666 ihipEventType_t type()
const {
return _type; };
668 ihipEventType_t _type;
669 hipEventStatus_t _state;
674 hc::completion_future _marker;
680 template <
typename MUTEX_TYPE>
703 void attachToCompletionFuture(
const hc::completion_future* cf,
hipStream_t stream,
704 ihipEventType_t eventType);
705 std::pair<hipEventStatus_t, uint64_t> refreshEventStatus();
711 return _criticalData._eventData;
728 template <
typename MUTEX_TYPE>
732 : _parent(parentDevice), _ctxCount(0){};
739 std::list<ihipCtx_t*>& ctxs() {
return _ctxs; };
740 const std::list<ihipCtx_t*>& const_ctxs()
const {
return _ctxs; };
741 int getcount() {
return _ctxCount; };
748 std::list<ihipCtx_t*> _ctxs;
762 ihipDevice_t(
unsigned deviceId,
unsigned deviceCnt, hc::accelerator& acc);
766 ihipCtx_t* getPrimaryCtx()
const {
return _primaryCtx; };
774 hc::accelerator _acc;
775 hsa_agent_t _hsaAgent;
804 std::vector<char> _arguments;
809 template <
typename MUTEX_TYPE>
813 : _parent(parentCtx), _peerCnt(0) {
814 _peerAgents =
new hsa_agent_t[deviceCnt];
818 if (_peerAgents !=
nullptr) {
820 _peerAgents =
nullptr;
827 std::list<ihipStream_t*>& streams() {
return _streams; };
828 const std::list<ihipStream_t*>& const_streams()
const {
return _streams; };
832 bool isPeerWatcher(
const ihipCtx_t* peer);
836 void resetPeerWatchers(
ihipCtx_t* thisDevice);
837 void printPeerWatchers(FILE* f)
const;
839 uint32_t peerCnt()
const {
return _peerCnt; };
840 hsa_agent_t* peerAgents()
const {
return _peerAgents; };
844 std::list<ihipCtx_t*> _peers;
846 std::stack<ihipExec_t> _execStack;
854 std::list<ihipStream_t*> _streams;
862 hsa_agent_t* _peerAgents;
864 void recomputePeerAgents();
895 void locked_waitAllStreams();
896 void locked_syncDefaultStream(
bool waitOnSelf,
bool syncHost);
900 const ihipDevice_t* getDevice()
const {
return _device; };
901 int getDeviceNum()
const {
return _device->_deviceId; };
904 ihipDevice_t* getWriteableDevice()
const {
return _device; };
906 std::string toString()
const;
930 extern unsigned g_deviceCnt;
931 extern hsa_agent_t g_cpu_agent;
932 extern hsa_agent_t* g_allAgents;
936 extern void ihipInit();
937 extern const char* ihipErrorString(hipError_t);
938 extern ihipCtx_t* ihipGetTlsDefaultCtx();
939 extern void ihipSetTlsDefaultCtx(
ihipCtx_t* ctx);
940 extern hipError_t ihipSynchronize(
void);
941 extern void ihipCtxStackUpdate();
942 extern hipError_t ihipDeviceSetState();
945 ihipCtx_t* ihipGetPrimaryCtx(
unsigned deviceIndex);
949 hipError_t ihipStreamSynchronize(
hipStream_t stream);
953 inline std::ostream& operator<<(std::ostream& os,
const ihipStream_t& s) {
955 os << s.getDevice()->_deviceId;
962 inline std::ostream& operator<<(std::ostream& os,
const dim3& s) {
973 inline std::ostream& operator<<(std::ostream& os,
const gl_dim3& s) {
985 inline std::ostream& operator<<(std::ostream& os,
const hipEvent_t& e) {
986 os <<
"event:" << std::hex << static_cast<void*>(e);
990 inline std::ostream& operator<<(std::ostream& os,
const ihipCtx_t* c) {
991 os <<
"ctx:" <<
static_cast<const void*
>(c) <<
".dev:" << c->getDevice()->_deviceId;
998 hipError_t memcpyAsync(
void* dst,
const void* src,
size_t sizeBytes, hipMemcpyKind kind,
Definition: hip_hcc_internal.h:234
Definition: hip_hcc_internal.h:760
Definition: hip_hcc_internal.h:459
Definition: hip_hcc_internal.h:109
Definition: hip_hcc_internal.h:382
Definition: hip_hcc_internal.h:357
uint32_t x
x
Definition: hip_runtime_api.h:266
Definition: grid_launch.h:17
Definition: hip_hcc_internal.h:799
Definition: hip_hcc_internal.h:880
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:627
Definition: hip_hcc_internal.h:729
unsigned _computeUnits
Number of compute units supported by the device:
Definition: hip_hcc_internal.h:778
uint32_t z
z
Definition: hip_runtime_api.h:268
Definition: hip_runtime_api.h:83
Definition: hip_hcc_internal.h:655
Definition: hip_hcc_internal.h:681
Definition: hip_hcc_internal.h:997
Definition: hip_hcc_internal.h:333
Definition: hip_hcc_internal.h:700
Definition: hip_hcc_internal.h:810
Definition: hip_hcc_internal.h:519
Definition: hip_hcc_internal.h:471
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:360
Definition: hip_hcc_internal.h:423
Definition: hip_hcc_internal.h:367
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:92