-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCUDA_test
More file actions
73 lines (54 loc) · 2.82 KB
/
Copy pathCUDA_test
File metadata and controls
73 lines (54 loc) · 2.82 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'''
This function generates two random square matrices of dimensions (size, size) using NumPy and performs matrix multiplication (@ is the Python 3.x operator for matrix multiplication).
It returns the resultant matrix c.
Function: test_gpu(size=5000, chunk_size=1000, runs=500)
This function tests the GPU's performance in performing matrix multiplications.
Device Detection: It first checks whether a CUDA-compatible GPU is available. If so, it uses it; otherwise, it falls back to the CPU.
Initialization: times is an empty list that will hold the time taken for each GPU run.
The Loop: Runs the test runs times.
CPU Work: Calls do_cpu_work() to perform some CPU-intensive tasks, but the results aren't actually used in the subsequent steps.
Start Timer: Records the start time.
Chunk Generation: The script creates smaller "chunks" of tensors for matrix multiplication. The tensors are directly moved to the device (CPU or GPU).
Matrix Multiplication: For each pair of chunks, matrix multiplication is performed twice, each in its own CUDA stream. The results are appended to c1_list and c2_list.
Concatenation: The chunks are concatenated to form the final matrices c1 and c2.
End Timer: Records the end time and calculates the time taken for the GPU operations.
Average Time: Finally, it calculates the average time taken for all the runs.
''';
import torch
import time
import numpy as np
def do_cpu_work(size=10000):
a = np.random.rand(size, size)
b = np.random.rand(size, size)
c = a @ b
return c
def test_gpu(size=5000, chunk_size=1000, runs=500):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
times = []
for i in range(runs):
# CPU work
a = do_cpu_work()
b = do_cpu_work()
start_time = time.time()
# Generate matrices in smaller chunks
b_chunks = [torch.randn(chunk_size, chunk_size, device=device) for _ in range(size // chunk_size)]
a_chunks = [torch.randn(chunk_size, chunk_size, device=device) for _ in range(size // chunk_size)]
c1_list = []
c2_list = []
for a_chunk, b_chunk in zip(a_chunks, b_chunks):
with torch.cuda.stream(torch.cuda.Stream()):
c_chunk = torch.matmul(a_chunk, b_chunk)
c1_list.append(c_chunk)
with torch.cuda.stream(torch.cuda.Stream()):
c_chunk = torch.matmul(a_chunk, b_chunk)
c2_list.append(c_chunk)
c1 = torch.cat(c1_list)
c2 = torch.cat(c2_list)
end_time = time.time()
times.append(end_time - start_time)
print(f"Finished run {i+1}/{runs}", end='\r')
avg_time = sum(times) / len(times)
print(f"\nAverage time per run: {avg_time:.4f} seconds")
if __name__ == "__main__":
test_gpu(size=5000, chunk_size=1000, runs=500)