-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_41_multi_comms.py
215 lines (142 loc) · 4.94 KB
/
test_41_multi_comms.py
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
""""""
import os
import sys
import time
import random
import multiprocessing as mp
import multiprocessing.queues
import numpy as np
#os.environ['NUMBAPRO_CUDALIB'] = '/usr/local/cuda/lib64/'
# for: \numba\examples\cudajit\matmul.py
#os.environ['NUMBAPRO_NVVM'] = '/usr/local/cuda/nvvm/lib64/libnvvm.so'
#os.environ['NUMBAPRO_LIBDEVICE'] = '/usr/local/cuda/nvvm/libdevice/'
from numba import cuda
from numba.cuda import driver as cuda_driver
from ctypes import c_void_p, c_int, c_char, POINTER, byref
CURR_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT_DIR = os.path.dirname(CURR_DIR)
sys.path.insert(0, ROOT_DIR)
import pynccl
import torch
def test_41_002():
nrank = 4#8
# -------------------------------------
procs = []
q = mp.Queue()
# -------------------------------------
for i in range(nrank):
worker = mp.Process(
target=gpu_worker_proc_41_2,
args=(None, nrank, i, i, q))
worker.daemon = True
worker.start()
procs.append(worker)
for worker in procs:
worker.join()
def gpu_worker_proc_41_2(api, kn, rank, gpu_i, q):
# NOTE: do this at first of all
cuda.select_device(gpu_i)
nk = pynccl.Nccl()
#nc = nk._nccl # cuNccl
#api = nc._api # libnccl
pg0 = list(range(kn))
comm_0 = cre_nccl_comm_fn(nk, q, rank, pg0)
pg1 = list(range(kn))[1:]
comm_1 = cre_nccl_comm_fn(nk, q, rank, pg1)
pg2 = list(range(kn))[2:]
comm_2 = cre_nccl_comm_fn(nk, q, rank, pg2)
time.sleep(2)
nccl_fn_on_comm(nk, comm_0, q, rank, pg0, gpu_i)
time.sleep(1)
nccl_fn_on_comm(nk, comm_1, q, rank, pg1, gpu_i)
time.sleep(1)
nccl_fn_on_comm(nk, comm_2, q, rank, pg2, gpu_i)
time.sleep(1)
r = nk.comm_destroy(comm_0)
print(rank, '>>> ncclCommDestroy ', r)
r = nk.comm_destroy(comm_1)
print(rank, '>>> ncclCommDestroy ', r)
r = nk.comm_destroy(comm_2)
print(rank, '>>> ncclCommDestroy ', r)
def cre_nccl_comm_fn(nk, q, pg_rank, pg_ranks):
if pg_rank not in pg_ranks:
return # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ranks = sorted(pg_ranks)
kn = len(ranks)
if pg_rank == ranks[0]:
nuid = nk.get_unique_id()
for j in range(kn - 1):
q.put(nuid)
else:
nuid = q.get()
comm_i = nk.get_comm()
nRanks = kn
myRank = ranks.index(pg_rank)
r = nk.comm_init_rank(byref(comm_i), nRanks, nuid, myRank)
print(pg_rank, '>>> ncclCommInitRank ', r)
return comm_i
def nccl_fn_on_comm(nk, comm_i, q, pg_rank, pg_ranks, gpu_i):
if pg_rank not in pg_ranks:
return # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ranks = sorted(pg_ranks)
kn = len(ranks)
if pg_rank == ranks[0]:
w = torch.Tensor(np.random.random((kn * 10, 5))) # w
for j in range(kn - 1):
q.put(w)
else:
w = q.get()
rank = ranks.index(pg_rank)
#arr_send = w[rank]
arr_send = w[rank*10:(rank+1)*10, :]
arr_recv = torch.zeros((kn * 10, 5)) # recv
#arr_send[1][1] = random.random()
#print(arr_send[1][1])
#x#sz = arr_send.size
sz = np.prod(arr_send.size()) #* arr_send.element_size()
d_arr_send = arr_send.cuda(gpu_i)
d_arr_recv = arr_recv.cuda(gpu_i)
stream_i = nk.get_stream()
# for test: rank-0 's sleep will block the others allreduce
if rank == 0:
print('-=' * 40, rank)
time.sleep(3)
print('==' * 40, rank)
r = nk.group_start()
print(rank, '>>> ncclGroupStart ', r)
# NOTE: in pytorch, the t() function of Tensor does NOT change the
# original memory, so here we should create a new Tensor to nccl
#t_arr_send = d_arr_send
t_arr_send = torch.Tensor(d_arr_send.cpu()).cuda()
#p_arr_send = d_arr_send.data_ptr()
p_arr_send = t_arr_send.data_ptr()
p_arr_recv = d_arr_recv.data_ptr()
'''
r = nk.all_reduce(p_arr_send, p_arr_recv,
sz,
pynccl.binding.ncclFloat, pynccl.binding.ncclSum,
comm_i, stream_i.handle) # NOTE:
#comm_i, c_void_p(0)) # NOTE:
print(rank, '>>> ncclAllReduce ', rank, r)
'''
r = nk.all_gather(p_arr_send, p_arr_recv,
sz,
pynccl.binding.ncclFloat,
comm_i, stream_i.handle)
print(rank, '>>> ncclAllGather ', r)
r = nk.group_end()
print(rank, '>>> ncclGroupEnd ', r)
stream_i.synchronize()
r_arr = d_arr_recv.cpu().numpy()
time.sleep(rank)
print(rank, 'r_arr', r_arr)
#print(rank, w.cpu().numpy() == r_arr)
print(rank, (w.cpu().numpy() - r_arr) < 1e-6)
#r = nk.comm_destroy(comm_i)
#print(rank, '>>> ncclCommDestroy ', r)
if __name__ == '__main__':
os.environ['NCCL_DEBUG'] = 'WARN'
#os.environ['NUMBAPRO_CUDALIB'] = '/usr/local/cuda/lib64/'
os.environ['NUMBA_NCCLLIB'] = '/usr/lib/x86_64-linux-gnu/'
#os.environ['NCCL_SOCKET_IFNAME'] = 'enp11s0' # TODO: for IB
test_41_002()