-
Notifications
You must be signed in to change notification settings - Fork 7
/
comm.h
826 lines (782 loc) · 31.8 KB
/
comm.h
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
template <typename T>
class Comm {
public :
// IMPLEMENTATION LIBRARY
const library lib;
// STATS
int benchid;
int numcomm = 0;
// REGISTRY
int numsend;
int numrecv;
std::vector<T*> sendbuf;
std::vector<T*> recvbuf;
std::vector<int> sendproc;
std::vector<int> recvproc;
std::vector<size_t> sendcount;
std::vector<size_t> recvcount;
std::vector<size_t> sendoffset;
std::vector<size_t> recvoffset;
// MEMORY
std::vector<T*> buffer_list;
std::vector<size_t> buffer_count;
// MPI
std::vector<MPI_Request> sendrequest;
std::vector<MPI_Request> recvrequest;
// XCCL
#if defined CAP_NCCL && defined PORT_CUDA
cudaStream_t stream_nccl;
#elif defined CAP_NCCL && defined PORT_HIP
hipStream_t stream_nccl;
#elif defined CAP_ONECCL
ccl::stream *stream_ccl;
#endif
// IPC
std::vector<T*> remotebuf;
std::vector<size_t> remoteoffset;
std::vector<int> ack_sender;
std::vector<int> ack_recver;
#ifdef PORT_CUDA
std::vector<cudaStream_t> stream_ipc;
#elif defined PORT_HIP
std::vector<hipStream_t> stream_ipc;
#elif defined PORT_SYCL
std::vector<sycl::queue> q_ipc;
#endif
// IPC ZE
// #define IPC_ze
#ifdef IPC_ze
std::vector<ze_command_list_handle_t> command_list;
std::vector<ze_command_queue_handle_t> command_queue;
bool command_list_closed = false;
int ordinal2index = 0;
#endif
Comm(library lib);
void free();
void add(T *sendbuf, size_t sendoffset, T *recvbuf, size_t recvoffset, size_t count, int sendid, int recvid);
void add(T *sendbuf, T *recvbuf, size_t count, int sendid, int recvid);
void add_lazy(size_t count, int sendid, int recvid);
void pyadd(pyalloc<T> sendbuf, size_t sendoffset, pyalloc<T> recvbuf, size_t recvoffset, size_t count, int sendid, int recvid);
void start();
void wait();
void measure(int warmup, int numiter);
void measure(int warmup, int numiter, size_t data);
void getMatrix(std::vector<size_t> &matrix);
void report();
void allocate(T *&buffer, size_t n);
void allocate(T *&buffer, size_t n, int i);
#include "util.h"
};
template <typename T>
Comm<T>::Comm(library lib) : lib(lib) {
benchid = numbench;
numbench++;
int init_mpi;
MPI_Initialized(&init_mpi);
if(!init_mpi_comm) {
if(!init_mpi) {
MPI_Init(NULL, NULL);
}
MPI_Comm_dup(MPI_COMM_WORLD, &comm_mpi); // CREATE SEPARATE COMMUNICATOR EXPLICITLY
MPI_Comm_rank(comm_mpi, &myid);
MPI_Comm_size(comm_mpi, &numproc);
setup_gpu();
init_mpi_comm = true;
if(myid == printid) {
if(!init_mpi)
printf("#################### MPI IS INITIALIZED, it is user's responsibility to finalize.\n");
printf("******************** MPI COMMUNICATOR IS CREATED\n");
}
}
numsend = 0;
numrecv = 0;
if(myid == printid) {
printf("printid: %d Create Bench %d with %d processors\n", printid, benchid, numproc);
printf(" Port: ");
#ifdef PORT_CUDA
printf("CUDA ");
#elif defined PORT_HIP
printf("HIP, ");
#elif defined PORT_SYCL
printf("SYCL, ");
#else
printf("CPU, ");
#endif
printf("Library: ");
print_lib(lib);
printf("\n");
}
if(lib == XCCL) {
#ifdef CAP_NCCL
if(!init_nccl_comm) {
ncclUniqueId id;
if(myid == 0)
ncclGetUniqueId(&id);
MPI_Bcast(&id, sizeof(id), MPI_BYTE, 0, comm_mpi);
ncclCommInitRank(&comm_nccl, numproc, id, myid);
if(myid == printid)
printf("******************** NCCL COMMUNICATOR IS CREATED\n");
init_nccl_comm = true;
}
#ifdef PORT_CUDA
cudaStreamCreate(&stream_nccl);
#elif defined PORT_HIP
hipStreamCreate(&stream_nccl);
#endif
#elif defined CAP_ONECCL
if(!init_ccl_comm) {
/* initialize ccl */
ccl::init();
/* create kvs */
ccl::shared_ptr_class<ccl::kvs> kvs;
ccl::kvs::address_type main_addr;
if (myid == 0) {
kvs = ccl::create_main_kvs();
main_addr = kvs->get_address();
MPI_Bcast((void *)main_addr.data(), main_addr.size(), MPI_BYTE, 0, comm_mpi);
}
else {
MPI_Bcast((void *)main_addr.data(), main_addr.size(), MPI_BYTE, 0, comm_mpi);
kvs = ccl::create_kvs(main_addr);
}
/* create communicator */
auto dev = ccl::create_device(CommBench::q.get_device());
auto ctx = ccl::create_context(CommBench::q.get_context());
comm_ccl = new ccl::communicator(ccl::create_communicator(numproc, myid, dev, ctx, kvs));
init_ccl_comm = true;
if(myid == printid)
printf("******************** ONECCL COMMUNICATOR IS CREATED\n");
stream_ccl = new ccl::stream(ccl::create_stream(CommBench::q));
}
#endif
}
if(lib == IPC || lib == IPC_get) {
#ifdef IPC_ze
ze_context_handle_t hContext = sycl::get_native<sycl::backend::ext_oneapi_level_zero>(q.get_context());
ze_device_handle_t hDevice = sycl::get_native<sycl::backend::ext_oneapi_level_zero>(q.get_device());
ze_device_properties_t device_properties = {ZE_STRUCTURE_TYPE_DEVICE_PROPERTIES, nullptr};
zeDeviceGetProperties(hDevice, &device_properties);
uint32_t numQueueGroups = 0;
zeDeviceGetCommandQueueGroupProperties(hDevice, &numQueueGroups, nullptr);
std::vector<ze_command_queue_group_properties_t> queueProperties(numQueueGroups);
for (ze_command_queue_group_properties_t &prop : queueProperties)
prop = {ZE_STRUCTURE_TYPE_COMMAND_QUEUE_GROUP_PROPERTIES, nullptr};
zeDeviceGetCommandQueueGroupProperties(hDevice, &numQueueGroups, queueProperties.data());
int n_commands_lists = 0;
if(myid == printid)
printf("device descovery:\n");
for (uint32_t i = 0; i < numQueueGroups; i++) {
bool isCompute = false;
bool isCopy = false;
if (queueProperties[i].flags & ZE_COMMAND_QUEUE_GROUP_PROPERTY_FLAG_COMPUTE)
isCompute = true;
if ((queueProperties[i].flags & ZE_COMMAND_QUEUE_GROUP_PROPERTY_FLAG_COPY))
isCopy = true;
if(myid == printid)
printf("group %d isCompute %d isCopy %d\n", i, isCompute, isCopy);
for (uint32_t j = 0; j < queueProperties[i].numQueues; j++) {
if(myid == printid)
printf(" queue: %d\n", j);
n_commands_lists++;
}
}
{
int q = 0;
for(int ord = 0; ord < numQueueGroups; ord++) {
for(int ind = 0; ind < queueProperties[ord].numQueues; ind++) {
ze_command_queue_desc_t cmdQueueDesc = {};
cmdQueueDesc.stype = ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC;
cmdQueueDesc.ordinal = ord;
cmdQueueDesc.index = ind;
ze_command_list_desc_t command_list_description{};
command_list_description.stype = ZE_STRUCTURE_TYPE_COMMAND_LIST_DESC;
command_list_description.commandQueueGroupOrdinal = ord;
command_list_description.pNext = nullptr;
command_queue.push_back(ze_command_queue_handle_t());
command_list.push_back(ze_command_list_handle_t());
zeCommandQueueCreate(hContext, hDevice, &cmdQueueDesc, &command_queue[q]);
zeCommandListCreate(hContext, hDevice, &command_list_description, &command_list[q]);
q++;
}
}
if(myid == printid)
printf("number of command queues: %ld\n", command_queue.size());
}
#endif
}
}
template <typename T>
void Comm<T>::free() {
for(T *ptr : buffer_list)
CommBench::free(ptr);
buffer_list.clear();
buffer_count.clear();
if(myid == printid)
printf("memory freed.\n");
}
template <typename T>
void Comm<T>::allocate(T *&buffer, size_t count) {
for (int i = 0; i < numproc; i++)
allocate(buffer, count, i);
}
template <typename T>
void Comm<T>::allocate(T *&buffer, size_t count, int i) {
if(myid == i) {
MPI_Send(&count, sizeof(size_t), MPI_BYTE, printid, 0, comm_mpi);
if(count) {
CommBench::allocate(buffer, count);
buffer_list.push_back(buffer);
buffer_count.push_back(count);
MPI_Send(&buffer, sizeof(T*), MPI_BYTE, printid, 0, comm_mpi);
}
}
if(myid == printid) {
MPI_Recv(&count, sizeof(size_t), MPI_BYTE, i, 0, comm_mpi, MPI_STATUS_IGNORE);
if(count) {
T *ptr = nullptr;
MPI_Recv(&ptr, sizeof(T*), MPI_BYTE, i, 0, comm_mpi, MPI_STATUS_IGNORE);
printf("Bench %d proc %d allocate %p count %ld (", benchid, i, ptr, count);
print_data(count * sizeof(T));
printf(")\n");
}
}
}
template <typename T>
void Comm<T>::add_lazy(size_t count, int sendid, int recvid) {
T *sendbuf;
T *recvbuf;
allocate(sendbuf, count, sendid);
allocate(recvbuf, count, recvid);
add(sendbuf, 0, recvbuf, 0, count, sendid, recvid);
}
template <typename T>
void Comm<T>::add(T *sendbuf, T *recvbuf, size_t count, int sendid, int recvid) {
add(sendbuf, 0, recvbuf, 0, count, sendid, recvid);
}
template <typename T>
void Comm<T>::add(T *sendbuf, size_t sendoffset, T *recvbuf, size_t recvoffset, size_t count, int sendid, int recvid) {
if(count == 0) {
if(myid == printid)
printf("Bench %d communication (%d->%d) count = 0 (skipped)\n", benchid, sendid, recvid);
return;
}
MPI_Barrier(comm_mpi); // THIS IS NECESSARY FOR AURORA
// REPORT
if(printid > -1) {
if(myid == sendid) {
MPI_Send(&sendbuf, sizeof(T*), MPI_BYTE, printid, 0, comm_mpi);
MPI_Send(&sendoffset, sizeof(size_t), MPI_BYTE, printid, 0, comm_mpi);
}
if(myid == recvid) {
MPI_Send(&recvbuf, sizeof(T*), MPI_BYTE, printid, 0, comm_mpi);
MPI_Send(&recvoffset, sizeof(size_t), MPI_BYTE, printid, 0, comm_mpi);
}
if(myid == printid) {
T* sendbuf_sendid;
T* recvbuf_recvid;
size_t sendoffset_sendid;
size_t recvoffset_recvid;
MPI_Recv(&sendbuf_sendid, sizeof(T*), MPI_BYTE, sendid, 0, comm_mpi, MPI_STATUS_IGNORE);
MPI_Recv(&sendoffset_sendid, sizeof(size_t), MPI_BYTE, sendid, 0, comm_mpi, MPI_STATUS_IGNORE);
MPI_Recv(&recvbuf_recvid, sizeof(T*), MPI_BYTE, recvid, 0, comm_mpi, MPI_STATUS_IGNORE);
MPI_Recv(&recvoffset_recvid, sizeof(size_t), MPI_BYTE, recvid, 0, comm_mpi, MPI_STATUS_IGNORE);
printf("Bench %d comm %d (%d->%d) sendbuf %p sendoffset %zu recvbuf %p recvoffset %zu count %zu (", benchid, numcomm, sendid, recvid, sendbuf_sendid, sendoffset_sendid, recvbuf_recvid, recvoffset_recvid, count);
print_data(count * sizeof(T));
printf(") ");
print_lib(lib);
printf("\n");
}
}
numcomm++;
#ifdef IPC_ze
// queue selection for copy engines
int queue = -1; // invalid queue
if((lib == IPC) || (lib == IPC_get)) {
if(sendid / 2 == recvid / 2) {
// in the same device
if(sendid == recvid)
queue = 0; // self
else
queue = 1; // across tiles in the same device
}
else {
// tiles across devices
queue = 2 + (ordinal2index % 7); // roundrobin: 2, 3, 4, 5, 6, 7, 8, 2, 3, ...
if(((lib == IPC) && (myid == sendid)) || ((lib == IPC_get) && (myid == recvid)))
ordinal2index++;
}
// REPORT QUEUE
if(printid > -1) {
if(lib == IPC) {
// PUT (SENDER INITIALIZES)
if(myid == sendid)
MPI_Send(&queue, 1, MPI_INT, printid, 0, comm_mpi);
if(myid == printid) {
int queue_sender;
MPI_Recv(&queue_sender, 1, MPI_INT, sendid, 0, comm_mpi, MPI_STATUS_IGNORE);
printf("selected put queue: %d\n", queue_sender);
}
}
if(lib == IPC_get) {
// GET (RECVER INITIALIZES)
if(myid == recvid)
MPI_Send(&queue, 1, MPI_INT, printid, 0, comm_mpi);
if(myid == printid) {
int queue_recver;
MPI_Recv(&queue_recver, 1, MPI_INT, recvid, 0, comm_mpi, MPI_STATUS_IGNORE);
printf("selected get queue: %d\n", queue_recver);
}
}
}
}
#endif
// SENDER DATA STRUCTURES
if(myid == sendid) {
// EXTEND REGISTRY
this->sendbuf.push_back(sendbuf);
this->sendproc.push_back(recvid);
this->sendcount.push_back(count);
this->sendoffset.push_back(sendoffset);
// SETUP CAPABILITY
switch(lib) {
case dummy:
break;
case MPI:
sendrequest.push_back(MPI_Request());
break;
case XCCL:
break;
case IPC:
ack_sender.push_back(int());
remotebuf.push_back(recvbuf);
remoteoffset.push_back(recvoffset);
// CREATE STREAMS
#ifdef PORT_CUDA
stream_ipc.push_back(cudaStream_t());
cudaStreamCreate(&stream_ipc[numsend]);
#elif defined PORT_HIP
stream_ipc.push_back(hipStream_t());
hipStreamCreate(&stream_ipc[numsend]);
#elif defined PORT_SYCL
q_ipc.push_back(sycl::queue(sycl::gpu_selector_v));
#endif
// RECIEVE REMOTE MEMORY HANDLE
if(sendid != recvid) {
int error = -1;
#ifdef PORT_CUDA
cudaIpcMemHandle_t memhandle;
MPI_Recv(&memhandle, sizeof(cudaIpcMemHandle_t), MPI_BYTE, recvid, 0, comm_mpi, MPI_STATUS_IGNORE);
error = cudaIpcOpenMemHandle((void**)&remotebuf[numsend], memhandle, cudaIpcMemLazyEnablePeerAccess);
#elif defined PORT_HIP
hipIpcMemHandle_t memhandle;
MPI_Recv(&memhandle, sizeof(hipIpcMemHandle_t), MPI_BYTE, recvid, 0, comm_mpi, MPI_STATUS_IGNORE);
error = hipIpcOpenMemHandle((void**)&remotebuf[numsend], memhandle, hipIpcMemLazyEnablePeerAccess);
#elif defined PORT_SYCL
ze_ipc_mem_handle_t memhandle;
{
typedef struct { int fd; pid_t pid ; } clone_mem_t;
clone_mem_t what_intel_should_have_done;
MPI_Recv(&what_intel_should_have_done, sizeof(clone_mem_t), MPI_BYTE, recvid, 0, comm_mpi, MPI_STATUS_IGNORE);
int pidfd = syscall(SYS_pidfd_open,what_intel_should_have_done.pid,0);
// int myfd = syscall(SYS_pidfd_getfd,pidfd,what_intel_should_have_done.fd,0);
int myfd = syscall(438,pidfd,what_intel_should_have_done.fd,0);
memcpy((void *)&memhandle,(void *)&myfd,sizeof(int));
}
// MPI_Recv(&memhandle, sizeof(ze_ipc_mem_handle_t), MPI_BYTE, recvid, 0, comm_mpi, MPI_STATUS_IGNORE);
auto zeContext = sycl::get_native<sycl::backend::ext_oneapi_level_zero>(q.get_context());
auto zeDevice = sycl::get_native<sycl::backend::ext_oneapi_level_zero>(q.get_device());
error = zeMemOpenIpcHandle(zeContext, zeDevice, memhandle, 0, (void**)&remotebuf[numsend]);
#endif
if(error)
printf("IpcOpenMemHandle error %d\n", error);
MPI_Recv(&remoteoffset[numsend], sizeof(size_t), MPI_BYTE, recvid, 0, comm_mpi, MPI_STATUS_IGNORE);
}
#ifdef IPC_ze
zeCommandListAppendMemoryCopy(command_list[queue], remotebuf[numsend] + remoteoffset[numsend], this->sendbuf[numsend] + this->sendoffset[numsend], this->sendcount[numsend], nullptr, 0, nullptr);
#endif
break;
case IPC_get:
ack_sender.push_back(int());
// SEND REMOTE MEMORY HANDLE
if(sendid != recvid)
{
int error = -1;
#ifdef PORT_CUDA
cudaIpcMemHandle_t memhandle;
error = cudaIpcGetMemHandle(&memhandle, sendbuf);
MPI_Send(&memhandle, sizeof(cudaIpcMemHandle_t), MPI_BYTE, recvid, 0, comm_mpi);
#elif defined PORT_HIP
hipIpcMemHandle_t memhandle;
error = hipIpcGetMemHandle(&memhandle, sendbuf);
MPI_Send(&memhandle, sizeof(hipIpcMemHandle_t), MPI_BYTE, recvid, 0, comm_mpi);
#elif defined PORT_SYCL
ze_ipc_mem_handle_t memhandle;
auto zeContext = sycl::get_native<sycl::backend::ext_oneapi_level_zero>(q.get_context());
error = zeMemGetIpcHandle(zeContext, sendbuf, &memhandle);
{
typedef struct { int fd; pid_t pid ; } clone_mem_t;
clone_mem_t what_intel_should_have_done;
memcpy((void *)&what_intel_should_have_done.fd,(void *)&memhandle,sizeof(int));
what_intel_should_have_done.pid = getpid();
MPI_Send(&what_intel_should_have_done, sizeof(clone_mem_t), MPI_BYTE, recvid, 0, comm_mpi);
}
// MPI_Send(&memhandle, sizeof(ze_ipc_mem_handle_t), MPI_BYTE, sendid, 0, comm_mpi);
#endif
if(error)
printf("IpcGetMemHandle error %d\n", error);
MPI_Send(&sendoffset, sizeof(size_t), MPI_BYTE, recvid, 0, comm_mpi);
}
break;
case numlib:
break;
} // switch(lib)
numsend++;
}
// RECEIVER DATA STRUCTURES
if(myid == recvid) {
// EXTEND REGISTRY
this->recvbuf.push_back(recvbuf);
this->recvproc.push_back(sendid);
this->recvcount.push_back(count);
this->recvoffset.push_back(recvoffset);
// SETUP LIBRARY
switch(lib) {
case dummy:
break;
case MPI:
recvrequest.push_back(MPI_Request());
break;
case XCCL:
break;
case IPC:
ack_recver.push_back(int());
// SEND REMOTE MEMORY HANDLE
if(sendid != recvid)
{
int error = -1;
#ifdef PORT_CUDA
cudaIpcMemHandle_t memhandle;
error = cudaIpcGetMemHandle(&memhandle, recvbuf);
MPI_Send(&memhandle, sizeof(cudaIpcMemHandle_t), MPI_BYTE, sendid, 0, comm_mpi);
#elif defined PORT_HIP
hipIpcMemHandle_t memhandle;
error = hipIpcGetMemHandle(&memhandle, recvbuf);
MPI_Send(&memhandle, sizeof(hipIpcMemHandle_t), MPI_BYTE, sendid, 0, comm_mpi);
#elif defined PORT_SYCL
ze_ipc_mem_handle_t memhandle;
auto zeContext = sycl::get_native<sycl::backend::ext_oneapi_level_zero>(q.get_context());
error = zeMemGetIpcHandle(zeContext, recvbuf, &memhandle);
{
typedef struct { int fd; pid_t pid ; } clone_mem_t;
clone_mem_t what_intel_should_have_done;
memcpy((void *)&what_intel_should_have_done.fd,(void *)&memhandle,sizeof(int));
what_intel_should_have_done.pid = getpid();
MPI_Send(&what_intel_should_have_done, sizeof(clone_mem_t), MPI_BYTE, sendid, 0, comm_mpi);
}
// MPI_Send(&memhandle, sizeof(ze_ipc_mem_handle_t), MPI_BYTE, sendid, 0, comm_mpi);
#endif
if(error)
printf("IpcGetMemHandle error %d\n", error);
MPI_Send(&recvoffset, sizeof(size_t), MPI_BYTE, sendid, 0, comm_mpi);
}
break;
case IPC_get:
ack_recver.push_back(int());
remotebuf.push_back(sendbuf);
remoteoffset.push_back(sendoffset);
// CREATE STREAMS
#ifdef PORT_CUDA
stream_ipc.push_back(cudaStream_t());
cudaStreamCreate(&stream_ipc[numrecv]);
#elif defined PORT_HIP
stream_ipc.push_back(hipStream_t());
hipStreamCreate(&stream_ipc[numrecv]);
#elif defined PORT_SYCL
q_ipc.push_back(sycl::queue(sycl::gpu_selector_v));
#endif
// RECV REMOTE MEMORY HANDLE
if(sendid != recvid) {
int error = -1;
#ifdef PORT_CUDA
cudaIpcMemHandle_t memhandle;
MPI_Recv(&memhandle, sizeof(cudaIpcMemHandle_t), MPI_BYTE, sendid, 0, comm_mpi, MPI_STATUS_IGNORE);
error = cudaIpcOpenMemHandle((void**)&remotebuf[numrecv], memhandle, cudaIpcMemLazyEnablePeerAccess);
#elif defined PORT_HIP
hipIpcMemHandle_t memhandle;
MPI_Recv(&memhandle, sizeof(hipIpcMemHandle_t), MPI_BYTE, sendid, 0, comm_mpi, MPI_STATUS_IGNORE);
error = hipIpcOpenMemHandle((void**)&remotebuf[numrecv], memhandle, hipIpcMemLazyEnablePeerAccess);
#elif defined PORT_SYCL
ze_ipc_mem_handle_t memhandle;
{
typedef struct { int fd; pid_t pid ; } clone_mem_t;
clone_mem_t what_intel_should_have_done;
MPI_Recv(&what_intel_should_have_done, sizeof(clone_mem_t), MPI_BYTE, sendid, 0, comm_mpi, MPI_STATUS_IGNORE);
int pidfd = syscall(SYS_pidfd_open,what_intel_should_have_done.pid,0);
// int myfd = syscall(SYS_pidfd_getfd,pidfd,what_intel_should_have_done.fd,0);
int myfd = syscall(438,pidfd,what_intel_should_have_done.fd,0);
memcpy((void *)&memhandle,(void *)&myfd,sizeof(int));
}
// MPI_Recv(&memhandle, sizeof(ze_ipc_mem_handle_t), MPI_BYTE, sendid, 0, comm_mpi, MPI_STATUS_IGNORE);
auto zeContext = sycl::get_native<sycl::backend::ext_oneapi_level_zero>(q.get_context());
auto zeDevice = sycl::get_native<sycl::backend::ext_oneapi_level_zero>(q.get_device());
error = zeMemOpenIpcHandle(zeContext, zeDevice, memhandle, 0, (void**)&remotebuf[numrecv]);
#endif
if(error)
printf("IpcOpenMemHandle error %d\n", error);
MPI_Recv(&remoteoffset[numrecv], sizeof(size_t), MPI_BYTE, sendid, 0, comm_mpi, MPI_STATUS_IGNORE);
}
#ifdef IPC_ze
zeCommandListAppendMemoryCopy(command_list[queue], this->recvbuf[numrecv] + this->recvoffset[numrecv], remotebuf[numrecv] + remoteoffset[numrecv], this->recvcount[numrecv], nullptr, 0, nullptr);
#endif
break;
case numlib:
break;
} // switch(lib)
numrecv++;
}
}
template <typename T>
void Comm<T>::measure(int warmup, int numiter) {
long count_total = 0;
for(int send = 0; send < numsend; send++)
count_total += sendcount[send];
MPI_Allreduce(MPI_IN_PLACE, &count_total, 1, MPI_LONG, MPI_SUM, comm_mpi);
measure(warmup, numiter, count_total);
}
template <typename T>
void Comm<T>::measure(int warmup, int numiter, size_t count) {
this->report();
double minTime;
double medTime;
double maxTime;
double avgTime;
CommBench::measure(warmup, numiter, minTime, medTime, maxTime, avgTime, *this);
if(myid == printid) {
size_t data = count * sizeof(T);
printf("data: "); print_data(data); printf("\n");
printf("minTime: %.4e us, %.4e ms/GB, %.4e GB/s\n", minTime * 1e6, minTime / data * 1e12, data / minTime / 1e9);
printf("medTime: %.4e us, %.4e ms/GB, %.4e GB/s\n", medTime * 1e6, medTime / data * 1e12, data / medTime / 1e9);
printf("maxTime: %.4e us, %.4e ms/GB, %.4e GB/s\n", maxTime * 1e6, maxTime / data * 1e12, data / maxTime / 1e9);
printf("avgTime: %.4e us, %.4e ms/GB, %.4e GB/s\n", avgTime * 1e6, avgTime / data * 1e12, data / avgTime / 1e9);
printf("\n");
}
};
template <typename T>
void Comm<T>::report() {
std::vector<size_t> matrix;
getMatrix(matrix);
if(myid == printid) {
printf("\nCommBench %d: ", benchid);
print_lib(lib);
printf(" communication matrix (reciever x sender) nnz: %d\n", numcomm);
for(int recver = 0; recver < numproc; recver++) {
for(int sender = 0; sender < numproc; sender++) {
size_t count = matrix[sender * numproc + recver];
if(count)
// printf("%ld ", count);
printf("1 ");
else
printf(". ");
}
printf("\n");
}
}
long sendTotal = 0;
long recvTotal = 0;
for(int send = 0; send < numsend; send++)
sendTotal += sendcount[send];
for(int recv = 0; recv < numrecv; recv++)
recvTotal += recvcount[recv];
MPI_Allreduce(MPI_IN_PLACE, &sendTotal, 1, MPI_LONG, MPI_SUM, comm_mpi);
MPI_Allreduce(MPI_IN_PLACE, &recvTotal, 1, MPI_LONG, MPI_SUM, comm_mpi);
int numbuf = buffer_list.size();
MPI_Allreduce(MPI_IN_PLACE, &numbuf, 1, MPI_INT, MPI_SUM, comm_mpi);
if(numbuf) {
int total_buff = buffer_list.size();
std::vector<int> total_buffs(numproc);
MPI_Allgather(&total_buff, 1, MPI_INT, total_buffs.data(), 1, MPI_INT, comm_mpi);
MPI_Allreduce(MPI_IN_PLACE, &total_buff, 1, MPI_INT, MPI_SUM, comm_mpi);
long total_count = 0;
for(size_t count : buffer_count)
total_count += count;
std::vector<long> total_counts(numproc);
MPI_Allgather(&total_count, sizeof(long), MPI_BYTE, total_counts.data(), sizeof(long), MPI_BYTE, comm_mpi);
MPI_Allreduce(MPI_IN_PLACE, &total_count, 1, MPI_LONG, MPI_SUM, comm_mpi);
if(myid == printid) {
for(int p = 0; p < numproc; p++) {
printf("proc %d: %d pieces count %ld ", p, total_buffs[p], total_counts[p]);
print_data(total_counts[p] * sizeof(T));
printf("\n");
}
printf("total pieces: %d count %ld ", total_buff, total_count);
print_data(total_count * sizeof(T));
printf("\n");
}
}
if(myid == printid) {
printf("send footprint: %ld ", sendTotal);
print_data(sendTotal * sizeof(T));
printf("\n");
printf("recv footprint: %ld ", recvTotal);
print_data(recvTotal * sizeof(T));
printf("\n");
printf("\n");
}
}
template <typename T>
void Comm<T>::getMatrix(std::vector<size_t> &matrix) {
std::vector<size_t> sendcount_temp(numproc, 0);
std::vector<size_t> recvcount_temp(numproc, 0);
for (int send = 0; send < numsend; send++)
sendcount_temp[sendproc[send]] += sendcount[send];
for (int recv = 0; recv < numrecv; recv++)
recvcount_temp[recvproc[recv]] += recvcount[recv];
std::vector<size_t> sendmatrix(numproc * numproc);
std::vector<size_t> recvmatrix(numproc * numproc);
MPI_Allgather(sendcount_temp.data(), numproc * sizeof(size_t), MPI_BYTE, sendmatrix.data(), numproc * sizeof(size_t), MPI_BYTE, comm_mpi);
MPI_Allgather(recvcount_temp.data(), numproc * sizeof(size_t), MPI_BYTE, recvmatrix.data(), numproc * sizeof(size_t), MPI_BYTE, comm_mpi);
for (int sender = 0; sender < numproc; sender++)
for (int recver = 0; recver < numproc; recver++)
matrix.push_back(sendmatrix[sender * numproc + recver]);
/* if(myid == printid) {
char filename[2048];
sprintf(filename, "matrix_%d.txt", benchid);
FILE *matfile = fopen(filename, "w");
for(int recver = 0; recver < numproc; recver++) {
for(int sender = 0; sender < numproc; sender++)
fprintf(matfile, "%ld ", matrix[sender * numproc + recver]);
fprintf(matfile, "\n");
}
fclose(matfile);
}*/
}
template <typename T>
void Comm<T>::start() {
switch(lib) {
case MPI:
for (int send = 0; send < numsend; send++)
MPI_Isend_c(sendbuf[send] + sendoffset[send], sendcount[send] * sizeof(T), MPI_BYTE, sendproc[send], 0, comm_mpi, &sendrequest[send]);
for (int recv = 0; recv < numrecv; recv++)
MPI_Irecv_c(recvbuf[recv] + recvoffset[recv], recvcount[recv] * sizeof(T), MPI_BYTE, recvproc[recv], 0, comm_mpi, &recvrequest[recv]);
break;
case XCCL:
#ifdef CAP_NCCL
ncclGroupStart();
for(int send = 0; send < numsend; send++)
ncclSend(sendbuf[send] + sendoffset[send], sendcount[send] * sizeof(T), ncclInt8, sendproc[send], comm_nccl, stream_nccl);
for(int recv = 0; recv < numrecv; recv++)
ncclRecv(recvbuf[recv] + recvoffset[recv], recvcount[recv] * sizeof(T), ncclInt8, recvproc[recv], comm_nccl, stream_nccl);
ncclGroupEnd();
#elif defined CAP_ONECCL
for (int i = 0; i < numsend; i++)
ccl::send<T>(sendbuf[i] + sendoffset[i], sendcount[i], sendproc[i], *comm_ccl, *stream_ccl);
for (int i = 0; i < numrecv; i++)
ccl::recv<T>(recvbuf[i] + recvoffset[i], recvcount[i], recvproc[i], *comm_ccl, *stream_ccl);
#endif
break;
case IPC:
for(int recv = 0; recv < numrecv; recv++)
MPI_Send(&ack_recver[recv], 1, MPI_INT, recvproc[recv], 0, comm_mpi);
for(int send = 0; send < numsend; send++) {
MPI_Recv(&ack_sender[send], 1, MPI_INT, sendproc[send], 0, comm_mpi, MPI_STATUS_IGNORE);
#ifdef PORT_CUDA
cudaMemcpyAsync(remotebuf[send] + remoteoffset[send], sendbuf[send] + sendoffset[send], sendcount[send] * sizeof(T), cudaMemcpyDeviceToDevice, stream_ipc[send]);
#elif defined PORT_HIP
hipMemcpyAsync(remotebuf[send] + remoteoffset[send], sendbuf[send] + sendoffset[send], sendcount[send] * sizeof(T), hipMemcpyDeviceToDevice, stream_ipc[send]);
#elif defined PORT_SYCL && !defined IPC_ze
q_ipc[send].memcpy(remotebuf[send] + remoteoffset[send], sendbuf[send] + sendoffset[send], sendcount[send] * sizeof(T));
#endif
}
#ifdef IPC_ze
if(!command_list_closed) {
for(int i = 0; i < command_queue.size(); i++)
zeCommandListClose(command_list[i]);
command_list_closed = true;
}
for(int i = 0; i < command_queue.size(); i++)
zeCommandQueueExecuteCommandLists(command_queue[i], 1, &command_list[i], nullptr);
#endif
break;
case IPC_get:
for(int send = 0; send < numsend; send++)
MPI_Send(&ack_sender[send], 1, MPI_INT, sendproc[send], 0, comm_mpi);
for(int recv = 0; recv < numrecv; recv++) {
MPI_Recv(&ack_recver[recv], 1, MPI_INT, recvproc[recv], 0, comm_mpi, MPI_STATUS_IGNORE);
#ifdef PORT_CUDA
cudaMemcpyAsync(recvbuf[recv] + recvoffset[recv], remotebuf[recv] + remoteoffset[recv], recvcount[recv] * sizeof(T), cudaMemcpyDeviceToDevice, stream_ipc[recv]);
#elif defined PORT_HIP
hipMemcpyAsync(recvbuf[recv] + recvoffset[recv], remotebuf[recv] + remoteoffset[recv], recvcount[recv] * sizeof(T), hipMemcpyDeviceToDevice, stream_ipc[recv]);
#elif defined PORT_SYCL && !defined IPC_ze
q_ipc[recv].memcpy(recvbuf[recv] + recvoffset[recv], remotebuf[recv] + remoteoffset[recv], recvcount[recv] * sizeof(T));
#endif
}
#ifdef IPC_ze
if(!command_list_closed) {
for(int i = 0; i < command_queue.size(); i++)
zeCommandListClose(command_list[i]);
command_list_closed = true;
}
for(int i = 0; i < command_queue.size(); i++)
zeCommandQueueExecuteCommandLists(command_queue[i], 1, &command_list[i], nullptr);
#endif
break;
default:
break;
}
}
template <typename T>
void Comm<T>::wait() {
switch(lib) {
case MPI:
MPI_Waitall(numsend, sendrequest.data(), MPI_STATUSES_IGNORE);
MPI_Waitall(numrecv, recvrequest.data(), MPI_STATUSES_IGNORE);
break;
case XCCL:
#if defined CAP_NCCL && defined PORT_CUDA
cudaStreamSynchronize(stream_nccl);
#elif defined CAP_NCCL && defined PORT_HIP
hipStreamSynchronize(stream_nccl);
#elif defined CAP_ONECCL
q.wait();
#endif
break;
case IPC:
#ifdef IPC_ze
for(int i = 0; i < command_queue.size(); i++)
zeCommandQueueSynchronize(command_queue[i], UINT64_MAX);
#endif
for(int send = 0; send < numsend; send++) {
#ifdef PORT_CUDA
cudaStreamSynchronize(stream_ipc[send]);
#elif defined PORT_HIP
hipStreamSynchronize(stream_ipc[send]);
#elif defined PORT_SYCL && !defined IPC_ze
q_ipc[send].wait();
#endif
MPI_Send(&ack_sender[send], 1, MPI_INT, sendproc[send], 0, comm_mpi);
}
for(int recv = 0; recv < numrecv; recv++)
MPI_Recv(&ack_recver[recv], 1, MPI_INT, recvproc[recv], 0, comm_mpi, MPI_STATUS_IGNORE);
break;
case IPC_get:
#ifdef IPC_ze
for(int i = 0; i < command_queue.size(); i++)
zeCommandQueueSynchronize(command_queue[i], UINT64_MAX);
#endif
for(int recv = 0; recv < numrecv; recv++) {
#ifdef PORT_CUDA
cudaStreamSynchronize(stream_ipc[recv]);
#elif defined PORT_HIP
hipStreamSynchronize(stream_ipc[recv]);
#elif defined PORT_SYCL && !defined IPC_ze
q_ipc[recv].wait();
#endif
MPI_Send(&ack_recver[recv], 1, MPI_INT, recvproc[recv], 0, comm_mpi);
}
for(int send = 0; send < numsend; send++)
MPI_Recv(&ack_sender[send], 1, MPI_INT, sendproc[send], 0, comm_mpi, MPI_STATUS_IGNORE);
break;
default:
break;
}
}