Skip to content

Instantly share code, notes, and snippets.

@mkolod
Last active February 15, 2018 17:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkolod/e08ae63b7b1c2511a77d16bf8405e2c6 to your computer and use it in GitHub Desktop.
Save mkolod/e08ae63b7b1c2511a77d16bf8405e2c6 to your computer and use it in GitHub Desktop.
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)
@mkolod
Copy link
Author

mkolod commented Feb 8, 2018

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).

@malcolmgreaves
Copy link

Neat -- so this is your preferred way of direct CUDA API interaction via Python?

@mkolod
Copy link
Author

mkolod commented Feb 15, 2018

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