使用Pytorch测试cuda设备的性能(单卡或多卡并行)

21 篇文章 1 订阅
11 篇文章 0 订阅

以下CUDA设备泛指NVIDIA显卡 或 启用ROCm的AMD显卡

  • 测试环境:
    • Distributor ID: Ubuntu
    • Description: Ubuntu 22.04.3 LTS
    • Release: 22.04
    • Codename: jammy

1.首先,简单使用torch.ones测试CUDA设备

import torch
import time

def cuda_benchmark(device_id, N=1000000):
    # 指定要使用的显卡设备
    torch.cuda.set_device(device_id)

    # 创建输入数据
    data = torch.ones(N).cuda()

    # 启动CUDA操作,并记录执行时间
    start_time = time.time()
    for i in range(10000):
        data += 1
    torch.cuda.synchronize()  # 等待CUDA操作执行完成
    end_time = time.time()

    # 将结果从GPU内存下载到主机内存
    result = data.cpu().numpy()

    # 打印Benchmark结果和执行时间
    print(f"Benchmark结果:{result[:10]}")
    print(f"执行时间:{end_time - start_time} 秒")


if __name__ == '__main__':
	# 测试第一块显卡
	device_id = 0
    cuda_benchmark(device_id,10000000)

2.使用自带的CUDABenchmarkModel测试CUDA设备

import torch
import torch.nn as nn
import time

class CUDABenchmarkModel(nn.Module):
    def __init__(self):
        super(CUDABenchmarkModel, self).__init__()
        self.fc = nn.Linear(10, 10).cuda()

    def forward(self, x):
        return self.fc(x)

def cuda_benchmark(device_ids, N=10000000):
    # 创建模型
    model = CUDABenchmarkModel()
    model = nn.DataParallel(model, device_ids=device_ids)

    # 创建输入数据
    data = torch.ones(N, 10).cuda()

    # 启动CUDA操作,并记录执行时间
    start_time = time.time()
    for i in range(10000):
        output = model(data)
    torch.cuda.synchronize()  # 等待CUDA操作执行完成
    end_time = time.time()

    # 打印执行时间
    print(f"执行时间:{end_time - start_time} 秒")

if __name__ == '__main__':
	# 同时测试3块显卡
	device_ids = [0, 1, 2]
    cuda_benchmark(device_ids=device_ids)

3.使用nccl多进程的方式测试CUDA设备

import torch
import torch.nn as nn
import torch.distributed as dist
import torch.multiprocessing as mp
import time

def cuda_benchmark(device_id, N=10000000):
    # 指定要使用的显卡设备
    torch.cuda.set_device(device_id)
    print(f"该GPU的核心数量为:{torch.cuda.get_device_properties(device_id).multi_processor_count}")
    # 创建输入数据
    data = torch.ones(N).cuda()

    # 启动CUDA操作,并记录执行时间
    start_time = time.time()
    for i in range(10000):
        data += 1
    torch.cuda.synchronize()  # 等待CUDA操作执行完成
    end_time = time.time()

    # 将结果从GPU内存下载到主机内存
    result = data.cpu().numpy()

    # 打印Benchmark结果和执行时间
    print(f"Benchmark结果:{result[:10]}")
    print(f"执行时间:{end_time - start_time} 秒")

def main(num):
    # 初始化多进程
    mp.spawn(run, args=(num,), nprocs=num)

def run(rank,world_size):
    """每个进程的入口函数"""
    # 初始化进程组
    dist.init_process_group("nccl", init_method="tcp://127.0.0.1:23456", rank=rank, world_size=world_size)
    # 指定设备ID
    device_id = rank

    # 在多个GPU上并行执行操作
    model = cuda_benchmark(device_id)

if __name__ == '__main__':
	# 同时启用3个进程(一个进程对应一块显卡)
	device_numbers = 3
    main(device_numbers)


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木法星人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值