0

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

TheMaster
  • 45,448
  • 6
  • 62
  • 85
zheyuanWang
  • 1,158
  • 2
  • 16
  • 30
  • 1
    You have to define what you even mean by “CUDA Version”. Do you mean toolkit? Do you mean runtime libraries? Do you mean driver version or driver version support level? How you determine those things are all different. There is no one answer – talonmies Aug 09 '22 at 12:40
  • Does this answer your question? [How to compare two strings in dot separated version format in Bash?](https://stackoverflow.com/q/4023830/7939871) – Léa Gris Aug 09 '22 at 13:55

3 Answers3

1

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
zheyuanWang
  • 1,158
  • 2
  • 16
  • 30
0

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
Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18
  • Thanks for your reply, I tried you command with ```#!/usr/bin/env bash set -e if $(nvcc --version | awk '/V([0-9]+.){2}.[0-9]+$/ { sub("V","",$NF);if ($NF>11.3) { exit 0 } else { exit 1 } }') then echo '11' else echo '22' fi``` in two enviroments with "Cuda compilation tools, release 11.1, V11.1.74" and "Cuda compilation tools, release 11.3, V11.3.109". But they give the same output '11'; – zheyuanWang Aug 09 '22 at 13:16
  • could we simplify the case by replacing `nvcc -V` with `nvcc -V | grep release` ? – zheyuanWang Aug 09 '22 at 13:21
  • 1
    There is never a need to pipe grep into awk as awk can search for text natively. – Raman Sailopal Aug 09 '22 at 13:50
0
nvcc --version | mawk 'NF*=/ release /' OFS='\f' FS='^.+[Vv]|[.]'  
11
  3
   58

and this solution is very adaptable to other version number scenarios too - say i wanna extract out the xnu version (i.e. very-core-macos) out :

# this is one single line, re-formatted for readability

Darwin 21.6.0 Darwin Kernel Version 21.6.0: Sat Jun 18 17:07:22 PDT 2022; 
root:xnu-8020.140.41~1/RELEASE_ARM64_T6000 arm64 arm
uname -mprsv | 

mawk 'NF*=/[:]xnu[-]/' OFS='\f' FS='^.+[:]xnu[-]|[.~]|[/].+$' 
8020
    140
       41
         1
RARE Kpop Manifesto
  • 2,453
  • 3
  • 11