Skip to content

Commit 2dd0ce6

Browse files
XuehaiPanwookayin
authored andcommitted
Add noexcept funcitons gpu_count and is_available
1 parent fc99788 commit 2dd0ce6

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ __pycache__/
1212
# Distribution / packaging
1313
.Python
1414
env/
15+
venv/
1516
build/
1617
develop-eggs/
1718
dist/

gpustat/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414
) from ex
1515

1616
from .core import GPUStat, GPUStatCollection
17-
from .core import new_query
17+
from .core import new_query, gpu_count, is_available
1818
from .cli import print_gpustat, main
1919

2020

2121
__all__ = (
2222
'__version__',
23-
'GPUStat', 'GPUStatCollection',
23+
'GPUStat',
24+
'GPUStatCollection',
2425
'new_query',
25-
'print_gpustat', 'main',
26+
'gpu_count',
27+
'is_available',
28+
'print_gpustat',
29+
'main',
2630
)

gpustat/core.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,3 +740,26 @@ def new_query():
740740
to get the list of GPUs and running process information.
741741
'''
742742
return GPUStatCollection.new_query()
743+
744+
745+
def gpu_count() -> int:
746+
'''
747+
Return the number of GPUs in the system.
748+
'''
749+
try:
750+
N.nvmlInit()
751+
return N.nvmlDeviceGetCount()
752+
except N.NVMLError:
753+
return 0 # fallback
754+
finally:
755+
try:
756+
N.nvmlShutdown()
757+
except N.NVMLError:
758+
pass
759+
760+
761+
def is_available() -> bool:
762+
'''
763+
Return True if the NVML library and GPU devices are available.
764+
'''
765+
return gpu_count() > 0

0 commit comments

Comments
 (0)