-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathibutils.hpp
1147 lines (814 loc) · 40 KB
/
ibutils.hpp
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2024, Hammurabi Mendes.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef IB_UTILS
#define IB_UTILS
#include <cstdint>
#include <atomic>
#include <iostream>
#include <vector>
#include <arpa/inet.h>
#include <infiniband/verbs.h>
#include "utils/TimeHolder.hpp"
#include "utils/Synchronizer.hpp"
// Maximum for my device is 928
#define MAX_INLINE_DATA 64
using std::atomic;
using std::cout;
using std::cerr;
using std::endl;
using std::vector;
class IBContext;
class IBQueuePair;
class RDMAMemory;
class RDMAMemoryLocator;
class SentMessageInformation;
class ReceivedMessageInformation;
class alignas(64) RDMAMemory {
private:
void *buffer;
uint64_t size;
uint64_t offset = 0;
struct ibv_mr *region;
uint32_t local_key;
uint32_t remote_key;
bool internal_memory;
bool internal_register;
public:
uint64_t operation_timestamp;
uint64_t operation_tag: 63;
uint64_t sticky: 1;
atomic<bool> ready{true};
// Not inline, they perform memory registration and use the context class
RDMAMemory(IBContext *context, uint64_t size = 4096, uint64_t mode = IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_READ | IBV_ACCESS_REMOTE_WRITE);
RDMAMemory(IBContext *context, void *buffer, uint64_t size, uint64_t mode = IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_READ | IBV_ACCESS_REMOTE_WRITE);
// Inline, they are simply "views" of larger buffers or other RDMAMemory objects
inline RDMAMemory(void *buffer, uint64_t size): buffer{buffer}, size{size}, internal_memory{false}, internal_register{false}, operation_timestamp{0}, operation_tag{0}, sticky{false} {
region = NULL;
local_key = 0;
remote_key = 0;
}
inline RDMAMemory(RDMAMemory *other, uint64_t size, uint64_t offset): size{size}, region{other->region}, internal_memory{false}, internal_register{false}, operation_timestamp{0}, operation_tag{0}, sticky{false} {
buffer = other->get_buffer(offset);
local_key = region->lkey;
remote_key = region->lkey;
}
inline ~RDMAMemory() {
if(internal_register && region != NULL) {
ibv_dereg_mr(region);
}
if(internal_memory && buffer != NULL) {
std::free(buffer);
}
}
inline void *get_buffer() const noexcept {
char *base = reinterpret_cast<char *>(buffer);
return reinterpret_cast<void *>(base + offset);
}
inline void *get_buffer(uint64_t additional_offset) const noexcept {
char *base = reinterpret_cast<char *>(buffer);
return reinterpret_cast<void *>(base + offset + additional_offset);
}
inline uint64_t get_size() const noexcept {
return size - offset;
}
inline uint32_t get_local_key() const noexcept {
return local_key;
}
inline uint32_t get_remote_key() const noexcept {
return remote_key;
}
inline void update_offset(uint64_t new_offset) noexcept {
offset = new_offset;
}
};
class RDMAMemoryLocator {
public:
void *buffer;
uint32_t remote_key;
RDMAMemoryLocator() = default;
RDMAMemoryLocator(const RDMAMemoryLocator &other) = default;
RDMAMemoryLocator(RDMAMemory *memory) {
buffer = memory->get_buffer();
remote_key = memory->get_remote_key();
}
RDMAMemoryLocator(RDMAMemory *memory, uint64_t offset) {
buffer = memory->get_buffer(offset);
remote_key = memory->get_remote_key();
}
inline void *get_buffer() const noexcept {
return buffer;
}
inline void *get_buffer(uint64_t offset) const noexcept {
char *base = reinterpret_cast<char *>(buffer);
return reinterpret_cast<void *>(base + offset);
}
inline uint32_t get_remote_key() const noexcept {
return remote_key;
}
inline RDMAMemoryLocator &operator+=(int bytes) noexcept {
buffer = (reinterpret_cast<char *>(buffer) + bytes);
return *this;
}
inline RDMAMemoryLocator &operator-=(int bytes) noexcept {
buffer = (reinterpret_cast<char *>(buffer) - bytes);
return *this;
}
};
class SentMessageInformation: public ibv_wc {
inline RDMAMemory *get_memory() {
return reinterpret_cast<RDMAMemory *>(wr_id);
}
inline void *get_buffer() {
return get_memory()->get_buffer();
}
inline uint32_t get_size() {
return byte_len;
}
inline bool has_immediate() {
return (wc_flags & IBV_WC_WITH_IMM);
}
inline uint32_t get_immediate() {
return ntohl(imm_data);
}
inline uint32_t get_queue_pair_number() {
return this->qp_num;
}
};
class ReceivedMessageInformation: public ibv_wc {
public:
inline RDMAMemory *get_memory() {
return reinterpret_cast<RDMAMemory *>(wr_id);
}
inline void *get_buffer() {
return get_memory()->get_buffer();
}
inline uint32_t get_size() {
return byte_len;
}
inline bool has_immediate() {
return (wc_flags & IBV_WC_WITH_IMM);
}
inline uint32_t get_immediate() {
return ntohl(imm_data);
}
inline uint32_t get_queue_pair_number() {
return this->qp_num;
}
};
class IBCompletionQueues {
public:
struct ibv_cq *send_completion_queue;
struct ibv_cq *recv_completion_queue;
struct ibv_comp_channel *recv_event_channel;
bool recv_event_only_solicited;
bool initialized;
IBCompletionQueues(ibv_context *context, int size, struct ibv_comp_channel *recv_event_channel = nullptr, bool recv_event_only_solicited = false);
~IBCompletionQueues();
inline bool set_recv_event_notification() {
if(recv_event_channel == nullptr) {
return false;
}
bool result = (ibv_req_notify_cq(recv_completion_queue, (recv_event_only_solicited ? 1 : 0)) == 0);
return result;
}
inline IBCompletionQueues *wait_recv_event() {
struct ibv_cq *recv_completion_queue_detected;
void *recv_completion_context_detected;
if(ibv_get_cq_event(recv_event_channel, &recv_completion_queue_detected, &recv_completion_context_detected) != 0) {
cerr << "Error waiting for receive event" << endl;
return nullptr;
}
ibv_ack_cq_events(recv_completion_queue_detected, 1);
if(!set_recv_event_notification()) {
return nullptr;
}
return reinterpret_cast<IBCompletionQueues *>(recv_completion_context_detected);
}
inline int poll_send_completion_queue(SentMessageInformation *completed_work_request, int quantity = 1) noexcept {
int number_completed = ibv_poll_cq(send_completion_queue, quantity, reinterpret_cast<ibv_wc *>(completed_work_request));
for(int i = 0; i < number_completed; i++) {
if(completed_work_request[i].status != IBV_WC_SUCCESS) {
cerr << "Error sending data with ID " << completed_work_request[i].wr_id << ": " << ibv_wc_status_str(completed_work_request[i].status) << endl;
continue;
}
uint64_t id = completed_work_request[i].wr_id;
if(id) {
Synchronizer *synchronizer = reinterpret_cast<Synchronizer *>(id);
synchronizer->decrease();
}
}
return number_completed;
}
inline int poll_recv_completion_queue(ReceivedMessageInformation *completed_work_request, int quantity = 1) noexcept {
int number_completed = ibv_poll_cq(recv_completion_queue, quantity, reinterpret_cast<ibv_wc *>(completed_work_request));
for(int i = 0; i < number_completed; i++) {
if(completed_work_request[i].status != IBV_WC_SUCCESS) {
cerr << "Error receiving data with ID " << completed_work_request[i].wr_id << ": " << ibv_wc_status_str(completed_work_request[i].status) << endl;
continue;
}
}
return number_completed;
}
inline int flush_send_completion_queue() noexcept {
SentMessageInformation sent_message_information[128];
int total_number_completed = 0;
int number_completed = 0;
while((number_completed = poll_send_completion_queue(sent_message_information, 128)) > 0) {
total_number_completed += number_completed;
}
return total_number_completed;
}
};
class IBContext {
public:
int device_number;
int port_number;
struct ibv_context *context;
struct ibv_pd *protection_domain;
struct ibv_port_attr port_information;
struct ibv_device_attr device_information;
struct ibv_comp_channel *recv_event_channel;
bool create_completion_channel;
IBCompletionQueues *completion_queues;
bool private_completion_queues;
struct ibv_srq *shared_receive_queue;
bool initialized;
static int get_device_quantity();
static int get_port_quantity(int device_number);
IBContext(int device_number = 0, int port_number = 1, bool private_completion_queues = false, bool create_completion_channel = false);
virtual ~IBContext();
inline bool post_receive(RDMAMemory *memory) const noexcept {
return post_receive(memory->get_buffer(), memory->get_size(), memory->get_local_key(), reinterpret_cast<uint64_t>(memory));
}
inline bool post_receive(RDMAMemory &memory) const noexcept {
return post_receive(memory.get_buffer(), memory.get_size(), memory.get_local_key(), reinterpret_cast<uint64_t>(&memory));
}
inline bool post_receive(void *buffer, uint32_t buffer_size, uint32_t buffer_key) const noexcept {
return post_receive(buffer, buffer_size, buffer_key, reinterpret_cast<uint64_t>(buffer));
}
inline bool post_receive(void *buffer, uint32_t buffer_size, uint32_t buffer_key, uint64_t request_id) const noexcept {
struct ibv_sge sge_entry;
memset(&sge_entry, 0, sizeof(struct ibv_sge));
sge_entry.addr = reinterpret_cast<uint64_t>(buffer);
sge_entry.length = buffer_size;
sge_entry.lkey = buffer_key;
struct ibv_recv_wr work_request;
memset(&work_request, 0, sizeof(struct ibv_recv_wr));
work_request.wr_id = request_id;
work_request.sg_list = &sge_entry;
work_request.num_sge = 1;
struct ibv_recv_wr *bad_work_request;
int return_value = ibv_post_srq_recv(shared_receive_queue, &work_request, &bad_work_request);
if(return_value != 0) {
cerr << "Error posting recv " << return_value << endl;
return false;
}
return true;
}
};
class IBQueuePair {
public:
struct ibv_qp *queue_pair;
IBCompletionQueues *completion_queues;
bool private_completion_queues;
struct ibv_comp_channel *recv_event_channel;
bool create_completion_channel;
bool initialized;
IBQueuePair(IBContext *context, bool private_completion_queues = false, bool create_completion_channel = false);
virtual ~IBQueuePair();
bool setup(IBContext *context, uint32_t peer_queue_pair, uint16_t peer_lid);
bool reset();
inline bool post_send(RDMAMemory *memory, uint32_t offset, uint32_t size, uint64_t flags = 0, uint32_t *immediate_value = 0, Synchronizer *synchronizer = nullptr) const noexcept {
struct ibv_sge sge_entry;
memset(&sge_entry, 0, sizeof(struct ibv_sge));
sge_entry.addr = reinterpret_cast<uint64_t>(memory->get_buffer(offset));
sge_entry.length = size;
sge_entry.lkey = memory->get_local_key();
struct ibv_send_wr work_request;
memset(&work_request, 0, sizeof(struct ibv_send_wr));
work_request.wr_id = reinterpret_cast<uint64_t>(synchronizer);
work_request.sg_list = &sge_entry;
work_request.num_sge = 1;
work_request.opcode = IBV_WR_SEND;
if(immediate_value != nullptr) {
work_request.opcode = IBV_WR_SEND_WITH_IMM;
work_request.imm_data = htonl(*immediate_value);
}
if(synchronizer != nullptr) {
flags |= IBV_SEND_SIGNALED;
}
work_request.send_flags = flags;
struct ibv_send_wr *bad_work_request;
int return_value = ibv_post_send(queue_pair, &work_request, &bad_work_request);
if(return_value != 0) {
cerr << "Error posting send " << return_value << endl;
return false;
}
return true;
}
inline bool post_send_multiple(RDMAMemory **memories, uint32_t *offsets, uint32_t *sizes, uint32_t amount_buffers, uint64_t flags = 0, uint32_t *immediate_value = 0, Synchronizer *synchronizer = nullptr) const noexcept {
struct ibv_sge sge_entries[amount_buffers];
memset(&sge_entries, 0, amount_buffers * sizeof(struct ibv_sge));
for(int i = 0; i < amount_buffers; i++) {
sge_entries[i].addr = reinterpret_cast<uint64_t>(memories[i]->get_buffer(offsets[i]));
sge_entries[i].length = sizes[i];
sge_entries[i].lkey = memories[i]->get_local_key();
}
struct ibv_send_wr work_request;
memset(&work_request, 0, sizeof(struct ibv_send_wr));
work_request.wr_id = reinterpret_cast<uint64_t>(synchronizer);
work_request.sg_list = sge_entries;
work_request.num_sge = amount_buffers;
work_request.opcode = IBV_WR_SEND;
if(immediate_value != nullptr) {
work_request.opcode = IBV_WR_SEND_WITH_IMM;
work_request.imm_data = htonl(*immediate_value);
}
if(synchronizer != nullptr) {
flags |= IBV_SEND_SIGNALED;
}
work_request.send_flags = flags;
struct ibv_send_wr *bad_work_request;
int return_value = ibv_post_send(queue_pair, &work_request, &bad_work_request);
if(return_value != 0) {
cerr << "Error posting send " << return_value << endl;
return false;
}
return true;
}
inline bool post_rdma_read(RDMAMemory *memory, uint32_t offset, uint32_t size, const RDMAMemoryLocator *remote_memory_locator, uint64_t flags = 0, Synchronizer *synchronizer = nullptr) const noexcept {
struct ibv_sge sge_entry;
memset(&sge_entry, 0, sizeof(struct ibv_sge));
sge_entry.addr = reinterpret_cast<uint64_t>(memory->get_buffer(offset));
sge_entry.length = size;
sge_entry.lkey = memory->get_local_key();
struct ibv_send_wr work_request;
memset(&work_request, 0, sizeof(struct ibv_send_wr));
work_request.wr_id = reinterpret_cast<uint64_t>(synchronizer);
work_request.sg_list = &sge_entry;
work_request.num_sge = 1;
work_request.opcode = IBV_WR_RDMA_READ;
if(synchronizer != nullptr) {
flags |= IBV_SEND_SIGNALED;
}
work_request.send_flags = flags;
work_request.wr.rdma.remote_addr = reinterpret_cast<uint64_t>(remote_memory_locator->get_buffer());
work_request.wr.rdma.rkey = remote_memory_locator->get_remote_key();
struct ibv_send_wr *bad_work_request;
int return_value = ibv_post_send(queue_pair, &work_request, &bad_work_request);
if(return_value != 0) {
cerr << "Error posting send " << return_value << endl;
return false;
}
return true;
}
inline bool post_rdma_read_multiple(RDMAMemory **memories, uint64_t *offsets, uint32_t *sizes, uint32_t amount_buffers, const RDMAMemoryLocator *remote_memory_locator, uint64_t flags = 0, Synchronizer *synchronizer = nullptr) const noexcept {
struct ibv_sge sge_entries[amount_buffers];
memset(&sge_entries, 0, amount_buffers * sizeof(struct ibv_sge));
for(int i = 0; i < amount_buffers; i++) {
sge_entries[i].addr = reinterpret_cast<uint64_t>(memories[i]->get_buffer(offsets[i]));
sge_entries[i].length = sizes[i];
sge_entries[i].lkey = memories[i]->get_local_key();
}
struct ibv_send_wr work_request;
memset(&work_request, 0, sizeof(struct ibv_send_wr));
work_request.wr_id = reinterpret_cast<uint64_t>(synchronizer);
work_request.sg_list = sge_entries;
work_request.num_sge = amount_buffers;
work_request.opcode = IBV_WR_RDMA_READ;
if(synchronizer != nullptr) {
flags |= IBV_SEND_SIGNALED;
}
work_request.send_flags = flags;
work_request.wr.rdma.remote_addr = reinterpret_cast<uint64_t>(remote_memory_locator->get_buffer());
work_request.wr.rdma.rkey = remote_memory_locator->get_remote_key();
struct ibv_send_wr *bad_work_request;
int return_value = ibv_post_send(queue_pair, &work_request, &bad_work_request);
if(return_value != 0) {
cerr << "Error posting send " << return_value << endl;
return false;
}
return true;
}
inline bool post_rdma_write(RDMAMemory *memory, uint32_t offset, uint32_t size, const RDMAMemoryLocator *remote_memory_locator, uint64_t flags = 0, uint32_t *immediate_value = 0, Synchronizer *synchronizer = nullptr) const noexcept {
struct ibv_sge sge_entry;
memset(&sge_entry, 0, sizeof(struct ibv_sge));
sge_entry.addr = reinterpret_cast<uint64_t>(memory->get_buffer(offset));
sge_entry.length = size;
sge_entry.lkey = memory->get_local_key();
struct ibv_send_wr work_request;
memset(&work_request, 0, sizeof(struct ibv_send_wr));
work_request.wr_id = reinterpret_cast<uint64_t>(synchronizer);
work_request.sg_list = &sge_entry;
work_request.num_sge = 1;
work_request.opcode = IBV_WR_RDMA_WRITE;
if(immediate_value != nullptr) {
work_request.opcode = IBV_WR_RDMA_WRITE_WITH_IMM;
work_request.imm_data = htonl(*immediate_value);
}
if(synchronizer != nullptr) {
flags |= IBV_SEND_SIGNALED;
}
work_request.send_flags = flags;
work_request.wr.rdma.remote_addr = reinterpret_cast<uint64_t>(remote_memory_locator->get_buffer());
work_request.wr.rdma.rkey = remote_memory_locator->get_remote_key();
struct ibv_send_wr *bad_work_request;
int return_value = ibv_post_send(queue_pair, &work_request, &bad_work_request);
if(return_value != 0) {
cerr << "Error posting send " << return_value << endl;
return false;
}
return true;
}
inline bool post_rdma_write_multiple(RDMAMemory **memories, uint64_t *offsets, uint32_t *sizes, uint32_t amount_buffers, const RDMAMemoryLocator *remote_memory_locator, uint64_t flags = 0, uint32_t *immediate_value = 0, Synchronizer *synchronizer = nullptr) const noexcept {
struct ibv_sge sge_entries[amount_buffers];
memset(&sge_entries, 0, amount_buffers * sizeof(struct ibv_sge));
for(int i = 0; i < amount_buffers; i++) {
sge_entries[i].addr = reinterpret_cast<uint64_t>(memories[i]->get_buffer(offsets[i]));
sge_entries[i].length = sizes[i];
sge_entries[i].lkey = memories[i]->get_local_key();
}
struct ibv_send_wr work_request;
memset(&work_request, 0, sizeof(struct ibv_send_wr));
work_request.wr_id = reinterpret_cast<uint64_t>(synchronizer);
work_request.sg_list = sge_entries;
work_request.num_sge = amount_buffers;
work_request.opcode = IBV_WR_RDMA_WRITE;
if(immediate_value != nullptr) {
work_request.opcode = IBV_WR_RDMA_WRITE_WITH_IMM;
work_request.imm_data = htonl(*immediate_value);
}
if(synchronizer != nullptr) {
flags |= IBV_SEND_SIGNALED;
}
work_request.send_flags = flags;
work_request.wr.rdma.remote_addr = reinterpret_cast<uint64_t>(remote_memory_locator->get_buffer());
work_request.wr.rdma.rkey = remote_memory_locator->get_remote_key();
struct ibv_send_wr *bad_work_request;
int return_value = ibv_post_send(queue_pair, &work_request, &bad_work_request);
if(return_value != 0) {
cerr << "Error posting send " << return_value << endl;
return false;
}
return true;
}
inline bool post_cas(RDMAMemory *memory, uint32_t offset, uint32_t size, const RDMAMemoryLocator *remote_memory_locator, uint64_t expected, uint64_t changed, uint64_t flags = 0, Synchronizer *synchronizer = nullptr) const noexcept {
struct ibv_sge sge_entry;
memset(&sge_entry, 0, sizeof(struct ibv_sge));
sge_entry.addr = reinterpret_cast<uint64_t>(memory->get_buffer(offset));
sge_entry.length = size;
sge_entry.lkey = memory->get_local_key();
struct ibv_send_wr work_request;
memset(&work_request, 0, sizeof(struct ibv_send_wr));
work_request.wr_id = reinterpret_cast<uint64_t>(synchronizer);
work_request.sg_list = &sge_entry;
work_request.num_sge = 1;
work_request.opcode = IBV_WR_ATOMIC_CMP_AND_SWP;
if(synchronizer != nullptr) {
flags |= IBV_SEND_SIGNALED;
}
work_request.send_flags = flags;
work_request.wr.atomic.remote_addr = reinterpret_cast<uint64_t>(remote_memory_locator->get_buffer());
work_request.wr.atomic.rkey = remote_memory_locator->get_remote_key();
work_request.wr.atomic.compare_add = expected;
work_request.wr.atomic.swap = changed;
struct ibv_send_wr *bad_work_request;
int return_value = ibv_post_send(queue_pair, &work_request, &bad_work_request);
if(return_value != 0) {
cerr << "Error posting send " << return_value << endl;
return false;
}
return true;
}
inline bool post_fetch_increment(RDMAMemory *memory, uint32_t offset, uint32_t size, const RDMAMemoryLocator *remote_memory_locator, uint64_t increment, uint64_t flags = 0, Synchronizer *synchronizer = nullptr) const noexcept {
struct ibv_sge sge_entry;
memset(&sge_entry, 0, sizeof(struct ibv_sge));
sge_entry.addr = reinterpret_cast<uint64_t>(memory->get_buffer(offset));
sge_entry.length = size;
sge_entry.lkey = memory->get_local_key();
struct ibv_send_wr work_request;
memset(&work_request, 0, sizeof(struct ibv_send_wr));
work_request.wr_id = reinterpret_cast<uint64_t>(synchronizer);
work_request.sg_list = &sge_entry;
work_request.num_sge = 1;
work_request.opcode = IBV_WR_ATOMIC_FETCH_AND_ADD;
if(synchronizer != nullptr) {
flags |= IBV_SEND_SIGNALED;
}
work_request.send_flags = flags;
work_request.wr.atomic.remote_addr = reinterpret_cast<uint64_t>(remote_memory_locator->get_buffer());
work_request.wr.atomic.rkey = remote_memory_locator->get_remote_key();
work_request.wr.atomic.compare_add = increment;
struct ibv_send_wr *bad_work_request;
int return_value = ibv_post_send(queue_pair, &work_request, &bad_work_request);
if(return_value != 0) {
cerr << "Error posting send " << return_value << endl;
return false;
}
return true;
}
inline bool post_receive(RDMAMemory *memory) const noexcept {
return post_receive(memory->get_buffer(), memory->get_size(), memory->get_local_key(), reinterpret_cast<uint64_t>(memory));
}
inline bool post_receive(RDMAMemory &memory) const noexcept {
return post_receive(memory.get_buffer(), memory.get_size(), memory.get_local_key(), reinterpret_cast<uint64_t>(&memory));
}
inline bool post_receive(void *buffer, uint32_t buffer_size, uint32_t buffer_key) const noexcept {
return post_receive(buffer, buffer_size, buffer_key, reinterpret_cast<uint64_t>(buffer));
}
inline bool post_receive(void *buffer, uint32_t buffer_size, uint32_t buffer_key, uint64_t request_id) const noexcept {
struct ibv_sge sge_entry;
memset(&sge_entry, 0, sizeof(struct ibv_sge));
sge_entry.addr = reinterpret_cast<uint64_t>(buffer);
sge_entry.length = buffer_size;
sge_entry.lkey = buffer_key;
struct ibv_recv_wr work_request;
memset(&work_request, 0, sizeof(struct ibv_recv_wr));
work_request.wr_id = request_id;
work_request.sg_list = &sge_entry;
work_request.num_sge = 1;
struct ibv_recv_wr *bad_work_request;
int return_value = ibv_post_recv(queue_pair, &work_request, &bad_work_request);
if(return_value != 0) {
cerr << "Error posting recv " << return_value << endl;
return false;
}
return true;
}
};
template<uint64_t FLUSH_INTERVAL = 15360, uint64_t THREAD_FLUSH_INTERVAL = 128>
struct alignas(64) IBTransmitter {
atomic<uint64_t> outgoing_batches;
atomic<uint64_t> completed_flushes;
atomic<uint64_t> completed_timestamp;
IBContext *context;
IBQueuePair *queue_pair;
IBCompletionQueues *completion_queues;
uint64_t operation_tag;
IBTransmitter(IBContext *context, IBQueuePair *queue_pair, uint64_t operation_tag): context{context}, queue_pair{queue_pair}, operation_tag{operation_tag}, outgoing_batches{FLUSH_INTERVAL}, completed_flushes{1}, completed_timestamp{1} {
if(context->completion_queues) {
completion_queues = context->completion_queues;
}
else {
completion_queues = queue_pair->completion_queues;
}
}
inline uint64_t update_timestamp(uint64_t new_completed_timestamp) {
uint64_t observed = completed_timestamp;
uint64_t proposed = new_completed_timestamp;
while(observed < proposed) {
if(completed_timestamp.compare_exchange_strong(observed, proposed)) {
return proposed;
}
}
return observed;
}
inline uint64_t update_flush_counters(uint64_t flush_number, uint64_t new_completed_timestamp) {
uint64_t observed = completed_flushes;
uint64_t proposed = flush_number;
if(observed + 1 == proposed) {
completed_flushes.compare_exchange_strong(observed, proposed);
}
return update_timestamp(new_completed_timestamp);
}
inline bool send_batches(RDMAMemory *memory, uint64_t offset, uint64_t size, uint64_t flags, uint32_t *immediate_value, Synchronizer *synchronizer) noexcept {
thread_local uint64_t thread_outgoing = 0;
uint64_t number_batches = size / INT32_MAX;
if(size % INT32_MAX != 0) {
number_batches++;
}
uint32_t batch_size = INT32_MAX;
for(uint64_t batch = 0; batch < number_batches; batch++) {
if(batch == number_batches - 1) {
batch_size = size % INT32_MAX;
}
uint64_t snapshot_outgoing_batches = outgoing_batches.fetch_add(1, std::memory_order::memory_order_relaxed) + 1;
uint64_t flush_number = (snapshot_outgoing_batches / FLUSH_INTERVAL);
if((thread_outgoing++ % THREAD_FLUSH_INTERVAL) == 0 || completed_flushes < flush_number) {
Synchronizer flush_synchronizer{1};
queue_pair->post_send(memory, offset, batch_size, flags, immediate_value, &flush_synchronizer);
while(flush_synchronizer.get_number_operations_left() > 0) {
completion_queues->flush_send_completion_queue();
}
if(batch == number_batches - 1 && synchronizer) {
synchronizer->decrease();
}
update_flush_counters(flush_number, snapshot_outgoing_batches);
}
else {
queue_pair->post_send(memory, offset, batch_size, flags, immediate_value, (batch == number_batches - 1) ? synchronizer : nullptr);
}
offset += batch_size;
}
memory->operation_tag = operation_tag;
memory->operation_timestamp = outgoing_batches.load(std::memory_order::memory_order_relaxed);
return true;
}
inline bool rdma_write_batches(RDMAMemory *memory, uint64_t offset, uint64_t size, const RDMAMemoryLocator *remote_memory_locator, uint64_t flags, uint32_t *immediate_value, Synchronizer *synchronizer) noexcept {
thread_local uint64_t thread_outgoing = 0;
uint64_t number_batches = size / INT32_MAX;
if(size % INT32_MAX != 0) {
number_batches++;
}
RDMAMemoryLocator adjusted_memory_locator(*remote_memory_locator);
uint32_t batch_size = INT32_MAX;
for(uint64_t batch = 0; batch < number_batches; batch++) {
if(batch == number_batches - 1) {
batch_size = size % INT32_MAX;
}
uint64_t snapshot_outgoing_batches = outgoing_batches.fetch_add(1, std::memory_order::memory_order_relaxed) + 1;
uint64_t flush_number = (snapshot_outgoing_batches / FLUSH_INTERVAL);
if((thread_outgoing++ % THREAD_FLUSH_INTERVAL) == 0 || completed_flushes < flush_number) {
Synchronizer flush_synchronizer{1};
queue_pair->post_rdma_write(memory, offset, batch_size, &adjusted_memory_locator, flags, immediate_value, &flush_synchronizer);
while(flush_synchronizer.get_number_operations_left() > 0) {
completion_queues->flush_send_completion_queue();
}
if(batch == number_batches - 1 && synchronizer) {
synchronizer->decrease();
}
update_flush_counters(flush_number, snapshot_outgoing_batches);
}
else {
queue_pair->post_rdma_write(memory, offset, batch_size, &adjusted_memory_locator, flags, immediate_value, (batch == number_batches - 1) ? synchronizer : nullptr);
}
offset += batch_size;
adjusted_memory_locator += batch_size;
}
memory->operation_tag = operation_tag;
memory->operation_timestamp = outgoing_batches.load(std::memory_order::memory_order_relaxed);
return true;
}
inline bool rdma_read_batches(RDMAMemory *memory, uint64_t offset, uint64_t size, const RDMAMemoryLocator *remote_memory_locator, uint64_t flags, Synchronizer *synchronizer) noexcept {
thread_local uint64_t thread_outgoing = 0;
uint64_t number_batches = size / INT32_MAX;
if(size % INT32_MAX != 0) {
number_batches++;
}
RDMAMemoryLocator adjusted_memory_locator(*remote_memory_locator);
uint32_t batch_size = INT32_MAX;
for(uint64_t batch = 0; batch < number_batches; batch++) {
if(batch == number_batches - 1) {
batch_size = size % INT32_MAX;
}
uint64_t snapshot_outgoing_batches = outgoing_batches.fetch_add(1, std::memory_order::memory_order_relaxed) + 1;
uint64_t flush_number = (snapshot_outgoing_batches / FLUSH_INTERVAL);
if((thread_outgoing++ % THREAD_FLUSH_INTERVAL) == 0 || completed_flushes < flush_number) {
Synchronizer flush_synchronizer{1};
queue_pair->post_rdma_read(memory, offset, batch_size, &adjusted_memory_locator, flags, &flush_synchronizer);
while(flush_synchronizer.get_number_operations_left() > 0) {
completion_queues->flush_send_completion_queue();
}
if(batch == number_batches - 1 && synchronizer) {
synchronizer->decrease();
}
update_flush_counters(flush_number, snapshot_outgoing_batches);
}
else {
queue_pair->post_rdma_read(memory, offset, batch_size, &adjusted_memory_locator, flags, (batch == number_batches - 1) ? synchronizer : nullptr);
}
offset += batch_size;
adjusted_memory_locator += batch_size;
}
memory->operation_tag = operation_tag;
memory->operation_timestamp = outgoing_batches.load(std::memory_order::memory_order_relaxed);
return true;
}
inline bool send(RDMAMemory *memory, uint64_t offset, int32_t size, uint64_t flags = 0, uint32_t *immediate_value = 0, Synchronizer *synchronizer = nullptr) noexcept {
thread_local uint64_t thread_outgoing = 0;
uint64_t snapshot_outgoing_batches = outgoing_batches.fetch_add(1, std::memory_order::memory_order_relaxed) + 1;
uint64_t flush_number = (snapshot_outgoing_batches / FLUSH_INTERVAL);
if((thread_outgoing++ % THREAD_FLUSH_INTERVAL) == 0 || completed_flushes < flush_number) {
Synchronizer flush_synchronizer{1};
queue_pair->post_send(memory, offset, size, flags, immediate_value, &flush_synchronizer);
while(flush_synchronizer.get_number_operations_left() > 0) {
completion_queues->flush_send_completion_queue();
}