Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
move init check inside lock
  • Loading branch information
kkraus14 committed Aug 14, 2025
commit 5f4125ebc2c95f8dbc647b5f782649970eb2c20f
11 changes: 6 additions & 5 deletions cuda_bindings/cuda/bindings/_bindings/cydriver.pyx.in
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import os
import sys
import threading
cimport cuda.bindings._bindings.loader as loader
cdef object __symbol_lock = threading.Lock()
cdef object __symbol_lock = threading.RLock()
cdef bint __cuPythonInit = False
{{if 'cuGetErrorString' in found_functions}}cdef void *__cuGetErrorString = NULL{{endif}}
{{if 'cuGetErrorName' in found_functions}}cdef void *__cuGetErrorName = NULL{{endif}}
Expand Down Expand Up @@ -488,13 +488,14 @@ cdef bint __cuPythonInit = False

cdef int cuPythonInit() except -1 nogil:
global __cuPythonInit
if __cuPythonInit:
return 0
__cuPythonInit = True

cdef bint usePTDS
cdef char libPath[260]

with gil, __symbol_lock:
if __cuPythonInit:
return 0
__cuPythonInit = True

usePTDS = os.getenv('CUDA_PYTHON_CUDA_PER_THREAD_DEFAULT_STREAM', default=0)

# Load library
Expand Down
5 changes: 3 additions & 2 deletions cuda_bindings/cuda/bindings/_bindings/cynvrtc.pyx.in
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ cdef bint __cuPythonInit = False

cdef int cuPythonInit() except -1 nogil:
global __cuPythonInit
if __cuPythonInit:
return 0

with gil, __symbol_lock:
if __cuPythonInit:
return 0

{{if 'Windows' == platform.system()}}
handle = load_nvidia_dynamic_lib("nvrtc")._handle_uint

Expand Down
7 changes: 4 additions & 3 deletions cuda_bindings/cuda/bindings/_internal/cufile_linux.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,13 @@ cdef void* load_library(const int driver_ver) except* with gil:

cdef int _check_or_init_cufile() except -1 nogil:
global __py_cufile_init
if __py_cufile_init:
return 0

cdef void* handle = NULL
cdef int err, driver_ver = 0

with gil, __symbol_lock:
if __py_cufile_init:
return 0

# Load driver to check version
handle = dlopen('libcuda.so.1', RTLD_NOW | RTLD_GLOBAL)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR, but... this handle is used to get the driver version, which is fed into the load_library call which doesn't use the driver version. This is likely a codegen issue, but we should probably just remove this?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either way, wanted to call it out here but we can defer it to a future PR

if handle == NULL:
Expand Down
7 changes: 4 additions & 3 deletions cuda_bindings/cuda/bindings/_internal/nvjitlink_linux.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ cdef void* load_library(int driver_ver) except* with gil:

cdef int _check_or_init_nvjitlink() except -1 nogil:
global __py_nvjitlink_init
if __py_nvjitlink_init:
return 0

cdef void* handle = NULL
cdef int err, driver_ver = 0

with gil, __symbol_lock:
if __py_nvjitlink_init:
return 0

# Load driver to check version
handle = dlopen('libcuda.so.1', RTLD_NOW | RTLD_GLOBAL)
if handle == NULL:
Expand Down
7 changes: 4 additions & 3 deletions cuda_bindings/cuda/bindings/_internal/nvjitlink_windows.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ cdef void* __nvJitLinkVersion = NULL

cdef int _check_or_init_nvjitlink() except -1 nogil:
global __py_nvjitlink_init
if __py_nvjitlink_init:
return 0

cdef int err, driver_ver = 0

with gil, __symbol_lock:
if __py_nvjitlink_init:
return 0

# Load driver to check version
try:
handle = win32api.LoadLibraryEx("nvcuda.dll", 0, LOAD_LIBRARY_SEARCH_SYSTEM32)
Expand Down
7 changes: 4 additions & 3 deletions cuda_bindings/cuda/bindings/_internal/nvvm_linux.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ cdef void* load_library(const int driver_ver) except* with gil:

cdef int _check_or_init_nvvm() except -1 nogil:
global __py_nvvm_init
if __py_nvvm_init:
return 0

cdef void* handle = NULL
cdef int err, driver_ver = 0

with gil, __symbol_lock:
if __py_nvvm_init:
return 0

# Load driver to check version
handle = dlopen('libcuda.so.1', RTLD_NOW | RTLD_GLOBAL)
if handle == NULL:
Expand Down
7 changes: 4 additions & 3 deletions cuda_bindings/cuda/bindings/_internal/nvvm_windows.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ cdef void* __nvvmGetProgramLog = NULL

cdef int _check_or_init_nvvm() except -1 nogil:
global __py_nvvm_init
if __py_nvvm_init:
return 0

cdef int err, driver_ver = 0

with gil, __symbol_lock:
if __py_nvvm_init:
return 0

# Load driver to check version
try:
handle = win32api.LoadLibraryEx("nvcuda.dll", 0, LOAD_LIBRARY_SEARCH_SYSTEM32)
Expand Down