2022-08-02 14:42:27 +00:00
|
|
|
import ctypes
|
|
|
|
from dataclasses import dataclass, field
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class CudaLibVals:
|
2022-08-03 04:27:36 +00:00
|
|
|
# code bits taken from
|
2022-08-02 14:42:27 +00:00
|
|
|
# https://gist.github.com/f0k/63a664160d016a491b2cbea15913d549
|
|
|
|
|
2022-08-03 04:27:36 +00:00
|
|
|
nGpus: ctypes.c_int = field(default=ctypes.c_int())
|
|
|
|
cc_major: ctypes.c_int = field(default=ctypes.c_int())
|
|
|
|
cc_minor: ctypes.c_int = field(default=ctypes.c_int())
|
|
|
|
device: ctypes.c_int = field(default=ctypes.c_int())
|
|
|
|
error_str: ctypes.c_char_p = field(default=ctypes.c_char_p())
|
2022-08-02 14:42:27 +00:00
|
|
|
cuda: ctypes.CDLL = field(init=False, repr=False)
|
|
|
|
ccs: List[str, ...] = field(init=False)
|
|
|
|
|
2022-08-03 04:27:36 +00:00
|
|
|
def _initialize_driver_API(self):
|
|
|
|
self.check_cuda_result(self.cuda.cuInit(0))
|
|
|
|
|
|
|
|
def _load_cuda_lib(self):
|
2022-08-02 14:42:27 +00:00
|
|
|
"""
|
|
|
|
1. find libcuda.so library (GPU driver) (/usr/lib)
|
|
|
|
init_device -> init variables -> call function by reference
|
|
|
|
"""
|
2022-08-03 04:27:36 +00:00
|
|
|
libnames = "libcuda.so"
|
2022-08-02 14:42:27 +00:00
|
|
|
for libname in libnames:
|
|
|
|
try:
|
|
|
|
self.cuda = ctypes.CDLL(libname)
|
|
|
|
except OSError:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise OSError("could not load any of: " + " ".join(libnames))
|
|
|
|
|
2022-08-03 04:27:36 +00:00
|
|
|
def call_cuda_func(self, function_obj, **kwargs):
|
|
|
|
CUDA_SUCCESS = 0 # constant taken from cuda.h
|
|
|
|
pass
|
|
|
|
# if (CUDA_SUCCESS := function_obj(
|
|
|
|
|
|
|
|
def _error_handle(cuda_lib_call_return_value):
|
2022-08-02 14:42:27 +00:00
|
|
|
"""
|
2022-08-03 04:27:36 +00:00
|
|
|
2. call extern C function to determine CC
|
2022-08-02 14:42:27 +00:00
|
|
|
(see https://docs.nvidia.com/cuda/cuda-driver-api/group__CUDA__DEVICE__DEPRECATED.html)
|
|
|
|
"""
|
2022-08-03 04:27:36 +00:00
|
|
|
CUDA_SUCCESS = 0 # constant taken from cuda.h
|
2022-08-02 14:42:27 +00:00
|
|
|
|
2022-08-03 04:27:36 +00:00
|
|
|
if cuda_lib_call_return_value != CUDA_SUCCESS:
|
|
|
|
self.cuda.cuGetErrorString(
|
|
|
|
cuda_lib_call_return_value,
|
|
|
|
ctypes.byref(self.error_str),
|
|
|
|
)
|
2022-08-02 14:42:27 +00:00
|
|
|
print("Count not initialize CUDA - failure!")
|
|
|
|
raise Exception("CUDA exception!")
|
2022-08-03 04:27:36 +00:00
|
|
|
return cuda_lib_call_return_value
|
2022-08-02 14:42:27 +00:00
|
|
|
|
|
|
|
def __post_init__(self):
|
2022-08-03 04:27:36 +00:00
|
|
|
self._load_cuda_lib()
|
|
|
|
self._initialize_driver_API()
|
|
|
|
self.check_cuda_result(
|
|
|
|
self.cuda, self.cuda.cuDeviceGetCount(ctypes.byref(self.nGpus))
|
|
|
|
)
|
2022-08-02 14:42:27 +00:00
|
|
|
tmp_ccs = []
|
|
|
|
for gpu_index in range(self.nGpus.value):
|
|
|
|
check_cuda_result(
|
2022-08-03 04:27:36 +00:00
|
|
|
self.cuda,
|
|
|
|
self.cuda.cuDeviceGet(ctypes.byref(self.device), gpu_index),
|
2022-08-02 14:42:27 +00:00
|
|
|
)
|
|
|
|
check_cuda_result(
|
|
|
|
self.cuda,
|
|
|
|
self.cuda.cuDeviceComputeCapability(
|
2022-08-03 04:27:36 +00:00
|
|
|
ctypes.byref(self.cc_major),
|
|
|
|
ctypes.byref(self.cc_minor),
|
|
|
|
self.device,
|
2022-08-02 14:42:27 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
tmp_ccs.append(f"{self.cc_major.value}.{self.cc_minor.value}")
|
|
|
|
self.ccs = sorted(tmp_ccs, reverse=True)
|