├── .gitignore ├── MANIFEST ├── PKG-INFO ├── README.txt ├── nvidia_smi.py ├── pynvml.py └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | -------------------------------------------------------------------------------- /MANIFEST: -------------------------------------------------------------------------------- 1 | # file GENERATED by distutils, do NOT edit 2 | README.txt 3 | nvidia_smi.py 4 | pynvml.py 5 | setup.py 6 | -------------------------------------------------------------------------------- /PKG-INFO: -------------------------------------------------------------------------------- 1 | Metadata-Version: 1.1 2 | Name: nvidia-ml-py 3 | Version: 7.352.1 4 | Summary: Python Bindings for the NVIDIA Management Library 5 | Home-page: http://www.nvidia.com/ 6 | Author: NVIDIA Corporation 7 | Author-email: nvml-bindings@nvidia.com 8 | License: BSD 9 | Description: UNKNOWN 10 | Platform: UNKNOWN 11 | Classifier: Development Status :: 5 - Production/Stable 12 | Classifier: Intended Audience :: Developers 13 | Classifier: Intended Audience :: System Administrators 14 | Classifier: License :: OSI Approved :: BSD License 15 | Classifier: Operating System :: Microsoft :: Windows 16 | Classifier: Operating System :: POSIX :: Linux 17 | Classifier: Programming Language :: Python 18 | Classifier: Topic :: Software Development :: Libraries :: Python Modules 19 | Classifier: Topic :: System :: Hardware 20 | Classifier: Topic :: System :: Systems Administration 21 | -------------------------------------------------------------------------------- /README.txt: -------------------------------------------------------------------------------- 1 | ====== 2 | pyNVML 3 | ====== 4 | 5 | *** 6 | Patched to support Python 3 (and Python 2) 7 | *** 8 | 9 | ------------------------------------------------ 10 | Python bindings to the NVIDIA Management Library 11 | ------------------------------------------------ 12 | 13 | Provides a Python interface to GPU management and monitoring functions. 14 | 15 | This is a wrapper around the NVML library. 16 | For information about the NVML library, see the NVML developer page 17 | http://developer.nvidia.com/nvidia-management-library-nvml 18 | 19 | Download the latest package from: 20 | https://pypi.python.org/pypi/nvidia-ml-py3 21 | 22 | Note this file can be run with 'python -m doctest -v README.txt' 23 | although the results are system dependent 24 | 25 | REQUIRES 26 | -------- 27 | Python 2.5, or an earlier version with the ctypes module. 28 | 29 | INSTALLATION 30 | ------------ 31 | 32 | sudo python setup.py install 33 | 34 | or 35 | 36 | pip install nvidia-ml-py3 37 | 38 | USAGE 39 | ----- 40 | 41 | >>> from pynvml import * 42 | >>> nvmlInit() 43 | >>> print "Driver Version:", nvmlSystemGetDriverVersion() 44 | Driver Version: 352.00 45 | >>> deviceCount = nvmlDeviceGetCount() 46 | >>> for i in range(deviceCount): 47 | ... handle = nvmlDeviceGetHandleByIndex(i) 48 | ... print "Device", i, ":", nvmlDeviceGetName(handle) 49 | ... 50 | Device 0 : Tesla K40c 51 | 52 | >>> nvmlShutdown() 53 | 54 | Additionally, see nvidia_smi.py. A sample application. 55 | 56 | FUNCTIONS 57 | --------- 58 | Python methods wrap NVML functions, implemented in a C shared library. 59 | Each function's use is the same with the following exceptions: 60 | 61 | - Instead of returning error codes, failing error codes are raised as 62 | Python exceptions. 63 | 64 | >>> try: 65 | ... nvmlDeviceGetCount() 66 | ... except NVMLError as error: 67 | ... print error 68 | ... 69 | Uninitialized 70 | 71 | - C function output parameters are returned from the corresponding 72 | Python function left to right. 73 | 74 | :: 75 | 76 | nvmlReturn_t nvmlDeviceGetEccMode(nvmlDevice_t device, 77 | nvmlEnableState_t *current, 78 | nvmlEnableState_t *pending); 79 | 80 | >>> nvmlInit() 81 | >>> handle = nvmlDeviceGetHandleByIndex(0) 82 | >>> (current, pending) = nvmlDeviceGetEccMode(handle) 83 | 84 | - C structs are converted into Python classes. 85 | 86 | :: 87 | 88 | nvmlReturn_t DECLDIR nvmlDeviceGetMemoryInfo(nvmlDevice_t device, 89 | nvmlMemory_t *memory); 90 | typedef struct nvmlMemory_st { 91 | unsigned long long total; 92 | unsigned long long free; 93 | unsigned long long used; 94 | } nvmlMemory_t; 95 | 96 | >>> info = nvmlDeviceGetMemoryInfo(handle) 97 | >>> print "Total memory:", info.total 98 | Total memory: 5636292608 99 | >>> print "Free memory:", info.free 100 | Free memory: 5578420224 101 | >>> print "Used memory:", info.used 102 | Used memory: 57872384 103 | 104 | - Python handles string buffer creation. 105 | 106 | :: 107 | 108 | nvmlReturn_t nvmlSystemGetDriverVersion(char* version, 109 | unsigned int length); 110 | 111 | >>> version = nvmlSystemGetDriverVersion(); 112 | >>> nvmlShutdown() 113 | 114 | For usage information see the NVML documentation. 115 | 116 | VARIABLES 117 | --------- 118 | All meaningful NVML constants and enums are exposed in Python. 119 | 120 | The NVML_VALUE_NOT_AVAILABLE constant is not used. Instead None is mapped to the field. 121 | 122 | RELEASE NOTES 123 | ------------- 124 | Version 2.285.0 125 | - Added new functions for NVML 2.285. See NVML documentation for more information. 126 | - Ported to support Python 3.0 and Python 2.0 syntax. 127 | - Added nvidia_smi.py tool as a sample app. 128 | Version 3.295.0 129 | - Added new functions for NVML 3.295. See NVML documentation for more information. 130 | - Updated nvidia_smi.py tool 131 | - Includes additional error handling 132 | Version 4.304.0 133 | - Added new functions for NVML 4.304. See NVML documentation for more information. 134 | - Updated nvidia_smi.py tool 135 | Version 4.304.3 136 | - Fixing nvmlUnitGetDeviceCount bug 137 | Version 5.319.0 138 | - Added new functions for NVML 5.319. See NVML documentation for more information. 139 | Version 6.340.0 140 | - Added new functions for NVML 6.340. See NVML documentation for more information. 141 | Version 7.346.0 142 | - Added new functions for NVML 7.346. See NVML documentation for more information. 143 | Version 7.352.0 144 | - Added new functions for NVML 7.352. See NVML documentation for more information. 145 | Version 7.352.1 146 | - Bump version number so requirements.txt will pick up the update for windows/system32 147 | 148 | COPYRIGHT 149 | --------- 150 | Copyright (c) 2011-2015, NVIDIA Corporation. All rights reserved. 151 | 152 | LICENSE 153 | ------- 154 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 155 | 156 | - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 157 | 158 | - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 159 | 160 | - Neither the name of the NVIDIA Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 161 | 162 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 163 | -------------------------------------------------------------------------------- /nvidia_smi.py: -------------------------------------------------------------------------------- 1 | ##### 2 | # Copyright (c) 2011-2015, NVIDIA Corporation. All rights reserved. 3 | # 4 | # Redistribution and use in source and binary forms, with or without 5 | # modification, are permitted provided that the following conditions are met: 6 | # 7 | # * Redistributions of source code must retain the above copyright notice, 8 | # this list of conditions and the following disclaimer. 9 | # * Redistributions in binary form must reproduce the above copyright 10 | # notice, this list of conditions and the following disclaimer in the 11 | # documentation and/or other materials provided with the distribution. 12 | # * Neither the name of the NVIDIA Corporation nor the names of its 13 | # contributors may be used to endorse or promote products derived from 14 | # this software without specific prior written permission. 15 | # 16 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 26 | # THE POSSIBILITY OF SUCH DAMAGE. 27 | ##### 28 | 29 | # 30 | # nvidia_smi 31 | # nvml_bindings nvidia com 32 | # 33 | # Sample code that attempts to reproduce the output of nvidia-smi -q -x 34 | # For many cases the output should match 35 | # 36 | # Can be used as a library or a command line script 37 | # 38 | # To Run: 39 | # $ python nvidia_smi.py 40 | # 41 | 42 | from pynvml import * 43 | import datetime 44 | 45 | # 46 | # Helper functions 47 | # 48 | def GetEccByType(handle, counterType, errorType): 49 | strResult = '' 50 | 51 | try: 52 | deviceMemory = nvmlDeviceGetMemoryErrorCounter(handle, errorType, counterType, 53 | NVML_MEMORY_LOCATION_DEVICE_MEMORY) 54 | except NVMLError as err: 55 | deviceMemory = handleError(err) 56 | strResult += ' ' + str(deviceMemory) + '\n' 57 | 58 | try: 59 | registerFile = nvmlDeviceGetMemoryErrorCounter(handle, errorType, counterType, 60 | NVML_MEMORY_LOCATION_REGISTER_FILE) 61 | except NVMLError as err: 62 | registerFile = handleError(err) 63 | 64 | strResult += ' ' + str(registerFile) + '\n' 65 | 66 | try: 67 | l1Cache = nvmlDeviceGetMemoryErrorCounter(handle, errorType, counterType, 68 | NVML_MEMORY_LOCATION_L1_CACHE) 69 | except NVMLError as err: 70 | l1Cache = handleError(err) 71 | strResult += ' ' + str(l1Cache) + '\n' 72 | 73 | try: 74 | l2Cache = nvmlDeviceGetMemoryErrorCounter(handle, errorType, counterType, 75 | NVML_MEMORY_LOCATION_L2_CACHE) 76 | except NVMLError as err: 77 | l2Cache = handleError(err) 78 | strResult += ' ' + str(l2Cache) + '\n' 79 | 80 | try: 81 | textureMemory = nvmlDeviceGetMemoryErrorCounter(handle, errorType, counterType, 82 | NVML_MEMORY_LOCATION_TEXTURE_MEMORY) 83 | except NVMLError as err: 84 | textureMemory = handleError(err) 85 | strResult += ' ' + str(textureMemory) + '\n' 86 | 87 | try: 88 | count = str(nvmlDeviceGetTotalEccErrors(handle, errorType, counterType)) 89 | except NVMLError as err: 90 | count = handleError(err) 91 | strResult += ' ' + count + '\n' 92 | 93 | return strResult 94 | 95 | def GetEccByCounter(handle, counterType): 96 | strResult = '' 97 | strResult += ' \n' 98 | strResult += str(GetEccByType(handle, counterType, NVML_MEMORY_ERROR_TYPE_CORRECTED)) 99 | strResult += ' \n' 100 | strResult += ' \n' 101 | strResult += str(GetEccByType(handle, counterType, NVML_MEMORY_ERROR_TYPE_UNCORRECTED)) 102 | strResult += ' \n' 103 | return strResult 104 | 105 | def GetEccStr(handle): 106 | strResult = '' 107 | strResult += ' \n' 108 | strResult += str(GetEccByCounter(handle, NVML_VOLATILE_ECC)) 109 | strResult += ' \n' 110 | strResult += ' \n' 111 | strResult += str(GetEccByCounter(handle, NVML_AGGREGATE_ECC)) 112 | strResult += ' \n' 113 | return strResult 114 | 115 | def GetRetiredPagesByCause(handle, cause): 116 | strResult = '' 117 | try: 118 | pages = nvmlDeviceGetRetiredPages(handle, cause) 119 | count = str(len(pages)) 120 | except NVMLError as err: 121 | error = handleError(err) 122 | pages = None 123 | count = error 124 | strResult += ' ' + count + '\n' 125 | if pages is not None: 126 | strResult += ' \n' 127 | for page in pages: 128 | strResult += ' ' + "0x%016x" % page + '\n' 129 | strResult += ' \n' 130 | else: 131 | strResult += ' ' + error + '\n' 132 | return strResult 133 | 134 | def GetRetiredPagesStr(handle): 135 | strResult = '' 136 | causes = [ "multiple_single_bit_retirement", "double_bit_retirement" ] 137 | for idx in range(NVML_PAGE_RETIREMENT_CAUSE_COUNT): 138 | strResult += ' <' + causes[idx] + '>\n' 139 | strResult += GetRetiredPagesByCause(handle, idx) 140 | strResult += ' \n' 141 | 142 | strResult += ' ' 143 | try: 144 | if NVML_FEATURE_DISABLED == nvmlDeviceGetRetiredPagesPendingStatus(handle): 145 | strResult += "No" 146 | else: 147 | strResult += "Yes" 148 | except NVMLError as err: 149 | strResult += handleError(err) 150 | strResult += '\n' 151 | return strResult 152 | 153 | def StrGOM(mode): 154 | if mode == NVML_GOM_ALL_ON: 155 | return "All On"; 156 | elif mode == NVML_GOM_COMPUTE: 157 | return "Compute"; 158 | elif mode == NVML_GOM_LOW_DP: 159 | return "Low Double Precision"; 160 | else: 161 | return "Unknown"; 162 | 163 | def GetClocksThrottleReasons(handle): 164 | throttleReasons = [ 165 | [nvmlClocksThrottleReasonGpuIdle, "clocks_throttle_reason_gpu_idle"], 166 | [nvmlClocksThrottleReasonUserDefinedClocks, "clocks_throttle_reason_user_defined_clocks"], 167 | [nvmlClocksThrottleReasonApplicationsClocksSetting, "clocks_throttle_reason_applications_clocks_setting"], 168 | [nvmlClocksThrottleReasonSwPowerCap, "clocks_throttle_reason_sw_power_cap"], 169 | [nvmlClocksThrottleReasonHwSlowdown, "clocks_throttle_reason_hw_slowdown"], 170 | [nvmlClocksThrottleReasonUnknown, "clocks_throttle_reason_unknown"] 171 | ]; 172 | 173 | strResult = '' 174 | 175 | try: 176 | supportedClocksThrottleReasons = nvmlDeviceGetSupportedClocksThrottleReasons(handle); 177 | clocksThrottleReasons = nvmlDeviceGetCurrentClocksThrottleReasons(handle); 178 | strResult += ' \n' 179 | for (mask, name) in throttleReasons: 180 | if (name != "clocks_throttle_reason_user_defined_clocks"): 181 | if (mask & supportedClocksThrottleReasons): 182 | val = "Active" if mask & clocksThrottleReasons else "Not Active"; 183 | else: 184 | val = handleError(NVML_ERROR_NOT_SUPPORTED); 185 | strResult += " <%s>%s\n" % (name, val, name); 186 | strResult += ' \n' 187 | except NVMLError as err: 188 | strResult += ' %s\n' % (handleError(err)); 189 | 190 | return strResult; 191 | 192 | # 193 | # Converts errors into string messages 194 | # 195 | def handleError(err): 196 | if (err.value == NVML_ERROR_NOT_SUPPORTED): 197 | return "N/A" 198 | else: 199 | return err.__str__() 200 | 201 | ####### 202 | def XmlDeviceQuery(): 203 | 204 | strResult = '' 205 | try: 206 | # 207 | # Initialize NVML 208 | # 209 | nvmlInit() 210 | 211 | strResult += '\n' 212 | strResult += '\n' 213 | strResult += '\n' 214 | 215 | strResult += ' ' + str(datetime.date.today()) + '\n' 216 | strResult += ' ' + str(nvmlSystemGetDriverVersion()) + '\n' 217 | 218 | deviceCount = nvmlDeviceGetCount() 219 | strResult += ' ' + str(deviceCount) + '\n' 220 | 221 | for i in range(0, deviceCount): 222 | handle = nvmlDeviceGetHandleByIndex(i) 223 | 224 | pciInfo = nvmlDeviceGetPciInfo(handle) 225 | 226 | strResult += ' \n' % pciInfo.busId 227 | 228 | strResult += ' ' + nvmlDeviceGetName(handle) + '\n' 229 | 230 | brandNames = {NVML_BRAND_UNKNOWN : "Unknown", 231 | NVML_BRAND_QUADRO : "Quadro", 232 | NVML_BRAND_TESLA : "Tesla", 233 | NVML_BRAND_NVS : "NVS", 234 | NVML_BRAND_GRID : "Grid", 235 | NVML_BRAND_GEFORCE : "GeForce", 236 | } 237 | 238 | try: 239 | # if nvmlDeviceGetBrand() succeeds it is guaranteed to be in the dictionary 240 | brandName = brandNames[nvmlDeviceGetBrand(handle)] 241 | except NVMLError as err: 242 | brandName = handleError(err) 243 | 244 | 245 | strResult += ' ' + brandName + '\n' 246 | 247 | try: 248 | state = ('Enabled' if (nvmlDeviceGetDisplayMode(handle) != 0) else 'Disabled') 249 | except NVMLError as err: 250 | state = handleError(err) 251 | 252 | strResult += ' ' + state + '\n' 253 | 254 | try: 255 | state = ('Enabled' if (nvmlDeviceGetDisplayActive(handle) != 0) else 'Disabled') 256 | except NVMLError as err: 257 | state = handleError(err) 258 | 259 | strResult += ' ' + state + '\n' 260 | 261 | try: 262 | mode = 'Enabled' if (nvmlDeviceGetPersistenceMode(handle) != 0) else 'Disabled' 263 | except NVMLError as err: 264 | mode = handleError(err) 265 | 266 | strResult += ' ' + mode + '\n' 267 | 268 | try: 269 | mode = 'Enabled' if (nvmlDeviceGetAccountingMode(handle) != 0) else 'Disabled' 270 | except NVMLError as err: 271 | mode = handleError(err) 272 | 273 | strResult += ' ' + mode + '\n' 274 | 275 | try: 276 | bufferSize = str(nvmlDeviceGetAccountingBufferSize(handle)) 277 | except NVMLError as err: 278 | bufferSize = handleError(err) 279 | 280 | strResult += ' ' + bufferSize + '\n' 281 | 282 | strResult += ' \n' 283 | 284 | try: 285 | current = 'WDDM' if (nvmlDeviceGetCurrentDriverModel(handle) == NVML_DRIVER_WDDM) else 'TCC' 286 | except NVMLError as err: 287 | current = handleError(err) 288 | strResult += ' ' + current + '\n' 289 | 290 | try: 291 | pending = 'WDDM' if (nvmlDeviceGetPendingDriverModel(handle) == NVML_DRIVER_WDDM) else 'TCC' 292 | except NVMLError as err: 293 | pending = handleError(err) 294 | 295 | strResult += ' ' + pending + '\n' 296 | 297 | strResult += ' \n' 298 | 299 | try: 300 | serial = nvmlDeviceGetSerial(handle) 301 | except NVMLError as err: 302 | serial = handleError(err) 303 | 304 | strResult += ' ' + serial + '\n' 305 | 306 | try: 307 | uuid = nvmlDeviceGetUUID(handle) 308 | except NVMLError as err: 309 | uuid = handleError(err) 310 | 311 | strResult += ' ' + uuid + '\n' 312 | 313 | try: 314 | minor_number = nvmlDeviceGetMinorNumber(handle) 315 | except NVMLError as err: 316 | minor_number = handleError(err) 317 | 318 | strResult += ' ' + str(minor_number) + '\n' 319 | 320 | try: 321 | vbios = nvmlDeviceGetVbiosVersion(handle) 322 | except NVMLError as err: 323 | vbios = handleError(err) 324 | 325 | strResult += ' ' + vbios + '\n' 326 | 327 | try: 328 | multiGpuBool = nvmlDeviceGetMultiGpuBoard(handle) 329 | except NVMLError as err: 330 | multiGpuBool = handleError(err); 331 | 332 | if multiGpuBool == "N/A": 333 | strResult += ' ' + 'N/A' + '\n' 334 | elif multiGpuBool: 335 | strResult += ' ' + 'Yes' + '\n' 336 | else: 337 | strResult += ' ' + 'No' + '\n' 338 | 339 | try: 340 | boardId = nvmlDeviceGetBoardId(handle) 341 | except NVMLError as err: 342 | boardId = handleError(err) 343 | 344 | try: 345 | hexBID = "0x%x" % boardId 346 | except: 347 | hexBID = boardId 348 | 349 | strResult += ' ' + hexBID + '\n' 350 | 351 | strResult += ' \n' 352 | 353 | try: 354 | img = nvmlDeviceGetInforomImageVersion(handle) 355 | except NVMLError as err: 356 | img = handleError(err) 357 | 358 | strResult += ' ' + img + '\n' 359 | 360 | try: 361 | oem = nvmlDeviceGetInforomVersion(handle, NVML_INFOROM_OEM) 362 | except NVMLError as err: 363 | oem = handleError(err) 364 | 365 | strResult += ' ' + oem + '\n' 366 | 367 | try: 368 | ecc = nvmlDeviceGetInforomVersion(handle, NVML_INFOROM_ECC) 369 | except NVMLError as err: 370 | ecc = handleError(err) 371 | 372 | strResult += ' ' + ecc + '\n' 373 | 374 | try: 375 | pwr = nvmlDeviceGetInforomVersion(handle, NVML_INFOROM_POWER) 376 | except NVMLError as err: 377 | pwr = handleError(err) 378 | 379 | strResult += ' ' + pwr + '\n' 380 | 381 | strResult += ' \n' 382 | 383 | strResult += ' \n' 384 | 385 | try: 386 | current = StrGOM(nvmlDeviceGetCurrentGpuOperationMode(handle)) 387 | except NVMLError as err: 388 | current = handleError(err) 389 | strResult += ' ' + current + '\n' 390 | 391 | try: 392 | pending = StrGOM(nvmlDeviceGetPendingGpuOperationMode(handle)) 393 | except NVMLError as err: 394 | pending = handleError(err) 395 | 396 | strResult += ' ' + pending + '\n' 397 | 398 | strResult += ' \n' 399 | 400 | strResult += ' \n' 401 | strResult += ' %02X\n' % pciInfo.bus 402 | strResult += ' %02X\n' % pciInfo.device 403 | strResult += ' %04X\n' % pciInfo.domain 404 | strResult += ' %08X\n' % (pciInfo.pciDeviceId) 405 | strResult += ' ' + str(pciInfo.busId) + '\n' 406 | strResult += ' %08X\n' % (pciInfo.pciSubSystemId) 407 | strResult += ' \n' 408 | 409 | 410 | strResult += ' \n' 411 | 412 | try: 413 | gen = str(nvmlDeviceGetMaxPcieLinkGeneration(handle)) 414 | except NVMLError as err: 415 | gen = handleError(err) 416 | 417 | strResult += ' ' + gen + '\n' 418 | 419 | try: 420 | gen = str(nvmlDeviceGetCurrPcieLinkGeneration(handle)) 421 | except NVMLError as err: 422 | gen = handleError(err) 423 | 424 | strResult += ' ' + gen + '\n' 425 | strResult += ' \n' 426 | strResult += ' \n' 427 | 428 | try: 429 | width = str(nvmlDeviceGetMaxPcieLinkWidth(handle)) + 'x' 430 | except NVMLError as err: 431 | width = handleError(err) 432 | 433 | strResult += ' ' + width + '\n' 434 | 435 | try: 436 | width = str(nvmlDeviceGetCurrPcieLinkWidth(handle)) + 'x' 437 | except NVMLError as err: 438 | width = handleError(err) 439 | 440 | strResult += ' ' + width + '\n' 441 | 442 | strResult += ' \n' 443 | strResult += ' \n' 444 | 445 | 446 | strResult += ' \n' 447 | 448 | try: 449 | bridgeHierarchy = nvmlDeviceGetBridgeChipInfo(handle) 450 | bridge_type = '' 451 | if bridgeHierarchy.bridgeChipInfo[0].type == 0: 452 | bridge_type += 'PLX' 453 | else: 454 | bridge_type += 'BR04' 455 | strResult += ' ' + bridge_type + '\n' 456 | 457 | if bridgeHierarchy.bridgeChipInfo[0].fwVersion == 0: 458 | strFwVersion = 'N/A' 459 | else: 460 | strFwVersion = '%08X' % (bridgeHierarchy.bridgeChipInfo[0].fwVersion) 461 | strResult += ' %s\n' % (strFwVersion) 462 | except NVMLError as err: 463 | strResult += ' ' + handleError(err) + '\n' 464 | strResult += ' ' + handleError(err) + '\n' 465 | 466 | # Add additional code for hierarchy of bridges for Bug # 1382323 467 | strResult += ' \n' 468 | 469 | try: 470 | replay = nvmlDeviceGetPcieReplayCounter(handle) 471 | strResult += ' ' + str(replay) + '' 472 | except NVMLError as err: 473 | strResult += ' ' + handleError(err) + '' 474 | 475 | try: 476 | tx_bytes = nvmlDeviceGetPcieThroughput(handle, NVML_PCIE_UTIL_TX_BYTES) 477 | strResult += ' ' + str(tx_bytes) + ' KB/s' + '' 478 | except NVMLError as err: 479 | strResult += ' ' + handleError(err) + '' 480 | 481 | try: 482 | rx_bytes = nvmlDeviceGetPcieThroughput(handle, NVML_PCIE_UTIL_RX_BYTES) 483 | strResult += ' ' + str(rx_bytes) + ' KB/s' + '' 484 | except NVMLError as err: 485 | strResult += ' ' + handleError(err) + '' 486 | 487 | 488 | strResult += ' \n' 489 | 490 | try: 491 | fan = str(nvmlDeviceGetFanSpeed(handle)) + ' %' 492 | except NVMLError as err: 493 | fan = handleError(err) 494 | strResult += ' ' + fan + '\n' 495 | 496 | try: 497 | perfState = nvmlDeviceGetPowerState(handle) 498 | perfStateStr = 'P%s' % perfState 499 | except NVMLError as err: 500 | perfStateStr = handleError(err) 501 | strResult += ' ' + perfStateStr + '\n' 502 | 503 | strResult += GetClocksThrottleReasons(handle); 504 | 505 | try: 506 | memInfo = nvmlDeviceGetMemoryInfo(handle) 507 | mem_total = str(memInfo.total / 1024 / 1024) + ' MiB' 508 | mem_used = str(memInfo.used / 1024 / 1024) + ' MiB' 509 | mem_free = str(memInfo.total / 1024 / 1024 - memInfo.used / 1024 / 1024) + ' MiB' 510 | except NVMLError as err: 511 | error = handleError(err) 512 | mem_total = error 513 | mem_used = error 514 | mem_free = error 515 | 516 | strResult += ' \n' 517 | strResult += ' ' + mem_total + '\n' 518 | strResult += ' ' + mem_used + '\n' 519 | strResult += ' ' + mem_free + '\n' 520 | strResult += ' \n' 521 | 522 | try: 523 | memInfo = nvmlDeviceGetBAR1MemoryInfo(handle) 524 | mem_total = str(memInfo.bar1Total / 1024 / 1024) + ' MiB' 525 | mem_used = str(memInfo.bar1Used / 1024 / 1024) + ' MiB' 526 | mem_free = str(memInfo.bar1Total / 1024 / 1024 - memInfo.bar1Used / 1024 / 1024) + ' MiB' 527 | except NVMLError as err: 528 | error = handleError(err) 529 | mem_total = error 530 | mem_used = error 531 | mem_free = error 532 | 533 | strResult += ' \n' 534 | strResult += ' ' + mem_total + '\n' 535 | strResult += ' ' + mem_used + '\n' 536 | strResult += ' ' + mem_free + '\n' 537 | strResult += ' \n' 538 | 539 | try: 540 | mode = nvmlDeviceGetComputeMode(handle) 541 | if mode == NVML_COMPUTEMODE_DEFAULT: 542 | modeStr = 'Default' 543 | elif mode == NVML_COMPUTEMODE_EXCLUSIVE_THREAD: 544 | modeStr = 'Exclusive Thread' 545 | elif mode == NVML_COMPUTEMODE_PROHIBITED: 546 | modeStr = 'Prohibited' 547 | elif mode == NVML_COMPUTEMODE_EXCLUSIVE_PROCESS: 548 | modeStr = 'Exclusive_Process' 549 | else: 550 | modeStr = 'Unknown' 551 | except NVMLError as err: 552 | modeStr = handleError(err) 553 | 554 | strResult += ' ' + modeStr + '\n' 555 | 556 | try: 557 | util = nvmlDeviceGetUtilizationRates(handle) 558 | gpu_util = str(util.gpu) + ' %' 559 | mem_util = str(util.memory) + ' %' 560 | except NVMLError as err: 561 | error = handleError(err) 562 | gpu_util = error 563 | mem_util = error 564 | 565 | strResult += ' \n' 566 | strResult += ' ' + gpu_util + '\n' 567 | strResult += ' ' + mem_util + '\n' 568 | 569 | try: 570 | (util_int, ssize) = nvmlDeviceGetEncoderUtilization(handle) 571 | encoder_util = str(util_int) + ' %' 572 | except NVMLError as err: 573 | error = handleError(err) 574 | encoder_util = error 575 | 576 | strResult += ' ' + encoder_util + '\n' 577 | 578 | try: 579 | (util_int, ssize) = nvmlDeviceGetDecoderUtilization(handle) 580 | decoder_util = str(util_int) + ' %' 581 | except NVMLError as err: 582 | error = handleError(err) 583 | decoder_util = error 584 | 585 | strResult += ' ' + decoder_util + '\n' 586 | 587 | strResult += ' \n' 588 | 589 | try: 590 | (current, pending) = nvmlDeviceGetEccMode(handle) 591 | curr_str = 'Enabled' if (current != 0) else 'Disabled' 592 | pend_str = 'Enabled' if (pending != 0) else 'Disabled' 593 | except NVMLError as err: 594 | error = handleError(err) 595 | curr_str = error 596 | pend_str = error 597 | 598 | strResult += ' \n' 599 | strResult += ' ' + curr_str + '\n' 600 | strResult += ' ' + pend_str + '\n' 601 | strResult += ' \n' 602 | 603 | strResult += ' \n' 604 | strResult += GetEccStr(handle) 605 | strResult += ' \n' 606 | 607 | strResult += ' \n' 608 | strResult += GetRetiredPagesStr(handle) 609 | strResult += ' \n' 610 | 611 | try: 612 | temp = str(nvmlDeviceGetTemperature(handle, NVML_TEMPERATURE_GPU)) + ' C' 613 | except NVMLError as err: 614 | temp = handleError(err) 615 | 616 | strResult += ' \n' 617 | strResult += ' ' + temp + '\n' 618 | 619 | try: 620 | temp = str(nvmlDeviceGetTemperatureThreshold(handle, NVML_TEMPERATURE_THRESHOLD_SHUTDOWN)) + ' C' 621 | except NVMLError as err: 622 | temp = handleError(err) 623 | 624 | strResult += ' ' + temp + '\n' 625 | 626 | try: 627 | temp = str(nvmlDeviceGetTemperatureThreshold(handle, NVML_TEMPERATURE_THRESHOLD_SLOWDOWN)) + ' C' 628 | except NVMLError as err: 629 | temp = handleError(err) 630 | 631 | strResult += ' ' + temp + '\n' 632 | strResult += ' \n' 633 | 634 | strResult += ' \n' 635 | try: 636 | perfState = 'P' + str(nvmlDeviceGetPowerState(handle)) 637 | except NVMLError as err: 638 | perfState = handleError(err) 639 | strResult += ' %s\n' % perfState 640 | try: 641 | powMan = nvmlDeviceGetPowerManagementMode(handle) 642 | powManStr = 'Supported' if powMan != 0 else 'N/A' 643 | except NVMLError as err: 644 | powManStr = handleError(err) 645 | strResult += ' ' + powManStr + '\n' 646 | try: 647 | powDraw = (nvmlDeviceGetPowerUsage(handle) / 1000.0) 648 | powDrawStr = '%.2f W' % powDraw 649 | except NVMLError as err: 650 | powDrawStr = handleError(err) 651 | strResult += ' ' + powDrawStr + '\n' 652 | try: 653 | powLimit = (nvmlDeviceGetPowerManagementLimit(handle) / 1000.0) 654 | powLimitStr = '%.2f W' % powLimit 655 | except NVMLError as err: 656 | powLimitStr = handleError(err) 657 | strResult += ' ' + powLimitStr + '\n' 658 | try: 659 | powLimit = (nvmlDeviceGetPowerManagementDefaultLimit(handle) / 1000.0) 660 | powLimitStr = '%.2f W' % powLimit 661 | except NVMLError as err: 662 | powLimitStr = handleError(err) 663 | strResult += ' ' + powLimitStr + '\n' 664 | 665 | try: 666 | enforcedPowLimit = (nvmlDeviceGetEnforcedPowerLimit(handle) / 1000.0) 667 | enforcedPowLimitStr = '%.2f W' % enforcedPowLimit 668 | except NVMLError as err: 669 | enforcedPowLimitStr = handleError(err) 670 | 671 | strResult += ' ' + enforcedPowLimitStr + '\n' 672 | 673 | try: 674 | powLimit = nvmlDeviceGetPowerManagementLimitConstraints(handle) 675 | powLimitStrMin = '%.2f W' % (powLimit[0] / 1000.0) 676 | powLimitStrMax = '%.2f W' % (powLimit[1] / 1000.0) 677 | except NVMLError as err: 678 | error = handleError(err) 679 | powLimitStrMin = error 680 | powLimitStrMax = error 681 | strResult += ' ' + powLimitStrMin + '\n' 682 | strResult += ' ' + powLimitStrMax + '\n' 683 | 684 | strResult += ' \n' 685 | 686 | strResult += ' \n' 687 | try: 688 | graphics = str(nvmlDeviceGetClockInfo(handle, NVML_CLOCK_GRAPHICS)) + ' MHz' 689 | except NVMLError as err: 690 | graphics = handleError(err) 691 | strResult += ' ' +graphics + '\n' 692 | try: 693 | sm = str(nvmlDeviceGetClockInfo(handle, NVML_CLOCK_SM)) + ' MHz' 694 | except NVMLError as err: 695 | sm = handleError(err) 696 | strResult += ' ' + sm + '\n' 697 | try: 698 | mem = str(nvmlDeviceGetClockInfo(handle, NVML_CLOCK_MEM)) + ' MHz' 699 | except NVMLError as err: 700 | mem = handleError(err) 701 | strResult += ' ' + mem + '\n' 702 | strResult += ' \n' 703 | 704 | strResult += ' \n' 705 | try: 706 | graphics = str(nvmlDeviceGetApplicationsClock(handle, NVML_CLOCK_GRAPHICS)) + ' MHz' 707 | except NVMLError as err: 708 | graphics = handleError(err) 709 | strResult += ' ' +graphics + '\n' 710 | try: 711 | mem = str(nvmlDeviceGetApplicationsClock(handle, NVML_CLOCK_MEM)) + ' MHz' 712 | except NVMLError as err: 713 | mem = handleError(err) 714 | strResult += ' ' + mem + '\n' 715 | strResult += ' \n' 716 | 717 | strResult += ' \n' 718 | try: 719 | graphics = str(nvmlDeviceGetDefaultApplicationsClock(handle, NVML_CLOCK_GRAPHICS)) + ' MHz' 720 | except NVMLError as err: 721 | graphics = handleError(err) 722 | strResult += ' ' +graphics + '\n' 723 | try: 724 | mem = str(nvmlDeviceGetDefaultApplicationsClock(handle, NVML_CLOCK_MEM)) + ' MHz' 725 | except NVMLError as err: 726 | mem = handleError(err) 727 | strResult += ' ' + mem + '\n' 728 | strResult += ' \n' 729 | 730 | strResult += ' \n' 731 | try: 732 | graphics = str(nvmlDeviceGetMaxClockInfo(handle, NVML_CLOCK_GRAPHICS)) + ' MHz' 733 | except NVMLError as err: 734 | graphics = handleError(err) 735 | strResult += ' ' + graphics + '\n' 736 | try: 737 | sm = str(nvmlDeviceGetMaxClockInfo(handle, NVML_CLOCK_SM)) + ' MHz' 738 | except NVMLError as err: 739 | sm = handleError(err) 740 | strResult += ' ' + sm + '\n' 741 | try: 742 | mem = str(nvmlDeviceGetMaxClockInfo(handle, NVML_CLOCK_MEM)) + ' MHz' 743 | except NVMLError as err: 744 | mem = handleError(err) 745 | strResult += ' ' + mem + '\n' 746 | strResult += ' \n' 747 | 748 | strResult += ' \n' 749 | try: 750 | boostedState, boostedDefaultState = nvmlDeviceGetAutoBoostedClocksEnabled(handle) 751 | if boostedState == NVML_FEATURE_DISABLED: 752 | autoBoostStr = "Off" 753 | else: 754 | autoBoostStr = "On" 755 | 756 | if boostedDefaultState == NVML_FEATURE_DISABLED: 757 | autoBoostDefaultStr = "Off" 758 | else: 759 | autoBoostDefaultStr = "On" 760 | 761 | except NVMLError_NotSupported: 762 | autoBoostStr = "N/A" 763 | autoBoostDefaultStr = "N/A" 764 | except NVMLError as err: 765 | autoBoostStr = handleError(err) 766 | autoBoostDefaultStr = handleError(err) 767 | pass 768 | strResult += ' ' + autoBoostStr + '\n' 769 | strResult += ' ' + autoBoostDefaultStr + '\n' 770 | strResult += ' \n' 771 | 772 | try: 773 | memClocks = nvmlDeviceGetSupportedMemoryClocks(handle) 774 | strResult += ' \n' 775 | 776 | for m in memClocks: 777 | strResult += ' \n' 778 | strResult += ' %d MHz\n' % m 779 | try: 780 | clocks = nvmlDeviceGetSupportedGraphicsClocks(handle, m) 781 | for c in clocks: 782 | strResult += ' %d MHz\n' % c 783 | except NVMLError as err: 784 | strResult += ' %s\n' % handleError(err) 785 | strResult += ' \n' 786 | 787 | strResult += ' \n' 788 | except NVMLError as err: 789 | strResult += ' ' + handleError(err) + '\n' 790 | 791 | try: 792 | procs = nvmlDeviceGetComputeRunningProcesses(handle) 793 | strResult += ' \n' 794 | 795 | for p in procs: 796 | try: 797 | name = str(nvmlSystemGetProcessName(p.pid)) 798 | except NVMLError as err: 799 | if (err.value == NVML_ERROR_NOT_FOUND): 800 | # probably went away 801 | continue 802 | else: 803 | name = handleError(err) 804 | 805 | strResult += ' \n' 806 | strResult += ' %d\n' % p.pid 807 | strResult += ' ' + name + '\n' 808 | 809 | if (p.usedGpuMemory == None): 810 | mem = 'N\A' 811 | else: 812 | mem = '%d MiB' % (p.usedGpuMemory / 1024 / 1024) 813 | strResult += ' ' + mem + '\n' 814 | strResult += ' \n' 815 | 816 | strResult += ' \n' 817 | except NVMLError as err: 818 | strResult += ' ' + handleError(err) + '\n' 819 | 820 | 821 | try: 822 | pids = nvmlDeviceGetAccountingPids(handle) 823 | strResult += ' \n' 824 | 825 | for pid in pids : 826 | try: 827 | stats = nvmlDeviceGetAccountingStats(handle, pid) 828 | gpuUtilization = "%d %%" % stats.gpuUtilization 829 | memoryUtilization = "%d %%" % stats.memoryUtilization 830 | if (stats.maxMemoryUsage == None): 831 | maxMemoryUsage = 'N\A' 832 | else: 833 | maxMemoryUsage = '%d MiB' % (stats.maxMemoryUsage / 1024 / 1024) 834 | time = "%d ms" % stats.time 835 | is_running = "%d" % stats.isRunning 836 | except NVMLError as err: 837 | if (err.value == NVML_ERROR_NOT_FOUND): 838 | # probably went away 839 | continue 840 | err = handleError(err) 841 | gpuUtilization = err 842 | memoryUtilization = err 843 | maxMemoryUsage = err 844 | time = err 845 | is_running = err 846 | 847 | strResult += ' \n' 848 | strResult += ' %d\n' % pid 849 | strResult += ' ' + gpuUtilization + '\n' 850 | strResult += ' ' + memoryUtilization + '\n' 851 | strResult += ' ' + maxMemoryUsage+ '\n' 852 | strResult += ' \n' 853 | strResult += ' ' + is_running + '\n' 854 | strResult += ' \n' 855 | 856 | strResult += ' \n' 857 | except NVMLError as err: 858 | strResult += ' ' + handleError(err) + '\n' 859 | 860 | strResult += ' \n' 861 | 862 | strResult += '\n' 863 | 864 | except NVMLError as err: 865 | strResult += 'nvidia_smi.py: ' + err.__str__() + '\n' 866 | 867 | nvmlShutdown() 868 | 869 | return strResult 870 | 871 | # this is not exectued when module is imported 872 | if __name__ == "__main__": 873 | print(XmlDeviceQuery()) 874 | -------------------------------------------------------------------------------- /pynvml.py: -------------------------------------------------------------------------------- 1 | ##### 2 | # Copyright (c) 2011-2015, NVIDIA Corporation. All rights reserved. 3 | # 4 | # Redistribution and use in source and binary forms, with or without 5 | # modification, are permitted provided that the following conditions are met: 6 | # 7 | # * Redistributions of source code must retain the above copyright notice, 8 | # this list of conditions and the following disclaimer. 9 | # * Redistributions in binary form must reproduce the above copyright 10 | # notice, this list of conditions and the following disclaimer in the 11 | # documentation and/or other materials provided with the distribution. 12 | # * Neither the name of the NVIDIA Corporation nor the names of its 13 | # contributors may be used to endorse or promote products derived from 14 | # this software without specific prior written permission. 15 | # 16 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 26 | # THE POSSIBILITY OF SUCH DAMAGE. 27 | ##### 28 | 29 | ## 30 | # Python bindings for the NVML library 31 | ## 32 | from ctypes import * 33 | from ctypes.util import find_library 34 | import sys 35 | import os 36 | import threading 37 | import string 38 | 39 | ## C Type mappings ## 40 | ## Enums 41 | _nvmlEnableState_t = c_uint 42 | NVML_FEATURE_DISABLED = 0 43 | NVML_FEATURE_ENABLED = 1 44 | 45 | _nvmlBrandType_t = c_uint 46 | NVML_BRAND_UNKNOWN = 0 47 | NVML_BRAND_QUADRO = 1 48 | NVML_BRAND_TESLA = 2 49 | NVML_BRAND_NVS = 3 50 | NVML_BRAND_GRID = 4 51 | NVML_BRAND_GEFORCE = 5 52 | NVML_BRAND_COUNT = 6 53 | 54 | _nvmlTemperatureThresholds_t = c_uint 55 | NVML_TEMPERATURE_THRESHOLD_SHUTDOWN = 0 56 | NVML_TEMPERATURE_THRESHOLD_SLOWDOWN = 1 57 | NVML_TEMPERATURE_THRESHOLD_COUNT = 1 58 | 59 | _nvmlTemperatureSensors_t = c_uint 60 | NVML_TEMPERATURE_GPU = 0 61 | NVML_TEMPERATURE_COUNT = 1 62 | 63 | _nvmlComputeMode_t = c_uint 64 | NVML_COMPUTEMODE_DEFAULT = 0 65 | NVML_COMPUTEMODE_EXCLUSIVE_THREAD = 1 66 | NVML_COMPUTEMODE_PROHIBITED = 2 67 | NVML_COMPUTEMODE_EXCLUSIVE_PROCESS = 3 68 | NVML_COMPUTEMODE_COUNT = 4 69 | 70 | _nvmlMemoryLocation_t = c_uint 71 | NVML_MEMORY_LOCATION_L1_CACHE = 0 72 | NVML_MEMORY_LOCATION_L2_CACHE = 1 73 | NVML_MEMORY_LOCATION_DEVICE_MEMORY = 2 74 | NVML_MEMORY_LOCATION_REGISTER_FILE = 3 75 | NVML_MEMORY_LOCATION_TEXTURE_MEMORY = 4 76 | NVML_MEMORY_LOCATION_COUNT = 5 77 | 78 | # These are deprecated, instead use _nvmlMemoryErrorType_t 79 | _nvmlEccBitType_t = c_uint 80 | NVML_SINGLE_BIT_ECC = 0 81 | NVML_DOUBLE_BIT_ECC = 1 82 | NVML_ECC_ERROR_TYPE_COUNT = 2 83 | 84 | _nvmlEccCounterType_t = c_uint 85 | NVML_VOLATILE_ECC = 0 86 | NVML_AGGREGATE_ECC = 1 87 | NVML_ECC_COUNTER_TYPE_COUNT = 2 88 | 89 | _nvmlMemoryErrorType_t = c_uint 90 | NVML_MEMORY_ERROR_TYPE_CORRECTED = 0 91 | NVML_MEMORY_ERROR_TYPE_UNCORRECTED = 1 92 | NVML_MEMORY_ERROR_TYPE_COUNT = 2 93 | 94 | _nvmlClockType_t = c_uint 95 | NVML_CLOCK_GRAPHICS = 0 96 | NVML_CLOCK_SM = 1 97 | NVML_CLOCK_MEM = 2 98 | NVML_CLOCK_COUNT = 3 99 | 100 | _nvmlDriverModel_t = c_uint 101 | NVML_DRIVER_WDDM = 0 102 | NVML_DRIVER_WDM = 1 103 | 104 | _nvmlPstates_t = c_uint 105 | NVML_PSTATE_0 = 0 106 | NVML_PSTATE_1 = 1 107 | NVML_PSTATE_2 = 2 108 | NVML_PSTATE_3 = 3 109 | NVML_PSTATE_4 = 4 110 | NVML_PSTATE_5 = 5 111 | NVML_PSTATE_6 = 6 112 | NVML_PSTATE_7 = 7 113 | NVML_PSTATE_8 = 8 114 | NVML_PSTATE_9 = 9 115 | NVML_PSTATE_10 = 10 116 | NVML_PSTATE_11 = 11 117 | NVML_PSTATE_12 = 12 118 | NVML_PSTATE_13 = 13 119 | NVML_PSTATE_14 = 14 120 | NVML_PSTATE_15 = 15 121 | NVML_PSTATE_UNKNOWN = 32 122 | 123 | _nvmlInforomObject_t = c_uint 124 | NVML_INFOROM_OEM = 0 125 | NVML_INFOROM_ECC = 1 126 | NVML_INFOROM_POWER = 2 127 | NVML_INFOROM_COUNT = 3 128 | 129 | _nvmlReturn_t = c_uint 130 | NVML_SUCCESS = 0 131 | NVML_ERROR_UNINITIALIZED = 1 132 | NVML_ERROR_INVALID_ARGUMENT = 2 133 | NVML_ERROR_NOT_SUPPORTED = 3 134 | NVML_ERROR_NO_PERMISSION = 4 135 | NVML_ERROR_ALREADY_INITIALIZED = 5 136 | NVML_ERROR_NOT_FOUND = 6 137 | NVML_ERROR_INSUFFICIENT_SIZE = 7 138 | NVML_ERROR_INSUFFICIENT_POWER = 8 139 | NVML_ERROR_DRIVER_NOT_LOADED = 9 140 | NVML_ERROR_TIMEOUT = 10 141 | NVML_ERROR_IRQ_ISSUE = 11 142 | NVML_ERROR_LIBRARY_NOT_FOUND = 12 143 | NVML_ERROR_FUNCTION_NOT_FOUND = 13 144 | NVML_ERROR_CORRUPTED_INFOROM = 14 145 | NVML_ERROR_GPU_IS_LOST = 15 146 | NVML_ERROR_RESET_REQUIRED = 16 147 | NVML_ERROR_OPERATING_SYSTEM = 17 148 | NVML_ERROR_LIB_RM_VERSION_MISMATCH = 18 149 | NVML_ERROR_UNKNOWN = 999 150 | 151 | _nvmlFanState_t = c_uint 152 | NVML_FAN_NORMAL = 0 153 | NVML_FAN_FAILED = 1 154 | 155 | _nvmlLedColor_t = c_uint 156 | NVML_LED_COLOR_GREEN = 0 157 | NVML_LED_COLOR_AMBER = 1 158 | 159 | _nvmlGpuOperationMode_t = c_uint 160 | NVML_GOM_ALL_ON = 0 161 | NVML_GOM_COMPUTE = 1 162 | NVML_GOM_LOW_DP = 2 163 | 164 | _nvmlPageRetirementCause_t = c_uint 165 | NVML_PAGE_RETIREMENT_CAUSE_DOUBLE_BIT_ECC_ERROR = 0 166 | NVML_PAGE_RETIREMENT_CAUSE_MULTIPLE_SINGLE_BIT_ECC_ERRORS = 1 167 | NVML_PAGE_RETIREMENT_CAUSE_COUNT = 2 168 | 169 | _nvmlRestrictedAPI_t = c_uint 170 | NVML_RESTRICTED_API_SET_APPLICATION_CLOCKS = 0 171 | NVML_RESTRICTED_API_SET_AUTO_BOOSTED_CLOCKS = 1 172 | NVML_RESTRICTED_API_COUNT = 2 173 | 174 | _nvmlBridgeChipType_t = c_uint 175 | NVML_BRIDGE_CHIP_PLX = 0 176 | NVML_BRIDGE_CHIP_BRO4 = 1 177 | NVML_MAX_PHYSICAL_BRIDGE = 128 178 | 179 | _nvmlValueType_t = c_uint 180 | NVML_VALUE_TYPE_DOUBLE = 0 181 | NVML_VALUE_TYPE_UNSIGNED_INT = 1 182 | NVML_VALUE_TYPE_UNSIGNED_LONG = 2 183 | NVML_VALUE_TYPE_UNSIGNED_LONG_LONG = 3 184 | NVML_VALUE_TYPE_COUNT = 4 185 | 186 | _nvmlPerfPolicyType_t = c_uint 187 | NVML_PERF_POLICY_POWER = 0 188 | NVML_PERF_POLICY_THERMAL = 1 189 | NVML_PERF_POLICY_COUNT = 2 190 | 191 | _nvmlSamplingType_t = c_uint 192 | NVML_TOTAL_POWER_SAMPLES = 0 193 | NVML_GPU_UTILIZATION_SAMPLES = 1 194 | NVML_MEMORY_UTILIZATION_SAMPLES = 2 195 | NVML_ENC_UTILIZATION_SAMPLES = 3 196 | NVML_DEC_UTILIZATION_SAMPLES = 4 197 | NVML_PROCESSOR_CLK_SAMPLES = 5 198 | NVML_MEMORY_CLK_SAMPLES = 6 199 | NVML_SAMPLINGTYPE_COUNT = 7 200 | 201 | _nvmlPcieUtilCounter_t = c_uint 202 | NVML_PCIE_UTIL_TX_BYTES = 0 203 | NVML_PCIE_UTIL_RX_BYTES = 1 204 | NVML_PCIE_UTIL_COUNT = 2 205 | 206 | _nvmlGpuTopologyLevel_t = c_uint 207 | NVML_TOPOLOGY_INTERNAL = 0 208 | NVML_TOPOLOGY_SINGLE = 10 209 | NVML_TOPOLOGY_MULTIPLE = 20 210 | NVML_TOPOLOGY_HOSTBRIDGE = 30 211 | NVML_TOPOLOGY_CPU = 40 212 | NVML_TOPOLOGY_SYSTEM = 50 213 | 214 | # C preprocessor defined values 215 | nvmlFlagDefault = 0 216 | nvmlFlagForce = 1 217 | 218 | # buffer size 219 | NVML_DEVICE_INFOROM_VERSION_BUFFER_SIZE = 16 220 | NVML_DEVICE_UUID_BUFFER_SIZE = 80 221 | NVML_SYSTEM_DRIVER_VERSION_BUFFER_SIZE = 81 222 | NVML_SYSTEM_NVML_VERSION_BUFFER_SIZE = 80 223 | NVML_DEVICE_NAME_BUFFER_SIZE = 64 224 | NVML_DEVICE_SERIAL_BUFFER_SIZE = 30 225 | NVML_DEVICE_VBIOS_VERSION_BUFFER_SIZE = 32 226 | NVML_DEVICE_PCI_BUS_ID_BUFFER_SIZE = 16 227 | 228 | NVML_VALUE_NOT_AVAILABLE_ulonglong = c_ulonglong(-1) 229 | NVML_VALUE_NOT_AVAILABLE_uint = c_uint(-1) 230 | 231 | ## Lib loading ## 232 | nvmlLib = None 233 | libLoadLock = threading.Lock() 234 | _nvmlLib_refcount = 0 # Incremented on each nvmlInit and decremented on nvmlShutdown 235 | 236 | ## Error Checking ## 237 | class NVMLError(Exception): 238 | _valClassMapping = dict() 239 | # List of currently known error codes 240 | _errcode_to_string = { 241 | NVML_ERROR_UNINITIALIZED: "Uninitialized", 242 | NVML_ERROR_INVALID_ARGUMENT: "Invalid Argument", 243 | NVML_ERROR_NOT_SUPPORTED: "Not Supported", 244 | NVML_ERROR_NO_PERMISSION: "Insufficient Permissions", 245 | NVML_ERROR_ALREADY_INITIALIZED: "Already Initialized", 246 | NVML_ERROR_NOT_FOUND: "Not Found", 247 | NVML_ERROR_INSUFFICIENT_SIZE: "Insufficient Size", 248 | NVML_ERROR_INSUFFICIENT_POWER: "Insufficient External Power", 249 | NVML_ERROR_DRIVER_NOT_LOADED: "Driver Not Loaded", 250 | NVML_ERROR_TIMEOUT: "Timeout", 251 | NVML_ERROR_IRQ_ISSUE: "Interrupt Request Issue", 252 | NVML_ERROR_LIBRARY_NOT_FOUND: "NVML Shared Library Not Found", 253 | NVML_ERROR_FUNCTION_NOT_FOUND: "Function Not Found", 254 | NVML_ERROR_CORRUPTED_INFOROM: "Corrupted infoROM", 255 | NVML_ERROR_GPU_IS_LOST: "GPU is lost", 256 | NVML_ERROR_RESET_REQUIRED: "GPU requires restart", 257 | NVML_ERROR_OPERATING_SYSTEM: "The operating system has blocked the request.", 258 | NVML_ERROR_LIB_RM_VERSION_MISMATCH: "RM has detected an NVML/RM version mismatch.", 259 | NVML_ERROR_UNKNOWN: "Unknown Error", 260 | } 261 | def __new__(typ, value): 262 | ''' 263 | Maps value to a proper subclass of NVMLError. 264 | See _extractNVMLErrorsAsClasses function for more details 265 | ''' 266 | if typ == NVMLError: 267 | typ = NVMLError._valClassMapping.get(value, typ) 268 | obj = Exception.__new__(typ) 269 | obj.value = value 270 | return obj 271 | def __str__(self): 272 | try: 273 | if self.value not in NVMLError._errcode_to_string: 274 | NVMLError._errcode_to_string[self.value] = str(nvmlErrorString(self.value)) 275 | return NVMLError._errcode_to_string[self.value] 276 | except NVMLError_Uninitialized: 277 | return "NVML Error with code %d" % self.value 278 | def __eq__(self, other): 279 | return self.value == other.value 280 | 281 | def _extractNVMLErrorsAsClasses(): 282 | ''' 283 | Generates a hierarchy of classes on top of NVMLError class. 284 | 285 | Each NVML Error gets a new NVMLError subclass. This way try,except blocks can filter appropriate 286 | exceptions more easily. 287 | 288 | NVMLError is a parent class. Each NVML_ERROR_* gets it's own subclass. 289 | e.g. NVML_ERROR_ALREADY_INITIALIZED will be turned into NVMLError_AlreadyInitialized 290 | ''' 291 | this_module = sys.modules[__name__] 292 | nvmlErrorsNames = filter(lambda x: x.startswith("NVML_ERROR_"), dir(this_module)) 293 | for err_name in nvmlErrorsNames: 294 | # e.g. Turn NVML_ERROR_ALREADY_INITIALIZED into NVMLError_AlreadyInitialized 295 | class_name = "NVMLError_" + string.capwords(err_name.replace("NVML_ERROR_", ""), "_").replace("_", "") 296 | err_val = getattr(this_module, err_name) 297 | def gen_new(val): 298 | def new(typ): 299 | obj = NVMLError.__new__(typ, val) 300 | return obj 301 | return new 302 | new_error_class = type(class_name, (NVMLError,), {'__new__': gen_new(err_val)}) 303 | new_error_class.__module__ = __name__ 304 | setattr(this_module, class_name, new_error_class) 305 | NVMLError._valClassMapping[err_val] = new_error_class 306 | _extractNVMLErrorsAsClasses() 307 | 308 | def _nvmlCheckReturn(ret): 309 | if (ret != NVML_SUCCESS): 310 | raise NVMLError(ret) 311 | return ret 312 | 313 | ## Function access ## 314 | _nvmlGetFunctionPointer_cache = dict() # function pointers are cached to prevent unnecessary libLoadLock locking 315 | def _nvmlGetFunctionPointer(name): 316 | global nvmlLib 317 | 318 | if name in _nvmlGetFunctionPointer_cache: 319 | return _nvmlGetFunctionPointer_cache[name] 320 | 321 | libLoadLock.acquire() 322 | try: 323 | # ensure library was loaded 324 | if (nvmlLib == None): 325 | raise NVMLError(NVML_ERROR_UNINITIALIZED) 326 | try: 327 | _nvmlGetFunctionPointer_cache[name] = getattr(nvmlLib, name) 328 | return _nvmlGetFunctionPointer_cache[name] 329 | except AttributeError: 330 | raise NVMLError(NVML_ERROR_FUNCTION_NOT_FOUND) 331 | finally: 332 | # lock is always freed 333 | libLoadLock.release() 334 | 335 | ## Alternative object 336 | # Allows the object to be printed 337 | # Allows mismatched types to be assigned 338 | # - like None when the Structure variant requires c_uint 339 | class nvmlFriendlyObject(object): 340 | def __init__(self, dictionary): 341 | for x in dictionary: 342 | setattr(self, x, dictionary[x]) 343 | def __str__(self): 344 | return self.__dict__.__str__() 345 | 346 | def nvmlStructToFriendlyObject(struct): 347 | d = {} 348 | for x in struct._fields_: 349 | key = x[0] 350 | value = getattr(struct, key) 351 | d[key] = value 352 | obj = nvmlFriendlyObject(d) 353 | return obj 354 | 355 | # pack the object so it can be passed to the NVML library 356 | def nvmlFriendlyObjectToStruct(obj, model): 357 | for x in model._fields_: 358 | key = x[0] 359 | value = obj.__dict__[key] 360 | setattr(model, key, value) 361 | return model 362 | 363 | ## Unit structures 364 | class struct_c_nvmlUnit_t(Structure): 365 | pass # opaque handle 366 | c_nvmlUnit_t = POINTER(struct_c_nvmlUnit_t) 367 | 368 | class _PrintableStructure(Structure): 369 | """ 370 | Abstract class that produces nicer __str__ output than ctypes.Structure. 371 | e.g. instead of: 372 | >>> print str(obj) 373 | 374 | this class will print 375 | class_name(field_name: formatted_value, field_name: formatted_value) 376 | 377 | _fmt_ dictionary of -> 378 | e.g. class that has _field_ 'hex_value', c_uint could be formatted with 379 | _fmt_ = {"hex_value" : "%08X"} 380 | to produce nicer output. 381 | Default fomratting string for all fields can be set with key "" like: 382 | _fmt_ = {"" : "%d MHz"} # e.g all values are numbers in MHz. 383 | If not set it's assumed to be just "%s" 384 | 385 | Exact format of returned str from this class is subject to change in the future. 386 | """ 387 | _fmt_ = {} 388 | def __str__(self): 389 | result = [] 390 | for x in self._fields_: 391 | key = x[0] 392 | value = getattr(self, key) 393 | fmt = "%s" 394 | if key in self._fmt_: 395 | fmt = self._fmt_[key] 396 | elif "" in self._fmt_: 397 | fmt = self._fmt_[""] 398 | result.append(("%s: " + fmt) % (key, value)) 399 | return self.__class__.__name__ + "(" + string.join(result, ", ") + ")" 400 | 401 | class c_nvmlUnitInfo_t(_PrintableStructure): 402 | _fields_ = [ 403 | ('name', c_char * 96), 404 | ('id', c_char * 96), 405 | ('serial', c_char * 96), 406 | ('firmwareVersion', c_char * 96), 407 | ] 408 | 409 | class c_nvmlLedState_t(_PrintableStructure): 410 | _fields_ = [ 411 | ('cause', c_char * 256), 412 | ('color', _nvmlLedColor_t), 413 | ] 414 | 415 | class c_nvmlPSUInfo_t(_PrintableStructure): 416 | _fields_ = [ 417 | ('state', c_char * 256), 418 | ('current', c_uint), 419 | ('voltage', c_uint), 420 | ('power', c_uint), 421 | ] 422 | 423 | class c_nvmlUnitFanInfo_t(_PrintableStructure): 424 | _fields_ = [ 425 | ('speed', c_uint), 426 | ('state', _nvmlFanState_t), 427 | ] 428 | 429 | class c_nvmlUnitFanSpeeds_t(_PrintableStructure): 430 | _fields_ = [ 431 | ('fans', c_nvmlUnitFanInfo_t * 24), 432 | ('count', c_uint) 433 | ] 434 | 435 | ## Device structures 436 | class struct_c_nvmlDevice_t(Structure): 437 | pass # opaque handle 438 | c_nvmlDevice_t = POINTER(struct_c_nvmlDevice_t) 439 | 440 | class nvmlPciInfo_t(_PrintableStructure): 441 | _fields_ = [ 442 | ('busId', c_char * 16), 443 | ('domain', c_uint), 444 | ('bus', c_uint), 445 | ('device', c_uint), 446 | ('pciDeviceId', c_uint), 447 | 448 | # Added in 2.285 449 | ('pciSubSystemId', c_uint), 450 | ('reserved0', c_uint), 451 | ('reserved1', c_uint), 452 | ('reserved2', c_uint), 453 | ('reserved3', c_uint), 454 | ] 455 | _fmt_ = { 456 | 'domain' : "0x%04X", 457 | 'bus' : "0x%02X", 458 | 'device' : "0x%02X", 459 | 'pciDeviceId' : "0x%08X", 460 | 'pciSubSystemId' : "0x%08X", 461 | } 462 | 463 | class c_nvmlMemory_t(_PrintableStructure): 464 | _fields_ = [ 465 | ('total', c_ulonglong), 466 | ('free', c_ulonglong), 467 | ('used', c_ulonglong), 468 | ] 469 | _fmt_ = {'': "%d B"} 470 | 471 | class c_nvmlBAR1Memory_t(_PrintableStructure): 472 | _fields_ = [ 473 | ('bar1Total', c_ulonglong), 474 | ('bar1Free', c_ulonglong), 475 | ('bar1Used', c_ulonglong), 476 | ] 477 | _fmt_ = {'': "%d B"} 478 | 479 | # On Windows with the WDDM driver, usedGpuMemory is reported as None 480 | # Code that processes this structure should check for None, I.E. 481 | # 482 | # if (info.usedGpuMemory == None): 483 | # # TODO handle the error 484 | # pass 485 | # else: 486 | # print("Using %d MiB of memory" % (info.usedGpuMemory / 1024 / 1024)) 487 | # 488 | # See NVML documentation for more information 489 | class c_nvmlProcessInfo_t(_PrintableStructure): 490 | _fields_ = [ 491 | ('pid', c_uint), 492 | ('usedGpuMemory', c_ulonglong), 493 | ] 494 | _fmt_ = {'usedGpuMemory': "%d B"} 495 | 496 | class c_nvmlBridgeChipInfo_t(_PrintableStructure): 497 | _fields_ = [ 498 | ('type', _nvmlBridgeChipType_t), 499 | ('fwVersion', c_uint), 500 | ] 501 | 502 | class c_nvmlBridgeChipHierarchy_t(_PrintableStructure): 503 | _fields_ = [ 504 | ('bridgeCount', c_uint), 505 | ('bridgeChipInfo', c_nvmlBridgeChipInfo_t * 128), 506 | ] 507 | 508 | class c_nvmlEccErrorCounts_t(_PrintableStructure): 509 | _fields_ = [ 510 | ('l1Cache', c_ulonglong), 511 | ('l2Cache', c_ulonglong), 512 | ('deviceMemory', c_ulonglong), 513 | ('registerFile', c_ulonglong), 514 | ] 515 | 516 | class c_nvmlUtilization_t(_PrintableStructure): 517 | _fields_ = [ 518 | ('gpu', c_uint), 519 | ('memory', c_uint), 520 | ] 521 | _fmt_ = {'': "%d %%"} 522 | 523 | # Added in 2.285 524 | class c_nvmlHwbcEntry_t(_PrintableStructure): 525 | _fields_ = [ 526 | ('hwbcId', c_uint), 527 | ('firmwareVersion', c_char * 32), 528 | ] 529 | 530 | class c_nvmlValue_t(Union): 531 | _fields_ = [ 532 | ('dVal', c_double), 533 | ('uiVal', c_uint), 534 | ('ulVal', c_ulong), 535 | ('ullVal', c_ulonglong), 536 | ] 537 | 538 | class c_nvmlSample_t(_PrintableStructure): 539 | _fields_ = [ 540 | ('timeStamp', c_ulonglong), 541 | ('sampleValue', c_nvmlValue_t), 542 | ] 543 | 544 | class c_nvmlViolationTime_t(_PrintableStructure): 545 | _fields_ = [ 546 | ('referenceTime', c_ulonglong), 547 | ('violationTime', c_ulonglong), 548 | ] 549 | 550 | ## Event structures 551 | class struct_c_nvmlEventSet_t(Structure): 552 | pass # opaque handle 553 | c_nvmlEventSet_t = POINTER(struct_c_nvmlEventSet_t) 554 | 555 | nvmlEventTypeSingleBitEccError = 0x0000000000000001 556 | nvmlEventTypeDoubleBitEccError = 0x0000000000000002 557 | nvmlEventTypePState = 0x0000000000000004 558 | nvmlEventTypeXidCriticalError = 0x0000000000000008 559 | nvmlEventTypeClock = 0x0000000000000010 560 | nvmlEventTypeNone = 0x0000000000000000 561 | nvmlEventTypeAll = ( 562 | nvmlEventTypeNone | 563 | nvmlEventTypeSingleBitEccError | 564 | nvmlEventTypeDoubleBitEccError | 565 | nvmlEventTypePState | 566 | nvmlEventTypeClock | 567 | nvmlEventTypeXidCriticalError 568 | ) 569 | 570 | ## Clock Throttle Reasons defines 571 | nvmlClocksThrottleReasonGpuIdle = 0x0000000000000001 572 | nvmlClocksThrottleReasonApplicationsClocksSetting = 0x0000000000000002 573 | nvmlClocksThrottleReasonUserDefinedClocks = nvmlClocksThrottleReasonApplicationsClocksSetting # deprecated, use nvmlClocksThrottleReasonApplicationsClocksSetting 574 | nvmlClocksThrottleReasonSwPowerCap = 0x0000000000000004 575 | nvmlClocksThrottleReasonHwSlowdown = 0x0000000000000008 576 | nvmlClocksThrottleReasonUnknown = 0x8000000000000000 577 | nvmlClocksThrottleReasonNone = 0x0000000000000000 578 | nvmlClocksThrottleReasonAll = ( 579 | nvmlClocksThrottleReasonNone | 580 | nvmlClocksThrottleReasonGpuIdle | 581 | nvmlClocksThrottleReasonApplicationsClocksSetting | 582 | nvmlClocksThrottleReasonSwPowerCap | 583 | nvmlClocksThrottleReasonHwSlowdown | 584 | nvmlClocksThrottleReasonUnknown 585 | ) 586 | 587 | class c_nvmlEventData_t(_PrintableStructure): 588 | _fields_ = [ 589 | ('device', c_nvmlDevice_t), 590 | ('eventType', c_ulonglong), 591 | ('eventData', c_ulonglong) 592 | ] 593 | _fmt_ = {'eventType': "0x%08X"} 594 | 595 | class c_nvmlAccountingStats_t(_PrintableStructure): 596 | _fields_ = [ 597 | ('gpuUtilization', c_uint), 598 | ('memoryUtilization', c_uint), 599 | ('maxMemoryUsage', c_ulonglong), 600 | ('time', c_ulonglong), 601 | ('startTime', c_ulonglong), 602 | ('isRunning', c_uint), 603 | ('reserved', c_uint * 5) 604 | ] 605 | 606 | ## C function wrappers ## 607 | def nvmlInit(): 608 | _LoadNvmlLibrary() 609 | 610 | # 611 | # Initialize the library 612 | # 613 | fn = _nvmlGetFunctionPointer("nvmlInit_v2") 614 | ret = fn() 615 | _nvmlCheckReturn(ret) 616 | 617 | # Atomically update refcount 618 | global _nvmlLib_refcount 619 | libLoadLock.acquire() 620 | _nvmlLib_refcount += 1 621 | libLoadLock.release() 622 | return None 623 | 624 | def _LoadNvmlLibrary(): 625 | ''' 626 | Load the library if it isn't loaded already 627 | ''' 628 | global nvmlLib 629 | 630 | if (nvmlLib == None): 631 | # lock to ensure only one caller loads the library 632 | libLoadLock.acquire() 633 | 634 | try: 635 | # ensure the library still isn't loaded 636 | if (nvmlLib == None): 637 | try: 638 | if (sys.platform[:3] == "win"): 639 | searchPaths = [ 640 | os.path.join(os.getenv("ProgramFiles", r"C:\Program Files"), r"NVIDIA Corporation\NVSMI\nvml.dll"), 641 | os.path.join(os.getenv("WinDir", r"C:\Windows"), r"System32\nvml.dll"), 642 | ] 643 | nvmlPath = next((x for x in searchPaths if os.path.isfile(x)), None) 644 | if (nvmlPath == None): 645 | _nvmlCheckReturn(NVML_ERROR_LIBRARY_NOT_FOUND) 646 | else: 647 | # cdecl calling convention 648 | nvmlLib = CDLL(nvmlPath) 649 | else: 650 | # assume linux 651 | nvmlLib = CDLL("libnvidia-ml.so.1") 652 | except OSError as ose: 653 | _nvmlCheckReturn(NVML_ERROR_LIBRARY_NOT_FOUND) 654 | if (nvmlLib == None): 655 | _nvmlCheckReturn(NVML_ERROR_LIBRARY_NOT_FOUND) 656 | finally: 657 | # lock is always freed 658 | libLoadLock.release() 659 | 660 | def nvmlShutdown(): 661 | # 662 | # Leave the library loaded, but shutdown the interface 663 | # 664 | fn = _nvmlGetFunctionPointer("nvmlShutdown") 665 | ret = fn() 666 | _nvmlCheckReturn(ret) 667 | 668 | # Atomically update refcount 669 | global _nvmlLib_refcount 670 | libLoadLock.acquire() 671 | if (0 < _nvmlLib_refcount): 672 | _nvmlLib_refcount -= 1 673 | libLoadLock.release() 674 | return None 675 | 676 | # Added in 2.285 677 | def nvmlErrorString(result): 678 | fn = _nvmlGetFunctionPointer("nvmlErrorString") 679 | fn.restype = c_char_p # otherwise return is an int 680 | ret = fn(result) 681 | return ret 682 | 683 | # Added in 2.285 684 | def nvmlSystemGetNVMLVersion(): 685 | c_version = create_string_buffer(NVML_SYSTEM_NVML_VERSION_BUFFER_SIZE) 686 | fn = _nvmlGetFunctionPointer("nvmlSystemGetNVMLVersion") 687 | ret = fn(c_version, c_uint(NVML_SYSTEM_NVML_VERSION_BUFFER_SIZE)) 688 | _nvmlCheckReturn(ret) 689 | return c_version.value 690 | 691 | # Added in 2.285 692 | def nvmlSystemGetProcessName(pid): 693 | c_name = create_string_buffer(1024) 694 | fn = _nvmlGetFunctionPointer("nvmlSystemGetProcessName") 695 | ret = fn(c_uint(pid), c_name, c_uint(1024)) 696 | _nvmlCheckReturn(ret) 697 | return c_name.value 698 | 699 | def nvmlSystemGetDriverVersion(): 700 | c_version = create_string_buffer(NVML_SYSTEM_DRIVER_VERSION_BUFFER_SIZE) 701 | fn = _nvmlGetFunctionPointer("nvmlSystemGetDriverVersion") 702 | ret = fn(c_version, c_uint(NVML_SYSTEM_DRIVER_VERSION_BUFFER_SIZE)) 703 | _nvmlCheckReturn(ret) 704 | return c_version.value 705 | 706 | # Added in 2.285 707 | def nvmlSystemGetHicVersion(): 708 | c_count = c_uint(0) 709 | hics = None 710 | fn = _nvmlGetFunctionPointer("nvmlSystemGetHicVersion") 711 | 712 | # get the count 713 | ret = fn(byref(c_count), None) 714 | 715 | # this should only fail with insufficient size 716 | if ((ret != NVML_SUCCESS) and 717 | (ret != NVML_ERROR_INSUFFICIENT_SIZE)): 718 | raise NVMLError(ret) 719 | 720 | # if there are no hics 721 | if (c_count.value == 0): 722 | return [] 723 | 724 | hic_array = c_nvmlHwbcEntry_t * c_count.value 725 | hics = hic_array() 726 | ret = fn(byref(c_count), hics) 727 | _nvmlCheckReturn(ret) 728 | return hics 729 | 730 | ## Unit get functions 731 | def nvmlUnitGetCount(): 732 | c_count = c_uint() 733 | fn = _nvmlGetFunctionPointer("nvmlUnitGetCount") 734 | ret = fn(byref(c_count)) 735 | _nvmlCheckReturn(ret) 736 | return c_count.value 737 | 738 | def nvmlUnitGetHandleByIndex(index): 739 | c_index = c_uint(index) 740 | unit = c_nvmlUnit_t() 741 | fn = _nvmlGetFunctionPointer("nvmlUnitGetHandleByIndex") 742 | ret = fn(c_index, byref(unit)) 743 | _nvmlCheckReturn(ret) 744 | return unit 745 | 746 | def nvmlUnitGetUnitInfo(unit): 747 | c_info = c_nvmlUnitInfo_t() 748 | fn = _nvmlGetFunctionPointer("nvmlUnitGetUnitInfo") 749 | ret = fn(unit, byref(c_info)) 750 | _nvmlCheckReturn(ret) 751 | return c_info 752 | 753 | def nvmlUnitGetLedState(unit): 754 | c_state = c_nvmlLedState_t() 755 | fn = _nvmlGetFunctionPointer("nvmlUnitGetLedState") 756 | ret = fn(unit, byref(c_state)) 757 | _nvmlCheckReturn(ret) 758 | return c_state 759 | 760 | def nvmlUnitGetPsuInfo(unit): 761 | c_info = c_nvmlPSUInfo_t() 762 | fn = _nvmlGetFunctionPointer("nvmlUnitGetPsuInfo") 763 | ret = fn(unit, byref(c_info)) 764 | _nvmlCheckReturn(ret) 765 | return c_info 766 | 767 | def nvmlUnitGetTemperature(unit, type): 768 | c_temp = c_uint() 769 | fn = _nvmlGetFunctionPointer("nvmlUnitGetTemperature") 770 | ret = fn(unit, c_uint(type), byref(c_temp)) 771 | _nvmlCheckReturn(ret) 772 | return c_temp.value 773 | 774 | def nvmlUnitGetFanSpeedInfo(unit): 775 | c_speeds = c_nvmlUnitFanSpeeds_t() 776 | fn = _nvmlGetFunctionPointer("nvmlUnitGetFanSpeedInfo") 777 | ret = fn(unit, byref(c_speeds)) 778 | _nvmlCheckReturn(ret) 779 | return c_speeds 780 | 781 | # added to API 782 | def nvmlUnitGetDeviceCount(unit): 783 | c_count = c_uint(0) 784 | # query the unit to determine device count 785 | fn = _nvmlGetFunctionPointer("nvmlUnitGetDevices") 786 | ret = fn(unit, byref(c_count), None) 787 | if (ret == NVML_ERROR_INSUFFICIENT_SIZE): 788 | ret = NVML_SUCCESS 789 | _nvmlCheckReturn(ret) 790 | return c_count.value 791 | 792 | def nvmlUnitGetDevices(unit): 793 | c_count = c_uint(nvmlUnitGetDeviceCount(unit)) 794 | device_array = c_nvmlDevice_t * c_count.value 795 | c_devices = device_array() 796 | fn = _nvmlGetFunctionPointer("nvmlUnitGetDevices") 797 | ret = fn(unit, byref(c_count), c_devices) 798 | _nvmlCheckReturn(ret) 799 | return c_devices 800 | 801 | ## Device get functions 802 | def nvmlDeviceGetCount(): 803 | c_count = c_uint() 804 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetCount_v2") 805 | ret = fn(byref(c_count)) 806 | _nvmlCheckReturn(ret) 807 | return c_count.value 808 | 809 | def nvmlDeviceGetHandleByIndex(index): 810 | c_index = c_uint(index) 811 | device = c_nvmlDevice_t() 812 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetHandleByIndex_v2") 813 | ret = fn(c_index, byref(device)) 814 | _nvmlCheckReturn(ret) 815 | return device 816 | 817 | def nvmlDeviceGetHandleBySerial(serial): 818 | c_serial = c_char_p(serial) 819 | device = c_nvmlDevice_t() 820 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetHandleBySerial") 821 | ret = fn(c_serial, byref(device)) 822 | _nvmlCheckReturn(ret) 823 | return device 824 | 825 | def nvmlDeviceGetHandleByUUID(uuid): 826 | c_uuid = c_char_p(uuid) 827 | device = c_nvmlDevice_t() 828 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetHandleByUUID") 829 | ret = fn(c_uuid, byref(device)) 830 | _nvmlCheckReturn(ret) 831 | return device 832 | 833 | def nvmlDeviceGetHandleByPciBusId(pciBusId): 834 | c_busId = c_char_p(pciBusId) 835 | device = c_nvmlDevice_t() 836 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetHandleByPciBusId_v2") 837 | ret = fn(c_busId, byref(device)) 838 | _nvmlCheckReturn(ret) 839 | return device 840 | 841 | def nvmlDeviceGetName(handle): 842 | c_name = create_string_buffer(NVML_DEVICE_NAME_BUFFER_SIZE) 843 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetName") 844 | ret = fn(handle, c_name, c_uint(NVML_DEVICE_NAME_BUFFER_SIZE)) 845 | _nvmlCheckReturn(ret) 846 | return c_name.value 847 | 848 | def nvmlDeviceGetBoardId(handle): 849 | c_id = c_uint(); 850 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetBoardId") 851 | ret = fn(handle, byref(c_id)) 852 | _nvmlCheckReturn(ret) 853 | return c_id.value 854 | 855 | def nvmlDeviceGetMultiGpuBoard(handle): 856 | c_multiGpu = c_uint(); 857 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetMultiGpuBoard") 858 | ret = fn(handle, byref(c_multiGpu)) 859 | _nvmlCheckReturn(ret) 860 | return c_multiGpu.value 861 | 862 | def nvmlDeviceGetBrand(handle): 863 | c_type = _nvmlBrandType_t() 864 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetBrand") 865 | ret = fn(handle, byref(c_type)) 866 | _nvmlCheckReturn(ret) 867 | return c_type.value 868 | 869 | def nvmlDeviceGetSerial(handle): 870 | c_serial = create_string_buffer(NVML_DEVICE_SERIAL_BUFFER_SIZE) 871 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetSerial") 872 | ret = fn(handle, c_serial, c_uint(NVML_DEVICE_SERIAL_BUFFER_SIZE)) 873 | _nvmlCheckReturn(ret) 874 | return c_serial.value 875 | 876 | def nvmlDeviceGetCpuAffinity(handle, cpuSetSize): 877 | affinity_array = c_ulonglong * cpuSetSize 878 | c_affinity = affinity_array() 879 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetCpuAffinity") 880 | ret = fn(handle, cpuSetSize, byref(c_affinity)) 881 | _nvmlCheckReturn(ret) 882 | return c_affinity 883 | 884 | def nvmlDeviceSetCpuAffinity(handle): 885 | fn = _nvmlGetFunctionPointer("nvmlDeviceSetCpuAffinity") 886 | ret = fn(handle) 887 | _nvmlCheckReturn(ret) 888 | return None 889 | 890 | def nvmlDeviceClearCpuAffinity(handle): 891 | fn = _nvmlGetFunctionPointer("nvmlDeviceClearCpuAffinity") 892 | ret = fn(handle) 893 | _nvmlCheckReturn(ret) 894 | return None 895 | 896 | def nvmlDeviceGetMinorNumber(handle): 897 | c_minor_number = c_uint() 898 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetMinorNumber") 899 | ret = fn(handle, byref(c_minor_number)) 900 | _nvmlCheckReturn(ret) 901 | return c_minor_number.value 902 | 903 | def nvmlDeviceGetUUID(handle): 904 | c_uuid = create_string_buffer(NVML_DEVICE_UUID_BUFFER_SIZE) 905 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetUUID") 906 | ret = fn(handle, c_uuid, c_uint(NVML_DEVICE_UUID_BUFFER_SIZE)) 907 | _nvmlCheckReturn(ret) 908 | return c_uuid.value 909 | 910 | def nvmlDeviceGetInforomVersion(handle, infoRomObject): 911 | c_version = create_string_buffer(NVML_DEVICE_INFOROM_VERSION_BUFFER_SIZE) 912 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetInforomVersion") 913 | ret = fn(handle, _nvmlInforomObject_t(infoRomObject), 914 | c_version, c_uint(NVML_DEVICE_INFOROM_VERSION_BUFFER_SIZE)) 915 | _nvmlCheckReturn(ret) 916 | return c_version.value 917 | 918 | # Added in 4.304 919 | def nvmlDeviceGetInforomImageVersion(handle): 920 | c_version = create_string_buffer(NVML_DEVICE_INFOROM_VERSION_BUFFER_SIZE) 921 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetInforomImageVersion") 922 | ret = fn(handle, c_version, c_uint(NVML_DEVICE_INFOROM_VERSION_BUFFER_SIZE)) 923 | _nvmlCheckReturn(ret) 924 | return c_version.value 925 | 926 | # Added in 4.304 927 | def nvmlDeviceGetInforomConfigurationChecksum(handle): 928 | c_checksum = c_uint() 929 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetInforomConfigurationChecksum") 930 | ret = fn(handle, byref(c_checksum)) 931 | _nvmlCheckReturn(ret) 932 | return c_checksum.value 933 | 934 | # Added in 4.304 935 | def nvmlDeviceValidateInforom(handle): 936 | fn = _nvmlGetFunctionPointer("nvmlDeviceValidateInforom") 937 | ret = fn(handle) 938 | _nvmlCheckReturn(ret) 939 | return None 940 | 941 | def nvmlDeviceGetDisplayMode(handle): 942 | c_mode = _nvmlEnableState_t() 943 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetDisplayMode") 944 | ret = fn(handle, byref(c_mode)) 945 | _nvmlCheckReturn(ret) 946 | return c_mode.value 947 | 948 | def nvmlDeviceGetDisplayActive(handle): 949 | c_mode = _nvmlEnableState_t() 950 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetDisplayActive") 951 | ret = fn(handle, byref(c_mode)) 952 | _nvmlCheckReturn(ret) 953 | return c_mode.value 954 | 955 | 956 | def nvmlDeviceGetPersistenceMode(handle): 957 | c_state = _nvmlEnableState_t() 958 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetPersistenceMode") 959 | ret = fn(handle, byref(c_state)) 960 | _nvmlCheckReturn(ret) 961 | return c_state.value 962 | 963 | def nvmlDeviceGetPciInfo(handle): 964 | c_info = nvmlPciInfo_t() 965 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetPciInfo_v2") 966 | ret = fn(handle, byref(c_info)) 967 | _nvmlCheckReturn(ret) 968 | return c_info 969 | 970 | def nvmlDeviceGetClockInfo(handle, type): 971 | c_clock = c_uint() 972 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetClockInfo") 973 | ret = fn(handle, _nvmlClockType_t(type), byref(c_clock)) 974 | _nvmlCheckReturn(ret) 975 | return c_clock.value 976 | 977 | # Added in 2.285 978 | def nvmlDeviceGetMaxClockInfo(handle, type): 979 | c_clock = c_uint() 980 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetMaxClockInfo") 981 | ret = fn(handle, _nvmlClockType_t(type), byref(c_clock)) 982 | _nvmlCheckReturn(ret) 983 | return c_clock.value 984 | 985 | # Added in 4.304 986 | def nvmlDeviceGetApplicationsClock(handle, type): 987 | c_clock = c_uint() 988 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetApplicationsClock") 989 | ret = fn(handle, _nvmlClockType_t(type), byref(c_clock)) 990 | _nvmlCheckReturn(ret) 991 | return c_clock.value 992 | 993 | # Added in 5.319 994 | def nvmlDeviceGetDefaultApplicationsClock(handle, type): 995 | c_clock = c_uint() 996 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetDefaultApplicationsClock") 997 | ret = fn(handle, _nvmlClockType_t(type), byref(c_clock)) 998 | _nvmlCheckReturn(ret) 999 | return c_clock.value 1000 | 1001 | # Added in 4.304 1002 | def nvmlDeviceGetSupportedMemoryClocks(handle): 1003 | # first call to get the size 1004 | c_count = c_uint(0) 1005 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetSupportedMemoryClocks") 1006 | ret = fn(handle, byref(c_count), None) 1007 | 1008 | if (ret == NVML_SUCCESS): 1009 | # special case, no clocks 1010 | return [] 1011 | elif (ret == NVML_ERROR_INSUFFICIENT_SIZE): 1012 | # typical case 1013 | clocks_array = c_uint * c_count.value 1014 | c_clocks = clocks_array() 1015 | 1016 | # make the call again 1017 | ret = fn(handle, byref(c_count), c_clocks) 1018 | _nvmlCheckReturn(ret) 1019 | 1020 | procs = [] 1021 | for i in range(c_count.value): 1022 | procs.append(c_clocks[i]) 1023 | 1024 | return procs 1025 | else: 1026 | # error case 1027 | raise NVMLError(ret) 1028 | 1029 | # Added in 4.304 1030 | def nvmlDeviceGetSupportedGraphicsClocks(handle, memoryClockMHz): 1031 | # first call to get the size 1032 | c_count = c_uint(0) 1033 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetSupportedGraphicsClocks") 1034 | ret = fn(handle, c_uint(memoryClockMHz), byref(c_count), None) 1035 | 1036 | if (ret == NVML_SUCCESS): 1037 | # special case, no clocks 1038 | return [] 1039 | elif (ret == NVML_ERROR_INSUFFICIENT_SIZE): 1040 | # typical case 1041 | clocks_array = c_uint * c_count.value 1042 | c_clocks = clocks_array() 1043 | 1044 | # make the call again 1045 | ret = fn(handle, c_uint(memoryClockMHz), byref(c_count), c_clocks) 1046 | _nvmlCheckReturn(ret) 1047 | 1048 | procs = [] 1049 | for i in range(c_count.value): 1050 | procs.append(c_clocks[i]) 1051 | 1052 | return procs 1053 | else: 1054 | # error case 1055 | raise NVMLError(ret) 1056 | 1057 | def nvmlDeviceGetFanSpeed(handle): 1058 | c_speed = c_uint() 1059 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetFanSpeed") 1060 | ret = fn(handle, byref(c_speed)) 1061 | _nvmlCheckReturn(ret) 1062 | return c_speed.value 1063 | 1064 | def nvmlDeviceGetTemperature(handle, sensor): 1065 | c_temp = c_uint() 1066 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetTemperature") 1067 | ret = fn(handle, _nvmlTemperatureSensors_t(sensor), byref(c_temp)) 1068 | _nvmlCheckReturn(ret) 1069 | return c_temp.value 1070 | 1071 | def nvmlDeviceGetTemperatureThreshold(handle, threshold): 1072 | c_temp = c_uint() 1073 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetTemperatureThreshold") 1074 | ret = fn(handle, _nvmlTemperatureThresholds_t(threshold), byref(c_temp)) 1075 | _nvmlCheckReturn(ret) 1076 | return c_temp.value 1077 | 1078 | # DEPRECATED use nvmlDeviceGetPerformanceState 1079 | def nvmlDeviceGetPowerState(handle): 1080 | c_pstate = _nvmlPstates_t() 1081 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetPowerState") 1082 | ret = fn(handle, byref(c_pstate)) 1083 | _nvmlCheckReturn(ret) 1084 | return c_pstate.value 1085 | 1086 | def nvmlDeviceGetPerformanceState(handle): 1087 | c_pstate = _nvmlPstates_t() 1088 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetPerformanceState") 1089 | ret = fn(handle, byref(c_pstate)) 1090 | _nvmlCheckReturn(ret) 1091 | return c_pstate.value 1092 | 1093 | def nvmlDeviceGetPowerManagementMode(handle): 1094 | c_pcapMode = _nvmlEnableState_t() 1095 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetPowerManagementMode") 1096 | ret = fn(handle, byref(c_pcapMode)) 1097 | _nvmlCheckReturn(ret) 1098 | return c_pcapMode.value 1099 | 1100 | def nvmlDeviceGetPowerManagementLimit(handle): 1101 | c_limit = c_uint() 1102 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetPowerManagementLimit") 1103 | ret = fn(handle, byref(c_limit)) 1104 | _nvmlCheckReturn(ret) 1105 | return c_limit.value 1106 | 1107 | # Added in 4.304 1108 | def nvmlDeviceGetPowerManagementLimitConstraints(handle): 1109 | c_minLimit = c_uint() 1110 | c_maxLimit = c_uint() 1111 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetPowerManagementLimitConstraints") 1112 | ret = fn(handle, byref(c_minLimit), byref(c_maxLimit)) 1113 | _nvmlCheckReturn(ret) 1114 | return [c_minLimit.value, c_maxLimit.value] 1115 | 1116 | # Added in 4.304 1117 | def nvmlDeviceGetPowerManagementDefaultLimit(handle): 1118 | c_limit = c_uint() 1119 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetPowerManagementDefaultLimit") 1120 | ret = fn(handle, byref(c_limit)) 1121 | _nvmlCheckReturn(ret) 1122 | return c_limit.value 1123 | 1124 | 1125 | # Added in 331 1126 | def nvmlDeviceGetEnforcedPowerLimit(handle): 1127 | c_limit = c_uint() 1128 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetEnforcedPowerLimit") 1129 | ret = fn(handle, byref(c_limit)) 1130 | _nvmlCheckReturn(ret) 1131 | return c_limit.value 1132 | 1133 | def nvmlDeviceGetPowerUsage(handle): 1134 | c_watts = c_uint() 1135 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetPowerUsage") 1136 | ret = fn(handle, byref(c_watts)) 1137 | _nvmlCheckReturn(ret) 1138 | return c_watts.value 1139 | 1140 | # Added in 4.304 1141 | def nvmlDeviceGetGpuOperationMode(handle): 1142 | c_currState = _nvmlGpuOperationMode_t() 1143 | c_pendingState = _nvmlGpuOperationMode_t() 1144 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetGpuOperationMode") 1145 | ret = fn(handle, byref(c_currState), byref(c_pendingState)) 1146 | _nvmlCheckReturn(ret) 1147 | return [c_currState.value, c_pendingState.value] 1148 | 1149 | # Added in 4.304 1150 | def nvmlDeviceGetCurrentGpuOperationMode(handle): 1151 | return nvmlDeviceGetGpuOperationMode(handle)[0] 1152 | 1153 | # Added in 4.304 1154 | def nvmlDeviceGetPendingGpuOperationMode(handle): 1155 | return nvmlDeviceGetGpuOperationMode(handle)[1] 1156 | 1157 | def nvmlDeviceGetMemoryInfo(handle): 1158 | c_memory = c_nvmlMemory_t() 1159 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetMemoryInfo") 1160 | ret = fn(handle, byref(c_memory)) 1161 | _nvmlCheckReturn(ret) 1162 | return c_memory 1163 | 1164 | def nvmlDeviceGetBAR1MemoryInfo(handle): 1165 | c_bar1_memory = c_nvmlBAR1Memory_t() 1166 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetBAR1MemoryInfo") 1167 | ret = fn(handle, byref(c_bar1_memory)) 1168 | _nvmlCheckReturn(ret) 1169 | return c_bar1_memory 1170 | 1171 | def nvmlDeviceGetComputeMode(handle): 1172 | c_mode = _nvmlComputeMode_t() 1173 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetComputeMode") 1174 | ret = fn(handle, byref(c_mode)) 1175 | _nvmlCheckReturn(ret) 1176 | return c_mode.value 1177 | 1178 | def nvmlDeviceGetEccMode(handle): 1179 | c_currState = _nvmlEnableState_t() 1180 | c_pendingState = _nvmlEnableState_t() 1181 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetEccMode") 1182 | ret = fn(handle, byref(c_currState), byref(c_pendingState)) 1183 | _nvmlCheckReturn(ret) 1184 | return [c_currState.value, c_pendingState.value] 1185 | 1186 | # added to API 1187 | def nvmlDeviceGetCurrentEccMode(handle): 1188 | return nvmlDeviceGetEccMode(handle)[0] 1189 | 1190 | # added to API 1191 | def nvmlDeviceGetPendingEccMode(handle): 1192 | return nvmlDeviceGetEccMode(handle)[1] 1193 | 1194 | def nvmlDeviceGetTotalEccErrors(handle, errorType, counterType): 1195 | c_count = c_ulonglong() 1196 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetTotalEccErrors") 1197 | ret = fn(handle, _nvmlMemoryErrorType_t(errorType), 1198 | _nvmlEccCounterType_t(counterType), byref(c_count)) 1199 | _nvmlCheckReturn(ret) 1200 | return c_count.value 1201 | 1202 | # This is deprecated, instead use nvmlDeviceGetMemoryErrorCounter 1203 | def nvmlDeviceGetDetailedEccErrors(handle, errorType, counterType): 1204 | c_counts = c_nvmlEccErrorCounts_t() 1205 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetDetailedEccErrors") 1206 | ret = fn(handle, _nvmlMemoryErrorType_t(errorType), 1207 | _nvmlEccCounterType_t(counterType), byref(c_counts)) 1208 | _nvmlCheckReturn(ret) 1209 | return c_counts 1210 | 1211 | # Added in 4.304 1212 | def nvmlDeviceGetMemoryErrorCounter(handle, errorType, counterType, locationType): 1213 | c_count = c_ulonglong() 1214 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetMemoryErrorCounter") 1215 | ret = fn(handle, 1216 | _nvmlMemoryErrorType_t(errorType), 1217 | _nvmlEccCounterType_t(counterType), 1218 | _nvmlMemoryLocation_t(locationType), 1219 | byref(c_count)) 1220 | _nvmlCheckReturn(ret) 1221 | return c_count.value 1222 | 1223 | def nvmlDeviceGetUtilizationRates(handle): 1224 | c_util = c_nvmlUtilization_t() 1225 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetUtilizationRates") 1226 | ret = fn(handle, byref(c_util)) 1227 | _nvmlCheckReturn(ret) 1228 | return c_util 1229 | 1230 | def nvmlDeviceGetEncoderUtilization(handle): 1231 | c_util = c_uint() 1232 | c_samplingPeriod = c_uint() 1233 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetEncoderUtilization") 1234 | ret = fn(handle, byref(c_util), byref(c_samplingPeriod)) 1235 | _nvmlCheckReturn(ret) 1236 | return [c_util.value, c_samplingPeriod.value] 1237 | 1238 | def nvmlDeviceGetDecoderUtilization(handle): 1239 | c_util = c_uint() 1240 | c_samplingPeriod = c_uint() 1241 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetDecoderUtilization") 1242 | ret = fn(handle, byref(c_util), byref(c_samplingPeriod)) 1243 | _nvmlCheckReturn(ret) 1244 | return [c_util.value, c_samplingPeriod.value] 1245 | 1246 | def nvmlDeviceGetPcieReplayCounter(handle): 1247 | c_replay = c_uint() 1248 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetPcieReplayCounter") 1249 | ret = fn(handle, byref(c_replay)) 1250 | _nvmlCheckReturn(ret) 1251 | return c_replay.value 1252 | 1253 | def nvmlDeviceGetDriverModel(handle): 1254 | c_currModel = _nvmlDriverModel_t() 1255 | c_pendingModel = _nvmlDriverModel_t() 1256 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetDriverModel") 1257 | ret = fn(handle, byref(c_currModel), byref(c_pendingModel)) 1258 | _nvmlCheckReturn(ret) 1259 | return [c_currModel.value, c_pendingModel.value] 1260 | 1261 | # added to API 1262 | def nvmlDeviceGetCurrentDriverModel(handle): 1263 | return nvmlDeviceGetDriverModel(handle)[0] 1264 | 1265 | # added to API 1266 | def nvmlDeviceGetPendingDriverModel(handle): 1267 | return nvmlDeviceGetDriverModel(handle)[1] 1268 | 1269 | # Added in 2.285 1270 | def nvmlDeviceGetVbiosVersion(handle): 1271 | c_version = create_string_buffer(NVML_DEVICE_VBIOS_VERSION_BUFFER_SIZE) 1272 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetVbiosVersion") 1273 | ret = fn(handle, c_version, c_uint(NVML_DEVICE_VBIOS_VERSION_BUFFER_SIZE)) 1274 | _nvmlCheckReturn(ret) 1275 | return c_version.value 1276 | 1277 | # Added in 2.285 1278 | def nvmlDeviceGetComputeRunningProcesses(handle): 1279 | # first call to get the size 1280 | c_count = c_uint(0) 1281 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetComputeRunningProcesses") 1282 | ret = fn(handle, byref(c_count), None) 1283 | 1284 | if (ret == NVML_SUCCESS): 1285 | # special case, no running processes 1286 | return [] 1287 | elif (ret == NVML_ERROR_INSUFFICIENT_SIZE): 1288 | # typical case 1289 | # oversize the array incase more processes are created 1290 | c_count.value = c_count.value * 2 + 5 1291 | proc_array = c_nvmlProcessInfo_t * c_count.value 1292 | c_procs = proc_array() 1293 | 1294 | # make the call again 1295 | ret = fn(handle, byref(c_count), c_procs) 1296 | _nvmlCheckReturn(ret) 1297 | 1298 | procs = [] 1299 | for i in range(c_count.value): 1300 | # use an alternative struct for this object 1301 | obj = nvmlStructToFriendlyObject(c_procs[i]) 1302 | if (obj.usedGpuMemory == NVML_VALUE_NOT_AVAILABLE_ulonglong.value): 1303 | # special case for WDDM on Windows, see comment above 1304 | obj.usedGpuMemory = None 1305 | procs.append(obj) 1306 | 1307 | return procs 1308 | else: 1309 | # error case 1310 | raise NVMLError(ret) 1311 | 1312 | def nvmlDeviceGetGraphicsRunningProcesses(handle): 1313 | # first call to get the size 1314 | c_count = c_uint(0) 1315 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetGraphicsRunningProcesses") 1316 | ret = fn(handle, byref(c_count), None) 1317 | 1318 | if (ret == NVML_SUCCESS): 1319 | # special case, no running processes 1320 | return [] 1321 | elif (ret == NVML_ERROR_INSUFFICIENT_SIZE): 1322 | # typical case 1323 | # oversize the array incase more processes are created 1324 | c_count.value = c_count.value * 2 + 5 1325 | proc_array = c_nvmlProcessInfo_t * c_count.value 1326 | c_procs = proc_array() 1327 | 1328 | # make the call again 1329 | ret = fn(handle, byref(c_count), c_procs) 1330 | _nvmlCheckReturn(ret) 1331 | 1332 | procs = [] 1333 | for i in range(c_count.value): 1334 | # use an alternative struct for this object 1335 | obj = nvmlStructToFriendlyObject(c_procs[i]) 1336 | if (obj.usedGpuMemory == NVML_VALUE_NOT_AVAILABLE_ulonglong.value): 1337 | # special case for WDDM on Windows, see comment above 1338 | obj.usedGpuMemory = None 1339 | procs.append(obj) 1340 | 1341 | return procs 1342 | else: 1343 | # error case 1344 | raise NVMLError(ret) 1345 | 1346 | def nvmlDeviceGetAutoBoostedClocksEnabled(handle): 1347 | c_isEnabled = _nvmlEnableState_t() 1348 | c_defaultIsEnabled = _nvmlEnableState_t() 1349 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetAutoBoostedClocksEnabled") 1350 | ret = fn(handle, byref(c_isEnabled), byref(c_defaultIsEnabled)) 1351 | _nvmlCheckReturn(ret) 1352 | return [c_isEnabled.value, c_defaultIsEnabled.value] 1353 | #Throws NVML_ERROR_NOT_SUPPORTED if hardware doesn't support setting auto boosted clocks 1354 | 1355 | ## Set functions 1356 | def nvmlUnitSetLedState(unit, color): 1357 | fn = _nvmlGetFunctionPointer("nvmlUnitSetLedState") 1358 | ret = fn(unit, _nvmlLedColor_t(color)) 1359 | _nvmlCheckReturn(ret) 1360 | return None 1361 | 1362 | def nvmlDeviceSetPersistenceMode(handle, mode): 1363 | fn = _nvmlGetFunctionPointer("nvmlDeviceSetPersistenceMode") 1364 | ret = fn(handle, _nvmlEnableState_t(mode)) 1365 | _nvmlCheckReturn(ret) 1366 | return None 1367 | 1368 | def nvmlDeviceSetComputeMode(handle, mode): 1369 | fn = _nvmlGetFunctionPointer("nvmlDeviceSetComputeMode") 1370 | ret = fn(handle, _nvmlComputeMode_t(mode)) 1371 | _nvmlCheckReturn(ret) 1372 | return None 1373 | 1374 | def nvmlDeviceSetEccMode(handle, mode): 1375 | fn = _nvmlGetFunctionPointer("nvmlDeviceSetEccMode") 1376 | ret = fn(handle, _nvmlEnableState_t(mode)) 1377 | _nvmlCheckReturn(ret) 1378 | return None 1379 | 1380 | def nvmlDeviceClearEccErrorCounts(handle, counterType): 1381 | fn = _nvmlGetFunctionPointer("nvmlDeviceClearEccErrorCounts") 1382 | ret = fn(handle, _nvmlEccCounterType_t(counterType)) 1383 | _nvmlCheckReturn(ret) 1384 | return None 1385 | 1386 | def nvmlDeviceSetDriverModel(handle, model): 1387 | fn = _nvmlGetFunctionPointer("nvmlDeviceSetDriverModel") 1388 | ret = fn(handle, _nvmlDriverModel_t(model)) 1389 | _nvmlCheckReturn(ret) 1390 | return None 1391 | 1392 | def nvmlDeviceSetAutoBoostedClocksEnabled(handle, enabled): 1393 | fn = _nvmlGetFunctionPointer("nvmlDeviceSetAutoBoostedClocksEnabled") 1394 | ret = fn(handle, _nvmlEnableState_t(enabled)) 1395 | _nvmlCheckReturn(ret) 1396 | return None 1397 | #Throws NVML_ERROR_NOT_SUPPORTED if hardware doesn't support setting auto boosted clocks 1398 | 1399 | def nvmlDeviceSetDefaultAutoBoostedClocksEnabled(handle, enabled, flags): 1400 | fn = _nvmlGetFunctionPointer("nvmlDeviceSetDefaultAutoBoostedClocksEnabled") 1401 | ret = fn(handle, _nvmlEnableState_t(enabled), c_uint(flags)) 1402 | _nvmlCheckReturn(ret) 1403 | return None 1404 | #Throws NVML_ERROR_NOT_SUPPORTED if hardware doesn't support setting auto boosted clocks 1405 | 1406 | # Added in 4.304 1407 | def nvmlDeviceSetApplicationsClocks(handle, maxMemClockMHz, maxGraphicsClockMHz): 1408 | fn = _nvmlGetFunctionPointer("nvmlDeviceSetApplicationsClocks") 1409 | ret = fn(handle, c_uint(maxMemClockMHz), c_uint(maxGraphicsClockMHz)) 1410 | _nvmlCheckReturn(ret) 1411 | return None 1412 | 1413 | # Added in 4.304 1414 | def nvmlDeviceResetApplicationsClocks(handle): 1415 | fn = _nvmlGetFunctionPointer("nvmlDeviceResetApplicationsClocks") 1416 | ret = fn(handle) 1417 | _nvmlCheckReturn(ret) 1418 | return None 1419 | 1420 | # Added in 4.304 1421 | def nvmlDeviceSetPowerManagementLimit(handle, limit): 1422 | fn = _nvmlGetFunctionPointer("nvmlDeviceSetPowerManagementLimit") 1423 | ret = fn(handle, c_uint(limit)) 1424 | _nvmlCheckReturn(ret) 1425 | return None 1426 | 1427 | # Added in 4.304 1428 | def nvmlDeviceSetGpuOperationMode(handle, mode): 1429 | fn = _nvmlGetFunctionPointer("nvmlDeviceSetGpuOperationMode") 1430 | ret = fn(handle, _nvmlGpuOperationMode_t(mode)) 1431 | _nvmlCheckReturn(ret) 1432 | return None 1433 | 1434 | # Added in 2.285 1435 | def nvmlEventSetCreate(): 1436 | fn = _nvmlGetFunctionPointer("nvmlEventSetCreate") 1437 | eventSet = c_nvmlEventSet_t() 1438 | ret = fn(byref(eventSet)) 1439 | _nvmlCheckReturn(ret) 1440 | return eventSet 1441 | 1442 | # Added in 2.285 1443 | def nvmlDeviceRegisterEvents(handle, eventTypes, eventSet): 1444 | fn = _nvmlGetFunctionPointer("nvmlDeviceRegisterEvents") 1445 | ret = fn(handle, c_ulonglong(eventTypes), eventSet) 1446 | _nvmlCheckReturn(ret) 1447 | return None 1448 | 1449 | # Added in 2.285 1450 | def nvmlDeviceGetSupportedEventTypes(handle): 1451 | c_eventTypes = c_ulonglong() 1452 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetSupportedEventTypes") 1453 | ret = fn(handle, byref(c_eventTypes)) 1454 | _nvmlCheckReturn(ret) 1455 | return c_eventTypes.value 1456 | 1457 | # Added in 2.285 1458 | # raises NVML_ERROR_TIMEOUT exception on timeout 1459 | def nvmlEventSetWait(eventSet, timeoutms): 1460 | fn = _nvmlGetFunctionPointer("nvmlEventSetWait") 1461 | data = c_nvmlEventData_t() 1462 | ret = fn(eventSet, byref(data), c_uint(timeoutms)) 1463 | _nvmlCheckReturn(ret) 1464 | return data 1465 | 1466 | # Added in 2.285 1467 | def nvmlEventSetFree(eventSet): 1468 | fn = _nvmlGetFunctionPointer("nvmlEventSetFree") 1469 | ret = fn(eventSet) 1470 | _nvmlCheckReturn(ret) 1471 | return None 1472 | 1473 | # Added in 3.295 1474 | def nvmlDeviceOnSameBoard(handle1, handle2): 1475 | fn = _nvmlGetFunctionPointer("nvmlDeviceOnSameBoard") 1476 | onSameBoard = c_int() 1477 | ret = fn(handle1, handle2, byref(onSameBoard)) 1478 | _nvmlCheckReturn(ret) 1479 | return (onSameBoard.value != 0) 1480 | 1481 | # Added in 3.295 1482 | def nvmlDeviceGetCurrPcieLinkGeneration(handle): 1483 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetCurrPcieLinkGeneration") 1484 | gen = c_uint() 1485 | ret = fn(handle, byref(gen)) 1486 | _nvmlCheckReturn(ret) 1487 | return gen.value 1488 | 1489 | # Added in 3.295 1490 | def nvmlDeviceGetMaxPcieLinkGeneration(handle): 1491 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetMaxPcieLinkGeneration") 1492 | gen = c_uint() 1493 | ret = fn(handle, byref(gen)) 1494 | _nvmlCheckReturn(ret) 1495 | return gen.value 1496 | 1497 | # Added in 3.295 1498 | def nvmlDeviceGetCurrPcieLinkWidth(handle): 1499 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetCurrPcieLinkWidth") 1500 | width = c_uint() 1501 | ret = fn(handle, byref(width)) 1502 | _nvmlCheckReturn(ret) 1503 | return width.value 1504 | 1505 | # Added in 3.295 1506 | def nvmlDeviceGetMaxPcieLinkWidth(handle): 1507 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetMaxPcieLinkWidth") 1508 | width = c_uint() 1509 | ret = fn(handle, byref(width)) 1510 | _nvmlCheckReturn(ret) 1511 | return width.value 1512 | 1513 | # Added in 4.304 1514 | def nvmlDeviceGetSupportedClocksThrottleReasons(handle): 1515 | c_reasons= c_ulonglong() 1516 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetSupportedClocksThrottleReasons") 1517 | ret = fn(handle, byref(c_reasons)) 1518 | _nvmlCheckReturn(ret) 1519 | return c_reasons.value 1520 | 1521 | # Added in 4.304 1522 | def nvmlDeviceGetCurrentClocksThrottleReasons(handle): 1523 | c_reasons= c_ulonglong() 1524 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetCurrentClocksThrottleReasons") 1525 | ret = fn(handle, byref(c_reasons)) 1526 | _nvmlCheckReturn(ret) 1527 | return c_reasons.value 1528 | 1529 | # Added in 5.319 1530 | def nvmlDeviceGetIndex(handle): 1531 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetIndex") 1532 | c_index = c_uint() 1533 | ret = fn(handle, byref(c_index)) 1534 | _nvmlCheckReturn(ret) 1535 | return c_index.value 1536 | 1537 | # Added in 5.319 1538 | def nvmlDeviceGetAccountingMode(handle): 1539 | c_mode = _nvmlEnableState_t() 1540 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetAccountingMode") 1541 | ret = fn(handle, byref(c_mode)) 1542 | _nvmlCheckReturn(ret) 1543 | return c_mode.value 1544 | 1545 | def nvmlDeviceSetAccountingMode(handle, mode): 1546 | fn = _nvmlGetFunctionPointer("nvmlDeviceSetAccountingMode") 1547 | ret = fn(handle, _nvmlEnableState_t(mode)) 1548 | _nvmlCheckReturn(ret) 1549 | return None 1550 | 1551 | def nvmlDeviceClearAccountingPids(handle): 1552 | fn = _nvmlGetFunctionPointer("nvmlDeviceClearAccountingPids") 1553 | ret = fn(handle) 1554 | _nvmlCheckReturn(ret) 1555 | return None 1556 | 1557 | def nvmlDeviceGetAccountingStats(handle, pid): 1558 | stats = c_nvmlAccountingStats_t() 1559 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetAccountingStats") 1560 | ret = fn(handle, c_uint(pid), byref(stats)) 1561 | _nvmlCheckReturn(ret) 1562 | if (stats.maxMemoryUsage == NVML_VALUE_NOT_AVAILABLE_ulonglong.value): 1563 | # special case for WDDM on Windows, see comment above 1564 | stats.maxMemoryUsage = None 1565 | return stats 1566 | 1567 | def nvmlDeviceGetAccountingPids(handle): 1568 | count = c_uint(nvmlDeviceGetAccountingBufferSize(handle)) 1569 | pids = (c_uint * count.value)() 1570 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetAccountingPids") 1571 | ret = fn(handle, byref(count), pids) 1572 | _nvmlCheckReturn(ret) 1573 | return map(int, pids[0:count.value]) 1574 | 1575 | def nvmlDeviceGetAccountingBufferSize(handle): 1576 | bufferSize = c_uint() 1577 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetAccountingBufferSize") 1578 | ret = fn(handle, byref(bufferSize)) 1579 | _nvmlCheckReturn(ret) 1580 | return int(bufferSize.value) 1581 | 1582 | def nvmlDeviceGetRetiredPages(device, sourceFilter): 1583 | c_source = _nvmlPageRetirementCause_t(sourceFilter) 1584 | c_count = c_uint(0) 1585 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetRetiredPages") 1586 | 1587 | # First call will get the size 1588 | ret = fn(device, c_source, byref(c_count), None) 1589 | 1590 | # this should only fail with insufficient size 1591 | if ((ret != NVML_SUCCESS) and 1592 | (ret != NVML_ERROR_INSUFFICIENT_SIZE)): 1593 | raise NVMLError(ret) 1594 | 1595 | # call again with a buffer 1596 | # oversize the array for the rare cases where additional pages 1597 | # are retired between NVML calls 1598 | c_count.value = c_count.value * 2 + 5 1599 | page_array = c_ulonglong * c_count.value 1600 | c_pages = page_array() 1601 | ret = fn(device, c_source, byref(c_count), c_pages) 1602 | _nvmlCheckReturn(ret) 1603 | return map(int, c_pages[0:c_count.value]) 1604 | 1605 | def nvmlDeviceGetRetiredPagesPendingStatus(device): 1606 | c_pending = _nvmlEnableState_t() 1607 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetRetiredPagesPendingStatus") 1608 | ret = fn(device, byref(c_pending)) 1609 | _nvmlCheckReturn(ret) 1610 | return int(c_pending.value) 1611 | 1612 | def nvmlDeviceGetAPIRestriction(device, apiType): 1613 | c_permission = _nvmlEnableState_t() 1614 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetAPIRestriction") 1615 | ret = fn(device, _nvmlRestrictedAPI_t(apiType), byref(c_permission)) 1616 | _nvmlCheckReturn(ret) 1617 | return int(c_permission.value) 1618 | 1619 | def nvmlDeviceSetAPIRestriction(handle, apiType, isRestricted): 1620 | fn = _nvmlGetFunctionPointer("nvmlDeviceSetAPIRestriction") 1621 | ret = fn(handle, _nvmlRestrictedAPI_t(apiType), _nvmlEnableState_t(isRestricted)) 1622 | _nvmlCheckReturn(ret) 1623 | return None 1624 | 1625 | def nvmlDeviceGetBridgeChipInfo(handle): 1626 | bridgeHierarchy = c_nvmlBridgeChipHierarchy_t() 1627 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetBridgeChipInfo") 1628 | ret = fn(handle, byref(bridgeHierarchy)) 1629 | _nvmlCheckReturn(ret) 1630 | return bridgeHierarchy 1631 | 1632 | def nvmlDeviceGetSamples(device, sampling_type, timeStamp): 1633 | c_sampling_type = _nvmlSamplingType_t(sampling_type) 1634 | c_time_stamp = c_ulonglong(timeStamp) 1635 | c_sample_count = c_uint(0) 1636 | c_sample_value_type = _nvmlValueType_t() 1637 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetSamples") 1638 | 1639 | ## First Call gets the size 1640 | ret = fn(device, c_sampling_type, c_time_stamp, byref(c_sample_value_type), byref(c_sample_count), None) 1641 | 1642 | # Stop if this fails 1643 | if (ret != NVML_SUCCESS): 1644 | raise NVMLError(ret) 1645 | 1646 | sampleArray = c_sample_count.value * c_nvmlSample_t 1647 | c_samples = sampleArray() 1648 | ret = fn(device, c_sampling_type, c_time_stamp, byref(c_sample_value_type), byref(c_sample_count), c_samples) 1649 | _nvmlCheckReturn(ret) 1650 | return (c_sample_value_type.value, c_samples[0:c_sample_count.value]) 1651 | 1652 | def nvmlDeviceGetViolationStatus(device, perfPolicyType): 1653 | c_perfPolicy_type = _nvmlPerfPolicyType_t(perfPolicyType) 1654 | c_violTime = c_nvmlViolationTime_t() 1655 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetViolationStatus") 1656 | 1657 | ## Invoke the method to get violation time 1658 | ret = fn(device, c_perfPolicy_type, byref(c_violTime)) 1659 | _nvmlCheckReturn(ret) 1660 | return c_violTime 1661 | 1662 | def nvmlDeviceGetPcieThroughput(device, counter): 1663 | c_util = c_uint() 1664 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetPcieThroughput") 1665 | ret = fn(device, _nvmlPcieUtilCounter_t(counter), byref(c_util)) 1666 | _nvmlCheckReturn(ret) 1667 | return c_util.value 1668 | 1669 | def nvmlSystemGetTopologyGpuSet(cpuNumber): 1670 | c_count = c_uint(0) 1671 | fn = _nvmlGetFunctionPointer("nvmlSystemGetTopologyGpuSet") 1672 | 1673 | # First call will get the size 1674 | ret = fn(cpuNumber, byref(c_count), None) 1675 | 1676 | if ret != NVML_SUCCESS: 1677 | raise NVMLError(ret) 1678 | print(c_count.value) 1679 | # call again with a buffer 1680 | device_array = c_nvmlDevice_t * c_count.value 1681 | c_devices = device_array() 1682 | ret = fn(cpuNumber, byref(c_count), c_devices) 1683 | _nvmlCheckReturn(ret) 1684 | return map(None, c_devices[0:c_count.value]) 1685 | 1686 | def nvmlDeviceGetTopologyNearestGpus(device, level): 1687 | c_count = c_uint(0) 1688 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetTopologyNearestGpus") 1689 | 1690 | # First call will get the size 1691 | ret = fn(device, level, byref(c_count), None) 1692 | 1693 | if ret != NVML_SUCCESS: 1694 | raise NVMLError(ret) 1695 | 1696 | # call again with a buffer 1697 | device_array = c_nvmlDevice_t * c_count.value 1698 | c_devices = device_array() 1699 | ret = fn(device, level, byref(c_count), c_devices) 1700 | _nvmlCheckReturn(ret) 1701 | return map(None, c_devices[0:c_count.value]) 1702 | 1703 | def nvmlDeviceGetTopologyCommonAncestor(device1, device2): 1704 | c_level = _nvmlGpuTopologyLevel_t() 1705 | fn = _nvmlGetFunctionPointer("nvmlDeviceGetTopologyCommonAncestor") 1706 | ret = fn(device1, device2, byref(c_level)) 1707 | _nvmlCheckReturn(ret) 1708 | return c_level.value 1709 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup 2 | from sys import version 3 | 4 | # earlier versions don't support all classifiers 5 | if version < '2.2.3': 6 | from distutils.dist import DistributionMetadata 7 | DistributionMetadata.classifiers = None 8 | DistributionMetadata.download_url = None 9 | 10 | _package_name='nvidia-ml-py3' 11 | 12 | setup(name=_package_name, 13 | version='7.352.1', 14 | description='Python Bindings for the NVIDIA Management Library', 15 | py_modules=['pynvml', 'nvidia_smi'], 16 | package_data={_package_name: ['Example.txt']}, 17 | license="BSD", 18 | url="http://www.nvidia.com/", 19 | author="NVIDIA Corporation", 20 | author_email="nvml-bindings@nvidia.com", 21 | classifiers=[ 22 | 'Development Status :: 5 - Production/Stable', 23 | 'Intended Audience :: Developers', 24 | 'Intended Audience :: System Administrators', 25 | 'License :: OSI Approved :: BSD License', 26 | 'Operating System :: Microsoft :: Windows', 27 | 'Operating System :: POSIX :: Linux', 28 | 'Programming Language :: Python', 29 | 'Topic :: Software Development :: Libraries :: Python Modules', 30 | 'Topic :: System :: Hardware', 31 | 'Topic :: System :: Systems Administration', 32 | ], 33 | ) 34 | --------------------------------------------------------------------------------