【发布时间】:2021-06-03 05:43:20
【问题描述】:
我编写了这个 pytorch 程序来计算 GPU 上 5000*5000 的矩阵乘法,100 次迭代。
import torch
import numpy as np
import time
N = 5000
x1 = np.random.rand(N, N)
######## a 5000*5000 matrix multiplication on GPU, 100 iterations #######
x2 = torch.tensor(x1, dtype=torch.float32).to("cuda:0")
start_time = time.time()
for n in range(100):
G2 = x2.t() @ x2
print(G2.size())
print("It takes", time.time() - start_time, "seconds to compute")
print("G2.device:", G2.device)
start_time2 = time.time()
# G4 = torch.zeros((5,5),device="cuda:0")
G4 = G2[:5, :5]
print("G4.device:", G4.device)
print("G4======", G4)
# G5=G4.cpu()
# print("G5.device:",G5.device)
print("It takes", time.time() - start_time2, "seconds to transfer or display")
这是我笔记本电脑上的结果:
torch.Size([5000, 5000])
计算需要 0.22243595123291016 秒
G2.device: cuda:0
G4.device: cuda:0
G4======张量([[1636.3195, 1227.1913, 1252.6871, 1242.4584, 1235.8160], [1227.1913、1653.0522、1260.2621、1246.9526、1250.2871]、 [1252.6871, 1260.2621, 1685.1147, 1257.2373, 1266.2213], [1242.4584, 1246.9526, 1257.2373, 1660.5951, 1239.5414], [1235.8160, 1250.2871, 1266.2213, 1239.5414, 1670.0034]], 设备='cuda:0')
传输或显示需要60.13639569282532秒
进程以退出代码 0 结束
我很困惑为什么在 GPU 上显示变量 G5 需要这么多时间,因为它的大小只有 5*5。 顺便说一句,我使用“G5=G4.cpu()”将GPU上的变量传输到CPU,也需要很多时间。
我的开发环境(相当旧的笔记本电脑):
-
pytorch 1.0.0
-
CUDA 8.0
-
英伟达 GeForce GT 730m
-
Windows 10 专业版
增加迭代次数时,计算时间没有明显增加,但传输或显示明显增加,为什么?谁能解释一下,非常感谢。
【问题讨论】: