3

I have some issue I just want to catch exact CUDA version using nvidia-smi from command line and it is working in shell: enter image description here

$ nvidia-smi | awk -F"CUDA Version:" 'NR==3{split($2,a," ");print a[1]}'
11.0

But when I am doing the same operation from makefile I have syntax error:

ver_cuda:
    CUDA = $(nvidia-smi | awk -F"CUDA Version:" 'NR==3{split($2,a," ");print a[1]}');
    VER_CUDA ?= $(CUDA);

Result:

awk: line 1: syntax error at or near ,
expr: syntax error: unexpected argument ‘11.0’
make: Nothing to be done for 'ver_cuda'

If somebody could help me I will be really apreciate !

1
  • Unfortunatelly issue still occured awk: line 1: syntax error at or near , expr: syntax error: unexpected argument ‘11.0’ CUDA = ;
    – Hub
    May 6 at 8:29
4

Assuming GNU make:

Add the shell keyword before the nvidia-smi command and double the dollars in awk command. The similarities in make n shell syntax are a source of confusion.

ver_cuda:
    CUDA = $(shell nvidia-smi | awk -F"CUDA Version:" 'NR==3{split($$2,a," ");print a[1]}')
    VER_CUDA ?= $(CUDA)
3

Part of the problem is that you're using $(nvidia-smi ...) instead of $(shell nvidia-smi ...). That's easily solved.

You also have to use $$2 instead of just $2 to prevent make from expanding it (probably expanding to nothing, resulting in the awk expression being just {print } and printing the entire line).

e.g. with a minimalist Makefile:

all:
    CUDA="$(shell nvidia-smi -q | awk -F': ' '/CUDA Version/ {print $$2}')"

Note the colon followed by space in awk's -F field separator.

$ make
CUDA="Not Found"

This is actually the correct value for my system, I don't have CUDA installed.

BTW, it's important to realise that while some make syntax looks a bit like shell syntax, it's very different....and, worse, the similarities can make it easy to write buggy makefiles.

See Makefile: how to sed correctly to edit a variable for a good answer relevant to your question.


On a more general note, you will probably find the output of nvidia-smi -q to be much easier to process with awk (or sed or perl or whatever).

e.g. on my system:

$ nvidia-smi -q | grep -i version
Driver Version                            : 460.56
CUDA Version                              : Not Found
    VBIOS Version                         : 86.06.0E.00.38
    Inforom Version
        Image Version                     : G001.0000.01.03

$ nvidia-smi -q | awk -F': ' '/CUDA Version/ {print $2}'
Not Found

I don't have CUDA stuff installed, so I just get "not found". I can get a useful result for Driver Version:

$ nvidia-smi -q | awk -F': ' '/Driver Version/ {print $2}'
460.56

nvidia-smi also has various --query-gpu options. e.g.

$ nvidia-smi --query-gpu=driver_version,vbios_version --format=csv
driver_version, vbios_version
460.56, 86.06.0E.00.38

Oddly, CUDA version isn't one of the query-gpu options. See nvidia-smi --help-query-gpu for full details.

1
  • Thank you your solution works ! I used nvidia-smi -q | awk -F': ' '/CUDA Version/ {print $2}' I have some information about syntax but variable is assigned.
    – Hub
    May 6 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.