Overall goal: I'm trying to assign my python program to the GPU with the most available memory.
Current issue: When I use this command:
nvidia-smi --query-gpu=memory.free --format=csv
I get the following output:
memory.free [MiB]
4800 MiB
5332 MiB
5346 MiB
Of course, that changes very frequently, so perhaps this is not the best way to accomplish my overall goal. For the moment, I'm trying to use this to figure out which GPU in the set of three has the most free memory. In this case, it's clearly GPU2 (they're labelled 0, 1, 2).
I created a bash script:
#/bin/bash
myarr=( $(nvidia-smi --query-gpu=memory.free --format=csv) )
echo $myarr
which outputs this to the screen:
memory.free
It used to output in this format:
memory.free [MiB] 4800 MiB 5332 MiB 5346 MiB
I then did this (with thanks to the first answer here):
myarr2=${myarr[2,4,6]}
echo $myarr2
IC=(`tr ' ' '\n' <<<$myarr2 | cat -n | sort -k2,2nr | head -n1`)
echo $IC
Ival=${IC[0]}
Cval=${IC[1]}
echo $Ival $Cval
However, it doesn't seem to matter what I do, I always get the index of the maximum to be at position 1. In the example I gave above, this was correct. In general, it is not correct.
The complete script:
#/bin/bash
myarr=( $(nvidia-smi --query-gpu=memory.free --format=csv) )
echo $myarr
myarr2=${myarr[2,4,6]}
echo $myarr2
IC=(`tr ' ' '\n' <<<$myarr2 | cat -n | sort -k2,2nr | head -n1`)
echo $IC
Ival=${IC[0]}
Cval=${IC[1]}
echo $Ival $Cval
What is wrong with how I'm searching for the position of the maximum?