-
Notifications
You must be signed in to change notification settings - Fork 545
/
gpu_cc.py
53 lines (41 loc) · 1.42 KB
/
gpu_cc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
# ported from https://gist.github.com/f0k/63a664160d016a491b2cbea15913d549
'''
import ctypes
CUDA_SUCCESS = 0
def get_gpu_archs():
libnames = ('libcuda.so', 'libcuda.dylib', 'cuda.dll')
for libname in libnames:
try:
cuda = ctypes.CDLL(libname)
except OSError:
continue
else:
break
else:
return
gpu_archs = set()
n_gpus = ctypes.c_int()
cc_major = ctypes.c_int()
cc_minor = ctypes.c_int()
result = ctypes.c_int()
device = ctypes.c_int()
error_str = ctypes.c_char_p()
result = cuda.cuInit(0)
if result != CUDA_SUCCESS:
cuda.cuGetErrorString(result, ctypes.byref(error_str))
# print('cuInit failed with error code %d: %s' % (result, error_str.value.decode()))
return []
result = cuda.cuDeviceGetCount(ctypes.byref(n_gpus))
if result != CUDA_SUCCESS:
cuda.cuGetErrorString(result, ctypes.byref(error_str))
# print('cuDeviceGetCount failed with error code %d: %s' % (result, error_str.value.decode()))
return []
for i in range(n_gpus.value):
if cuda.cuDeviceComputeCapability(ctypes.byref(cc_major), ctypes.byref(cc_minor), device) == CUDA_SUCCESS:
gpu_archs.add(str(cc_major.value) + str(cc_minor.value))
return list(gpu_archs)
if __name__ == '__main__':
print(' '.join(get_gpu_archs()))