Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions gpustack_runtime/detector/nvidia.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import math
import re
import threading
import time
from _ctypes import byref
from functools import lru_cache
Expand Down Expand Up @@ -75,10 +76,29 @@ def detect_pci_devices() -> dict[str, PCIDevice]:
return {}
return {dev.address: dev for dev in pci_devs}

_detect_lock = threading.Lock()

def __init__(self):
super().__init__(ManufacturerEnum.NVIDIA)

def detect(self) -> Devices | None: # noqa: PLR0915
def detect(self) -> Devices | None:
"""
Detect NVIDIA GPUs using pynvml with thread-safe locking.

Returns:
A list of detected NVIDIA GPU devices,
or None if not supported.

Raises:
If there is an error during detection.

"""
with self._detect_lock:
result = self._detect_impl()

return result

def _detect_impl(self) -> Devices | None: # noqa: PLR0915
"""
Detect NVIDIA GPUs using pynvml.

Expand All @@ -97,7 +117,6 @@ def detect(self) -> Devices | None: # noqa: PLR0915

try:
pci_devs = NVIDIADetector.detect_pci_devices()

pynvml.nvmlInit()
if not envs.GPUSTACK_RUNTIME_DETECT_NO_TOOLKIT_CALL:
try:
Expand Down
Loading