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;
88 extern int HCC_OPT_FLUSH;
96 int tid()
const {
return _shortTid; };
97 uint64_t incApiSeqNum() {
return ++_apiSeqNum; };
98 uint64_t apiSeqNum()
const {
return _apiSeqNum; };
108 static const uint64_t MAX_TRIGGER = std::numeric_limits<uint64_t>::max();
110 void print(
int tid) {
111 std::cout <<
"Enabling tracing for ";
112 for (
auto iter = _profTrigger.begin(); iter != _profTrigger.end(); iter++) {
113 std::cout <<
"tid:" << tid <<
"." << *iter <<
",";
118 uint64_t nextTrigger() {
return _profTrigger.empty() ? MAX_TRIGGER : _profTrigger.back(); };
119 void add(uint64_t trigger) { _profTrigger.push_back(trigger); };
120 void sort() { std::sort(_profTrigger.begin(), _profTrigger.end(), std::greater<int>()); };
123 std::vector<uint64_t> _profTrigger;
129 extern thread_local hipError_t tls_lastHipError;
130 extern thread_local
TidInfo tls_tidInfo;
131 extern thread_local
bool tls_getPrimaryCtx;
133 extern std::vector<ProfTrigger> g_dbStartTriggers;
134 extern std::vector<ProfTrigger> g_dbStopTriggers;
144 #define KNRM "\x1B[0m" 145 #define KRED "\x1B[31m" 146 #define KGRN "\x1B[32m" 147 #define KYEL "\x1B[33m" 148 #define KBLU "\x1B[34m" 149 #define KMAG "\x1B[35m" 150 #define KCYN "\x1B[36m" 151 #define KWHT "\x1B[37m" 153 extern const char* API_COLOR;
154 extern const char* API_COLOR_END;
160 #define EVENT_THREAD_SAFE 1 162 #define STREAM_THREAD_SAFE 1 164 #define CTX_THREAD_SAFE 1 166 #define DEVICE_THREAD_SAFE 1 171 #define COMPILE_HIP_DB 1 179 #define COMPILE_HIP_TRACE_API 0x3 184 #ifndef COMPILE_HIP_ATP_MARKER 185 #define COMPILE_HIP_ATP_MARKER 0 194 #if COMPILE_HIP_ATP_MARKER 195 #include "CXLActivityLogger.h" 196 #define MARKER_BEGIN(markerName, group) amdtBeginMarker(markerName, group, nullptr); 197 #define MARKER_END() amdtEndMarker(); 198 #define RESUME_PROFILING amdtResumeProfiling(AMDT_ALL_PROFILING); 199 #define STOP_PROFILING amdtStopProfiling(AMDT_ALL_PROFILING); 202 #define MARKER_BEGIN(markerName, group) 204 #define RESUME_PROFILING 205 #define STOP_PROFILING 211 #define TRACE_ALL 0 // 0x01 212 #define TRACE_KCMD 1 // 0x02, kernel command 213 #define TRACE_MCMD 2 // 0x04, memory command 214 #define TRACE_MEM 3 // 0x08, memory allocation or deallocation. 215 #define TRACE_SYNC 4 // 0x10, synchronization (host or hipStreamWaitEvent) 216 #define TRACE_QUERY 5 // 0x20, hipEventRecord, hipEventQuery, hipStreamQuery 226 #define DB_MAX_FLAG 5 233 const char* _shortName;
237 static const DbName dbName[] = {
239 {KYEL,
"sync"}, {KCYN,
"mem"}, {KMAG,
"copy"}, {KRED,
"warn"},
244 #define tprintf(trace_level, ...) \ 246 if (HIP_DB & (1 << (trace_level))) { \ 248 snprintf(msgStr, sizeof(msgStr), __VA_ARGS__); \ 249 fprintf(stderr, " %ship-%s tid:%d:%s%s", dbName[trace_level]._color, \ 250 dbName[trace_level]._shortName, tls_tidInfo.tid(), msgStr, KNRM); \ 255 #define tprintf(trace_level, ...) 259 static inline uint64_t getTicks() {
return hc::get_system_ticks(); }
262 extern uint64_t recordApiTrace(std::string* fullStr,
const std::string& apiStr);
264 #if COMPILE_HIP_ATP_MARKER || (COMPILE_HIP_TRACE_API & 0x1) 265 #define API_TRACE(forceTrace, ...) \ 266 uint64_t hipApiStartTick = 0; \ 268 tls_tidInfo.incApiSeqNum(); \ 270 (HIP_PROFILE_API || (COMPILE_HIP_DB && (HIP_TRACE_API & (1 << TRACE_ALL))))) { \ 271 std::string apiStr = std::string(__func__) + " (" + ToString(__VA_ARGS__) + ')'; \ 272 std::string fullStr; \ 273 hipApiStartTick = recordApiTrace(&fullStr, apiStr); \ 274 if (HIP_PROFILE_API == 0x1) { \ 275 MARKER_BEGIN(__func__, "HIP") \ 276 } else if (HIP_PROFILE_API == 0x2) { \ 277 MARKER_BEGIN(fullStr.c_str(), "HIP"); \ 284 #define API_TRACE(IS_CMD, ...) tls_tidInfo.incApiSeqNum(); 290 std::call_once(hip_initialized, ihipInit); \ 291 ihipCtxStackUpdate(); 292 #define HIP_SET_DEVICE() ihipDeviceSetState(); 298 #define HIP_INIT_API(...) \ 300 API_TRACE(0, __VA_ARGS__); 306 #define HIP_INIT_SPECIAL_API(tbit, ...) \ 308 API_TRACE((HIP_TRACE_API & (1 << tbit)), __VA_ARGS__); 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 tid:%d.%lu %-30s ret=%2d (%s)>> +%lu ns%s\n", \ 322 (localHipStatus == 0) ? API_COLOR : KRED, 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 = {};
373 if (executable.handle) hsa_executable_destroy(executable);
374 if (coReader.handle) hsa_code_object_reader_destroy(coReader);
384 bool try_lock() {
return true; }
388 #if EVENT_THREAD_SAFE 389 typedef std::mutex EventMutex;
391 #warning "Stream thread-safe disabled" 395 #if STREAM_THREAD_SAFE 396 typedef std::mutex StreamMutex;
398 #warning "Stream thread-safe disabled" 404 typedef std::mutex CtxMutex;
407 #warning "Ctx thread-safe disabled" 410 #if DEVICE_THREAD_SAFE 411 typedef std::mutex DeviceMutex;
414 #warning "Device thread-safe disabled" 421 template <
typename T>
425 : _criticalData(&criticalData),
426 _autoUnlock(autoUnlock)
429 tprintf(DB_SYNC,
"locking criticalData=%p for %s..\n", _criticalData,
430 ToString(_criticalData->_parent).c_str());
431 _criticalData->_mutex.lock();
436 tprintf(DB_SYNC,
"auto-unlocking criticalData=%p for %s...\n", _criticalData,
437 ToString(_criticalData->_parent).c_str());
438 _criticalData->_mutex.unlock();
443 tprintf(DB_SYNC,
"unlocking criticalData=%p for %s...\n", _criticalData,
444 ToString(_criticalData->_parent).c_str());
445 _criticalData->_mutex.unlock();
449 T* operator->() {
return _criticalData; };
457 template <
typename MUTEX_TYPE>
461 void lock() { _mutex.lock(); }
462 void unlock() { _mutex.unlock(); }
463 bool try_lock() {
return _mutex.try_lock(); }
469 template <
typename MUTEX_TYPE>
473 : _kernelCnt(0), _av(av), _parent(parentStream){};
483 tprintf(DB_SYNC,
"munlocking criticalData=%p for %s...\n",
this,
484 ToString(this->_parent).c_str());
490 tprintf(DB_SYNC,
"mtry_locking=%d criticalData=%p for %s...\n", gotLock,
this,
491 ToString(this->_parent).c_str());
492 return gotLock ?
this :
nullptr;
499 hc::accelerator_view _av;
520 enum ScheduleMode { Auto, Spin, Yield };
521 typedef uint64_t SeqNum_t;
528 void locked_copySync(
void* dst,
const void* src,
size_t sizeBytes,
unsigned kind,
529 bool resolveOn =
true);
531 void locked_copy2DSync(
void* dst,
const void* src,
size_t width,
size_t height,
size_t srcPitch,
532 size_t dstPitch,
unsigned kind,
bool resolveOn =
true);
534 void locked_copyAsync(
void* dst,
const void* src,
size_t sizeBytes,
unsigned kind);
536 void locked_copy2DAsync(
void* dst,
const void* src,
size_t width,
size_t height,
size_t srcPitch,
537 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 std::once_flag hip_initialized;
931 extern unsigned g_deviceCnt;
932 extern hsa_agent_t g_cpu_agent;
933 extern hsa_agent_t* g_allAgents;
937 extern void ihipInit();
938 extern const char* ihipErrorString(hipError_t);
939 extern ihipCtx_t* ihipGetTlsDefaultCtx();
940 extern void ihipSetTlsDefaultCtx(
ihipCtx_t* ctx);
941 extern hipError_t ihipSynchronize(
void);
942 extern void ihipCtxStackUpdate();
943 extern hipError_t ihipDeviceSetState();
946 ihipCtx_t* ihipGetPrimaryCtx(
unsigned deviceIndex);
950 hipError_t ihipStreamSynchronize(
hipStream_t stream);
954 inline std::ostream& operator<<(std::ostream& os,
const ihipStream_t& s) {
956 os << s.getDevice()->_deviceId;
963 inline std::ostream& operator<<(std::ostream& os,
const dim3& s) {
974 inline std::ostream& operator<<(std::ostream& os,
const gl_dim3& s) {
986 inline std::ostream& operator<<(std::ostream& os,
const hipEvent_t& e) {
987 os <<
"event:" << std::hex << static_cast<void*>(e);
991 inline std::ostream& operator<<(std::ostream& os,
const ihipCtx_t* c) {
992 os <<
"ctx:" <<
static_cast<const void*
>(c) <<
".dev:" << c->getDevice()->_deviceId;
999 hipError_t memcpyAsync(
void* dst,
const void* src,
size_t sizeBytes, hipMemcpyKind kind,
Definition: hip_hcc_internal.h:231
Definition: hip_hcc_internal.h:760
Definition: hip_hcc_internal.h:458
Definition: hip_hcc_internal.h:107
Definition: hip_hcc_internal.h:381
Definition: hip_hcc_internal.h:357
uint32_t x
x
Definition: hip_runtime_api.h:241
Definition: hip_hcc_internal.h:799
Definition: hip_hcc_internal.h:880
Definition: hip_runtime_api.h:240
uint32_t y
y
Definition: hip_runtime_api.h:242
void(* hipStreamCallback_t)(hipStream_t stream, hipError_t status, void *userData)
Definition: hip_runtime_api.h:738
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:243
Definition: hip_runtime_api.h:83
Definition: hip_hcc_internal.h:655
Definition: hip_hcc_internal.h:681
Definition: hip_hcc_internal.h:998
Definition: hip_hcc_internal.h:333
Definition: hip_hcc_internal.h:700
Definition: hip_hcc_internal.h:810
Definition: hip_hcc_internal.h:518
Definition: hip_hcc_internal.h:470
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:422
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:118
Definition: hip_hcc_internal.h:92