Coverage for cuda / core / system / _system.pyx: 75.51%
49 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-08 01:07 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-08 01:07 +0000
1# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2#
3# SPDX-License-Identifier: Apache-2.0
6# This file needs to either use NVML exclusively, or when `cuda.bindings.nvml`
7# isn't available, fall back to non-NVML-based methods for backward
8# compatibility.
11CUDA_BINDINGS_NVML_IS_COMPATIBLE: bool
13try:
14 from cuda.bindings._version import __version_tuple__ as _BINDINGS_VERSION
15except ImportError:
16 CUDA_BINDINGS_NVML_IS_COMPATIBLE = False
17else:
18 CUDA_BINDINGS_NVML_IS_COMPATIBLE = _BINDINGS_VERSION >= (13, 1, 2) or (_BINDINGS_VERSION[0] == 12 and _BINDINGS_VERSION[1:3] >= (9, 6))
21if CUDA_BINDINGS_NVML_IS_COMPATIBLE:
22 try:
23 from cuda.bindings import nvml
24 except ImportError:
25 CUDA_BINDINGS_NVML_IS_COMPATIBLE = False
26 else:
27 # TODO: We need to be even more specific than version numbers for development.
28 # This can be removed once we have a release including everything we need.
29 for member in ["FieldId", "ClocksEventReasons"]:
30 if not hasattr(nvml, member):
31 CUDA_BINDINGS_NVML_IS_COMPATIBLE = False
32 break
34if CUDA_BINDINGS_NVML_IS_COMPATIBLE:
35 from ._nvml_context import initialize
36else:
37 from cuda.core._utils.cuda_utils import driver, handle_return, runtime
40def get_driver_version(kernel_mode: bool = False) -> tuple[int, int]:
41 """
42 Get the driver version.
44 Parameters
45 ----------
46 kernel_mode: bool
47 When `True`, return the kernel-mode driver version, e.g. 580.65.06.
48 Otherwise, return the user-mode driver version, e.g. 13.0.1.
50 Returns
51 -------
52 version: tuple[int, int]
53 Tuple in the format `(MAJOR, MINOR)`.
54 """
55 return get_driver_version_full(kernel_mode)[:2] 1bc
58def get_driver_version_full(kernel_mode: bool = False) -> tuple[int, int, int]:
59 """
60 Get the full driver version.
62 Parameters
63 ----------
64 kernel_mode: bool
65 When `True`, return the kernel-mode driver version, e.g. 580.65.06.
66 Otherwise, return the user-mode driver version, e.g. 13.0.1.
68 Returns
69 -------
70 version: tuple[int, int, int]
71 Tuple in the format `(MAJOR, MINOR, PATCH)`.
72 """
73 cdef int v
74 if kernel_mode: 1dbc
75 if not CUDA_BINDINGS_NVML_IS_COMPATIBLE: 1c
76 raise ValueError("Kernel-mode driver version requires NVML support")
77 initialize() 1c
78 return tuple(int(v) for v in nvml.system_get_driver_version().split(".")) 1c
79 else:
80 if CUDA_BINDINGS_NVML_IS_COMPATIBLE: 1db
81 initialize() 1db
82 v = nvml.system_get_cuda_driver_version() 1db
83 else:
84 v = handle_return(driver.cuDriverGetVersion())
85 return (v // 1000, (v // 10) % 100, v % 10) 1db
88def get_nvml_version() -> tuple[int, ...]:
89 """
90 The version of the NVML library.
91 """
92 if not CUDA_BINDINGS_NVML_IS_COMPATIBLE: 1V
93 raise RuntimeError("NVML library is not available")
94 return tuple(int(v) for v in nvml.system_get_nvml_version().split(".")) 1V
97def get_driver_branch() -> str:
98 """
99 Retrieves the driver branch of the NVIDIA driver installed on the system.
100 """
101 if not CUDA_BINDINGS_NVML_IS_COMPATIBLE: 1e
102 raise RuntimeError("NVML library is not available")
103 initialize() 1e
104 return nvml.system_get_driver_branch() 1e
107def get_num_devices() -> int:
108 """
109 Return the number of devices in the system.
110 """
111 if CUDA_BINDINGS_NVML_IS_COMPATIBLE: 1fghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTU
112 initialize() 1fghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTU
113 return nvml.device_get_count_v2() 1fghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTU
114 else:
115 return handle_return(runtime.cudaGetDeviceCount())
118def get_process_name(pid: int) -> str:
119 """
120 The name of process with given PID.
122 Parameters
123 ----------
124 pid: int
125 The PID of the process for which to get the name.
127 Returns
128 -------
129 name: str
130 The process name.
131 """
132 initialize() 1W
133 return nvml.system_get_process_name(pid) 1W
136__all__ = [
137 "get_driver_branch",
138 "get_driver_version",
139 "get_driver_version_full",
140 "get_nvml_version",
141 "get_num_devices",
142 "get_process_name",
143 "CUDA_BINDINGS_NVML_IS_COMPATIBLE",
144]