|
| 1 | +# Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED. |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE |
| 4 | + |
| 5 | +from typing import Tuple |
| 6 | + |
| 7 | +from cuda import cuda, cudart |
| 8 | +from cuda.core.experimental._device import Device |
| 9 | +from cuda.core.experimental._utils import handle_return |
| 10 | + |
| 11 | + |
| 12 | +class System: |
| 13 | + """Provide information about the cuda system. |
| 14 | + This class is a singleton and should not be instantiated directly. |
| 15 | + """ |
| 16 | + |
| 17 | + _instance = None |
| 18 | + |
| 19 | + def __new__(cls): |
| 20 | + if cls._instance is None: |
| 21 | + cls._instance = super().__new__(cls) |
| 22 | + return cls._instance |
| 23 | + |
| 24 | + def __init__(self): |
| 25 | + if hasattr(self, "_initialized") and self._initialized: |
| 26 | + return |
| 27 | + self._initialized = True |
| 28 | + |
| 29 | + @property |
| 30 | + def driver_version(self) -> Tuple[int, int]: |
| 31 | + """ |
| 32 | + Query the CUDA driver version. |
| 33 | +
|
| 34 | + Returns |
| 35 | + ------- |
| 36 | + tuple of int |
| 37 | + A 2-tuple of (major, minor) version numbers. |
| 38 | + """ |
| 39 | + version = handle_return(cuda.cuDriverGetVersion()) |
| 40 | + major = version // 1000 |
| 41 | + minor = (version % 1000) // 10 |
| 42 | + return (major, minor) |
| 43 | + |
| 44 | + @property |
| 45 | + def num_devices(self) -> int: |
| 46 | + """ |
| 47 | + Query the number of available GPUs. |
| 48 | +
|
| 49 | + Returns |
| 50 | + ------- |
| 51 | + int |
| 52 | + The number of available GPU devices. |
| 53 | + """ |
| 54 | + return handle_return(cudart.cudaGetDeviceCount()) |
| 55 | + |
| 56 | + @property |
| 57 | + def devices(self) -> tuple: |
| 58 | + """ |
| 59 | + Query the available device instances. |
| 60 | +
|
| 61 | + Returns |
| 62 | + ------- |
| 63 | + tuple of Device |
| 64 | + A tuple containing instances of available devices. |
| 65 | + """ |
| 66 | + total = self.num_devices |
| 67 | + return tuple(Device(device_id) for device_id in range(total)) |
0 commit comments