bashaliasmulti-gpu

How to simplify CUDA_VISIBLE_DEVICES=0,1,6,7


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 workaround. 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 a more elegant way to replace the alias list above?


Solution

  • 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.