-2

I can use alias gpu0='CUDA_VISIBLE_DEVICES=0' to set gpu0, but what if CUDA_VISIBLE_DEVICES=0,1,2?

talonmies
  • 70,661
  • 34
  • 192
  • 269

2 Answers2

0

I come up with a bash function to this. Put this function inside .bashrc:

gpu() {
  export CUDA_VISIBLE_DEVICES="$1"
}

and use it with gpu 0 or gpu 0,1

-1

share my script to generate all possible alias situation

import itertools
num = 8
gpu_list = []
for i in range(num):
    gpu_list.extend(list(itertools.combinations(
        [str(j) for j in range(num)], i + 1)))
print(gpu_list)
f = open('gpu.txt', 'w')
for gpu in gpu_list:
    sub1 = ''.join(gpu)
    sub2 = ','.join(gpu)
    f.writelines(f"alias gpu{sub1}=\'CUDA_VISIBLE_DEVICES={sub2}\'\n")
  • since I the shell-function answer above does not work well for me, I think this answer is a practical walk-around ; ) – zheyuanWang Sep 13 '22 at 05:27