Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

cuda-version.py 5.5 KB

You have to be logged in to leave a comment. Sign In
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Outputs some information on CUDA-enabled devices on your computer,
  5. including current memory usage.
  6. It's a port of https://gist.github.com/f0k/0d6431e3faa60bffc788f8b4daa029b1
  7. from C to Python with ctypes, so it can run without compiling anything. Note
  8. that this is a direct translation with no attempt to make the code Pythonic.
  9. It's meant as a general demonstration on how to obtain CUDA device information
  10. from Python without resorting to nvidia-smi or a compiled Python extension.
  11. Author: Jan Schlüter
  12. """
  13. import sys
  14. import ctypes
  15. # Some constants taken from cuda.h
  16. CUDA_SUCCESS = 0
  17. CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT = 16
  18. CU_DEVICE_ATTRIBUTE_MAX_THREADS_PER_MULTIPROCESSOR = 39
  19. CU_DEVICE_ATTRIBUTE_CLOCK_RATE = 13
  20. CU_DEVICE_ATTRIBUTE_MEMORY_CLOCK_RATE = 36
  21. def ConvertSMVer2Cores(major, minor):
  22. # Returns the number of CUDA cores per multiprocessor for a given
  23. # Compute Capability version. There is no way to retrieve that via
  24. # the API, so it needs to be hard-coded.
  25. # See _ConvertSMVer2Cores in helper_cuda.h in NVIDIA's CUDA Samples.
  26. return {(1, 0): 8, # Tesla
  27. (1, 1): 8,
  28. (1, 2): 8,
  29. (1, 3): 8,
  30. (2, 0): 32, # Fermi
  31. (2, 1): 48,
  32. (3, 0): 192, # Kepler
  33. (3, 2): 192,
  34. (3, 5): 192,
  35. (3, 7): 192,
  36. (5, 0): 128, # Maxwell
  37. (5, 2): 128,
  38. (5, 3): 128,
  39. (6, 0): 64, # Pascal
  40. (6, 1): 128,
  41. (6, 2): 128,
  42. (7, 0): 64, # Volta
  43. (7, 2): 64,
  44. (7, 5): 64, # Turing
  45. }.get((major, minor), 0)
  46. def main():
  47. libnames = ('libcuda.so', 'libcuda.dylib', 'cuda.dll')
  48. for libname in libnames:
  49. try:
  50. cuda = ctypes.CDLL(libname)
  51. except OSError:
  52. continue
  53. else:
  54. break
  55. else:
  56. raise OSError("could not load any of: " + ' '.join(libnames))
  57. nGpus = ctypes.c_int()
  58. name = b' ' * 100
  59. cc_major = ctypes.c_int()
  60. cc_minor = ctypes.c_int()
  61. cores = ctypes.c_int()
  62. threads_per_core = ctypes.c_int()
  63. clockrate = ctypes.c_int()
  64. freeMem = ctypes.c_size_t()
  65. totalMem = ctypes.c_size_t()
  66. result = ctypes.c_int()
  67. device = ctypes.c_int()
  68. context = ctypes.c_void_p()
  69. error_str = ctypes.c_char_p()
  70. result = cuda.cuInit(0)
  71. if result != CUDA_SUCCESS:
  72. cuda.cuGetErrorString(result, ctypes.byref(error_str))
  73. print("cuInit failed with error code %d: %s" %
  74. (result, error_str.value.decode()))
  75. return 1
  76. result = cuda.cuDeviceGetCount(ctypes.byref(nGpus))
  77. if result != CUDA_SUCCESS:
  78. cuda.cuGetErrorString(result, ctypes.byref(error_str))
  79. print("cuDeviceGetCount failed with error code %d: %s" %
  80. (result, error_str.value.decode()))
  81. return 1
  82. print("Found %d device(s)." % nGpus.value)
  83. for i in range(nGpus.value):
  84. result = cuda.cuDeviceGet(ctypes.byref(device), i)
  85. if result != CUDA_SUCCESS:
  86. cuda.cuGetErrorString(result, ctypes.byref(error_str))
  87. print("cuDeviceGet failed with error code %d: %s" %
  88. (result, error_str.value.decode()))
  89. return 1
  90. print("Device: %d" % i)
  91. if cuda.cuDeviceGetName(ctypes.c_char_p(name), len(name), device) == CUDA_SUCCESS:
  92. print(" Name: %s" % (name.split(b'\0', 1)[0].decode()))
  93. if cuda.cuDeviceComputeCapability(ctypes.byref(cc_major), ctypes.byref(cc_minor), device) == CUDA_SUCCESS:
  94. print(" Compute Capability: %d.%d" %
  95. (cc_major.value, cc_minor.value))
  96. if cuda.cuDeviceGetAttribute(ctypes.byref(cores), CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT, device) == CUDA_SUCCESS:
  97. print(" Multiprocessors: %d" % cores.value)
  98. print(" CUDA Cores: %s" % (
  99. cores.value * ConvertSMVer2Cores(cc_major.value, cc_minor.value) or "unknown"))
  100. if cuda.cuDeviceGetAttribute(ctypes.byref(threads_per_core), CU_DEVICE_ATTRIBUTE_MAX_THREADS_PER_MULTIPROCESSOR, device) == CUDA_SUCCESS:
  101. print(" Concurrent threads: %d" %
  102. (cores.value * threads_per_core.value))
  103. if cuda.cuDeviceGetAttribute(ctypes.byref(clockrate), CU_DEVICE_ATTRIBUTE_CLOCK_RATE, device) == CUDA_SUCCESS:
  104. print(" GPU clock: %g MHz" % (clockrate.value / 1000.))
  105. if cuda.cuDeviceGetAttribute(ctypes.byref(clockrate), CU_DEVICE_ATTRIBUTE_MEMORY_CLOCK_RATE, device) == CUDA_SUCCESS:
  106. print(" Memory clock: %g MHz" % (clockrate.value / 1000.))
  107. result = cuda.cuCtxCreate(ctypes.byref(context), 0, device)
  108. if result != CUDA_SUCCESS:
  109. cuda.cuGetErrorString(result, ctypes.byref(error_str))
  110. print("cuCtxCreate failed with error code %d: %s" %
  111. (result, error_str.value.decode()))
  112. else:
  113. result = cuda.cuMemGetInfo(
  114. ctypes.byref(freeMem), ctypes.byref(totalMem))
  115. if result == CUDA_SUCCESS:
  116. print(" Total Memory: %ld MiB" % (totalMem.value / 1024**2))
  117. print(" Free Memory: %ld MiB" % (freeMem.value / 1024**2))
  118. else:
  119. cuda.cuGetErrorString(result, ctypes.byref(error_str))
  120. print("cuMemGetInfo failed with error code %d: %s" %
  121. (result, error_str.value.decode()))
  122. cuda.cuCtxDetach(context)
  123. return 0
  124. if __name__ == "__main__":
  125. sys.exit(main())
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...