-
Notifications
You must be signed in to change notification settings - Fork 60
/
test_from_worker_address.py
243 lines (176 loc) · 7.4 KB
/
test_from_worker_address.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import asyncio
import multiprocessing as mp
import os
import struct
import numpy as np
import pytest
import ucp
from ucp.utils import get_event_loop
mp = mp.get_context("spawn")
def _test_from_worker_address_server(queue):
async def run():
# Send worker address to client process via multiprocessing.Queue
address = ucp.get_worker_address()
queue.put(address)
# Receive address size
address_size = np.empty(1, dtype=np.int64)
await ucp.recv(address_size, tag=0)
# Receive address buffer on tag 0 and create UCXAddress from it
remote_address = bytearray(address_size[0])
await ucp.recv(remote_address, tag=0)
remote_address = ucp.get_ucx_address_from_buffer(remote_address)
# Create endpoint to remote worker using the received address
ep = await ucp.create_endpoint_from_worker_address(remote_address)
# Send data to client's endpoint
send_msg = np.arange(10, dtype=np.int64)
await ep.send(send_msg, tag=1, force_tag=True)
get_event_loop().run_until_complete(run())
def _test_from_worker_address_client(queue):
async def run():
# Read local worker address
address = ucp.get_worker_address()
# Receive worker address from server via multiprocessing.Queue, create
# endpoint to server
remote_address = queue.get()
ep = await ucp.create_endpoint_from_worker_address(remote_address)
# Send local address to server on tag 0
await ep.send(np.array(address.length, np.int64), tag=0, force_tag=True)
await ep.send(address, tag=0, force_tag=True)
# Receive message from server
recv_msg = np.empty(10, dtype=np.int64)
await ep.recv(recv_msg, tag=1, force_tag=True)
np.testing.assert_array_equal(recv_msg, np.arange(10, dtype=np.int64))
get_event_loop().run_until_complete(run())
def test_from_worker_address():
queue = mp.Queue()
server = mp.Process(
target=_test_from_worker_address_server,
args=(queue,),
)
server.start()
client = mp.Process(
target=_test_from_worker_address_client,
args=(queue,),
)
client.start()
client.join()
server.join()
assert not server.exitcode
assert not client.exitcode
def _get_address_info(address=None):
# Fixed frame size
frame_size = 10000
# Header format: Recv Tag (Q) + Send Tag (Q) + UCXAddress.length (Q)
header_fmt = "QQQ"
# Data length
data_length = frame_size - struct.calcsize(header_fmt)
# Padding length
padding_length = None if address is None else (data_length - address.length)
# Header + UCXAddress string + padding
fixed_size_address_buffer_fmt = header_fmt + str(data_length) + "s"
assert struct.calcsize(fixed_size_address_buffer_fmt) == frame_size
return {
"frame_size": frame_size,
"data_length": data_length,
"padding_length": padding_length,
"fixed_size_address_buffer_fmt": fixed_size_address_buffer_fmt,
}
def _pack_address_and_tag(address, recv_tag, send_tag):
address_info = _get_address_info(address)
fixed_size_address_packed = struct.pack(
address_info["fixed_size_address_buffer_fmt"],
recv_tag, # Recv Tag
send_tag, # Send Tag
address.length, # Address buffer length
(
bytearray(address) + bytearray(address_info["padding_length"])
), # Address buffer + padding
)
assert len(fixed_size_address_packed) == address_info["frame_size"]
return fixed_size_address_packed
def _unpack_address_and_tag(address_packed):
address_info = _get_address_info()
recv_tag, send_tag, address_length, address_padded = struct.unpack(
address_info["fixed_size_address_buffer_fmt"],
address_packed,
)
# Swap send and recv tags, as they are used by the remote process in the
# opposite direction.
return {
"address": address_padded[:address_length],
"recv_tag": send_tag,
"send_tag": recv_tag,
}
def _test_from_worker_address_server_fixedsize(num_nodes, queue):
async def run():
async def _handle_client(packed_remote_address):
# Unpack the fixed-size address+tag buffer
unpacked = _unpack_address_and_tag(packed_remote_address)
remote_address = ucp.get_ucx_address_from_buffer(unpacked["address"])
# Create endpoint to remote worker using the received address
ep = await ucp.create_endpoint_from_worker_address(remote_address)
# Send data to client's endpoint
send_msg = np.arange(10, dtype=np.int64)
await ep.send(send_msg, tag=unpacked["send_tag"], force_tag=True)
# Receive data from client's endpoint
recv_msg = np.empty(20, dtype=np.int64)
await ep.recv(recv_msg, tag=unpacked["recv_tag"], force_tag=True)
np.testing.assert_array_equal(recv_msg, np.arange(20, dtype=np.int64))
# Send worker address to client processes via multiprocessing.Queue,
# one entry for each client.
address = ucp.get_worker_address()
for i in range(num_nodes):
queue.put(address)
address_info = _get_address_info()
server_tasks = []
for i in range(num_nodes):
# Receive fixed-size address+tag buffer on tag 0
packed_remote_address = bytearray(address_info["frame_size"])
await ucp.recv(packed_remote_address, tag=0)
# Create an async task for client
server_tasks.append(_handle_client(packed_remote_address))
# Await handling each client request
await asyncio.gather(*server_tasks)
get_event_loop().run_until_complete(run())
def _test_from_worker_address_client_fixedsize(queue):
async def run():
# Read local worker address
address = ucp.get_worker_address()
recv_tag = ucp.utils.hash64bits(os.urandom(16))
send_tag = ucp.utils.hash64bits(os.urandom(16))
packed_address = _pack_address_and_tag(address, recv_tag, send_tag)
# Receive worker address from server via multiprocessing.Queue, create
# endpoint to server
remote_address = queue.get()
ep = await ucp.create_endpoint_from_worker_address(remote_address)
# Send local address to server on tag 0
await ep.send(packed_address, tag=0, force_tag=True)
# Receive message from server
recv_msg = np.empty(10, dtype=np.int64)
await ep.recv(recv_msg, tag=recv_tag, force_tag=True)
np.testing.assert_array_equal(recv_msg, np.arange(10, dtype=np.int64))
# Send message to server
send_msg = np.arange(20, dtype=np.int64)
await ep.send(send_msg, tag=send_tag, force_tag=True)
get_event_loop().run_until_complete(run())
@pytest.mark.parametrize("num_nodes", [1, 2, 4, 8])
def test_from_worker_address_multinode(num_nodes):
queue = mp.Queue()
server = mp.Process(
target=_test_from_worker_address_server_fixedsize,
args=(num_nodes, queue),
)
server.start()
clients = []
for i in range(num_nodes):
client = mp.Process(
target=_test_from_worker_address_client_fixedsize,
args=(queue,),
)
client.start()
clients.append(client)
for client in clients:
client.join()
server.join()
assert not server.exitcode
assert not client.exitcode