363 using FN_cuInit = CUresult (*)(
unsigned int);
364 using FN_cuDeviceGetCount = CUresult (*)(
int*);
365 using FN_cuDeviceGet = CUresult (*)(CUdevice*, int);
366 using FN_cuDeviceGetAttribute = CUresult (*)(
int*, CUdevice_attribute, CUdevice);
367 using FN_cuDevicePrimaryCtxRetain = CUresult (*)(CUcontext*, CUdevice);
368 using FN_cuDevicePrimaryCtxRelease = CUresult (*)(CUdevice);
369 using FN_cuCtxSetCurrent = CUresult (*)(CUcontext);
370 using FN_cuCtxGetDevice = CUresult (*)(CUdevice*);
371 using FN_cuCtxPushCurrent = CUresult (*)(CUcontext);
372 using FN_cuCtxPopCurrent = CUresult (*)(CUcontext*);
375 FN_cuDeviceGetCount deviceGetCount{};
376 FN_cuDeviceGet deviceGet{};
377 FN_cuDeviceGetAttribute deviceGetAttribute{};
378 FN_cuDevicePrimaryCtxRetain primaryCtxRetain{};
379 FN_cuDevicePrimaryCtxRelease primaryCtxRelease{};
380 FN_cuCtxSetCurrent ctxSetCurrent{};
381 FN_cuCtxGetDevice ctxGetDevice{};
382 FN_cuCtxPushCurrent ctxPush{};
383 FN_cuCtxPopCurrent ctxPop{};
386 using FN_cuStreamCreate = CUresult (*)(CUstream*,
unsigned int);
387 using FN_cuStreamDestroy = CUresult (*)(CUstream);
388 using FN_cuStreamSynchronize = CUresult (*)(CUstream);
390 FN_cuStreamCreate streamCreate{};
391 FN_cuStreamDestroy streamDestroy{};
392 FN_cuStreamSynchronize streamSync{};
395 using FN_cuGetErrorString = CUresult (*)(CUresult,
const char**);
396 FN_cuGetErrorString getErrorString{};
399 using FN_cuGraphicsMapResources
400 = CUresult (*)(
unsigned int, CUgraphicsResource*, CUstream);
401 using FN_cuGraphicsUnmapResources
402 = CUresult (*)(
unsigned int, CUgraphicsResource*, CUstream);
403 using FN_cuGraphicsUnregisterResource = CUresult (*)(CUgraphicsResource);
404 using FN_cuGraphicsResourceGetMappedPointer
405 = CUresult (*)(CUdeviceptr*,
size_t*, CUgraphicsResource);
407 FN_cuGraphicsMapResources graphicsMap{};
408 FN_cuGraphicsUnmapResources graphicsUnmap{};
409 FN_cuGraphicsUnregisterResource graphicsUnregister{};
410 FN_cuGraphicsResourceGetMappedPointer graphicsGetMappedPointer{};
413 using FN_cuGraphicsD3D11RegisterResource
414 = CUresult (*)(CUgraphicsResource*, ID3D11Resource*,
unsigned int);
415 FN_cuGraphicsD3D11RegisterResource graphicsD3D11Register{};
418 using FN_cuGraphicsGLRegisterBuffer
419 = CUresult (*)(CUgraphicsResource*,
unsigned int,
unsigned int);
420 FN_cuGraphicsGLRegisterBuffer graphicsGLRegisterBuffer{};
428 using FN_cuGraphicsGLRegisterImage = CUresult (*)(
429 CUgraphicsResource*,
unsigned int ,
unsigned int ,
431 FN_cuGraphicsGLRegisterImage graphicsGLRegisterImage{};
433 using FN_cuGraphicsSubResourceGetMappedArray = CUresult (*)(
434 CUarray*, CUgraphicsResource,
unsigned int ,
436 FN_cuGraphicsSubResourceGetMappedArray graphicsSubResourceGetMappedArray{};
439 using FN_cuImportExternalMemory
441 using FN_cuExternalMemoryGetMappedBuffer = CUresult (*)(
443 using FN_cuExternalMemoryGetMappedMipmappedArray = CUresult (*)(
444 CUmipmappedArray*, CUexternalMemory,
446 using FN_cuMipmappedArrayGetLevel
447 = CUresult (*)(CUarray*, CUmipmappedArray,
unsigned int);
448 using FN_cuMipmappedArrayDestroy = CUresult (*)(CUmipmappedArray);
449 using FN_cuDestroyExternalMemory = CUresult (*)(CUexternalMemory);
451 FN_cuImportExternalMemory importExtMem{};
452 FN_cuExternalMemoryGetMappedBuffer extMemGetMappedBuffer{};
453 FN_cuExternalMemoryGetMappedMipmappedArray getMapArray{};
454 FN_cuMipmappedArrayGetLevel getLevel{};
455 FN_cuMipmappedArrayDestroy destroyMipArray{};
456 FN_cuDestroyExternalMemory destroyExtMem{};
459 using FN_cuImportExternalSemaphore = CUresult (*)(
461 using FN_cuWaitExternalSemaphoresAsync = CUresult (*)(
462 const CUexternalSemaphore*,
464 using FN_cuDestroyExternalSemaphore = CUresult (*)(CUexternalSemaphore);
466 FN_cuImportExternalSemaphore importExtSem{};
467 FN_cuWaitExternalSemaphoresAsync waitExtSems{};
468 FN_cuDestroyExternalSemaphore destroyExtSem{};
471 using FN_cuMemcpy2DAsync = CUresult (*)(
const CUDA_MEMCPY2D*, CUstream);
472 FN_cuMemcpy2DAsync memcpy2DAsync{};
476 using FN_cuMemcpy2DUnaligned = CUresult (*)(
const CUDA_MEMCPY2D*);
477 FN_cuMemcpy2DUnaligned memcpy2DUnaligned{};
478 using FN_cuMemcpyHtoD = CUresult (*)(CUdeviceptr,
const void*, size_t);
479 FN_cuMemcpyHtoD memcpyHtoD{};
480 using FN_cuMemcpyDtoH = CUresult (*)(
void*, CUdeviceptr, size_t);
481 FN_cuMemcpyDtoH memcpyDtoH{};
482 using FN_cuMemcpyDtoDAsync
483 = CUresult (*)(CUdeviceptr, CUdeviceptr, size_t, CUstream);
484 FN_cuMemcpyDtoDAsync memcpyDtoDAsync{};
490 using FN_cuMemAlloc = CUresult (*)(CUdeviceptr*, size_t);
491 using FN_cuMemFree = CUresult (*)(CUdeviceptr);
492 FN_cuMemAlloc memAlloc{};
493 FN_cuMemFree memFree{};
500 = CUresult (*)(CUmemGenericAllocationHandle*, size_t,
502 using FN_cuMemAddressReserve
503 = CUresult (*)(CUdeviceptr*, size_t, size_t, CUdeviceptr,
506 = CUresult (*)(CUdeviceptr, size_t, size_t,
507 CUmemGenericAllocationHandle,
unsigned long long);
508 using FN_cuMemSetAccess
510 using FN_cuMemUnmap = CUresult (*)(CUdeviceptr, size_t);
511 using FN_cuMemAddressFree = CUresult (*)(CUdeviceptr, size_t);
512 using FN_cuMemRelease = CUresult (*)(CUmemGenericAllocationHandle);
513 using FN_cuMemExportToShareableHandle
514 = CUresult (*)(
void*, CUmemGenericAllocationHandle, int,
unsigned long long);
515 using FN_cuMemGetAllocationGranularity
517 CUmemAllocationGranularity_flags);
519 FN_cuMemCreate memCreate{};
520 FN_cuMemAddressReserve memAddressReserve{};
521 FN_cuMemMap memMap{};
522 FN_cuMemSetAccess memSetAccess{};
523 FN_cuMemUnmap memUnmap{};
524 FN_cuMemAddressFree memAddressFree{};
525 FN_cuMemRelease memRelease{};
530 FN_cuMemGetAllocationGranularity memGetGranularity{};
538 = CUresult (*)(
void*, CUdeviceptr, size_t, CUmemRangeHandleType,
547 using FN_cuPointerSetAttribute
548 = CUresult (*)(
const void*,
int , CUdeviceptr);
549 FN_cuPointerSetAttribute pointerSetAttribute{};
555 bool loaded() const noexcept {
return lib !=
nullptr; }
569 lib = (
void*)LoadLibraryA(
"nvcuda.dll");
572 auto sym = [
this](
const char* n) {
573 return (
void*)GetProcAddress((HMODULE)lib, n);
576 lib = dlopen(
"libcuda.so.1", RTLD_NOW);
579 auto sym = [
this](
const char* n) {
return dlsym(lib, n); };
583 init = (FN_cuInit)sym(
"cuInit");
584 deviceGetCount = (FN_cuDeviceGetCount)sym(
"cuDeviceGetCount");
585 deviceGet = (FN_cuDeviceGet)sym(
"cuDeviceGet");
586 deviceGetAttribute = (FN_cuDeviceGetAttribute)sym(
"cuDeviceGetAttribute");
587 primaryCtxRetain = (FN_cuDevicePrimaryCtxRetain)sym(
"cuDevicePrimaryCtxRetain");
588 primaryCtxRelease = (FN_cuDevicePrimaryCtxRelease)sym(
"cuDevicePrimaryCtxRelease_v2");
589 ctxSetCurrent = (FN_cuCtxSetCurrent)sym(
"cuCtxSetCurrent");
590 ctxGetDevice = (FN_cuCtxGetDevice)sym(
"cuCtxGetDevice");
591 ctxPush = (FN_cuCtxPushCurrent)sym(
"cuCtxPushCurrent_v2");
592 ctxPop = (FN_cuCtxPopCurrent)sym(
"cuCtxPopCurrent_v2");
595 streamCreate = (FN_cuStreamCreate)sym(
"cuStreamCreate");
596 streamDestroy = (FN_cuStreamDestroy)sym(
"cuStreamDestroy_v2");
597 streamSync = (FN_cuStreamSynchronize)sym(
"cuStreamSynchronize");
600 getErrorString = (FN_cuGetErrorString)sym(
"cuGetErrorString");
603 graphicsMap = (FN_cuGraphicsMapResources)sym(
"cuGraphicsMapResources");
604 graphicsUnmap = (FN_cuGraphicsUnmapResources)sym(
"cuGraphicsUnmapResources");
606 = (FN_cuGraphicsUnregisterResource)sym(
"cuGraphicsUnregisterResource");
607 graphicsGetMappedPointer = (FN_cuGraphicsResourceGetMappedPointer)sym(
608 "cuGraphicsResourceGetMappedPointer_v2");
611 graphicsD3D11Register = (FN_cuGraphicsD3D11RegisterResource)sym(
612 "cuGraphicsD3D11RegisterResource");
614 graphicsGLRegisterBuffer
615 = (FN_cuGraphicsGLRegisterBuffer)sym(
"cuGraphicsGLRegisterBuffer");
616 graphicsGLRegisterImage
617 = (FN_cuGraphicsGLRegisterImage)sym(
"cuGraphicsGLRegisterImage");
618 graphicsSubResourceGetMappedArray
619 = (FN_cuGraphicsSubResourceGetMappedArray)sym(
620 "cuGraphicsSubResourceGetMappedArray");
623 importExtMem = (FN_cuImportExternalMemory)sym(
"cuImportExternalMemory");
624 extMemGetMappedBuffer = (FN_cuExternalMemoryGetMappedBuffer)sym(
625 "cuExternalMemoryGetMappedBuffer");
626 getMapArray = (FN_cuExternalMemoryGetMappedMipmappedArray)sym(
627 "cuExternalMemoryGetMappedMipmappedArray");
628 getLevel = (FN_cuMipmappedArrayGetLevel)sym(
"cuMipmappedArrayGetLevel");
629 destroyMipArray = (FN_cuMipmappedArrayDestroy)sym(
"cuMipmappedArrayDestroy");
630 destroyExtMem = (FN_cuDestroyExternalMemory)sym(
"cuDestroyExternalMemory");
634 = (FN_cuImportExternalSemaphore)sym(
"cuImportExternalSemaphore");
635 waitExtSems = (FN_cuWaitExternalSemaphoresAsync)sym(
636 "cuWaitExternalSemaphoresAsync");
638 = (FN_cuDestroyExternalSemaphore)sym(
"cuDestroyExternalSemaphore");
641 memcpy2DAsync = (FN_cuMemcpy2DAsync)sym(
"cuMemcpy2DAsync_v2");
642 memcpy2DUnaligned = (FN_cuMemcpy2DUnaligned)sym(
"cuMemcpy2DUnaligned_v2");
643 memcpyHtoD = (FN_cuMemcpyHtoD)sym(
"cuMemcpyHtoD_v2");
644 memcpyDtoH = (FN_cuMemcpyDtoH)sym(
"cuMemcpyDtoH_v2");
645 memcpyDtoDAsync = (FN_cuMemcpyDtoDAsync)sym(
"cuMemcpyDtoDAsync_v2");
648 memAlloc = (FN_cuMemAlloc)sym(
"cuMemAlloc_v2");
649 memFree = (FN_cuMemFree)sym(
"cuMemFree_v2");
654 memCreate = (FN_cuMemCreate)sym(
"cuMemCreate");
655 memAddressReserve = (FN_cuMemAddressReserve)sym(
"cuMemAddressReserve");
656 memMap = (FN_cuMemMap)sym(
"cuMemMap");
657 memSetAccess = (FN_cuMemSetAccess)sym(
"cuMemSetAccess");
658 memUnmap = (FN_cuMemUnmap)sym(
"cuMemUnmap");
659 memAddressFree = (FN_cuMemAddressFree)sym(
"cuMemAddressFree");
660 memRelease = (FN_cuMemRelease)sym(
"cuMemRelease");
662 = (FN_cuMemExportToShareableHandle)sym(
"cuMemExportToShareableHandle");
663 memGetGranularity = (FN_cuMemGetAllocationGranularity)sym(
664 "cuMemGetAllocationGranularity");
666 "cuMemGetHandleForAddressRange");
668 = (FN_cuPointerSetAttribute)sym(
"cuPointerSetAttribute");
669 vmmSupported = memCreate && memAddressReserve && memMap && memSetAccess
670 && memUnmap && memAddressFree && memRelease
671 && memGetGranularity;
673 const bool ok = init && deviceGetCount && deviceGet && deviceGetAttribute
674 && primaryCtxRetain && primaryCtxRelease && ctxSetCurrent
675 && ctxGetDevice && ctxPush && ctxPop && streamCreate
676 && streamDestroy && streamSync && getErrorString
677 && graphicsMap && graphicsUnmap && graphicsUnregister
678 && graphicsGetMappedPointer && importExtMem
679 && extMemGetMappedBuffer && getMapArray && getLevel
680 && destroyMipArray && destroyExtMem && importExtSem
681 && waitExtSems && destroyExtSem && memcpy2DAsync
682 && memcpyHtoD && memcpyDtoH && memcpyDtoDAsync && memAlloc
701 if(!deviceGetAttribute || !memGetHandleForAddressRange)
704 if(deviceGetAttribute(&v, CU_DEVICE_ATTRIBUTE_DMA_BUF_SUPPORTED, device)
710 void unload() noexcept
715 FreeLibrary((HMODULE)lib);
722 ~CudaFunctions() { unload(); }
724 CudaFunctions() =
default;
725 CudaFunctions(
const CudaFunctions&) =
delete;
726 CudaFunctions& operator=(
const CudaFunctions&) =
delete;