Last active
February 15, 2018 17:56
-
-
Save mkolod/e08ae63b7b1c2511a77d16bf8405e2c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ctypes as C | |
from ctypes.util import find_library | |
cuda = C.cdll.LoadLibrary(find_library("cudart")) | |
dev_ct = C.c_int() | |
dev_ct_addr = C.addressof(dev_ct) | |
dev_ct_ptr = C.cast(dev_ct_addr, C.POINTER(C.c_int)) | |
error_t = cuda.cudaGetDeviceCount(dev_ct_ptr) | |
print("Error code (no error if zero): %d" % error_t) | |
print("Device count: %d" % dev_ct.value) |
Neat -- so this is your preferred way of direct CUDA API interaction via Python?
No, not really. There's PyCUDA, but I wanted to find a simple demo that didn't rely on PyCUDA. For example, DL frameworks don't like to include PyCUDA for various reasons (or CuPy for that matter, which is another alternative). Also, you can always use SWIG, Ctypes are way too low level. But even though SWIG is prettier, SWIG builds are messy. Ctypes are nice only in the sense that if you just want to do something trivial in a few lines, it will be simpler than dealing with SWIG. But for large C/C++=Python bindings, I'd use SWIG, Pybind11, etc. Especially for C++ specifically, because PyBind11 has extensive support for C++11.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tested on Linux. Sample output:
Error code (no error if zero): 0
Device count: 2
Should probably work on the Mac (libcudart.dylib) and on Windows (libcudart.dll) as well (find_library looks up the shared lib regardless of platform-specific extension).