-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdpulib.c
424 lines (359 loc) · 11.2 KB
/
dpulib.c
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include "get_ip.h"
#include "dpulib.h"
// uint64_t pagesize;
// Initiate connection to DPU so it can be used for collective communication.
DPUContext *DPU_Init(char *dpu_addr, char *dpu_port, int queue_depth)
{
int ret = 0;
DPUContext *ctx = calloc(1, sizeof(DPUContext));
uint64_t pagesize = sysconf(_SC_PAGESIZE);
ctx->queue_depth = queue_depth;
ctx->status = calloc(1, sizeof(DPUStatus) * queue_depth);
if (!ctx->status)
{
fprintf(stderr, "Too many completions - could not allocate.\n");
return NULL;
}
new(struct rdma_addrinfo, hints);
struct rdma_addrinfo *host_res;
hints.ai_port_space = RDMA_PS_TCP;
ret = rdma_getaddrinfo(dpu_addr, dpu_port, &hints, &host_res);
if (ret)
{
fprintf(stderr, "Could not resolve host\n");
return NULL;
}
ctx->connid = malloc(sizeof(struct rdma_cm_id));
if (!ctx->connid)
{
fprintf(stderr, "Could not allocate space for connection context\n");
return NULL;
}
// Create endpoint
new(struct ibv_qp_init_attr, init_attr);
init_attr.cap.max_send_wr = 10;
init_attr.cap.max_recv_wr = 10;
init_attr.cap.max_send_sge = 10;
init_attr.cap.max_recv_sge = 10;
init_attr.sq_sig_all = 1;
ret = rdma_create_ep(&ctx->connid, host_res, NULL, &init_attr);
if (ret)
{
fprintf(stderr, "Could not create EP.\n");
return NULL;
}
// Connect to DPU
ret = rdma_connect(ctx->connid, NULL);
if (ret < 0)
{
fprintf(stderr, "Could not connect to DPU: %s\n", strerror(errno));
return NULL;
}
new(struct ibv_qp_init_attr, conn_attr);
new(struct ibv_qp_attr, conn_qp_attr);
ret = ibv_query_qp(ctx->connid->qp, &conn_qp_attr, IBV_QP_CAP, &conn_attr);
if (ret)
{
fprintf(stderr, "Could not query QP information.\n");
rdma_destroy_ep(ctx->connid);
}
ctx->qp_max_inline = init_attr.cap.max_inline_data;
// Allocate send and receive buffers
void *sndbuf = calloc(1, pagesize);
void *rcvbuf = calloc(1, pagesize);
if (!sndbuf || !rcvbuf)
{
fprintf(stderr, "Failed to allocate memory.\n");
return NULL;
}
ctx->meta_send_mr = rdma_reg_msgs(ctx->connid, sndbuf, pagesize);
if (!ctx->meta_send_mr)
{
fprintf(stderr, "SEND: Could not register memory (%lu bytes)\n", pagesize);
return NULL;
}
ctx->meta_recv_mr = rdma_reg_msgs(ctx->connid, rcvbuf, pagesize);
if (!ctx->meta_recv_mr)
{
fprintf(stderr, "RECV: Could not register memory (%lu bytes)\n", pagesize);
return NULL;
}
return ctx;
}
int DPU_Exit(DPUContext *ctx)
{
int ret;
struct ibv_wc wc;
//go away message
char *msg = "a";
ret = rdma_post_send(ctx->connid, NULL, msg, 1, NULL, IBV_SEND_INLINE);
if (ret)
{
fprintf(stderr, "We cannot tell the server to go away.\n");
return 1;
}
while (rdma_get_send_comp(ctx->connid, &wc) == 0);
ret = rdma_disconnect(ctx->connid);
ret = rdma_dereg_mr(ctx->meta_recv_mr);
if (ret)
{
fprintf(stderr, "Failed to deregister receive MR.\n");
}
ret = rdma_dereg_mr(ctx->meta_send_mr);
if (ret)
{
fprintf(stderr, "Failed to deregister send MR.\n");
}
free(ctx->status);
free(ctx);
return 0;
}
int DPU_MPI_Ialltoall( DPUContext *ctx,
void *sendbuf, int sendcount, MPI_Datatype sendtype,
void *recvbuf, int recvcount, MPI_Datatype recvtype,
int worldsize )
{
int ret;
OffloadReq req = OffloadReq_init_zero;
struct ibv_mr *send_mr, *recv_mr;
int send_size, recv_size;
ret = MPI_Type_size(sendtype, &send_size);
if (ret)
{
fprintf(stderr, "Invalid MPI type\n");
return -1;
}
ret = MPI_Type_size(sendtype, &recv_size);
if (ret)
{
fprintf(stderr, "Invalid MPI type\n");
return -1;
}
uint64_t send_alloc = send_size * sendcount * worldsize;
uint64_t recv_alloc = recv_size * recvcount * worldsize;
send_mr = ibv_reg_mr(ctx->connid->pd, sendbuf, send_alloc, IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_READ | IBV_ACCESS_REMOTE_WRITE);
if (!send_mr)
{
fprintf(stderr, "Error registering send memory region: %s", strerror(errno));
return -1;
}
recv_mr = ibv_reg_mr(ctx->connid->pd, recvbuf, recv_alloc, IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_READ | IBV_ACCESS_REMOTE_WRITE);
if (!recv_mr)
{
fprintf(stderr, "Error registering recv memory region: %s", strerror(errno));
return -1;
}
req.send_buf = (uint64_t) sendbuf;
req.send_datatype = (uint64_t) DPU_MPI_Type_Pack(sendtype);
req.send_elems = sendcount;
req.send_rkey = send_mr->rkey;
req.recv_buf = (uint64_t) recvbuf;
req.recv_datatype = (uint64_t) DPU_MPI_Type_Pack(recvtype);
req.recv_elems = recvcount;
req.recv_rkey = recv_mr->rkey;
req.cookie = rand();
pb_ostream_t out_stream = pb_ostream_from_buffer(ctx->meta_send_mr->addr, ctx->meta_send_mr->length);
ret = (int) pb_encode(&out_stream, OffloadReq_fields, &req);
if (!ret)
{
fprintf(stderr, "Error serializing into protobuf.\n");
return -1;
}
int flag = IBV_SEND_SIGNALED;
if (out_stream.bytes_written < ctx->qp_max_inline)
{
flag |= IBV_SEND_INLINE;
}
ret = rdma_post_send(ctx->connid, NULL, ctx->meta_send_mr->addr, out_stream.bytes_written, ctx->meta_send_mr, flag);
if (ret)
{
fprintf(stderr, "Failed to post send to offload engine.\n");
return -1;
}
struct ibv_wc wc;
while (rdma_get_send_comp(ctx->connid, &wc) == 0);
for (int i = 0; i < ctx->queue_depth; i++)
{
if (ctx->status[i].status == COMP_STATUS_EMPTY)
{
ctx->status[i].cookie = req.cookie;
ctx->status[i].send_mr = send_mr;
ctx->status[i].recv_mr = recv_mr;
ctx->status[i].status = COMP_STATUS_PENDING;
return i;
}
}
return -1;
}
/*
Poll the completion queue to find completion events.
Returns: Completion event position in queue.
-1 when no event is completed.
-2 on error.
*/
int DPU_MPI_Poll(DPUContext *ctx)
{
struct ibv_wc wc;
int ret;
// If we are not waiting on a message then do
if (!ctx->pending_recvs)
{
ret = rdma_post_recv(ctx->connid, NULL, ctx->meta_recv_mr->addr, ctx->meta_recv_mr->length, ctx->meta_recv_mr);
if (ret)
{
fprintf(stderr, "Could not post receive!\n");
return -2;
}
ctx->pending_recvs++;
}
ret = ibv_poll_cq(ctx->connid->recv_cq, 1, &wc);
if (ret < 0)
{
fprintf(stderr, "poll CQ failed %d: %s\n", ret, ibv_wc_status_str(ret));
return -2;
}
else if (ret > 0)
{
if (wc.status != IBV_WC_SUCCESS)
{
fprintf(stderr, "Transport failure: %s\n", ibv_wc_status_str(wc.status));
return -2;
}
// Subtract number of received messages from pending
ctx->pending_recvs -= ret;
int32_t imm = (int32_t) wc.imm_data;
// Determine if this event is a completion
for (int i = 0; i < ctx->queue_depth; i++)
{
if (ctx->status[i].cookie == imm)
{
ctx->status[i].status = COMP_STATUS_OK;
return i;
}
else if (ctx->status[i].cookie == -imm)
{
ctx->status[i].status = COMP_STATUS_ERROR;
return i;
}
}
// The DPU sent a completion event but we couldn't match the cookie
fprintf(stderr, "Invalid completion event from DPU!\n");
return -2;
}
return -1;
}
/*
Poll the completion queue to find completion events.
This has an increased latency but lower CPU usage.
Returns: Completion event position in queue.
-1 when no event is completed.
-2 on error.
*/
int DPU_MPI_Longpoll(DPUContext *ctx)
{
struct ibv_wc wc;
int ret;
// If we are not waiting on a message then do
if (!ctx->pending_recvs)
{
ret = rdma_post_recv(ctx->connid, NULL, ctx->meta_recv_mr->addr, ctx->meta_recv_mr->length, ctx->meta_recv_mr);
if (ret)
{
fprintf(stderr, "Could not post receive!\n");
return -2;
}
ctx->pending_recvs++;
}
ret = rdma_get_recv_comp(ctx->connid, &wc);
if (ret < 0)
{
fprintf(stderr, "poll CQ failed %d: %s\n", ret, ibv_wc_status_str(ret));
return -2;
}
else if (ret > 0)
{
if (wc.status != IBV_WC_SUCCESS)
{
fprintf(stderr, "Transport failure: %s\n", ibv_wc_status_str(wc.status));
return -2;
}
// Subtract number of received messages from pending
ctx->pending_recvs -= ret;
int32_t imm = (int32_t) wc.imm_data;
// Determine if this event is a completion
for (int i = 0; i < ctx->queue_depth; i++)
{
if (ctx->status[i].cookie == imm)
{
ctx->status[i].status = COMP_STATUS_OK;
return i;
}
else if (ctx->status[i].cookie == -imm)
{
ctx->status[i].status = COMP_STATUS_ERROR;
return i;
}
}
// The DPU sent a completion event but we couldn't match the cookie
fprintf(stderr, "Invalid completion event from DPU!\n");
return -2;
}
return -1;
}
/*
Test whether an offloaded operation is completed.
Returns the index if completed. -1 if not. -2 if error.
If alltoall was called correctly, the cookie should never be negative.
*/
int DPU_MPI_Test(DPUContext *ctx, int cookie)
{
// Ask network hardware for new recv or check existing ones
int ret = DPU_MPI_Poll(ctx);
if (ret == -2)
{
fprintf(stderr, "Error performing DPU_MPI_Poll\n");
return -2;
}
// Check the completion list for new cookies
for (int i = 0; i < ctx->queue_depth; i++)
{
if (ctx->status[i].cookie == cookie && ctx->status[i].status == COMP_STATUS_OK)
{
ret = ibv_dereg_mr(ctx->status[i].send_mr);
if (ret)
{
fprintf(stderr, "failed to cleanup...\n");
}
ret = ibv_dereg_mr(ctx->status[i].recv_mr);
if (ret)
{
fprintf(stderr, "failed to cleanup...\n");
}
ctx->status[i].status = COMP_STATUS_EMPTY;
return i;
}
}
return -1;
}
/*
Wait for a specific offloaded operation to complete.
Note request can block forever if the cookie is invalid.
*/
int DPU_MPI_Wait(DPUContext *ctx, int cookie)
{
int x;
do
{
x = DPU_MPI_Test(ctx, cookie);
}
while (x == -1);
if (x == -2)
{
fprintf(stderr, "Error!!\n");
return 1;
}
return 0;
}
int get_cookie(DPUContext *ctx, int index){
return ctx->status[index].cookie;
}