首页 > 其他分享 >CUDA_VISIBLE_DEVICES介绍

CUDA_VISIBLE_DEVICES介绍

时间:2022-10-08 20:35:37浏览次数:64  
标签:devices will VISIBLE DEVICES CUDA GPU

CUDA_VISIBLE_DEVICES介绍

os.environ['CUDA_VISIBLE_DEVICES'] = '0'

主要由以下几个问题:

  • 这行代码是什么意思?

  • 为什么要设置成'0'

1. 代码解释

  • os.environ

    os.environ设置环境变量

    例如:

       os.environ['HOME'] = '\tmp'
    
    • 设置环境变量HOME的值为\tmp
  • CUDA_VISIBLE_DEVICES

    • 英文介绍

      If you are writing GPU enabled code, you would typically use a device query to select the desired GPUs. However, a quick and easy solution for testing is to use the environment variable CUDA_VISIBLE_DEVICES to restrict the devices that your CUDA application sees. This can be useful if you are attempting to share resources on a node or you want your GPU enabled executable to target a specific GPU.

      Environment Variable Syntax Results
      CUDA_VISIBLE_DEVICES=1 Only device 1 will be seen
      CUDA_VISIBLE_DEVICES=0,1 Devices 0 and 1 will be visible
      CUDA_VISIBLE_DEVICES="0,1" Same as above, quotation marks are optional
      CUDA_VISIBLE_DEVICES=0,2,3 Devices 0, 2, 3 will be visible; device 1 is masked

      CUDA will enumerate the visible devices starting at zero. In the last case, devices 0, 2, 3 will appear as devices 0, 1, 2. If you change the order of the string to “2,3,0”, devices 2,3,0 will be enumerated as 0,1,2 respectively. If CUDA_VISIBLE_DEVICES is set to a device that does not exist, all devices will be masked. You can specify a mix of valid and invalid device numbers. All devices before the invalid value will be enumerated, while all devices after the invalid value will be masked.
      To determine the device ID for the available hardware in your system, you can run NVIDIA’s deviceQuery executable included in the CUDA SDK.

    • 大致解释

      可以通过CUDA_VISIBLE_DEVICES 环境变量来限制CUDA程序所能使用的GPU设备。CUDA应用运行时,CUDA将遍历当前可见的设备,并从零开始为可见设备编号。

2. 显卡编号

在这里插入图片描述

我们的设备中存在多个GPU时,使用命令nvidia-smi查看当前Ubuntu平台的GPU数量,其中每个GPU被编上了序号:[0,1,2,3]

也就是上面所说的:CUDA应用运行时,CUDA将遍历当前可见的设备,并从零开始为可见设备编号。

3. 代码解释

限制我们的代码运行时,默认情况下只能使用第0个序号的显卡

参考文章

标签:devices,will,VISIBLE,DEVICES,CUDA,GPU
From: https://www.cnblogs.com/zlbingo/p/16770107.html

相关文章