-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver3.cpp
More file actions
423 lines (322 loc) · 11.2 KB
/
Copy pathserver3.cpp
File metadata and controls
423 lines (322 loc) · 11.2 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
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
#include <rdma/rdma_cma.h>
#include <infiniband/verbs.h>
#include <arpa/inet.h>
#include <cassert>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
#include "common3.h"
class Server : public RDMAPeer {
public:
rdma_cm_id *serverId;
rdma_cm_id *clientId;
ibv_mr *memReg;
Server() : RDMAPeer(), serverId(NULL), clientId(NULL), memReg(NULL) {
assert((eventChannel = rdma_create_event_channel()) != NULL);
assert(rdma_create_id(eventChannel, &serverId, NULL, RDMA_PS_TCP) == 0);
sin = {};
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
sin.sin_addr.s_addr = htonl(INADDR_ANY);
assert(rdma_bind_addr(serverId, (sockaddr *) &sin) == 0);
assert(rdma_listen(serverId, 6) == 0);
}
virtual ~Server() {
if (clientId)
rdma_destroy_qp(clientId);
if (compQueue)
ibv_destroy_cq(compQueue);
if (protDomain)
ibv_dealloc_pd(protDomain);
rdma_destroy_id(serverId);
rdma_destroy_event_channel(eventChannel);
}
void HandleConnectRequest() {
assert(eventChannel != NULL);
assert(serverId != NULL);
assert(event == NULL);
D(std::cerr << "HandleConnectRequest\n");
assert(rdma_get_cm_event(eventChannel, &event) == 0);
assert(event->event == RDMA_CM_EVENT_CONNECT_REQUEST);
D(std::cerr << "Received RDMA_CM_EVENT_CONNECT_REQUEST\n");
clientId = (rdma_cm_id *) event->id;
// create a prot domain for the client rdma device
assert((protDomain = ibv_alloc_pd(clientId->verbs)) != NULL);
assert((compQueue = ibv_create_cq(clientId->verbs, 32, 0, 0, 0)) != NULL);
qpAttr.send_cq = qpAttr.recv_cq = compQueue;
// queue pair
assert(rdma_create_qp(clientId, protDomain, &qpAttr) == 0);
assert(rdma_accept(clientId, &connParams) == 0);
rdma_ack_cm_event(event);
}
void HandleConnectionEstablished() {
assert(event != NULL);
D(std::cerr << "HandleConnectionEstablished\n");
assert(rdma_get_cm_event(eventChannel, &event) == 0);
assert(event->event == RDMA_CM_EVENT_ESTABLISHED);
rdma_ack_cm_event(event);
}
void HandleDisconnect() {
assert(event != NULL);
D(std::cerr << "HandleDisconnect\n");
check_z(rdma_get_cm_event(eventChannel, &event));
assert(event->event == RDMA_CM_EVENT_DISCONNECTED);
rdma_ack_cm_event(event);
}
};
void srvServerSends(const opts &opt) {
Server Srv;
Srv.HandleConnectRequest();
uint32_t *Key = new uint32_t();
MemRegion KeyMR(Key, sizeof(uint32_t), Srv.protDomain);
PostWrRecv RecvKey((uint64_t) Key, sizeof(uint32_t), KeyMR.getRegion()->lkey,
Srv.clientId->qp);
unsigned int outputSize = getOutputSize(opt);
std::cout << "Server - server sends\n";
std::cout << "Will send Do buffer of size " << outputSize << "\n";
std::cout << "The cost of comp will be " << opt.CompCost << "\n";
uint32_t *Do = new uint32_t[outputSize]();
Do[outputSize - 1] = 0x1234;
MemRegion DoMR(Do, sizeof(uint32_t) * outputSize, Srv.protDomain);
PostWrSend SendDo((uint64_t) Do, sizeof(uint32_t) * outputSize, DoMR.getRegion()->lkey,
Srv.clientId->qp);
Perf perf(opt.Measure);
// WARM UP
for (unsigned it = 0; it < NUM_WARMUP; ++it) {
RecvKey.exec();
// This way we save ourselves from waiting for Do to be sent.
if (it == 0) {
Srv.HandleConnectionEstablished();
Srv.WaitForCompletion(1);
} else {
Srv.WaitForCompletion(2);
}
expensiveFunc(opt.CompCost);
SendDo.exec();
std::cout << "Warm up " << it << "\n";
}
for (unsigned it = 0; it < NUM_REP; ++it) {
perf.start();
RecvKey.exec();
Srv.WaitForCompletion(2);
// key can be used from this point forward safely
// assume the function needs a subset A of a large set B to exec. if we were to
// run the func locally on the client, we would need to transfer A first.
expensiveFunc(opt.CompCost);
Do[outputSize - 1] = it * 100;
SendDo.exec();
perf.stop();
std::cout << "key=" << *Key << "\n";
}
Srv.WaitForCompletion(1);
delete Key;
delete[] Do;
Srv.HandleDisconnect();
}
void srvServerWrites(const opts &opt) {
unsigned int outputSize = getOutputSize(opt);
std::cout << "Server - server writes\n";
std::cout << "Will write Do buffer of size " << outputSize << "\n";
std::cout << "The cost of comp will be " << opt.CompCost << "\n";
Server Srv;
Srv.HandleConnectRequest();
uint32_t *Key = new uint32_t();
MemRegion KeyMR(Key, sizeof(uint32_t), Srv.protDomain);
PostWrRecv RecvKey((uint64_t) Key, sizeof(uint32_t), KeyMR.getRegion()->lkey,
Srv.clientId->qp);
uint32_t *Do = new uint32_t[outputSize]();
size_t WriteSize = outputSize * sizeof(uint32_t);
Do[outputSize - 1] = 0x1234;
MemRegion DoMR(Do, WriteSize, Srv.protDomain);
Sge WriteSGE((uint64_t) Do, WriteSize, DoMR.getRegion()->lkey);
SendWR WriteWR(WriteSGE);
WriteWR.setOpcode(IBV_WR_RDMA_WRITE);
SendWR ZeroWR;
ZeroWR.setOpcode(IBV_WR_SEND);
RecvSI RecvSI(Srv.protDomain);
RecvSI.post(Srv.clientId->qp);
Srv.WaitForCompletion(1);
WriteWR.setRdma(RecvSI.Info->Addr, RecvSI.Info->RemoteKey);
Perf perf(opt.Measure);
for (unsigned it = 0; it < NUM_WARMUP; ++it) {
RecvKey.exec();
// The first time we are here, we have to establish the connection.
if (it == 0) {
Srv.HandleConnectionEstablished();
Srv.WaitForCompletion(1);
} else {
Srv.WaitForCompletion(3);
}
expensiveFunc(opt.CompCost);
WriteWR.post(Srv.clientId->qp);
ZeroWR.post(Srv.clientId->qp);
}
for (unsigned it = 0; it < NUM_REP; ++it) {
perf.start();
RecvKey.exec();
Srv.WaitForCompletion(3);
// key can be used from this point forward safely
// assume the function needs a subset A of a large set B to exec. if we were to
// run the func locally on the client, we would need to transfer A first.
expensiveFunc(opt.CompCost);
Do[outputSize - 1] = it * 100;
WriteWR.post(Srv.clientId->qp);
ZeroWR.post(Srv.clientId->qp);
perf.stop();
std::cout << "key=" << *Key << "\n";
}
Srv.WaitForCompletion(2);
delete Key;
delete[] Do;
Srv.HandleDisconnect();
}
void clntServerSends(const opts &opt) {
std::cout << "Client - server sends\n";
std::cout << "Will send Di buffer of size " << opt.DiSize << "\n";
Server Srv;
Srv.HandleConnectRequest();
uint32_t *Key = new uint32_t();
MemRegion KeyMR(Key, sizeof(uint32_t), Srv.protDomain);
PostWrRecv RecvKey((uint64_t) Key, sizeof(uint32_t), KeyMR.getRegion()->lkey,
Srv.clientId->qp);
uint32_t *Di = new uint32_t[opt.DiSize]();
Di[opt.DiSize - 1] = 0x1234;
MemRegion DiMR(Di, sizeof(uint32_t) * opt.DiSize, Srv.protDomain);
PostWrSend SendDi((uint64_t) Di, sizeof(uint32_t) * opt.DiSize, DiMR.getRegion()->lkey,
Srv.clientId->qp);
Perf perf(opt.Measure);
// WARM UP
for (unsigned it = 0; it < NUM_WARMUP; ++it) {
RecvKey.exec();
// The first time we are here, we have to establish the connection.
// Also, we wait for the key to be received (we need it down below).
// In all the other cases, we wait for 2 wr. That is, the Send request from
// down below and the Recv req from the beginning of the loop (for the key).
// This way we save ourselves from waiting for Do to be sent.
if (it == 0) {
Srv.HandleConnectionEstablished();
Srv.WaitForCompletion(1);
} else {
Srv.WaitForCompletion(2);
}
SendDi.exec();
std::cout << "Warm up " << it << "\n";
}
// REAL BENCHMARK
for (unsigned it = 0; it < NUM_REP; ++it) {
perf.start();
RecvKey.exec();
Srv.WaitForCompletion(2);
// key can be used from this point forward safely
Di[opt.DiSize - 1] = it * 100;
SendDi.exec();
perf.stop();
std::cout << "key=" << *Key << "\n";
}
Srv.WaitForCompletion(1);
delete Key;
delete[] Di;
Srv.HandleDisconnect();
}
void clntClientReads(const opts &opt) {
std::cout << "Client - client reads\n";
std::cout << "Will setup Di buffer of size " << opt.DiSize << "\n";
Server Srv;
Srv.HandleConnectRequest();
uint32_t *Key = new uint32_t();
MemRegion KeyMR(Key, sizeof(uint32_t), Srv.protDomain);
PostWrRecv RecvKey((uint64_t) Key, sizeof(uint32_t), KeyMR.getRegion()->lkey,
Srv.clientId->qp);
// setup Di buffer, send SI of it
uint32_t *Di = new uint32_t[opt.DiSize]();
Di[opt.DiSize - 1] = 0x1234;
MemRegion DiMR(Di, sizeof(uint32_t) * opt.DiSize, Srv.protDomain);
SendSI SendSI(Di, DiMR.getRegion(), Srv.protDomain);
SendWR ZeroWR;
ZeroWR.setOpcode(IBV_WR_SEND);
SendSI.post(Srv.clientId->qp);
Srv.HandleConnectionEstablished();
Di[opt.DiSize - 1] = 0;
Srv.WaitForCompletion(1);
Perf perf(opt.Measure);
for (unsigned it = 0; it < NUM_WARMUP; ++it) {
RecvKey.exec();
Srv.WaitForCompletion(1);
ZeroWR.post(Srv.clientId->qp);
Srv.WaitForCompletion(1);
}
for (unsigned it = 0; it < NUM_REP; ++it) {
perf.start();
RecvKey.exec(); // wait for the key to write our mem
Srv.WaitForCompletion(1);
Di[opt.DiSize - 1] = it * 100;
ZeroWR.post(Srv.clientId->qp); // notify the client to read the mem
Srv.WaitForCompletion(1);
perf.stop();
}
delete[] Key;
delete[] Di;
Srv.HandleDisconnect();
}
void srvClientReads(const opts &opt) {
unsigned int outputSize = getOutputSize(opt);
std::cout << "Server - client reads\n";
std::cout << "Will send Do buffer of size " << outputSize << "\n";
std::cout << "The cost of comp will be " << opt.CompCost << "\n";
Server Srv;
Srv.HandleConnectRequest();
uint32_t *Key = new uint32_t();
MemRegion KeyMR(Key, sizeof(uint32_t), Srv.protDomain);
PostWrRecv RecvKey((uint64_t) Key, sizeof(uint32_t), KeyMR.getRegion()->lkey,
Srv.clientId->qp);
uint32_t *Do = new uint32_t[outputSize]();
Do[outputSize - 1] = 0x1234;
MemRegion DoMR(Do, sizeof(uint32_t) * outputSize, Srv.protDomain);
SendSI SendSI(Do, DoMR.getRegion(), Srv.protDomain);
SendWR ZeroWR;
ZeroWR.setOpcode(IBV_WR_SEND);
SendSI.post(Srv.clientId->qp);
Srv.HandleConnectionEstablished();
Do[outputSize - 1] = 0;
Srv.WaitForCompletion(1);
Perf perf(opt.Measure);
for (unsigned it = 0; it < NUM_WARMUP; ++it) {
RecvKey.exec();
Srv.WaitForCompletion(1);
expensiveFunc(opt.CompCost);
ZeroWR.post(Srv.clientId->qp);
Srv.WaitForCompletion(1);
}
for (unsigned it = 0; it < NUM_REP; ++it) {
perf.start();
RecvKey.exec(); // wait for the key to write our mem
Srv.WaitForCompletion(1);
expensiveFunc(opt.CompCost);
Do[outputSize - 1] = it * 100;
ZeroWR.post(Srv.clientId->qp); // notify the client to read the mem
Srv.WaitForCompletion(1);
perf.stop();
std::cout << "key=" << *Key << "\n";
}
delete[] Key;
delete[] Do;
Srv.HandleDisconnect();
}
int main(int argc, char * argv[]) {
opts opt = parse_cl(argc, argv);
if (opt.send && opt.ExecServer) {
srvServerSends(opt);
} else if (opt.write && opt.ExecServer) {
srvServerWrites(opt);
} else if (opt.Read && opt.ExecServer) {
srvClientReads(opt);
} else if (opt.Read && opt.ExecClient) {
clntClientReads(opt);
} else if (opt.send && opt.ExecClient) {
clntServerSends(opt);
} else {
check(false, "Invalid combination of options");
}
return 0;
}