11#ifdef MNEME_ENABLE_HIP
12#include <hip/amd_detail/amd_hip_runtime.h>
13#include <hip/hip_runtime.h>
15#include <hsa/hsa_ext_amd.h>
17#define hipErrCheck(CALL) \
19 hipError_t err = CALL; \
20 if (err != hipSuccess) { \
21 printf("ERROR @ %s:%d -> %s\n", __FILE__, __LINE__, \
22 hipGetErrorString(err)); \
27#define hiprtcErrCheck(CALL) \
29 hiprtcResult err = CALL; \
30 if (err != HIPRTC_SUCCESS) { \
31 printf("ERROR @ %s:%d -> %s\n", __FILE__, __LINE__, \
32 hiprtcGetErrorString(err)); \
37#elif defined(MNEME_ENABLE_CUDA)
39#include <cuda_runtime.h>
41#define cudaErrCheck(CALL) \
43 cudaError_t err = CALL; \
44 if (err != cudaSuccess) { \
45 printf("ERROR @ %s:%d -> %s\n", __FILE__, __LINE__, \
46 hipGetErrorString(err)); \
51#define cuErrCheck(CALL) \
53 CUresult err = CALL; \
54 if (err != CUDA_SUCCESS) { \
55 const char *name = nullptr, *desc = nullptr; \
56 cuGetErrorName(err, &name); \
57 cuGetErrorString(err, &desc); \
58 fprintf(stderr, "CUDA Driver Error [%s]: %s\n", name ? name : "Unknown", \
59 desc ? desc : "No description"); \
77 void *Lib = dlopen(Name, RTLD_NOW);
79 LOG_FATAL(
"Could not load {}: {}", Name, dlerror());
83#if defined(MNEME_ENABLE_HIP)
85 using DeviceError_t = hipError_t;
86 using DeviceStream_t = hipStream_t;
87 using KernelFunction_t = hipFunction_t;
88 using MemoryAllocationHandle_t = hipMemGenericAllocationHandle_t;
89 using DeviceModule_t = hipModule_t;
90 using DevicePtr_t = hipDeviceptr_t;
91 using DeviceHandle_t = hipDevice_t;
92 using DeviceContext_t = hipCtx_t;
93 using DeviceFunction_t = hipFunction_t;
94 using DeviceEvent_t = hipEvent_t;
95 static constexpr auto DeviceSuccess = hipSuccess;
96 static constexpr auto DeviceOutOfMemory = hipErrorOutOfMemory;
98 static constexpr uint64_t VABlockSize = 1;
101 static constexpr bool PlacesManagedMemory =
true;
103 static inline void *getRTLib() {
104 static void *Lib =
loadRTLib(
"libamdhip64.so");
107 static constexpr const char *getLaunchKernelFnName() {
108 return "hipLaunchKernel";
110 static constexpr const char *getDeviceMallocFnName() {
return "hipMalloc"; }
111 static constexpr const char *getPinnedMallocFnName() {
112 return "hipHostMalloc";
114 static constexpr const char *getManagedMallocFnName() {
115 return "hipMallocManaged";
117 static constexpr const char *getDeviceFreeFnName() {
return "hipFree"; }
118 static constexpr const char *getAsyncFreeFnName() {
return "hipFreeAsync"; }
119 static constexpr const char *getPinnedFreeFnName() {
return "hipHostFree"; }
121 static const char *getDeviceGetIDFnName() {
return "hipGetDevice"; }
122 static const char *getDeviceSetIDFnName() {
return "hipSetDevice"; }
124 static constexpr bool hasFatBinEnd =
false;
126 static inline std::optional<std::string>
127 DeviceErrorCheck(hipError_t ErrorCode) {
128 if (ErrorCode == hipSuccess)
130 return std::string(hipGetErrorString(ErrorCode));
133 static hipError_t DeviceStreamSynchronize(hipStream_t Stream) {
134 return hipStreamSynchronize(Stream);
137 static hipError_t DeviceMemset(
void *DevPtr,
int Value,
size_t Bytes) {
138 auto EC = hipMemset(DevPtr, Value,
Bytes);
142 static hipError_t DeviceMalloc(
void **ptr,
size_t size) {
146 static hipError_t DeviceFree(
void *ptr) {
return hipFree(ptr); }
148 static hipError_t DeviceCopy(
void *Dest,
void *Src,
size_t SizeBytes,
149 hipMemcpyKind Kind) {
150 return hipMemcpy(Dest, Src, SizeBytes, Kind);
153 static hipError_t DeviceCopyAsync(
void *Dest,
void *Src,
size_t SizeBytes,
154 hipMemcpyKind Kind, hipStream_t Stream) {
155 return hipMemcpyAsync(Dest, Src, SizeBytes, Kind, Stream);
158 static hipError_t DeviceSynchronize() {
return hipDeviceSynchronize(); }
161 static std::pair<hipError_t, bool>
162 deviceStreamIsCapturing(hipStream_t Stream) {
163 hipStreamCaptureStatus Status;
164 hipError_t Err = hipStreamIsCapturing(Stream, &Status);
165 return {Err, Err == hipSuccess && Status == hipStreamCaptureStatusActive};
168 static std::string GetDeviceArch() {
171 auto EC = DeviceErrorCheck(hipInit(0));
173 LOG_FATAL(
"Could not initialize device\n EC:" + EC.value());
177 LOG_FATAL(
"Could not get device\n EC:" + EC.value());
179 hipDeviceProp_t device_prop;
182 EC = DeviceErrorCheck(hipGetDeviceProperties(&device_prop, Dev));
184 LOG_FATAL(
"Could not get device properties\n EC:" + EC.value());
186 std::string arch_name = device_prop.gcnArchName;
187 LOG_DEBUG(
"Architecture name is {}", arch_name);
189 std::string HipArch = arch_name.substr(0, arch_name.find(
':'));
191 return std::string(HipArch);
194 static DeviceModule_t getDeviceModuleFromImage(
const void *Image) {
195 hipModule_t HipModule;
197 auto EC = DeviceErrorCheck(hipModuleLoadData(&HipModule, Image));
199 LOG_FATAL(
"Error with loading data from module\nEC:" + EC.value());
203 static void DeviceModuleUnload(DeviceModule_t Module) {
204 auto EC = DeviceErrorCheck(hipModuleUnload(Module));
206 LOG_FATAL(
"Cannot unload module\nEC:" + EC.value());
209 static std::pair<void *, size_t>
210 getGlobalAddrFromModule(hipModule_t &HipModule,
211 const std::string &GlobalName) {
213 hipDeviceptr_t DevPtr;
214 auto EC = DeviceErrorCheck(
215 hipModuleGetGlobal(&DevPtr, &
Size, HipModule, GlobalName.c_str()));
217 LOG_FATAL(
"Could not load global variable '" + GlobalName +
218 "' from device module\n:EC:" + EC.value());
219 return std::make_pair((
void *)DevPtr,
Size);
222 static hipFunction_t getKernelFunctionFromImage(hipModule_t &HipModule,
223 std::string &KernelName) {
224 hipFunction_t KernelFunc;
225 auto EC = DeviceErrorCheck(
226 hipModuleGetFunction(&KernelFunc, HipModule, KernelName.c_str()));
228 LOG_FATAL(
"Error with loading kernel from Module");
233 static hipError_t getDeviceCount(
int &devCount) {
234 return hipGetDeviceCount(&devCount);
237 static hipError_t setDevice(
int DeviceId) {
return hipSetDevice(DeviceId); }
239 static hipError_t getDevice(
int &DeviceId) {
return hipGetDevice(&DeviceId); }
241 static hipError_t launchKernelFunction(hipFunction_t KernelFunc, dim3 GridDim,
242 dim3 BlockDim,
void **KernelArgs,
244 hipStream_t Stream) {
245 return hipModuleLaunchKernel(KernelFunc, GridDim.x, GridDim.y, GridDim.z,
246 BlockDim.x, BlockDim.y, BlockDim.z, ShmemSize,
247 Stream, KernelArgs,
nullptr);
250 static inline uint64_t
251 getPageSize(
int DeviceID,
252 const hipMemAllocationGranularity_flags Granularity) {
254 hipMemAllocationProp Prop = {};
255 Prop.type = hipMemAllocationTypePinned;
256 Prop.location.type = hipMemLocationTypeDevice;
257 Prop.location.id = DeviceID;
262 hipErrCheck(hipMemGetAllocationGranularity(&PageSize, &Prop, Granularity));
266 static constexpr hipMemcpyKind MemcpyHostToDeviceKind() {
267 return hipMemcpyHostToDevice;
270 static constexpr hipMemcpyKind MemcpyDeviceToHostKind() {
271 return hipMemcpyDeviceToHost;
274 static constexpr hipMemcpyKind MemcpyDeviceToDeviceKind() {
275 return hipMemcpyDeviceToDevice;
281 static void grantHostAccess(
void *Addr, uint64_t
Size,
int DeviceID) {
283 hipErrCheck(hipDeviceGetAttribute(&Integrated, hipDeviceAttributeIntegrated,
288 static hsa_agent_t CpuAgent = [] {
289 hsa_agent_t Agent{0};
291 [](hsa_agent_t A,
void *Data) {
292 hsa_device_type_t Type;
293 hsa_agent_get_info(A, HSA_AGENT_INFO_DEVICE, &Type);
294 if (Type != HSA_DEVICE_TYPE_CPU)
295 return HSA_STATUS_SUCCESS;
296 *
static_cast<hsa_agent_t *
>(Data) = A;
297 return HSA_STATUS_INFO_BREAK;
302 if (!CpuAgent.handle)
303 LOG_FATAL(
"Cannot find the HSA CPU agent to grant host access");
305 hsa_amd_memory_access_desc_t Desc{HSA_ACCESS_PERMISSION_RW, CpuAgent};
306 if (hsa_amd_vmem_set_access(Addr,
Size, &Desc, 1) != HSA_STATUS_SUCCESS)
307 LOG_FATAL(
"Cannot grant host access to device memory at {} size {}", Addr,
311 static uint64_t getMinPageSize(
int DeviceID) {
312 return getPageSize(DeviceID, hipMemAllocationGranularityMinimum);
319 static std::vector<hipMemGenericAllocationHandle_t> &
320 freeHandles(
int DeviceID, uint64_t
Size) {
322 new std::map<std::pair<int, uint64_t>,
323 std::vector<hipMemGenericAllocationHandle_t>>();
324 return (*Pool)[{DeviceID,
Size}];
327 static void releaseHandle(
int DeviceID, uint64_t
Size,
328 hipMemGenericAllocationHandle_t H) {
329 static const bool ReleaseLeaks = [] {
331 (void)hipRuntimeGetVersion(&Version);
332 return Version < 60443484;
335 freeHandles(DeviceID,
Size).push_back(H);
337 (
void)hipMemRelease(H);
341 static MapStatus mapFixed(
void *Addr, uint64_t
Size, uint64_t Alignment,
342 int DeviceID, hipMemGenericAllocationHandle_t &H) {
344 hipError_t Err = hipMemAddressReserve(&Got,
Size, Alignment, Addr, 0);
345 if (Err == hipErrorOutOfMemory)
347 if (Err != hipSuccess)
349 hipGetErrorString(Err));
351 (void)hipMemAddressFree(Got,
Size);
355 auto &Free = freeHandles(DeviceID,
Size);
360 hipMemAllocationProp Prop = {};
361 Prop.type = hipMemAllocationTypePinned;
362 Prop.location.type = hipMemLocationTypeDevice;
363 Prop.location.id = DeviceID;
364 if (hipMemCreate(&H,
Size, &Prop, 0) != hipSuccess) {
365 (void)hipMemAddressFree(Addr,
Size);
370 hipMemAccessDesc ADesc = {};
371 ADesc.location.type = hipMemLocationTypeDevice;
372 ADesc.location.id = DeviceID;
373 ADesc.flags = hipMemAccessFlagsProtReadWrite;
374 if (hipMemMap(Addr,
Size, 0, H, 0) == hipSuccess) {
375 if (hipMemSetAccess(Addr,
Size, &ADesc, 1) == hipSuccess) {
376 grantHostAccess(Addr,
Size, DeviceID);
379 (void)hipMemUnmap(Addr,
Size);
381 releaseHandle(DeviceID,
Size, H);
382 (void)hipMemAddressFree(Addr,
Size);
386 static void unmapFixed(
void *Addr, uint64_t
Size,
int DeviceID,
387 hipMemGenericAllocationHandle_t H) {
388 auto EC = DeviceErrorCheck(hipMemUnmap(Addr,
Size));
390 releaseHandle(DeviceID,
Size, H);
391 EC = DeviceErrorCheck(hipMemAddressFree(Addr,
Size));
394 LOG_WARN(
"Could not unmap {}: {}", Addr, *EC);
398 static void clearLastError() { (void)hipGetLastError(); }
400 static hipError_t DeviceStreamCreate(hipStream_t *Stream) {
401 return hipStreamCreate(Stream);
404 static hipError_t deviceStreamDestroy(hipStream_t Stream) {
405 return hipStreamDestroy(Stream);
408 static hipError_t deviceLaunchKernel(
const void *function_address,
409 dim3 numBlocks, dim3 dimBlocks,
410 void **args,
size_t sharedMemBytes,
411 hipStream_t stream) {
412 return hipLaunchKernel(function_address, numBlocks, dimBlocks, args,
413 sharedMemBytes, stream);
416 static hipError_t deviceGetSymbolAddress(
void **devPtr,
const void *symbol) {
417 return hipGetSymbolAddress(devPtr, symbol);
420 static hipError_t deviceEventCreate(hipEvent_t *event) {
421 return hipEventCreate(event);
424 static hipError_t deviceEventRecord(hipEvent_t event, hipStream_t stream) {
425 return hipEventRecord(event, stream);
428 static hipError_t deviceEventDestroy(hipEvent_t event) {
429 return hipEventDestroy(event);
432 static hipError_t deviceEventSynchronize(hipEvent_t event) {
433 return hipEventSynchronize(event);
436 static hipError_t deviceEventElapsedTime(
float *ms, hipEvent_t start,
438 return hipEventElapsedTime(ms, start, stop);
441 static hipError_t deviceGetAttribute(hipFunction_t &Func,
443 LOG_DEBUG(
"Going to request attributes of {}", (
void *)Func);
447 return hipFuncGetAttribute(&Value, HIP_FUNC_ATTRIBUTE_NUM_REGS, Func);
449 return hipFuncGetAttribute(&Value, HIP_FUNC_ATTRIBUTE_LOCAL_SIZE_BYTES,
452 return hipFuncGetAttribute(&Value, HIP_FUNC_ATTRIBUTE_CONST_SIZE_BYTES,
458 return hipErrorInvalidValue;
461#elif defined(MNEME_ENABLE_CUDA)
464 using DeviceError_t = cudaError_t;
465 using DeviceDriverError_t = CUresult;
467 using DeviceStream_t = cudaStream_t;
469 using KernelFunction_t = CUfunction;
471 using MemoryAllocationHandle_t = CUmemGenericAllocationHandle;
473 using DeviceModule_t = CUmodule;
474 using DevicePtr_t = CUdeviceptr;
476 using DeviceHandle_t = CUdevice;
477 using DeviceContext_t = CUcontext;
478 using DeviceFunction_t = CUfunction;
480 using DeviceEvent_t = cudaEvent_t;
481 static constexpr auto DeviceSuccess = cudaSuccess;
482 static constexpr auto DeviceDriverSuccess = CUDA_SUCCESS;
483 static constexpr auto DeviceOutOfMemory = cudaErrorMemoryAllocation;
487 static constexpr uint64_t VABlockSize = 32ULL << 20;
489 static constexpr bool PlacesManagedMemory =
false;
491 static inline void *getRTLib() {
492 static void *Lib =
loadRTLib(
"libcudart.so");
495 static constexpr const char *getLaunchKernelFnName() {
496 return "cudaLaunchKernel";
498 static constexpr const char *getDeviceMallocFnName() {
return "cudaMalloc"; }
499 static constexpr const char *getPinnedMallocFnName() {
500 return "cudaHostAlloc";
502 static constexpr const char *getManagedMallocFnName() {
503 return "cudaMallocManaged";
505 static constexpr const char *getDeviceFreeFnName() {
return "cudaFree"; }
506 static constexpr const char *getAsyncFreeFnName() {
return "cudaFreeAsync"; }
507 static constexpr const char *getPinnedFreeFnName() {
return "cudaFreeHost"; }
508 static constexpr const char *getUURegisterFunctionFnName() {
509 return "__cudaRegisterFunction";
511 static const char *getUURegisterVarFnName() {
return "__cudaRegisterVar"; }
512 static const char *getUURegisterFatbinFnName() {
513 return "__cudaRegisterFatBinary";
516 static const char *getUURegisterFatbinEndFnName() {
517 return "__cudaRegisterFatBinaryEnd";
520 static const char *getUUUnRegisterFatBinaryFnName() {
521 return "__cudaUnregisterFatBinary";
524 static const char *getDeviceGetIDFnName() {
return "cudaGetDevice"; }
525 static const char *getDeviceSetIDFnName() {
return "cudaSetDevice"; }
527 static constexpr bool hasFatBinEnd =
true;
529 static inline std::optional<std::string>
530 DeviceErrorCheck(DeviceError_t ErrorCode) {
531 if (ErrorCode == DeviceSuccess)
533 return std::string(cudaGetErrorString(ErrorCode));
536 static inline std::optional<std::string>
537 DeviceErrorCheck(DeviceDriverError_t ErrorCode) {
538 if (ErrorCode == DeviceDriverSuccess)
541 if (ErrorCode == CUDA_ERROR_DEINITIALIZED) {
542 LOG_WARN(
"Device is deinitialized ignoring errors");
546 const char *name =
nullptr, *desc =
nullptr;
547 cuGetErrorName(ErrorCode, &name);
548 cuGetErrorString(ErrorCode, &desc);
549 auto EC = std::string(
"Error:") + std::to_string(ErrorCode) +
":";
551 EC += std::string(name);
553 EC += std::string(
" description:") + std::string(name);
558 static DeviceError_t DeviceStreamSynchronize(DeviceStream_t Stream) {
559 return cudaStreamSynchronize(Stream);
562 static DeviceError_t DeviceMemset(
void *DevPtr,
int Value,
size_t Bytes) {
563 auto EC = cudaMemset(DevPtr, Value,
Bytes);
567 static DeviceError_t DeviceMalloc(
void **ptr,
size_t size) {
571 static DeviceError_t DeviceFree(
void *ptr) {
return cudaFree(ptr); }
573 static DeviceError_t DeviceCopy(
void *Dest,
void *Src,
size_t SizeBytes,
574 cudaMemcpyKind Kind) {
575 return cudaMemcpy(Dest, Src, SizeBytes, Kind);
578 static DeviceError_t DeviceCopyAsync(
void *Dest,
void *Src,
size_t SizeBytes,
580 DeviceStream_t Stream) {
581 return cudaMemcpyAsync(Dest, Src, SizeBytes, Kind, Stream);
584 static DeviceError_t DeviceSynchronize() {
return cudaDeviceSynchronize(); }
587 static std::pair<DeviceError_t, bool>
588 deviceStreamIsCapturing(DeviceStream_t Stream) {
589 cudaStreamCaptureStatus Status;
590 DeviceError_t Err = cudaStreamIsCapturing(Stream, &Status);
591 return {Err, Err == cudaSuccess && Status == cudaStreamCaptureStatusActive};
594 static std::string GetDeviceArch() {
597 auto EC = DeviceErrorCheck(cuInit(0));
599 LOG_FATAL(
"Could not initialize device\n EC:" + EC.value());
603 LOG_FATAL(
"Could not get device\n EC:" + EC.value());
605 EC = DeviceErrorCheck(cuCtxGetCurrent(&Ctx));
607 LOG_FATAL(
"Could not get current CUDA context\nEC:" + EC.value());
610 EC = DeviceErrorCheck(cuCtxCreate(&Ctx, 0, Dev));
612 LOG_FATAL(
"Could not create new context\nEC:" + EC.value());
616 EC = DeviceErrorCheck(cuDeviceGetAttribute(
617 &CCMajor, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, Dev));
619 LOG_FATAL(
"Could not get device major attribute\n EC:" + EC.value());
621 DeviceErrorCheck(cuDeviceGetAttribute(
622 &CCMinor, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, Dev));
624 LOG_FATAL(
"Could not get device minor attribute\n EC:" + EC.value());
626 std::string DeviceArch =
"sm_" + std::to_string(CCMajor * 10 + CCMinor);
631 static DeviceModule_t getDeviceModuleFromImage(
const void *Image) {
632 DeviceModule_t cudaModule;
634 auto EC = DeviceErrorCheck(cuModuleLoadData(&cudaModule, Image));
636 LOG_FATAL(
"Error with loading data from module\nEC:" + EC.value());
640 static void DeviceModuleUnload(DeviceModule_t Module) {
641 auto EC = DeviceErrorCheck(cuModuleUnload(Module));
643 LOG_FATAL(
"Cannot unload module\nEC:" + EC.value());
646 static std::pair<void *, size_t>
647 getGlobalAddrFromModule(DeviceModule_t &cudaModule,
648 const std::string &GlobalName) {
651 auto EC = DeviceErrorCheck(
652 cuModuleGetGlobal(&DevPtr, &
Size, cudaModule, GlobalName.c_str()));
654 LOG_FATAL(
"Could not load global variable '" + GlobalName +
655 "' from device module\n:EC:" + EC.value());
656 return std::make_pair((
void *)DevPtr,
Size);
659 static DeviceFunction_t getKernelFunctionFromImage(DeviceModule_t &Module,
660 std::string &KernelName) {
661 DeviceFunction_t KernelFunc;
662 auto EC = DeviceErrorCheck(
663 cuModuleGetFunction(&KernelFunc, Module, KernelName.c_str()));
665 LOG_FATAL(
"Error with loading kernel from Module");
670 static cudaError_t getDeviceCount(
int &devCount) {
671 return cudaGetDeviceCount(&devCount);
674 static cudaError_t setDevice(
int DeviceId) {
return cudaSetDevice(DeviceId); }
676 static cudaError_t getDevice(
int &DeviceId) {
680 static DeviceDriverError_t launchKernelFunction(DeviceFunction_t KernelFunc,
681 dim3 GridDim, dim3 BlockDim,
684 cudaStream_t Stream) {
685 return cuLaunchKernel(KernelFunc, GridDim.x, GridDim.y, GridDim.z,
686 BlockDim.x, BlockDim.y, BlockDim.z, ShmemSize, Stream,
687 KernelArgs,
nullptr);
690 static inline uint64_t
691 getPageSize(
int DeviceID,
692 const CUmemAllocationGranularity_flags Granularity) {
694 CUmemAllocationProp Prop = {};
695 Prop.type = CUmemAllocationType::CU_MEM_ALLOCATION_TYPE_PINNED;
696 Prop.location.type = CUmemLocationType::CU_MEM_LOCATION_TYPE_DEVICE;
697 Prop.location.id = DeviceID;
699 auto EC = DeviceErrorCheck(
700 cuMemGetAllocationGranularity(&PageSize, &Prop, Granularity));
702 LOG_FATAL(
"Could not get Allocation Granularity\nEC:" + EC.value());
706 static constexpr cudaMemcpyKind MemcpyHostToDeviceKind() {
707 return cudaMemcpyHostToDevice;
710 static constexpr cudaMemcpyKind MemcpyDeviceToHostKind() {
711 return cudaMemcpyDeviceToHost;
714 static constexpr cudaMemcpyKind MemcpyDeviceToDeviceKind() {
715 return cudaMemcpyDeviceToDevice;
718 static uint64_t getMinPageSize(
int DeviceID) {
719 return getPageSize(DeviceID, CU_MEM_ALLOC_GRANULARITY_MINIMUM);
723 static MapStatus mapFixed(
void *Addr, uint64_t
Size, uint64_t Alignment,
724 int DeviceID, MemoryAllocationHandle_t &H) {
725 DevicePtr_t Want =
reinterpret_cast<DevicePtr_t
>(Addr);
727 CUresult Err = cuMemAddressReserve(&Got,
Size, Alignment, Want, 0);
728 if (Err == CUDA_ERROR_OUT_OF_MEMORY)
730 if (
auto EC = DeviceErrorCheck(Err))
731 LOG_FATAL(
"Cannot reserve {} bytes at {}: {}",
Size, Addr, *EC);
733 (void)cuMemAddressFree(Got,
Size);
737 CUmemAllocationProp Prop = {};
738 Prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
739 Prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
740 Prop.location.id = DeviceID;
741 if (cuMemCreate(&H,
Size, &Prop, 0) != CUDA_SUCCESS) {
742 (void)cuMemAddressFree(Want,
Size);
746 CUmemAccessDesc ADesc = {};
747 ADesc.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
748 ADesc.location.id = DeviceID;
749 ADesc.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE;
750 if (cuMemMap(Want,
Size, 0, H, 0) == CUDA_SUCCESS) {
751 if (cuMemSetAccess(Want,
Size, &ADesc, 1) == CUDA_SUCCESS)
753 (void)cuMemUnmap(Want,
Size);
755 (void)cuMemRelease(H);
756 (void)cuMemAddressFree(Want,
Size);
760 static void unmapFixed(
void *Addr, uint64_t
Size,
int,
761 MemoryAllocationHandle_t H) {
762 DevicePtr_t Ptr =
reinterpret_cast<DevicePtr_t
>(Addr);
763 auto EC = DeviceErrorCheck(cuMemUnmap(Ptr,
Size));
765 EC = DeviceErrorCheck(cuMemRelease(H));
767 EC = DeviceErrorCheck(cuMemAddressFree(Ptr,
Size));
769 LOG_WARN(
"Could not unmap {}: {}", Addr, *EC);
773 static void clearLastError() {}
778 static DeviceError_t DeviceStreamCreate(DeviceStream_t *Stream) {
779 return cudaStreamCreate(Stream);
782 static DeviceError_t deviceStreamDestroy(DeviceStream_t Stream) {
783 return cudaStreamDestroy(Stream);
786 static DeviceError_t deviceLaunchKernel(
const void *kernelFunc, dim3 gridDim,
787 dim3 blockDim,
void **kernelArgs,
788 size_t sharedMemBytes,
789 DeviceStream_t stream) {
790 auto EC = DeviceErrorCheck(cudaLaunchKernel(
791 kernelFunc, gridDim, blockDim, kernelArgs, sharedMemBytes, stream));
793 LOG_WARN(
"Launching Kernel return error {}", EC.value());
795 return cudaGetLastError();
798 static DeviceError_t deviceEventCreate(DeviceEvent_t *event) {
799 return cudaEventCreate(event);
802 static DeviceError_t deviceEventRecord(DeviceEvent_t event,
803 DeviceStream_t stream) {
804 return cudaEventRecord(event, stream);
807 static DeviceError_t deviceEventDestroy(DeviceEvent_t event) {
808 return cudaEventDestroy(event);
811 static DeviceError_t deviceEventSynchronize(DeviceEvent_t event) {
812 return cudaEventSynchronize(event);
815 static DeviceError_t deviceEventElapsedTime(
float *ms, DeviceEvent_t start,
816 DeviceEvent_t stop) {
817 return cudaEventElapsedTime(ms, start, stop);
820 static DeviceError_t deviceGetSymbolAddress(
void **devPtr,
821 const void *symbol) {
822 return cudaGetSymbolAddress(devPtr, symbol);
825 static DeviceDriverError_t deviceGetAttribute(cudaFunction_t &Func,
828 LOG_DEBUG(
"Going to request attributes of {}", (
void *)Func);
832 return cuFuncGetAttribute(&Value, CU_FUNC_ATTRIBUTE_NUM_REGS, Func);
835 return cuFuncGetAttribute(&Value, CU_FUNC_ATTRIBUTE_LOCAL_SIZE_BYTES,
839 return cuFuncGetAttribute(&Value, CU_FUNC_ATTRIBUTE_CONST_SIZE_BYTES,
852template <DeviceVendors Vendor,
typename FnT>
857 LOG_FATAL(
"Could not find {} in the device runtime", Name);
#define LOG_DEBUG(...)
Definition MnemeLogger.hpp:109
#define LOG_FATAL(...)
Definition MnemeLogger.hpp:113
#define LOG_WARN(...)
Definition MnemeLogger.hpp:111
cudaError_t cudaFree(void *ptr)
Definition MnemeRecordPreloadCUDA.cpp:113
cudaError_t cudaGetDevice(int *deviceID)
Definition MnemeRecordPreloadCUDA.cpp:145
cudaError_t cudaMalloc(void **ptr, size_t size)
Definition MnemeRecordPreloadCUDA.cpp:43
cudaError_t cudaSetDevice(int deviceID)
Definition MnemeRecordPreloadCUDA.cpp:139
hipError_t hipMalloc(void **ptr, size_t size)
Definition MnemeRecordPreloadHIP.cpp:79
hipError_t hipFree(void *ptr)
Definition MnemeRecordPreloadHIP.cpp:158
hipError_t hipSetDevice(int deviceID)
Definition MnemeRecordPreloadHIP.cpp:182
hipError_t hipGetDevice(int *deviceID)
Definition MnemeRecordPreloadHIP.cpp:188
int64_t u64 Size
Definition cupti_profile.cpp:179
Definition MnemeAnnotation.cpp:3
void * loadRTLib(const char *Name)
Definition DeviceTraits.hpp:76
FuncAttributes
Definition DeviceTraits.hpp:69
@ LOCALMEM_USAGE
Definition DeviceTraits.hpp:69
@ CONSTMEM_USAGE
Definition DeviceTraits.hpp:69
@ REGISTER_USAGE
Definition DeviceTraits.hpp:69
CompareResult compareDeviceBlobs(const char *Blob1, const char *Blob2, uint64_t NumBytes, Metadata Md)
Definition MnemeDeviceCode.cpp:237
MapStatus
Definition DeviceTraits.hpp:71
@ Mapped
Definition DeviceTraits.hpp:71
@ OutOfMemory
Definition DeviceTraits.hpp:71
@ Occupied
Definition DeviceTraits.hpp:71
DeviceVendors
Definition DeviceTraits.hpp:67
@ CUDA
Definition DeviceTraits.hpp:67
@ HIP
Definition DeviceTraits.hpp:67
FnT getRuntimeFn(const char *Name)
Definition DeviceTraits.hpp:853
@ Bytes
Definition MnemeConfig.hpp:24
Definition DeviceTraits.hpp:73