Coverage for cuda / core / experimental / _system.py: 96%

25 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-10 01:19 +0000

1# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. 

2# 

3# SPDX-License-Identifier: Apache-2.0 

4 

5from cuda.core.experimental._device import Device 

6from cuda.core.experimental._utils.cuda_utils import driver, handle_return, runtime 

7 

8 

9class System: 

10 """Provide information about the cuda system. 

11 This class is a singleton and should not be instantiated directly. 

12 """ 

13 

14 _instance = None 

15 

16 def __new__(cls): 

17 if cls._instance is None: 

18 cls._instance = super().__new__(cls) 

19 return cls._instance 

20 

21 def __init__(self): 

22 if hasattr(self, "_initialized") and self._initialized: 

23 return 

24 self._initialized = True 

25 

26 @property 

27 def driver_version(self) -> tuple[int, int]: 

28 """ 

29 Query the CUDA driver version. 

30 

31 Returns 

32 ------- 

33 tuple of int 

34 A 2-tuple of (major, minor) version numbers. 

35 """ 

36 version = handle_return(driver.cuDriverGetVersion()) 

37 major = version // 1000 

38 minor = (version % 1000) // 10 

39 return (major, minor) 

40 

41 @property 

42 def num_devices(self) -> int: 

43 """ 

44 Query the number of available GPUs. 

45 

46 Returns 

47 ------- 

48 int 

49 The number of available GPU devices. 

50 """ 

51 return handle_return(runtime.cudaGetDeviceCount()) 

52 

53 @property 

54 def devices(self) -> tuple: 

55 """ 

56 Query the available device instances. 

57 

58 Returns 

59 ------- 

60 tuple of Device 

61 A tuple containing instances of available devices. 

62 """ 

63 total = self.num_devices 

64 return tuple(Device(device_id) for device_id in range(total))