Repository navigation
Expand file tree
/
Copy pathclient.cpp
More file actions
524 lines (454 loc) · 17.7 KB
/
Copy pathclient.cpp
File metadata and controls
524 lines (454 loc) · 17.7 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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#include <boost/program_options.hpp>
#include <errno.h>
#include <fcntl.h>
#include <infiniband/verbs.h>
#include <numeric>
#include <unistd.h>
#include "grpc.h"
#include "buffers.h"
#include "network.h"
// Attention please! This would include also CUDA libs, although not needed at the client
// One should have a separate include_all file without cuda/rdma-shim libs, or a more
// streamline include are here...
#include "include_all.h"
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <future>
#include <csignal>
namespace po = boost::program_options;
struct client_request {
request_t * local;
request_t * remote;
int id;
uint64_t sent=0;
uint64_t received=0;
bool failed = false;
};
std::vector<client_request> requests;
std::string remote_address;
std::string local_address;
int remote_port;
bool quiet;
uint64_t buffer_size;
int batch;
//int gpu_batch;
//int gpu_buffer_size;
int n_slots;
int repeat;
std::atomic<uint64_t> outstanding = 0;
std::atomic<uint64_t> completed = 0;
std::atomic<uint64_t> timedout= 0;
int max_outstanding;
bool stop = false;
uint64_t total_requests = 0;
bool rc;
const int repeat_mult = 100000;
uint64_t timeout;
int max_runtime;
uint64_t start;
int arg_parse(int argc, char **argv, po::variables_map &vm) {
// clang-format off
po::options_description desc("RDMA from GPU: client");
desc.add_options()
("help", "Print help message")
("address", po::value<std::string>(&remote_address)->default_value("127.0.0.1"), "Server address")
("local-address", po::value<std::string>(&local_address)->default_value("127.0.0.1"), "Server address")
("port", po::value<int>(&remote_port)->default_value(3333), "Server port")
("batch", po::value<int>(&batch)->default_value(2048), "How often to post signaled and consume cqe")
//("write-size", po::value<int>(&write_size)->default_value(1000), "Buffer size to write")
("quiet", po::value<bool>(&quiet)->default_value(0), "Be less verbose")
("slots", po::value<int>(&n_slots)->default_value(1500), "How many slots to request")
("rc", po::value<bool>(&rc)->default_value(false), "Use RC transport")
("timeout", po::value<uint64_t>(&timeout)->default_value((uint64_t)5e9), "Timeout in ns")
("repeat", po::value<int>(&repeat)->default_value(1), "How many times to repeat")
("outstanding", po::value<int>(&max_outstanding)->default_value(0), "How many outstanding requests to have. 0 means n_slots")
("buffer-size", po::value<uint64_t>(&buffer_size)->default_value(1024*1024*1024), "Local buffer size")
("max-runtime", po::value<int>(&max_runtime)->default_value(260), "How long to run. Use it to avoid NPF errors!");
//("remote-address", po::value<std::string>(&remote_address)->default_value("192.168.128.1"), "Remote address for the RDMA packets")
//("gpu-batch", po::value<int>(&gpu_batch)->default_value(1), "Number of requests to send ayncrhonously when running from GPU.");
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
// clang-format on
if (vm.count("help")) {
desc.print(std::cout);
return 1;
}
if (max_outstanding == 0)
max_outstanding = n_slots;
return 0;
}
using namespace std;
int do_inference_write(
ibv_qp * qp, ibv_mr * mr,
int rkey,
client_request * request,
int input_size, int slot_size, bool signaled )
{
//TODO: the header should be written as separate wr or sg to ensure ordering
// Or at least put as "tail" so that we are almost sure it get written at the end
request->local->client_slot = (uint64_t) request->local;
request->local->client_rkey = (uint64_t) mr->rkey;
request->local->status = request_status_t::INPUTS;
request->local->id = request->id;
ibv_send_wr * bad_wr;
ibv_send_wr wr{};
ibv_sge sglist{};
memset(&wr, 0, sizeof(ibv_send_wr));
memset(&sglist, 0, sizeof(ibv_sge));
wr.num_sge=1;
wr.sg_list = &sglist;
sglist.addr = (uint64_t) request->local;
DLOG(INFO) << "Sending local slot " << request->local << " with length " << slot_size << " to remote "<<request->remote;
sglist.length = slot_size;
sglist.lkey = mr->lkey;
wr.wr.rdma.remote_addr = (uint64_t)request->remote;
wr.wr.rdma.rkey = rkey;
wr.opcode = IBV_WR_RDMA_WRITE;
wr.wr_id =request->id;
wr.send_flags = signaled ? (IBV_SEND_SIGNALED | IBV_SEND_SOLICITED) : 0; // IBV_SEND_SOLICITED | IBV_SEND_FENCE;
//wr.imm_data =;
// printf("Sending a wr with sge at %p with %i wr\n",
// wr.sg_list, wr.num_sge);
request->sent = now();
int ret = ibv_post_send(qp, &wr, &bad_wr);
// printf("ibv_post_send returned %i\n", ret);
return ret;
}
std::thread send_poller(ibv_qp * qp, bool *stop)
{
// TODO: This is where we should check for the timings
return std::thread ([qp, stop](){
LOG(INFO) << "Starting send_poller thread";
ibv_wc wc;
while(!*stop){
int ret = poll_single_cq_standard(qp->send_cq, &wc);
if (!ret)
{
if (wc.opcode == IBV_WC_RDMA_WRITE)
{
DLOG(INFO) << ibv_opcode_str(wc.opcode)
<< " with id "<< wc.wr_id
<< " and status " << ibv_wc_status_str(wc.status) ;
CHECK(wc.status == IBV_WC_SUCCESS) << "Error doing some RDMA magics";
}
else
LOG(INFO) << ibv_opcode_str(wc.opcode) << ": This event shouldn't be in the send cq..." <<
" Anyhow, its status is " << ibv_wc_status_str(wc.status);
}
}
});
}
std::thread recv_poller(ibv_qp * qp, bool *stop)
{
// TODO: This is where we should check for the timings
return std::thread ([qp, stop](){
LOG(INFO) << "Starting recv_poller thread";
ibv_wc wc;
while(!*stop){
int ret = poll_single_cq_standard(qp->recv_cq, &wc);
if (!ret)
{
if (wc.opcode == IBV_WC_RECV_RDMA_WITH_IMM)
{
DLOG(INFO) << ibv_opcode_str(wc.opcode)
<< " with id "<< wc.wr_id
<< " with imm "<< wc.imm_data
<< " and status " << ibv_wc_status_str(wc.status) ;
CHECK(wc.status == IBV_WC_SUCCESS) << "Error doing some RDMA magics";
outstanding--;
post_recv(qp);
if ((wc.imm_data) < requests.size())
{
requests[wc.imm_data].received = now();
// We receive only the actual outputs...
requests[wc.imm_data].local->status = request_status_t::OUTPUTS;
completed++;
}
}
else
LOG(INFO) << ibv_opcode_str(wc.opcode) << ": This event shouldn't be in the recv cq..." <<
" Anyhow, its status is " << ibv_wc_status_str(wc.status);
}
}
});
}
inline void print_status(bool on_exit = false)
{
uint64_t t = now();
printf("%li-RESULT-CLIENT_OUTSTANDING %li\n", t, outstanding.load());
printf("%li-RESULT-CLIENT_COMPLETED %li\n", t, completed.load());
printf("%li-RESULT-CLIENT_TIMEDOUT %li\n", t, timedout.load());
printf("%li-RESULT-CLIENT_RATE %f\n", t, completed.load() / ( (t - start) / 1e9) );
if(on_exit)
{
printf("RESULT-CLIENT_OUTSTANDING %li\n", outstanding.load());
printf("RESULT-CLIENT_COMPLETED %li\n", completed.load());
printf("RESULT-CLIENT_TIMEDOUT %li\n", timedout.load());
printf("RESULT-CLIENT_RATE %f\n", completed.load() / ( (t - start) / 1e9) );
}
// std::string s = "Outstanding ("+ std::to_string(outstanding)+"): [ ";
// for(auto &r:requests)
// if (r.received == 0 && r.sent != 0 && ! r.failed)
// s+= std::to_string(r.id) + ", ";
// else if (r.received == 0 && r.sent == 0)
// break;
// s+= " ]";
// return s;
}
inline void print_times(bool onexit = false)
{
uint64_t sum = 0;
uint64_t exclude = 0.1 * completed;
std::string s = "Completion times: [ ";
std::vector<uint64_t> times;
times.reserve(requests.size());
for(auto &r:requests)
{
if (exclude > 0)
{
// This would exclude the initial requests we did
exclude--;
continue;
}
// LOG(INFO) << r.id << " " << r.received << " "<<r.sent;
if (r.received != 0 && r.sent != 0 && !r.failed)
{
times.push_back(r.received - r.sent);
}
else
if (r.received == 0 && r.sent == 0) // no point to go beyond this
break;
}
for (auto &t : times)
sum+=t;
double avg = sum/(1.0*times.size());
std::sort(times.begin(),times.end());
// Dumb way...
double a95 = 0;
int tails_n = times.size() * (100-95) / 100 / 2;
for (int i=tails_n; i< times.size() - tails_n; i++)
a95+=times[i];
a95/=(times.size() * 0.95);
uint64_t t = now();
printf("%li-RESULT-CLIENT_AVG %f\n",t, sum/(1.0*times.size()));
printf("%li-RESULT-CLIENT_A95 %f\n",t, a95);
if (onexit)
{
printf("RESULT-CLIENT_AVG %f\n", sum/(1.0*times.size()));
printf("RESULT-CLIENT_A95 %f\n", a95);
}
}
std::thread status_printer(bool *stop)
{
// TODO: This is where we should check for the timings
return std::thread ([requests,stop](){
LOG(INFO) << "Starting status thread";
while(!*stop)
{
usleep(500'000);
print_status();
print_times();
}
});
}
std::thread handle_timeouts(bool *stop)
{
// TODO: This is where we should check for the timings
return std::thread ([requests,stop](){
LOG(INFO) << "Starting timeout thread";
while(!*stop)
{
// TODO: we should handle thread-safeness somehow
uint64_t max_old = now() - timeout;
uint64_t timedout_this_round =0;
for (int i=0; i< requests.size(); i++)
{
if (requests.at(i).sent == 0)
break;
if (requests.at(i).sent != 0 && requests.at(i).received == 0 && requests.at(i).sent < max_old)
{
requests.at(i).received = max_old;
requests.at(i).failed = true;
//printf("request %i timedout\n", i);
timedout_this_round++;
outstanding--;
timedout++;
}
}
printf("%li requests timed out this round\n", timedout_this_round);
usleep(1'000'000);
}
});
}
void on_exit(int signal)
{
stop = 1;
print_status(true);
print_times(true);
}
std::thread self_killer(bool *stop)
{
return std::thread([stop](){
for(int i = 0; i< max_runtime && !*stop; i++)
sleep(1);
if (!*stop)
{
// This means we reached the end of the for loop
LOG(INFO) << "Reached max runtime of " << max_runtime << "s.";
raise(SIGINT);
}
});
}
int main(int argc, char **argv) {
po::variables_map vm;
int ret = arg_parse(argc, argv, vm);
if (ret) return 1;
ibv_qp * qp;
ibv_pd * pd = nullptr;
ibv_qp_attr * qp_attrs;
std::vector<std::string> ips;
ips.push_back(local_address);
LOG(INFO) << local_address;
CHECK(init_rdma(&qp, &pd, ips, remote_address, -1,
&qp_attrs,
rc ?IBV_QPT_RC : IBV_QPT_UC
, false) == 0) << "error while initializing RDMA stack!";
void * buffer = malloc(buffer_size);
CHECK(buffer) << "Error while allocating buffer";
auto mr = ibv_reg_mr(qp->pd, buffer, buffer_size,
IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_WRITE);
CHECK(mr) << "Error while registering buffer";
int fd = open("/dev/random", O_RDONLY);
read(fd, buffer, buffer_size);
close(fd);
WorkerHello worker_hello = grpc_client_connect(remote_address + ":" + std::to_string(remote_port),
local_address,
qp->qp_num,
mr, n_slots, rc);
int dst_qp = worker_hello.qpnum();
finalize_rdma_connection(qp, qp_attrs, dst_qp);
LOG(INFO) << "Established connection with client " << remote_address << " and QP "<< qp->qp_num << " " << dst_qp;
LOG(INFO) << "Got " << worker_hello.slots().size() << " slots";
// // std::vector<void*> remote_slots;
// std::vector<void*> local_slots;
// // There must be another way!
// remote_slots.reserve(worker_hello.slots().size());
// local_slots.reserve(worker_hello.slots().size());
// for(auto & s : worker_hello.slots())
// remote_slots.push_back( (void*) s);
int received_slots = worker_hello.slots().size();
int slot_size = worker_hello.slot_size();
int client_id = worker_hello.client_id();
int input_size = worker_hello.input_size();
int rkey = worker_hello.rkey();
LOG_IF(INFO, received_slots < n_slots) << "Warning! Received less slots than requested. Proceed anyway";
CHECK(buffer_size > received_slots * slot_size) << "You should preallocate more memory! We need " << slot_size << "*"<< received_slots << " = " << slot_size * received_slots<< "B to handle so many slots!";
LOG(INFO) << "Server rkey is " << rkey;
requests.reserve(received_slots * repeat);
LOG(INFO) << "received slots is " << received_slots << " and repeat " << repeat;
int id = 0;
for (int r = 0; r < repeat; r++)
{
for(int i=0; i<received_slots; i++)
{
client_request c;
// The local is always different, the remote repeats itself
// c.local = (request_t *)((uint64_t)buffer + id * slot_size);
c.local = (request_t *)((uint64_t)buffer + i * slot_size);
c.remote = (request_t *)worker_hello.slots().at(i);
// c.id = (r*repeat_mult)+i;
c.id = id;
requests.push_back(c);
id++;
}
}
int n_recv = max(max_outstanding*4, received_slots);
n_recv = n_recv > qp->recv_cq->cqe ? qp->recv_cq->cqe: n_recv;
LOG(INFO) << "Filling recv queue with " << n_recv << " entries";
// TODO: Check that this is not higher than queue capacity....
for(int i=0; i<max_outstanding*2 && ret == 0 ; i++)
ret = post_recv(qp);
// Here we can start with the proper client things (= send reads)
LOG(INFO) << "CQ are " << qp->send_cq << " " << qp->recv_cq;
auto send_poller_th = send_poller(qp, &stop);
auto recv_poller_th = recv_poller(qp, &stop);
auto status_th = status_printer(&stop);
auto timeouts_th = handle_timeouts(&stop);
signal(SIGINT,on_exit);
signal(SIGTERM,on_exit);
auto killer_thread = self_killer(&stop);
if (max_outstanding > received_slots)
{
LOG(INFO) << "Adjusting outstanding requests to " <<received_slots;
max_outstanding = received_slots;
}
LOG(INFO) << "Max outstanding " << max_outstanding;
LOG(INFO) << "received slots " << received_slots;
start = now();
printf("EVENT CLIENT_START\n");
printf("RESULT-CLIENT_START %li\n", start);
int slow_start_limit = max_outstanding * 2;
bool slow_start = false;
int current_limit = 8;
int last_increment = 0;
for(auto &r : requests)
{
// Here we should check the outstanding requests, and prevent them to overwrite existing input data...
// So we should look if the local request has received timestamp or not in the previous round.
while(outstanding >= max_outstanding && !stop){
usleep(10);
}
if (slow_start){
while((outstanding >= current_limit) && !stop){
usleep(10);
}
if ((completed - last_increment) > current_limit)
{
current_limit = current_limit * 2;
last_increment = total_requests;
LOG(INFO) << "Slow start limit increased to " << current_limit;
}
if (current_limit > slow_start_limit)
{
LOG(INFO) << "End of slow start phase!";
slow_start = false;
}
}
// We could even randomize local slots, but it shouldn't change performance worker-side
do_inference_write(qp, mr,
rkey,
&r,
input_size, slot_size,
(total_requests%batch)== 0);
//usleep(100);
outstanding++;
total_requests++;
if (stop)
break;
}
uint64_t max_waiting = 5'000'000'000;
uint64_t start_waiting = now();
while(outstanding >0 && (now() - start_waiting) < max_waiting && !stop)
{
LOG(INFO) << "Waiting for completions... " << outstanding << " missing";
usleep(100000);
}
uint64_t t_stop = now();
LOG_IF(INFO, outstanding>0) << "Giving up: there are still some missing results!";
printf("EVENT CLIENT_STOP\n");
printf("RESULT-CLIENT_STOP %li\n", now());
LOG(INFO) << "Done " << completed << " requests in " << (t_stop - start) / 1e9 << " s: " << ((t_stop - start) / 1e6) / completed << " ms/req";
on_exit(0);
// JUst ignore all proper handling
exit(0);
send_poller_th.join();
killer_thread.join();
recv_poller_th.join();
status_th.join();
timeouts_th.join();
LOG(INFO) << "Hejdȧ";
exit(0);
return 0;
}