Home > Net >  Check cuda version in bash scripts
Check cuda version in bash scripts

Time:08-10

Basically, I want to achieve a version control on CUDA, similar to this:

if python3 -c 'import sys; assert sys.version_info == (3,8)' > /dev/null
then
    exit;
fi

In my case, the CUDA version shown by ncvv -V must be >=11.3

according to this answer we could get cuda version via:

CUDA_VERSION=$(nvcc --version | sed -n 's/^.*release \([0-9]\ \.[0-9]\ \).*$/\1/p')

from the output of nvcc -V, in which the version is given after the key word "release":

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2021 NVIDIA Corporation
Built on Sun_Mar_21_19:15:46_PDT_2021
Cuda compilation tools, release 11.3, V11.3.58
Build cuda_11.3.r11.3/compiler.29745058_0

But I am having trouble on further comparing the version with 11.3

CodePudding user response:

I figured out a solution via python:

import os,re
b = os.popen('nvcc -V').readlines()
>>> b
['nvcc: NVIDIA (R) Cuda compiler driver\n', 'Copyright (c) 2005-2021 NVIDIA Corporation\n', 'Built on Sun_Mar_21_19:15:46_PDT_2021\n', 'Cuda compilation tools, release 11.3, V11.3.58\n', 'Build cuda_11.3.r11.3/compiler.29745058_0\n']
b = str(b)
c = re.findall(r"[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}",b)
>>> c[0]
'11.3.58'
cc = c[0].split(".")
>>> cc
['11', '3', '58']

>>> [int(x) for x in cc] >[11,3,0]
True
>>> [int(x) for x in cc] >[11,4,0]
False
>>> [int(x) for x in cc] >[8,4,0]
True
>>> [int(x) for x in cc] >[8,314,0]
True

to sum up:

#!/usr/bin/env bash

if python3 -c 'import os,re; assert [int(x) for x in re.findall(r"[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}", str(os.popen("nvcc -V").readlines()))[0].split(".")] >[11,3,0]'> /dev/null
then 
    echo '11'
else
    echo '22'
fi

CodePudding user response:

Awk will allow you to deal with the decimal places:

nvcc --version | awk '/V([0-9] .){2}.[0-9] $/ {  sub("V","",$NF);if ($NF>11.3) { exit 0 } else { exit 1 } }'

Run the output of the nvcc command into awk and then process lines with V followed by any number of digits, a full stop (2 times) and then any number of digits followed by an end of line (this regular expression may need amending for your use case)

Processing the resulting line, we take the last field and remove the "V" using awk's sub function. We then compare the new field to see whether it is greater than 11.3. If it is exit with 0, otherwise exit with 1.

Finally, within your script, you can utilise the command result with:

if $(nvcc --version | awk '/V([0-9] .){2}.[0-9] $/ {  sub("V","",$NF);if ($NF>11.3) { exit 0 } else { exit 1 } }')
then 
     exit
fi
  • Related