I can use alias gpu0='CUDA_VISIBLE_DEVICES=0'
to set gpu0, but what if CUDA_VISIBLE_DEVICES=0,1,2
?
Asked
Active
Viewed 381 times
-2

talonmies
- 70,661
- 34
- 192
- 269
-
1`alias gpu012='CUDA_VISIBLE_DEVICES=0,1,2'`? But a shell function that takes arguments would probably be better. – BlameTheBits Nov 05 '19 at 09:45
-
but it will be tedious if I have 8 gpus...maybe I need to write a script to generate it – Xionghui Wang Nov 05 '19 at 15:29
2 Answers
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

Phan Van Hoai Duc
- 77
- 1
-
-
Perhaps take a look at the answer from https://stackoverflow.com/questions/73697960/how-to-simplify-cuda-visible-devices-0-1-6-7 – Sean Yun-Shiuan Chuang May 01 '23 at 03:36
-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