-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cuda.py
More file actions
executable file
·36 lines (27 loc) · 1015 Bytes
/
test_cuda.py
File metadata and controls
executable file
·36 lines (27 loc) · 1015 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import time
from tqdm import *
import torch
import time
#for pbar in tqdm(range(10)):
# time.sleep(0.5)
print(torch.__version__) # 返回pytorch的版本
print(torch.cuda.is_available()) # 当CUDA可用时返回True
a = torch.randn(10000, 1000) # 返回10000行1000列的张量矩阵
b = torch.randn(1000, 2000) # 返回1000行2000列的张量矩阵
t0 = time.time() # 记录时间
c = torch.matmul(a, b) # 矩阵乘法运算
t1 = time.time() # 记录时间
print(a.device, t1 - t0, c.norm(2)) # c.norm(2)表示矩阵c的二范数
device = torch.device('cuda') # 用GPU来运行
a = a.to(device)
b = b.to(device)
# 初次调用GPU,需要数据传送,因此比较慢
t0 = time.time()
c = torch.matmul(a, b)
t2 = time.time()
print(a.device, t2 - t0, c.norm(2))
# 这才是GPU处理数据的真实运行时间,当数据量越大,GPU的优势越明显
t0 = time.time()
c = torch.matmul(a, b)
t2 = time.time()
print(a.device, t2 - t0, c.norm(2))