# Copyright 2021-2024 NVIDIA Corporation. All rights reserved. # # Please refer to the NVIDIA end user license agreement (EULA) associated # with this source code for terms and conditions that govern your use of # this software. Any use, reproduction, disclosure, or distribution of # this software and related documentation outside the terms of the EULA # is strictly prohibited. {{if 'Windows' == platform.system()}} import win32api import struct from pywintypes import error {{else}} cimport cuda._lib.dlfcn as dlfcn {{endif}} cdef bint __cuPythonInit = False {{if 'nvrtcGetErrorString' in found_functions}}cdef void *__nvrtcGetErrorString = NULL{{endif}} {{if 'nvrtcVersion' in found_functions}}cdef void *__nvrtcVersion = NULL{{endif}} {{if 'nvrtcGetNumSupportedArchs' in found_functions}}cdef void *__nvrtcGetNumSupportedArchs = NULL{{endif}} {{if 'nvrtcGetSupportedArchs' in found_functions}}cdef void *__nvrtcGetSupportedArchs = NULL{{endif}} {{if 'nvrtcCreateProgram' in found_functions}}cdef void *__nvrtcCreateProgram = NULL{{endif}} {{if 'nvrtcDestroyProgram' in found_functions}}cdef void *__nvrtcDestroyProgram = NULL{{endif}} {{if 'nvrtcCompileProgram' in found_functions}}cdef void *__nvrtcCompileProgram = NULL{{endif}} {{if 'nvrtcGetPTXSize' in found_functions}}cdef void *__nvrtcGetPTXSize = NULL{{endif}} {{if 'nvrtcGetPTX' in found_functions}}cdef void *__nvrtcGetPTX = NULL{{endif}} {{if 'nvrtcGetCUBINSize' in found_functions}}cdef void *__nvrtcGetCUBINSize = NULL{{endif}} {{if 'nvrtcGetCUBIN' in found_functions}}cdef void *__nvrtcGetCUBIN = NULL{{endif}} {{if 'nvrtcGetNVVMSize' in found_functions}}cdef void *__nvrtcGetNVVMSize = NULL{{endif}} {{if 'nvrtcGetNVVM' in found_functions}}cdef void *__nvrtcGetNVVM = NULL{{endif}} {{if 'nvrtcGetLTOIRSize' in found_functions}}cdef void *__nvrtcGetLTOIRSize = NULL{{endif}} {{if 'nvrtcGetLTOIR' in found_functions}}cdef void *__nvrtcGetLTOIR = NULL{{endif}} {{if 'nvrtcGetOptiXIRSize' in found_functions}}cdef void *__nvrtcGetOptiXIRSize = NULL{{endif}} {{if 'nvrtcGetOptiXIR' in found_functions}}cdef void *__nvrtcGetOptiXIR = NULL{{endif}} {{if 'nvrtcGetProgramLogSize' in found_functions}}cdef void *__nvrtcGetProgramLogSize = NULL{{endif}} {{if 'nvrtcGetProgramLog' in found_functions}}cdef void *__nvrtcGetProgramLog = NULL{{endif}} {{if 'nvrtcAddNameExpression' in found_functions}}cdef void *__nvrtcAddNameExpression = NULL{{endif}} {{if 'nvrtcGetLoweredName' in found_functions}}cdef void *__nvrtcGetLoweredName = NULL{{endif}} cdef int cuPythonInit() except -1 nogil: global __cuPythonInit if __cuPythonInit: return 0 __cuPythonInit = True # Load library {{if 'Windows' == platform.system()}} LOAD_LIBRARY_SAFE_CURRENT_DIRS = 0x00002000 with gil: try: handle = win32api.LoadLibraryEx("nvrtc64_120_0.dll", 0, LOAD_LIBRARY_SAFE_CURRENT_DIRS) except: raise RuntimeError('Failed to LoadLibraryEx nvrtc64_120_0.dll') {{else}} handle = NULL if handle == NULL: handle = dlfcn.dlopen('libnvrtc.so.12', dlfcn.RTLD_NOW) if handle == NULL: with gil: raise RuntimeError('Failed to dlopen libnvrtc.so.12') {{endif}} # Load function {{if 'Windows' == platform.system()}} with gil: {{if 'nvrtcGetErrorString' in found_functions}} try: global __nvrtcGetErrorString __nvrtcGetErrorString = win32api.GetProcAddress(handle, 'nvrtcGetErrorString') except: pass {{endif}} {{if 'nvrtcVersion' in found_functions}} try: global __nvrtcVersion __nvrtcVersion = win32api.GetProcAddress(handle, 'nvrtcVersion') except: pass {{endif}} {{if 'nvrtcGetNumSupportedArchs' in found_functions}} try: global __nvrtcGetNumSupportedArchs __nvrtcGetNumSupportedArchs = win32api.GetProcAddress(handle, 'nvrtcGetNumSupportedArchs') except: pass {{endif}} {{if 'nvrtcGetSupportedArchs' in found_functions}} try: global __nvrtcGetSupportedArchs __nvrtcGetSupportedArchs = win32api.GetProcAddress(handle, 'nvrtcGetSupportedArchs') except: pass {{endif}} {{if 'nvrtcCreateProgram' in found_functions}} try: global __nvrtcCreateProgram __nvrtcCreateProgram = win32api.GetProcAddress(handle, 'nvrtcCreateProgram') except: pass {{endif}} {{if 'nvrtcDestroyProgram' in found_functions}} try: global __nvrtcDestroyProgram __nvrtcDestroyProgram = win32api.GetProcAddress(handle, 'nvrtcDestroyProgram') except: pass {{endif}} {{if 'nvrtcCompileProgram' in found_functions}} try: global __nvrtcCompileProgram __nvrtcCompileProgram = win32api.GetProcAddress(handle, 'nvrtcCompileProgram') except: pass {{endif}} {{if 'nvrtcGetPTXSize' in found_functions}} try: global __nvrtcGetPTXSize __nvrtcGetPTXSize = win32api.GetProcAddress(handle, 'nvrtcGetPTXSize') except: pass {{endif}} {{if 'nvrtcGetPTX' in found_functions}} try: global __nvrtcGetPTX __nvrtcGetPTX = win32api.GetProcAddress(handle, 'nvrtcGetPTX') except: pass {{endif}} {{if 'nvrtcGetCUBINSize' in found_functions}} try: global __nvrtcGetCUBINSize __nvrtcGetCUBINSize = win32api.GetProcAddress(handle, 'nvrtcGetCUBINSize') except: pass {{endif}} {{if 'nvrtcGetCUBIN' in found_functions}} try: global __nvrtcGetCUBIN __nvrtcGetCUBIN = win32api.GetProcAddress(handle, 'nvrtcGetCUBIN') except: pass {{endif}} {{if 'nvrtcGetNVVMSize' in found_functions}} try: global __nvrtcGetNVVMSize __nvrtcGetNVVMSize = win32api.GetProcAddress(handle, 'nvrtcGetNVVMSize') except: pass {{endif}} {{if 'nvrtcGetNVVM' in found_functions}} try: global __nvrtcGetNVVM __nvrtcGetNVVM = win32api.GetProcAddress(handle, 'nvrtcGetNVVM') except: pass {{endif}} {{if 'nvrtcGetLTOIRSize' in found_functions}} try: global __nvrtcGetLTOIRSize __nvrtcGetLTOIRSize = win32api.GetProcAddress(handle, 'nvrtcGetLTOIRSize') except: pass {{endif}} {{if 'nvrtcGetLTOIR' in found_functions}} try: global __nvrtcGetLTOIR __nvrtcGetLTOIR = win32api.GetProcAddress(handle, 'nvrtcGetLTOIR') except: pass {{endif}} {{if 'nvrtcGetOptiXIRSize' in found_functions}} try: global __nvrtcGetOptiXIRSize __nvrtcGetOptiXIRSize = win32api.GetProcAddress(handle, 'nvrtcGetOptiXIRSize') except: pass {{endif}} {{if 'nvrtcGetOptiXIR' in found_functions}} try: global __nvrtcGetOptiXIR __nvrtcGetOptiXIR = win32api.GetProcAddress(handle, 'nvrtcGetOptiXIR') except: pass {{endif}} {{if 'nvrtcGetProgramLogSize' in found_functions}} try: global __nvrtcGetProgramLogSize __nvrtcGetProgramLogSize = win32api.GetProcAddress(handle, 'nvrtcGetProgramLogSize') except: pass {{endif}} {{if 'nvrtcGetProgramLog' in found_functions}} try: global __nvrtcGetProgramLog __nvrtcGetProgramLog = win32api.GetProcAddress(handle, 'nvrtcGetProgramLog') except: pass {{endif}} {{if 'nvrtcAddNameExpression' in found_functions}} try: global __nvrtcAddNameExpression __nvrtcAddNameExpression = win32api.GetProcAddress(handle, 'nvrtcAddNameExpression') except: pass {{endif}} {{if 'nvrtcGetLoweredName' in found_functions}} try: global __nvrtcGetLoweredName __nvrtcGetLoweredName = win32api.GetProcAddress(handle, 'nvrtcGetLoweredName') except: pass {{endif}} {{else}} {{if 'nvrtcGetErrorString' in found_functions}} global __nvrtcGetErrorString __nvrtcGetErrorString = dlfcn.dlsym(handle, 'nvrtcGetErrorString') {{endif}} {{if 'nvrtcVersion' in found_functions}} global __nvrtcVersion __nvrtcVersion = dlfcn.dlsym(handle, 'nvrtcVersion') {{endif}} {{if 'nvrtcGetNumSupportedArchs' in found_functions}} global __nvrtcGetNumSupportedArchs __nvrtcGetNumSupportedArchs = dlfcn.dlsym(handle, 'nvrtcGetNumSupportedArchs') {{endif}} {{if 'nvrtcGetSupportedArchs' in found_functions}} global __nvrtcGetSupportedArchs __nvrtcGetSupportedArchs = dlfcn.dlsym(handle, 'nvrtcGetSupportedArchs') {{endif}} {{if 'nvrtcCreateProgram' in found_functions}} global __nvrtcCreateProgram __nvrtcCreateProgram = dlfcn.dlsym(handle, 'nvrtcCreateProgram') {{endif}} {{if 'nvrtcDestroyProgram' in found_functions}} global __nvrtcDestroyProgram __nvrtcDestroyProgram = dlfcn.dlsym(handle, 'nvrtcDestroyProgram') {{endif}} {{if 'nvrtcCompileProgram' in found_functions}} global __nvrtcCompileProgram __nvrtcCompileProgram = dlfcn.dlsym(handle, 'nvrtcCompileProgram') {{endif}} {{if 'nvrtcGetPTXSize' in found_functions}} global __nvrtcGetPTXSize __nvrtcGetPTXSize = dlfcn.dlsym(handle, 'nvrtcGetPTXSize') {{endif}} {{if 'nvrtcGetPTX' in found_functions}} global __nvrtcGetPTX __nvrtcGetPTX = dlfcn.dlsym(handle, 'nvrtcGetPTX') {{endif}} {{if 'nvrtcGetCUBINSize' in found_functions}} global __nvrtcGetCUBINSize __nvrtcGetCUBINSize = dlfcn.dlsym(handle, 'nvrtcGetCUBINSize') {{endif}} {{if 'nvrtcGetCUBIN' in found_functions}} global __nvrtcGetCUBIN __nvrtcGetCUBIN = dlfcn.dlsym(handle, 'nvrtcGetCUBIN') {{endif}} {{if 'nvrtcGetNVVMSize' in found_functions}} global __nvrtcGetNVVMSize __nvrtcGetNVVMSize = dlfcn.dlsym(handle, 'nvrtcGetNVVMSize') {{endif}} {{if 'nvrtcGetNVVM' in found_functions}} global __nvrtcGetNVVM __nvrtcGetNVVM = dlfcn.dlsym(handle, 'nvrtcGetNVVM') {{endif}} {{if 'nvrtcGetLTOIRSize' in found_functions}} global __nvrtcGetLTOIRSize __nvrtcGetLTOIRSize = dlfcn.dlsym(handle, 'nvrtcGetLTOIRSize') {{endif}} {{if 'nvrtcGetLTOIR' in found_functions}} global __nvrtcGetLTOIR __nvrtcGetLTOIR = dlfcn.dlsym(handle, 'nvrtcGetLTOIR') {{endif}} {{if 'nvrtcGetOptiXIRSize' in found_functions}} global __nvrtcGetOptiXIRSize __nvrtcGetOptiXIRSize = dlfcn.dlsym(handle, 'nvrtcGetOptiXIRSize') {{endif}} {{if 'nvrtcGetOptiXIR' in found_functions}} global __nvrtcGetOptiXIR __nvrtcGetOptiXIR = dlfcn.dlsym(handle, 'nvrtcGetOptiXIR') {{endif}} {{if 'nvrtcGetProgramLogSize' in found_functions}} global __nvrtcGetProgramLogSize __nvrtcGetProgramLogSize = dlfcn.dlsym(handle, 'nvrtcGetProgramLogSize') {{endif}} {{if 'nvrtcGetProgramLog' in found_functions}} global __nvrtcGetProgramLog __nvrtcGetProgramLog = dlfcn.dlsym(handle, 'nvrtcGetProgramLog') {{endif}} {{if 'nvrtcAddNameExpression' in found_functions}} global __nvrtcAddNameExpression __nvrtcAddNameExpression = dlfcn.dlsym(handle, 'nvrtcAddNameExpression') {{endif}} {{if 'nvrtcGetLoweredName' in found_functions}} global __nvrtcGetLoweredName __nvrtcGetLoweredName = dlfcn.dlsym(handle, 'nvrtcGetLoweredName') {{endif}} {{endif}} {{if 'nvrtcGetErrorString' in found_functions}} cdef const char* _nvrtcGetErrorString(nvrtcResult result) except ?NULL nogil: global __nvrtcGetErrorString cuPythonInit() if __nvrtcGetErrorString == NULL: with gil: raise RuntimeError('Function "nvrtcGetErrorString" not found') err = ( __nvrtcGetErrorString)(result) return err {{endif}} {{if 'nvrtcVersion' in found_functions}} cdef nvrtcResult _nvrtcVersion(int* major, int* minor) except ?NVRTC_ERROR_INVALID_INPUT nogil: global __nvrtcVersion cuPythonInit() if __nvrtcVersion == NULL: with gil: raise RuntimeError('Function "nvrtcVersion" not found') err = ( __nvrtcVersion)(major, minor) return err {{endif}} {{if 'nvrtcGetNumSupportedArchs' in found_functions}} cdef nvrtcResult _nvrtcGetNumSupportedArchs(int* numArchs) except ?NVRTC_ERROR_INVALID_INPUT nogil: global __nvrtcGetNumSupportedArchs cuPythonInit() if __nvrtcGetNumSupportedArchs == NULL: with gil: raise RuntimeError('Function "nvrtcGetNumSupportedArchs" not found') err = ( __nvrtcGetNumSupportedArchs)(numArchs) return err {{endif}} {{if 'nvrtcGetSupportedArchs' in found_functions}} cdef nvrtcResult _nvrtcGetSupportedArchs(int* supportedArchs) except ?NVRTC_ERROR_INVALID_INPUT nogil: global __nvrtcGetSupportedArchs cuPythonInit() if __nvrtcGetSupportedArchs == NULL: with gil: raise RuntimeError('Function "nvrtcGetSupportedArchs" not found') err = ( __nvrtcGetSupportedArchs)(supportedArchs) return err {{endif}} {{if 'nvrtcCreateProgram' in found_functions}} cdef nvrtcResult _nvrtcCreateProgram(nvrtcProgram* prog, const char* src, const char* name, int numHeaders, const char** headers, const char** includeNames) except ?NVRTC_ERROR_INVALID_INPUT nogil: global __nvrtcCreateProgram cuPythonInit() if __nvrtcCreateProgram == NULL: with gil: raise RuntimeError('Function "nvrtcCreateProgram" not found') err = ( __nvrtcCreateProgram)(prog, src, name, numHeaders, headers, includeNames) return err {{endif}} {{if 'nvrtcDestroyProgram' in found_functions}} cdef nvrtcResult _nvrtcDestroyProgram(nvrtcProgram* prog) except ?NVRTC_ERROR_INVALID_INPUT nogil: global __nvrtcDestroyProgram cuPythonInit() if __nvrtcDestroyProgram == NULL: with gil: raise RuntimeError('Function "nvrtcDestroyProgram" not found') err = ( __nvrtcDestroyProgram)(prog) return err {{endif}} {{if 'nvrtcCompileProgram' in found_functions}} cdef nvrtcResult _nvrtcCompileProgram(nvrtcProgram prog, int numOptions, const char** options) except ?NVRTC_ERROR_INVALID_INPUT nogil: global __nvrtcCompileProgram cuPythonInit() if __nvrtcCompileProgram == NULL: with gil: raise RuntimeError('Function "nvrtcCompileProgram" not found') err = ( __nvrtcCompileProgram)(prog, numOptions, options) return err {{endif}} {{if 'nvrtcGetPTXSize' in found_functions}} cdef nvrtcResult _nvrtcGetPTXSize(nvrtcProgram prog, size_t* ptxSizeRet) except ?NVRTC_ERROR_INVALID_INPUT nogil: global __nvrtcGetPTXSize cuPythonInit() if __nvrtcGetPTXSize == NULL: with gil: raise RuntimeError('Function "nvrtcGetPTXSize" not found') err = ( __nvrtcGetPTXSize)(prog, ptxSizeRet) return err {{endif}} {{if 'nvrtcGetPTX' in found_functions}} cdef nvrtcResult _nvrtcGetPTX(nvrtcProgram prog, char* ptx) except ?NVRTC_ERROR_INVALID_INPUT nogil: global __nvrtcGetPTX cuPythonInit() if __nvrtcGetPTX == NULL: with gil: raise RuntimeError('Function "nvrtcGetPTX" not found') err = ( __nvrtcGetPTX)(prog, ptx) return err {{endif}} {{if 'nvrtcGetCUBINSize' in found_functions}} cdef nvrtcResult _nvrtcGetCUBINSize(nvrtcProgram prog, size_t* cubinSizeRet) except ?NVRTC_ERROR_INVALID_INPUT nogil: global __nvrtcGetCUBINSize cuPythonInit() if __nvrtcGetCUBINSize == NULL: with gil: raise RuntimeError('Function "nvrtcGetCUBINSize" not found') err = ( __nvrtcGetCUBINSize)(prog, cubinSizeRet) return err {{endif}} {{if 'nvrtcGetCUBIN' in found_functions}} cdef nvrtcResult _nvrtcGetCUBIN(nvrtcProgram prog, char* cubin) except ?NVRTC_ERROR_INVALID_INPUT nogil: global __nvrtcGetCUBIN cuPythonInit() if __nvrtcGetCUBIN == NULL: with gil: raise RuntimeError('Function "nvrtcGetCUBIN" not found') err = ( __nvrtcGetCUBIN)(prog, cubin) return err {{endif}} {{if 'nvrtcGetNVVMSize' in found_functions}} cdef nvrtcResult _nvrtcGetNVVMSize(nvrtcProgram prog, size_t* nvvmSizeRet) except ?NVRTC_ERROR_INVALID_INPUT nogil: global __nvrtcGetNVVMSize cuPythonInit() if __nvrtcGetNVVMSize == NULL: with gil: raise RuntimeError('Function "nvrtcGetNVVMSize" not found') err = ( __nvrtcGetNVVMSize)(prog, nvvmSizeRet) return err {{endif}} {{if 'nvrtcGetNVVM' in found_functions}} cdef nvrtcResult _nvrtcGetNVVM(nvrtcProgram prog, char* nvvm) except ?NVRTC_ERROR_INVALID_INPUT nogil: global __nvrtcGetNVVM cuPythonInit() if __nvrtcGetNVVM == NULL: with gil: raise RuntimeError('Function "nvrtcGetNVVM" not found') err = ( __nvrtcGetNVVM)(prog, nvvm) return err {{endif}} {{if 'nvrtcGetLTOIRSize' in found_functions}} cdef nvrtcResult _nvrtcGetLTOIRSize(nvrtcProgram prog, size_t* LTOIRSizeRet) except ?NVRTC_ERROR_INVALID_INPUT nogil: global __nvrtcGetLTOIRSize cuPythonInit() if __nvrtcGetLTOIRSize == NULL: with gil: raise RuntimeError('Function "nvrtcGetLTOIRSize" not found') err = ( __nvrtcGetLTOIRSize)(prog, LTOIRSizeRet) return err {{endif}} {{if 'nvrtcGetLTOIR' in found_functions}} cdef nvrtcResult _nvrtcGetLTOIR(nvrtcProgram prog, char* LTOIR) except ?NVRTC_ERROR_INVALID_INPUT nogil: global __nvrtcGetLTOIR cuPythonInit() if __nvrtcGetLTOIR == NULL: with gil: raise RuntimeError('Function "nvrtcGetLTOIR" not found') err = ( __nvrtcGetLTOIR)(prog, LTOIR) return err {{endif}} {{if 'nvrtcGetOptiXIRSize' in found_functions}} cdef nvrtcResult _nvrtcGetOptiXIRSize(nvrtcProgram prog, size_t* optixirSizeRet) except ?NVRTC_ERROR_INVALID_INPUT nogil: global __nvrtcGetOptiXIRSize cuPythonInit() if __nvrtcGetOptiXIRSize == NULL: with gil: raise RuntimeError('Function "nvrtcGetOptiXIRSize" not found') err = ( __nvrtcGetOptiXIRSize)(prog, optixirSizeRet) return err {{endif}} {{if 'nvrtcGetOptiXIR' in found_functions}} cdef nvrtcResult _nvrtcGetOptiXIR(nvrtcProgram prog, char* optixir) except ?NVRTC_ERROR_INVALID_INPUT nogil: global __nvrtcGetOptiXIR cuPythonInit() if __nvrtcGetOptiXIR == NULL: with gil: raise RuntimeError('Function "nvrtcGetOptiXIR" not found') err = ( __nvrtcGetOptiXIR)(prog, optixir) return err {{endif}} {{if 'nvrtcGetProgramLogSize' in found_functions}} cdef nvrtcResult _nvrtcGetProgramLogSize(nvrtcProgram prog, size_t* logSizeRet) except ?NVRTC_ERROR_INVALID_INPUT nogil: global __nvrtcGetProgramLogSize cuPythonInit() if __nvrtcGetProgramLogSize == NULL: with gil: raise RuntimeError('Function "nvrtcGetProgramLogSize" not found') err = ( __nvrtcGetProgramLogSize)(prog, logSizeRet) return err {{endif}} {{if 'nvrtcGetProgramLog' in found_functions}} cdef nvrtcResult _nvrtcGetProgramLog(nvrtcProgram prog, char* log) except ?NVRTC_ERROR_INVALID_INPUT nogil: global __nvrtcGetProgramLog cuPythonInit() if __nvrtcGetProgramLog == NULL: with gil: raise RuntimeError('Function "nvrtcGetProgramLog" not found') err = ( __nvrtcGetProgramLog)(prog, log) return err {{endif}} {{if 'nvrtcAddNameExpression' in found_functions}} cdef nvrtcResult _nvrtcAddNameExpression(nvrtcProgram prog, const char* name_expression) except ?NVRTC_ERROR_INVALID_INPUT nogil: global __nvrtcAddNameExpression cuPythonInit() if __nvrtcAddNameExpression == NULL: with gil: raise RuntimeError('Function "nvrtcAddNameExpression" not found') err = ( __nvrtcAddNameExpression)(prog, name_expression) return err {{endif}} {{if 'nvrtcGetLoweredName' in found_functions}} cdef nvrtcResult _nvrtcGetLoweredName(nvrtcProgram prog, const char* name_expression, const char** lowered_name) except ?NVRTC_ERROR_INVALID_INPUT nogil: global __nvrtcGetLoweredName cuPythonInit() if __nvrtcGetLoweredName == NULL: with gil: raise RuntimeError('Function "nvrtcGetLoweredName" not found') err = ( __nvrtcGetLoweredName)(prog, name_expression, lowered_name) return err {{endif}}