【知识】对比Share mem/Pin mem/GPU mem之间的传输速度

转载请注明出处:小锋学长生活大爆炸[xfxuezhagn.cn]

如果本文帮助到了你,欢迎[点赞、收藏、关注]哦~

目录

参考代码

运行结果


参考代码

import torch
import time
import matplotlib.pyplot as plt

# 初始化设备和张量
device = torch.device('cuda')
data_sizes = [100, 1000, 5000, 10000, 50000, 100000, 300000, 500000]  # 不同数据量
results = {'Shared to Pinned': [], 'Pinned to Shared': [],
           'GPU to Pinned': [], 'Pinned to GPU': [],
           'GPU to Shared': [], 'Shared to GPU': []}

# 测试不同数据量
for size in data_sizes:
    shared_tensor = torch.randn((size, 1000), dtype=torch.float32, device='cpu').share_memory_()
    pinned_tensor = torch.randn((size, 1000), dtype=torch.float32, device='cpu').pin_memory()
    gpu_tensor = torch.randn((size, 1000), dtype=torch.float32, device=device)

    # Shared Memory => Pinned Memory
    start_time = time.time()
    pinned_tensor.copy_(shared_tensor, non_blocking=True)
    end_time = time.time()
    results['Shared to Pinned'].append(end_time - start_time)
    torch.cuda.synchronize()

    # Pinned Memory => Shared Memory
    start_time = time.time()
    shared_tensor.copy_(pinned_tensor, non_blocking=True)
    end_time = time.time()
    results['Pinned to Shared'].append(end_time - start_time)
    torch.cuda.synchronize()

    # GPU Memory => Pinned Memory
    start_time = time.time()
    pinned_tensor.copy_(gpu_tensor, non_blocking=True)
    end_time = time.time()
    results['GPU to Pinned'].append(end_time - start_time)
    torch.cuda.synchronize()

    # Pinned Memory => GPU Memory
    start_time = time.time()
    gpu_tensor.copy_(pinned_tensor, non_blocking=True)
    end_time = time.time()
    results['Pinned to GPU'].append(end_time - start_time)
    torch.cuda.synchronize()

    # GPU Memory => Shared Memory
    start_time = time.time()
    shared_tensor.copy_(gpu_tensor, non_blocking=True)
    end_time = time.time()
    results['GPU to Shared'].append(end_time - start_time)
    torch.cuda.synchronize()

    # Shared Memory => GPU Memory
    start_time = time.time()
    gpu_tensor.copy_(shared_tensor, non_blocking=True)
    end_time = time.time()
    results['Shared to GPU'].append(end_time - start_time)
    torch.cuda.synchronize()

# 绘制图表
plt.figure(figsize=(10, 6))
for key, values in results.items():
    plt.plot(data_sizes, values, marker='o', label=key)

plt.xlabel('Data Size (rows)')
plt.ylabel('Time (seconds)')
plt.title('Memory Copy Time for Different Data Sizes')
plt.legend()
plt.grid(True)
plt.show()

运行结果

        所以,share to gpu是最慢的,而对于pin和gpu之间的互传非常快(异步传输)。以后如何选,心里也大概有个数了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小锋学长生活大爆炸

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

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

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

打赏作者

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

抵扣说明:

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

余额充值