欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > 【知识】对比Share mem/Pin mem/GPU mem之间的传输速度

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

2024/10/25 22:26:00 来源:https://blog.csdn.net/sxf1061700625/article/details/141730553  浏览:    关键词:【知识】对比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 Memorystart_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 Memorystart_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 Memorystart_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 Memorystart_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 Memorystart_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 Memorystart_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之间的互传非常快(异步传输)。以后如何选,心里也大概有个数了。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com