文章目录
单机多卡
单机多卡情况一般有,单机双卡或者八卡比较常见。一般通过nvidia-smi查看gpu的规格和使用情况。gpu会被编上序号:[0,1,2,3,4,5,6,7]等。
显卡编号
在默认情况下,标号为0的显卡为主卡
增加其他显卡为主卡语句:
os.environ["CUDA_VISIBLE_DEVICES"] = "3,2,0,1"
model = torch.nn.DataParallel(model, device_ids=[0,2,3]).cuda()
此时3号显卡成为主卡代替之前0号显卡的位置,即gup_ids :0123---->3201
在使用DataParallel时指定的device_ids=0,2,3号显卡实际上是原来的2,1,0号显卡
调用全部显卡
最简单的方法使用DataParallel,默认所有存在的显卡都会被使用
net = torch.nn.DataParallel(net)
调用指定编号显卡
方法1.最简单的方法修改DataParallel:
torch.nn.DataParallel(model, device_ids=[0, 1, 2])
方法2.
os.environ["CUDA_VISIBLE_DEVICES"] = ','.join(map(str, [0,1,2])) # 一般在程序开头设置
# 等价于os.environ["CUDA_VISIBLE_DEVICES"] = '0,1,2'
net = torch.nn.DataParallel(model)
CUDA_VISIBLE_DEVICES 表示当前可以被python环境程序检测到的显卡
1. os.environ[“CUDA_VISIBLE_DEVICES”]详解
1.os.environ[“CUDA_VISIBLE_DEVICES”] = '1,2’进行指定使用设备
2.os.environ[“CUDA_VISIBLE_DEVICES”] = '1,2’放在所有访问GPU
代码之前
2. torch.cuda主要函数
1.torch.cuda.is_available()判断GPU是否可用
print(torch.cuda.is_available())
True
2.torch.cuda.device_count()查看可用GPU数量
os.environ["CUDA_VISIBLE_DEVICES"] = '0,1,2'
print(torch.cuda.device_count())
3
3.torch.cuda.current_device()查看当前使用的GPU序号
os.environ["CUDA_VISIBLE_DEVICES"] = '1,2'
print(torch.cuda.current_device())
0
3.关于unet分割一段指定gpu代码
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
os.environ['CUDA_VISIBLE_DEVICES'] = '0,1'
device_ids = range(torch.cuda.device_count())
if device:
net = net.cuda()
if len(device_ids)>1:
net = nn.DataParallel(net, device_ids=[0, 1])
如果要和模型整体结合可以将声明args.gpu_ids 的值,而不需要再去找代码里的进行修改。
多机多卡
(待更。。。。)