Skip to content

Instantly share code, notes, and snippets.

@achimnol
Created September 1, 2017 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save achimnol/3bc59983d78cf0fa9c5b7841699d4879 to your computer and use it in GitHub Desktop.
Save achimnol/3bc59983d78cf0fa9c5b7841699d4879 to your computer and use it in GitHub Desktop.
import ctypes
import ctypes.util
_libcuda = ctypes.CDLL(ctypes.util.find_library('cudart'))
if __name__ == '__main__':
# You should put the path to the CUDA toolkit library into LD_LIBRARY_PATH envvar
# or into the system-wide configuration at /etc/ld.so.conf.d/.
count = ctypes.c_int(0)
# This will initialize a CUDA device context,
# which holds the handle to the device until this program exits.
ret = _libcuda.cudaGetDeviceCount(ctypes.byref(count))
assert ret == 0, 'cudaGetDeviceCount() has failed (error code {}).'.format(ret)
print('Number of GPUs detected: {}'.format(count.value))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment