|
1 | | -import sys |
2 | 1 | import ctypes |
3 | 2 | import ctypes.util |
4 | | -from numba import cuda as ncuda |
5 | | -from cffi import FFI |
| 3 | +import sys |
6 | 4 |
|
| 5 | +from cffi import FFI |
| 6 | +from numba import cuda |
7 | 7 | from numba import types |
8 | 8 |
|
| 9 | + |
9 | 10 | class Dl_info(ctypes.Structure): |
10 | | - """ |
11 | | - Structure of the Dl_info returned by the CFFI of dl.dladdr |
12 | | - """ |
| 11 | + """ |
| 12 | + Structure of the Dl_info returned by the CFFI of dl.dladdr |
| 13 | + """ |
13 | 14 |
|
14 | | - _fields_ = ( |
15 | | - ("dli_fname", ctypes.c_char_p), |
16 | | - ("dli_fbase", ctypes.c_void_p), |
17 | | - ("dli_sname", ctypes.c_char_p), |
18 | | - ("dli_saddr", ctypes.c_void_p), |
19 | | - ) |
| 15 | + _fields_ = ( |
| 16 | + ("dli_fname", ctypes.c_char_p), |
| 17 | + ("dli_fbase", ctypes.c_void_p), |
| 18 | + ("dli_sname", ctypes.c_char_p), |
| 19 | + ("dli_saddr", ctypes.c_void_p), |
| 20 | + ) |
20 | 21 |
|
21 | 22 |
|
22 | 23 | # Find the dynamic linker library path. Only works on unix-like os |
23 | 24 | libdl_path = ctypes.util.find_library("dl") |
24 | 25 | if libdl_path: |
25 | | - # Load the dynamic linker dynamically |
26 | | - libdl = ctypes.CDLL(libdl_path) |
27 | | - |
28 | | - # Define dladdr to get the pointer to a symbol in a shared |
29 | | - # library already loaded. |
30 | | - # https://man7.org/linux/man-pages/man3/dladdr.3.html |
31 | | - libdl.dladdr.argtypes = (ctypes.c_void_p, ctypes.POINTER(Dl_info)) |
32 | | - # restype is None as it returns by reference |
| 26 | + # Load the dynamic linker dynamically |
| 27 | + libdl = ctypes.CDLL(libdl_path) |
| 28 | + |
| 29 | + # Define dladdr to get the pointer to a symbol in a shared |
| 30 | + # library already loaded. |
| 31 | + # https://man7.org/linux/man-pages/man3/dladdr.3.html |
| 32 | + libdl.dladdr.argtypes = (ctypes.c_void_p, ctypes.POINTER(Dl_info)) |
| 33 | + # restype is None as it returns by reference |
33 | 34 | else: |
34 | | - # On Windows it is nontrivial to have libdl, so we disable everything about |
35 | | - # it and use other ways to find paths of libraries |
36 | | - libdl = None |
| 35 | + # On Windows it is nontrivial to have libdl, so we disable everything about |
| 36 | + # it and use other ways to find paths of libraries |
| 37 | + libdl = None |
37 | 38 |
|
38 | 39 |
|
39 | 40 | def find_path_of_symbol_in_library(symbol): |
40 | | - if libdl is None: |
41 | | - raise ValueError("libdl not found.") |
42 | | - |
43 | | - info = Dl_info() |
| 41 | + if libdl is None: |
| 42 | + raise ValueError("libdl not found.") |
44 | 43 |
|
45 | | - result = libdl.dladdr(symbol, ctypes.byref(info)) |
46 | | - |
47 | | - if result and info.dli_fname: |
48 | | - return info.dli_fname.decode(sys.getfilesystemencoding()) |
49 | | - else: |
50 | | - raise ValueError("Cannot determine path of Library.") |
| 44 | + info = Dl_info() |
| 45 | + result = libdl.dladdr(symbol, ctypes.byref(info)) |
| 46 | + if result and info.dli_fname: |
| 47 | + return info.dli_fname.decode(sys.getfilesystemencoding()) |
| 48 | + else: |
| 49 | + raise ValueError("Cannot determine path of Library.") |
51 | 50 |
|
52 | 51 |
|
53 | 52 | try: |
54 | | - _libcuda = ncuda.driver.find_driver() |
55 | | - |
56 | | - if sys.platform == "win32": |
57 | | - libcuda_path = ctypes.util.find_library(_libcuda._name) |
58 | | - else: |
59 | | - libcuda_path = find_path_of_symbol_in_library(_libcuda.cuMemcpy) |
60 | | - |
61 | | - numba_cffi_loaded = True |
| 53 | + _libcuda = cuda.driver.find_driver() |
| 54 | + if sys.platform == "win32": |
| 55 | + libcuda_path = ctypes.util.find_library(_libcuda._name) |
| 56 | + else: |
| 57 | + libcuda_path = find_path_of_symbol_in_library(_libcuda.cuMemcpy) |
| 58 | + numba_cffi_loaded = True |
62 | 59 | except Exception: |
63 | | - numba_cffi_loaded = False |
| 60 | + numba_cffi_loaded = False |
64 | 61 |
|
65 | | -if numba_cffi_loaded: |
66 | 62 |
|
67 | | - # functions needed |
68 | | - ffi = FFI() |
69 | | - ffi.cdef("int cuMemcpy(void* dst, void* src, unsigned int len, int type);") |
70 | | - ffi.cdef( |
71 | | - "int cuMemcpyAsync(void* dst, void* src, unsigned int len, int type, void* stream);" |
72 | | - ) |
73 | | - ffi.cdef("int cuStreamSynchronize(void* stream);") |
74 | | - |
75 | | - ffi.cdef("int cudaMallocHost(void** ptr, size_t size);") |
76 | | - ffi.cdef("int cudaFreeHost(void* ptr);") |
77 | | - |
78 | | - # load libraray |
79 | | - # could ncuda.driver.find_library() |
80 | | - libcuda = ffi.dlopen(libcuda_path) |
81 | | - cuMemcpy = libcuda.cuMemcpy |
82 | | - cuMemcpyAsync = libcuda.cuMemcpyAsync |
83 | | - cuStreamSynchronize = libcuda.cuStreamSynchronize |
84 | | - |
85 | | - memcpyHostToHost = types.int32(0) |
86 | | - memcpyHostToDevice = types.int32(1) |
87 | | - memcpyDeviceToHost = types.int32(2) |
88 | | - memcpyDeviceToDevice = types.int32(3) |
| 63 | +if numba_cffi_loaded: |
| 64 | + # functions needed |
| 65 | + ffi = FFI() |
| 66 | + ffi.cdef("int cuMemcpy(void* dst, void* src, unsigned int len, int type);") |
| 67 | + ffi.cdef("int cuMemcpyAsync(void* dst, void* src, unsigned int len, int type, void* stream);") |
| 68 | + ffi.cdef("int cuStreamSynchronize(void* stream);") |
| 69 | + ffi.cdef("int cudaMallocHost(void** ptr, size_t size);") |
| 70 | + ffi.cdef("int cudaFreeHost(void* ptr);") |
| 71 | + |
| 72 | + # load libraray |
| 73 | + # could ncuda.driver.find_library() |
| 74 | + libcuda = ffi.dlopen(libcuda_path) |
| 75 | + cuMemcpy = libcuda.cuMemcpy |
| 76 | + cuMemcpyAsync = libcuda.cuMemcpyAsync |
| 77 | + cuStreamSynchronize = libcuda.cuStreamSynchronize |
| 78 | + |
| 79 | + memcpyHostToHost = types.int32(0) |
| 80 | + memcpyHostToDevice = types.int32(1) |
| 81 | + memcpyDeviceToHost = types.int32(2) |
| 82 | + memcpyDeviceToDevice = types.int32(3) |
0 commit comments