@@ -242,12 +242,23 @@ static CUresult LOAD_LIBRARY(CUDADRIVER *pInstance)
242242
243243 if (* pInstance == NULL ) {
244244 printf ("LoadLibrary \"%s\" failed!\n" , __CudaLibName );
245- return CUDA_ERROR_UNKNOWN ;
245+ exit ( EXIT_FAILURE ) ;
246246 }
247247
248248 return CUDA_SUCCESS ;
249249}
250250
251+ CUresult GET_DRIVER_HANDLE (CUDADRIVER * pInstance )
252+ {
253+ * pInstance = GetModuleHandle (__CudaLibName );
254+ if (* pInstance ) {
255+ return CUDA_SUCCESS ;
256+ }
257+ else {
258+ return CUDA_ERROR_UNKNOWN ;
259+ }
260+ }
261+
251262#define GET_PROC_EX (name , alias , required ) \
252263 alias = (t##name *)GetProcAddress(CudaDrvLib, #name); \
253264 if (alias == NULL && required) { \
@@ -269,6 +280,13 @@ static CUresult LOAD_LIBRARY(CUDADRIVER *pInstance)
269280 return CUDA_ERROR_UNKNOWN; \
270281 }
271282
283+ #define GET_PROC_ERROR_FUNCTIONS (name , alias , required ) \
284+ alias = (t##name *)GetProcAddress(CudaDrvLib, #name); \
285+ if (alias == NULL && required) { \
286+ printf("Failed to find error function \"%s\" in %s\n", #name, __CudaLibName); \
287+ exit(EXIT_FAILURE); \
288+ }
289+
272290#elif defined(__unix__ ) || defined(__QNX__ ) || defined(__APPLE__ ) || defined(__MACOSX )
273291
274292#include <dlfcn.h>
@@ -293,12 +311,23 @@ static CUresult LOAD_LIBRARY(CUDADRIVER *pInstance)
293311
294312 if (* pInstance == NULL ) {
295313 printf ("dlopen \"%s\" failed!\n" , __CudaLibName );
296- return CUDA_ERROR_UNKNOWN ;
314+ exit ( EXIT_FAILURE ) ;
297315 }
298316
299317 return CUDA_SUCCESS ;
300318}
301319
320+ CUresult GET_DRIVER_HANDLE (CUDADRIVER * pInstance )
321+ {
322+ * pInstance = dlopen (__CudaLibName , RTLD_LAZY );
323+ if (* pInstance ) {
324+ return CUDA_SUCCESS ;
325+ }
326+ else {
327+ return CUDA_ERROR_UNKNOWN ;
328+ }
329+ }
330+
302331#define GET_PROC_EX (name , alias , required ) \
303332 alias = (t##name *)dlsym(CudaDrvLib, #name); \
304333 if (alias == NULL && required) { \
@@ -320,6 +349,13 @@ static CUresult LOAD_LIBRARY(CUDADRIVER *pInstance)
320349 return CUDA_ERROR_UNKNOWN; \
321350 }
322351
352+ #define GET_PROC_ERROR_FUNCTIONS (name , alias , required ) \
353+ alias = (t##name *)dlsym(CudaDrvLib, #name); \
354+ if (alias == NULL && required) { \
355+ printf("Failed to find error function \"%s\" in %s\n", #name, __CudaLibName); \
356+ exit(EXIT_FAILURE); \
357+ }
358+
323359#else
324360#error unsupported platform
325361#endif
@@ -338,11 +374,19 @@ static CUresult LOAD_LIBRARY(CUDADRIVER *pInstance)
338374#define GET_PROC_V2 (name ) GET_PROC_EX_V2(name, name, 1)
339375#define GET_PROC_V3 (name ) GET_PROC_EX_V3(name, name, 1)
340376
377+ CUresult INIT_ERROR_FUNCTIONS (void )
378+ {
379+ CUDADRIVER CudaDrvLib ;
380+ CUresult result = CUDA_SUCCESS ;
381+ result = GET_DRIVER_HANDLE (& CudaDrvLib );
382+ GET_PROC_ERROR_FUNCTIONS (cuGetErrorString , cuGetErrorString , 1 );
383+ return result ;
384+ }
385+
341386CUresult CUDAAPI cuInit (unsigned int Flags , int cudaVersion )
342387{
343388 CUDADRIVER CudaDrvLib ;
344389 int driverVer = 1000 ;
345-
346390 CHECKED_CALL (LOAD_LIBRARY (& CudaDrvLib ));
347391
348392 // cuInit is required; alias it to _cuInit
@@ -619,6 +663,5 @@ CUresult CUDAAPI cuInit(unsigned int Flags, int cudaVersion)
619663 GET_PROC (cuGraphicsD3D9RegisterResource );
620664#endif
621665 }
622-
623666 return CUDA_SUCCESS ;
624667}
0 commit comments