1

My machine has three GPUs with indexes 0, 1, and 2. To assign my program to a specific GPU, I used the lines below:

import os
os.environ["CUDA_VISIBLE_DEVICES"] = "1"

How can I print the GPU index on which my program is executing?

Ahmad
  • 645
  • 2
  • 6
  • 21
  • Does this answer your question? [How do I list all currently available GPUs with pytorch?](https://stackoverflow.com/questions/64776822/how-do-i-list-all-currently-available-gpus-with-pytorch) – 吴慈霆 Feb 22 '23 at 05:09
  • No, I just want to print my set "CUDA_VISIBLE_DEVICES". – Ahmad Feb 22 '23 at 05:18
  • Have you tried `torch.cuda.current_device()`? – Hamzah Feb 22 '23 at 09:05
  • @Hamzah thanks for the comment. It gives index "0" GPU even when I execute os.environ["CUDA_VISIBLE_DEVICES"] = "1" before execution `torch.cuda.current_device()`. – Ahmad Feb 22 '23 at 10:26
  • Have you tried also this: `device = torch.device("cuda:1")` and then `torch.cuda.current_device()`? – Hamzah Feb 22 '23 at 10:31
  • Still, it's gives "0". – Ahmad Feb 22 '23 at 10:44
  • 1
    I might misunderstand something, but on my computer this `print(os.environ["CUDA_VISIBLE_DEVICES"])` prints the number, you set before. – Franciska Feb 22 '23 at 11:25

0 Answers0