Home > Mobile >  How to simplify CUDA_VISIBLE_DEVICES=0,1,6,7
How to simplify CUDA_VISIBLE_DEVICES=0,1,6,7

Time:09-13

Every time when I start training, I need to manully type a command like CUDA_VISIBLE_DEVICES=0,1,6,7, depending on how many GPUs I am going to use and which ones are currently free.

This answer offered an ugly yet practical walk-around. I.e. write an alias in bashrc for every combination:

alias gpu4='CUDA_VISIBLE_DEVICES=4'
alias gpu25='CUDA_VISIBLE_DEVICES=2,5'
alias gpu256='CUDA_VISIBLE_DEVICES=2,5,6'
alias gpu0467='CUDA_VISIBLE_DEVICES=0,4,6,7'

This, for example, could save much time from typing `CUDA_VISIBLE_DEVICES'.

How to further simplify the use of CUDA_VISIBLE_DEVICES?

Or, could anyone share an elegenter way to replace the alias-list above?

CodePudding user response:

A simple function like this, perhaps?

cuda () {
    local devs=$1
    shift
    CUDA_VISIBLE_DEVICES="$devs" "$@"
}

You'd run it like

cuda 2,3,7 command --options

Generally, prefer functions over aliases.

  • Related