-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpu_benchmark.py
More file actions
190 lines (155 loc) · 5.81 KB
/
gpu_benchmark.py
File metadata and controls
190 lines (155 loc) · 5.81 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import argparse
import torch
import torch.distributed as dist
import torch.multiprocessing as mp
import os
import time
import sys
import numpy as np
def parse_args():
parser = argparse.ArgumentParser(description="Distributed GPU Benchmarking Script")
parser.add_argument(
"--master_addr", type=str, required=True, help="Master node address"
)
parser.add_argument(
"--master_port", type=str, required=True, help="Master node port"
)
parser.add_argument(
"--world_size",
type=int,
required=True,
help="Total number of processes across all nodes",
)
parser.add_argument(
"--starting_rank", type=int, required=True, help="Starting rank for this node"
)
return parser.parse_args()
def setup_process(
rank, world_size, master_addr, master_port, local_rank
):
os.environ["MASTER_ADDR"] = master_addr
os.environ["MASTER_PORT"] = master_port
torch.cuda.set_device(local_rank)
device = torch.device(f"cuda:{local_rank}")
dist.init_process_group(
backend="nccl", rank=rank, world_size=world_size, device_id=device
)
dist.barrier()
tensor_size = 1024 * 1024 # 1MB
send_tensor = torch.ones(tensor_size, device=device)
recv_tensor = torch.zeros(tensor_size, device=device)
# WARM UP
warmup_iterations = 10
for target_rank in range(world_size):
if target_rank == rank:
continue
for _ in range(warmup_iterations):
if rank < target_rank:
torch.cuda.synchronize()
dist.send(tensor=send_tensor, dst=target_rank)
dist.recv(tensor=recv_tensor, src=target_rank)
torch.cuda.synchronize()
dist.recv(tensor=recv_tensor, src=target_rank)
dist.send(tensor=send_tensor, dst=target_rank)
else:
torch.cuda.synchronize()
dist.recv(tensor=recv_tensor, src=target_rank)
dist.send(tensor=send_tensor, dst=target_rank)
torch.cuda.synchronize()
dist.send(tensor=send_tensor, dst=target_rank)
dist.recv(tensor=recv_tensor, src=target_rank)
dist.barrier()
# --------------------------------------------------------
bandwidths = {}
ITERS = 1000
for target_rank in range(world_size):
if target_rank == rank:
continue
if rank < target_rank:
torch.cuda.synchronize()
start_time = time.time()
for _ in range(ITERS):
dist.send(tensor=send_tensor, dst=target_rank)
dist.recv(tensor=recv_tensor, src=target_rank)
torch.cuda.synchronize()
dist.recv(tensor=recv_tensor, src=target_rank)
dist.send(tensor=send_tensor, dst=target_rank)
torch.cuda.synchronize()
end_time = time.time()
else:
# Receive first, then send
torch.cuda.synchronize()
start_time = time.time()
for _ in range(ITERS):
dist.recv(tensor=recv_tensor, src=target_rank)
dist.send(tensor=send_tensor, dst=target_rank)
torch.cuda.synchronize()
dist.send(tensor=send_tensor, dst=target_rank)
dist.recv(tensor=recv_tensor, src=target_rank)
torch.cuda.synchronize()
end_time = time.time()
# Calculate bandwidth in MB/s
elapsed_time = end_time - start_time
# Each send and receive involves tensor_size * 4 bytes (float32) * (2 tensors/iter * ITERS)
bandwidth = (
(tensor_size * 4 * 2) / (elapsed_time * 1024 * 1024) * 2 * ITERS
) # MB/s
bandwidths[target_rank] = bandwidth
print(f"Rank {rank} <-> Rank {target_rank}: {bandwidth:.2f} MB/s")
gather_tensor = torch.zeros(world_size, dtype=torch.float32, device=device)
for target_rank, bw in bandwidths.items():
gather_tensor[target_rank] = bw
if rank == 0:
gather_list = [
torch.zeros(world_size, dtype=torch.float32, device=device)
for _ in range(world_size)
]
else:
gather_list = None
dist.gather(tensor=gather_tensor, gather_list=gather_list, dst=0)
if rank == 0:
full_bandwidth = []
for proc in range(world_size):
# Copy data from gather_list
proc_bandwidth = gather_list[proc].cpu().numpy()
full_bandwidth.append(proc_bandwidth)
bandwidth_matrix = np.array(full_bandwidth)
np.fill_diagonal(bandwidth_matrix, 0)
min_bw = bandwidth_matrix.min()
max_bw = bandwidth_matrix.max()
if max_bw > min_bw:
normalized_matrix = (bandwidth_matrix - min_bw) / (max_bw - min_bw)
else:
normalized_matrix = (
bandwidth_matrix
)
print("\nNormalized Bandwidth Matrix (0 to 1):")
print(normalized_matrix)
np.save("gpu_speed.npy", normalized_matrix)
dist.barrier()
dist.destroy_process_group()
def worker(local_rank, args, results_queue):
global_rank = args.starting_rank + local_rank
setup_process(
global_rank,
args.world_size,
args.master_addr,
args.master_port,
local_rank,
results_queue,
)
def main():
args = parse_args()
num_gpus = torch.cuda.device_count()
if num_gpus == 0:
print("No GPUs found on this node.")
sys.exit(1)
if args.starting_rank + num_gpus > args.world_size:
print(
f"Error: starting_rank ({args.starting_rank}) + num_gpus ({num_gpus}) exceeds world_size ({args.world_size})."
)
sys.exit(1)
# Spawn one process per GPU
mp.spawn(worker, args=(args), nprocs=num_gpus, join=True)
if __name__ == "__main__":
main()