Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add noexcept funcitons gpu_count and is_available #145

Merged
merged 1 commit into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ __pycache__/
# Distribution / packaging
.Python
env/
venv/
build/
develop-eggs/
dist/
Expand Down
10 changes: 7 additions & 3 deletions gpustat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@
) from ex

from .core import GPUStat, GPUStatCollection
from .core import new_query
from .core import new_query, gpu_count, is_available
from .cli import print_gpustat, main


__all__ = (
'__version__',
'GPUStat', 'GPUStatCollection',
'GPUStat',
'GPUStatCollection',
'new_query',
'print_gpustat', 'main',
'gpu_count',
'is_available',
'print_gpustat',
'main',
)
23 changes: 23 additions & 0 deletions gpustat/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,3 +719,26 @@ def new_query():
to get the list of GPUs and running process information.
'''
return GPUStatCollection.new_query()


def gpu_count() -> int:
'''
Return the number of GPUs in the system.
'''
try:
N.nvmlInit()
return N.nvmlDeviceGetCount()
except N.NVMLError:
return 0 # fallback
wookayin marked this conversation as resolved.
Show resolved Hide resolved
finally:
try:
N.nvmlShutdown()
except N.NVMLError:
pass


def is_available() -> bool:
'''
Return True if the NVML library and GPU devices are available.
'''
return gpu_count() > 0