Skip to content

Commit f46d0f4

Browse files
committed
Fix #951: Fallback gracefully if a library can't be loaded
1 parent 85ff9c2 commit f46d0f4

File tree

9 files changed

+166
-117
lines changed

9 files changed

+166
-117
lines changed

cuda_bindings/cuda/bindings/_internal/cufile_linux.pyx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import threading
99

1010
from .utils import FunctionNotFoundError, NotSupportedError
1111

12-
from cuda.pathfinder import load_nvidia_dynamic_lib
12+
from cuda.pathfinder import load_nvidia_dynamic_lib, DynamicLibNotFoundError
1313

1414
import cython
1515

@@ -104,7 +104,11 @@ cdef void* __cuFileGetParameterPosixPoolSlabArray = NULL
104104

105105

106106
cdef void* load_library() except* with gil:
107-
cdef uintptr_t handle = load_nvidia_dynamic_lib("cufile")._handle_uint
107+
cdef uintptr_t handle
108+
try:
109+
handle = load_nvidia_dynamic_lib("cufile")._handle_uint
110+
except DynamicLibNotFoundError:
111+
handle = 0
108112
return <void*>handle
109113

110114

cuda_bindings/cuda/bindings/_internal/nvjitlink_linux.pyx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ from libc.stdint cimport intptr_t, uintptr_t
99
import threading
1010
from .utils import FunctionNotFoundError, NotSupportedError
1111

12-
from cuda.pathfinder import load_nvidia_dynamic_lib
12+
from cuda.pathfinder import load_nvidia_dynamic_lib, DynamicLibNotFoundError
1313

1414

1515
###############################################################################
@@ -73,7 +73,11 @@ cdef void* __nvJitLinkVersion = NULL
7373

7474

7575
cdef void* load_library() except* with gil:
76-
cdef uintptr_t handle = load_nvidia_dynamic_lib("nvJitLink")._handle_uint
76+
cdef uintptr_t handle
77+
try:
78+
handle = load_nvidia_dynamic_lib("nvJitLink")._handle_uint
79+
except DynamicLibNotFoundError:
80+
handle = 0
7781
return <void*>handle
7882

7983

cuda_bindings/cuda/bindings/_internal/nvjitlink_windows.pyx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
#
55
# This code was automatically generated across versions from 12.0.1 to 13.0.1. Do not modify it directly.
66

7-
from libc.stdint cimport intptr_t
7+
from libc.stdint cimport intptr_t, uintptr_t
88

99
import threading
1010
from .utils import FunctionNotFoundError, NotSupportedError
1111

12-
from cuda.pathfinder import load_nvidia_dynamic_lib
12+
from cuda.pathfinder import load_nvidia_dynamic_lib, DynamicLibNotFoundError
1313

1414
from libc.stddef cimport wchar_t
1515
from libc.stdint cimport uintptr_t
@@ -95,12 +95,14 @@ cdef void* __nvJitLinkVersion = NULL
9595

9696
cdef int __check_or_init_nvjitlink() except -1 nogil:
9797
global __py_nvjitlink_init
98-
if __py_nvjitlink_init:
99-
return 0
98+
cdef uintptr_t handle
10099

101100
with gil, __symbol_lock:
102101
# Load library
103-
handle = load_nvidia_dynamic_lib("nvJitLink")._handle_uint
102+
try:
103+
handle = load_nvidia_dynamic_lib("nvJitLink")._handle_uint
104+
except DynamicLibNotFoundError:
105+
handle = 0
104106

105107
# Load function
106108
global __nvJitLinkCreate

cuda_bindings/cuda/bindings/_internal/nvvm_linux.pyx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ from libc.stdint cimport intptr_t, uintptr_t
99
import threading
1010
from .utils import FunctionNotFoundError, NotSupportedError
1111

12-
from cuda.pathfinder import load_nvidia_dynamic_lib
12+
from cuda.pathfinder import load_nvidia_dynamic_lib, DynamicLibNotFoundError
1313

1414

1515
###############################################################################
@@ -72,7 +72,11 @@ cdef void* __nvvmGetProgramLog = NULL
7272

7373

7474
cdef void* load_library() except* with gil:
75-
cdef uintptr_t handle = load_nvidia_dynamic_lib("nvvm")._handle_uint
75+
cdef uintptr_t handle
76+
try:
77+
handle = load_nvidia_dynamic_lib("nvvm")._handle_uint
78+
except DynamicLibNotFoundError:
79+
handle = 0
7680
return <void*>handle
7781

7882

@@ -178,7 +182,7 @@ cdef int __check_or_init_nvvm() except -1 nogil:
178182
return 0
179183

180184

181-
cdef inline int _check_or_init_nvvm() except -1 nogil:
185+
cdef int _check_or_init_nvvm() except -1 nogil:
182186
if __py_nvvm_init:
183187
return 0
184188

cuda_bindings/cuda/bindings/_internal/nvvm_windows.pyx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
#
55
# This code was automatically generated across versions from 12.0.1 to 13.0.1. Do not modify it directly.
66

7-
from libc.stdint cimport intptr_t
7+
from libc.stdint cimport intptr_t, uintptr_t
88

99
import threading
1010
from .utils import FunctionNotFoundError, NotSupportedError
1111

12-
from cuda.pathfinder import load_nvidia_dynamic_lib
12+
from cuda.pathfinder import load_nvidia_dynamic_lib, DynamicLibNotFoundError
1313

1414
from libc.stddef cimport wchar_t
1515
from libc.stdint cimport uintptr_t
@@ -95,9 +95,14 @@ cdef void* __nvvmGetProgramLog = NULL
9595
cdef int __check_or_init_nvvm() except -1 nogil:
9696
global __py_nvvm_init
9797

98+
cdef uintptr_t handle
99+
98100
with gil, __symbol_lock:
99101
# Load library
100-
handle = load_nvidia_dynamic_lib("nvvm")._handle_uint
102+
try:
103+
handle = load_nvidia_dynamic_lib("nvvm")._handle_uint
104+
except DynamicLibNotFoundError:
105+
handle = 0
101106

102107
# Load function
103108
global __nvvmGetErrorString

0 commit comments

Comments
 (0)