|
| 1 | +import subprocess # nosec |
| 2 | +import xml.dom.minidom # nosec |
| 3 | +from .loghandler import _logger |
| 4 | +from .utils import CWLObjectType |
| 5 | + |
| 6 | +from typing import Tuple, cast |
| 7 | + |
| 8 | + |
| 9 | +def cuda_version_and_device_count() -> Tuple[str, int]: |
| 10 | + try: |
| 11 | + out = subprocess.check_output(["nvidia-smi", "-q", "-x"]) # nosec |
| 12 | + except Exception as e: |
| 13 | + _logger.warning("Error checking CUDA version with nvidia-smi: %s", e) |
| 14 | + return ("", 0) |
| 15 | + dm = xml.dom.minidom.parseString(out) # nosec |
| 16 | + ag = dm.getElementsByTagName("attached_gpus")[0].firstChild |
| 17 | + cv = dm.getElementsByTagName("cuda_version")[0].firstChild |
| 18 | + return (cv.data, int(ag.data)) |
| 19 | + |
| 20 | + |
| 21 | +def cuda_check(cuda_req: CWLObjectType) -> int: |
| 22 | + try: |
| 23 | + vmin = float(str(cuda_req["cudaVersionMin"])) |
| 24 | + version, devices = cuda_version_and_device_count() |
| 25 | + if version == "": |
| 26 | + # nvidia-smi not detected, or failed some other way |
| 27 | + return 0 |
| 28 | + versionf = float(version) |
| 29 | + if versionf < vmin: |
| 30 | + _logger.warning( |
| 31 | + "CUDA version '%s' is less than minimum version '%s'", version, vmin |
| 32 | + ) |
| 33 | + return 0 |
| 34 | + dmin = cast(int, cuda_req.get("deviceCountMin", 1)) |
| 35 | + dmax = cast(int, cuda_req.get("deviceCountMax", dmin)) |
| 36 | + if devices < dmin: |
| 37 | + _logger.warning( |
| 38 | + "Requested at least %d GPU devices but only %d available", dmin, devices |
| 39 | + ) |
| 40 | + return 0 |
| 41 | + return min(dmax, devices) |
| 42 | + except Exception as e: |
| 43 | + _logger.warning("Error checking CUDA requirements: %s", e) |
| 44 | + return 0 |
0 commit comments