|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +import os |
| 6 | +import threading |
| 7 | + |
| 8 | +from cuda.bindings import _nvml as nvml |
| 9 | + |
| 10 | + |
| 11 | +ctypedef enum _NVMLState: |
| 12 | + UNINITIALIZED = 0 |
| 13 | + INITIALIZED = 1 |
| 14 | + DISABLED_LIBRARY_NOT_FOUND = 2 |
| 15 | + |
| 16 | + |
| 17 | +# Initialisation must occur per-process, so an initialised state is a |
| 18 | +# (state, pid) pair |
| 19 | +_NVML_STATE = _NVMLState.UNINITIALIZED |
| 20 | +# """Current initialization state""" |
| 21 | + |
| 22 | +_NVML_OWNER_PID = 0 |
| 23 | +# """PID of process that successfully called pynvml.nvmlInit""" |
| 24 | + |
| 25 | + |
| 26 | +_lock = threading.Lock() |
| 27 | + |
| 28 | + |
| 29 | +def initialize() -> None: |
| 30 | + """Idempotent (per-process) initialization of NVUtil's NVML |
| 31 | + |
| 32 | + Notes |
| 33 | + ----- |
| 34 | + |
| 35 | + Modifies global variables _NVML_STATE and _NVML_OWNER_PID""" |
| 36 | + global _NVML_STATE, _NVML_OWNER_PID |
| 37 | + |
| 38 | + with _lock: |
| 39 | + if _NVML_STATE == _NVMLState.DISABLED_LIBRARY_NOT_FOUND or ( |
| 40 | + _NVML_STATE == _NVMLState.INITIALIZED and os.getpid() == _NVML_OWNER_PID |
| 41 | + ): |
| 42 | + return |
| 43 | + elif ( |
| 44 | + _NVML_STATE == _NVMLState.INITIALIZED and os.getpid() != _NVML_OWNER_PID |
| 45 | + ) or _NVML_STATE == _NVMLState.UNINITIALIZED: |
| 46 | + try: |
| 47 | + nvml.init_v2() |
| 48 | + except ( |
| 49 | + nvml.LibraryNotFoundError, |
| 50 | + nvml.DriverNotLoadedError, |
| 51 | + nvml.UnknownError, |
| 52 | + ): |
| 53 | + _NVML_STATE = _NVMLState.DISABLED_LIBRARY_NOT_FOUND |
| 54 | + return |
| 55 | + |
| 56 | + # initialization was successful |
| 57 | + _NVML_STATE = _NVMLState.INITIALIZED |
| 58 | + _NVML_OWNER_PID = os.getpid() |
| 59 | + else: |
| 60 | + raise RuntimeError(f"Unhandled initialisation state ({_NVML_STATE=}, {_NVML_OWNER_PID=})") |
| 61 | + |
| 62 | + |
| 63 | +def is_initialized() -> bool: |
| 64 | + """ |
| 65 | + Check whether the NVML context is initialized on this process. |
| 66 | + |
| 67 | + Returns |
| 68 | + ------- |
| 69 | + result: bool |
| 70 | + Whether the NVML context is initialized on this process. |
| 71 | + """ |
| 72 | + return _NVML_STATE == _NVMLState.INITIALIZED and os.getpid() == _NVML_OWNER_PID |
| 73 | + |
| 74 | + |
| 75 | +def validate() -> None: |
| 76 | + """ |
| 77 | + Validate NVML state. |
| 78 | + |
| 79 | + Validate that NVML is functional and that the system has at least one GPU available. |
| 80 | + |
| 81 | + Raises |
| 82 | + ------ |
| 83 | + nvml.LibraryNotFoundError |
| 84 | + If the NVML library could not be found. |
| 85 | + nvml.GpuNotFoundError |
| 86 | + If no GPUs are available. |
| 87 | + """ |
| 88 | + if _NVML_STATE == _NVMLState.DISABLED_LIBRARY_NOT_FOUND: |
| 89 | + raise nvml.LibraryNotFoundError("The underlying NVML library was not found") |
| 90 | + elif nvml.device_get_count_v2() == 0: |
| 91 | + raise nvml.GpuNotFoundError("No GPUs available") |
0 commit comments