-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMEL.hpp
6373 lines (5855 loc) · 282 KB
/
MEL.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
/*
The MIT License(MIT)
Copyright(c) 2016 Joss Whittle
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <mpi.h>
#include <cstdio>
#include <functional>
#include <memory>
#include <cstring>
#include <string>
#include <vector>
#include <complex>
#include <iostream>
#include <chrono>
#include <thread>
/**
* \file MEL.hpp
*/
namespace MEL {
/**
* \mainpage
* ### Version 0.01 Beta
* MEL is a C++11, header-only library, being developed with the goal of creating a light weight and robust framework for building parallel applications on top of MPI.
* MEL is designed to introduce no (or minimal) overheads while drastically reducing code complexity. It allows for a greater range of common MPI errors to be caught at
* compile-time rather than during program execution where it can be far more difficult to debug what is going wrong.
*
* A good example of this is type safety in the MPI standard. The standard does not dictate how many of the object types should be implemented leaving these details to
* the implementation vendor. For instance, in Intel MPI 5.1 `MPI_Comm` objects and many other simple types are implemented as indexes, `typedef int MPI_Comm`
* , leaving the implementation to use these indexes to manage the real objects internally. A drawback with this approach is it causes compile time type-checking of
* function parameters to not flag erroneous combinations of variables. The common signature `MPI_Send(void*, int, MPI_Datatype, int, int, MPI_Comm)` is actually seen
* by the compiler as `MPI_Send(void*, int, int, int, int, int)`, allowing any ordering of the last five variables to be compiled as valid MPI code, while causing
* catastrophic failure at run-time. In contrast, OpenMPI 1.10.2 implements these types as structs which are inherently type-safe.
*
* With MEL we aim to provide a consistent and unified function syntax that allows all MPI distributions to behave in a common and predictable way; while also providing
* some higher-level functionality that is not available from the MPI standard such as deep-copy, mutexes, RMA shared memory synchronization, and more.
*
* We plan to keep MEL in active development and hope that the research community will join us as we continue to grow the features and capabilities encompassed within the project.
* MEL is Open-Source and available on Github under the MIT license at: https://github.com/CS-Swansea/MEL .
*
* ## Todo
*
* - Add Distributed Graph Topology functions.
* - Add overloads for p2p/collective communications for transmitting `std::array`/`std::vector` by start/end iterators.
* - Improve error handler implementation. A rough version is currently in place.
* - Implement ranged-mutexes.
*
* \defgroup Errors Error Handling
* Error Handler Creation / Deletion
*
* \defgroup Utils Utilities
* Utility Functions for Cleaner Coding
*
* \defgroup Mem Memory Allocation
* Dynamic Memory Allocation using the underlying MPI_Alloc allocator
*
* \defgroup Comm Communicators & Groups
* Communicator & Group Creation / Deletion
*
* \defgroup Sync Synchronization
* Synchronization on Request objects
*
* \defgroup Datatype Derived Datatypes
* Derived Datatype Creation and Deletion
*
* \defgroup Topo Topology
* Cartesian & Distributed Graph Topologies
*
* \defgroup Ops Operations
* Builtin Functors and User Defined Operations
*
* \defgroup File File-IO
* File Creation / Deletion / Read / Write
*
* \defgroup P2P Point-2-Point Communication
* Send / Receive
*
* \defgroup COL Collective Communication
* Broadcast / Scatter / Gather / Alltoall / Reduce
*
* \defgroup Win RMA One-Sided Communication
* RMA Window Creation / Deletion / Get / Put / Accumulate
*
* \defgroup Mutex Mutex
* An implementation of Mutex Semantics between MPI processes. Based loosely off of Andreas Prell's mpi_mutex.c (https://gist.github.com/aprell/1486197) and R. Thakur, R. Ross, and R. Latham, "Implementing Byte-Range Locks Using MPI One-Sided Communication," in Proc. of the 12th European PVM/MPI Users' Group Meeting (Euro PVM/MPI 2005), Recent Advances in Parallel Virtual Machine and Message Passing Interface, Lecture Notes in Computer Science, LNCS 3666, Springer, September 2005, pp. 119-128.
*
* \defgroup Shared Shared Arrays
* A simple shared array implementation using Mutex locks and RMA one-sided communication
*/
#if (MPI_VERSION == 3)
#define MEL_3
#endif
typedef MPI_Aint Aint;
typedef MPI_Offset Offset;
#ifdef MEL_3
typedef MPI_Count Count;
#endif
/// Macro to help with return error codes
#ifndef MEL_NO_CHECK_ERROR_CODES
#define MEL_THROW(v, message) { int ierr = (v); if ((ierr) != MPI_SUCCESS) MEL::Abort((ierr), std::string(message)); }
#else
#define MEL_THROW(v, message) { (v); }
#endif
/**
* \ingroup Errors
* Calls MPI_Abort with the given error code and prints a string message to stderr
*
* \see MPI_Comm_rank, MPI_Comm_size, MPI_Error_class, MPI_Error_string, MPI_Abort
*
* \param[in] ierr The error code to throw
* \param[in] message The message to print to stderr describing what happened
*/
inline void Abort(int ierr, const std::string &message) {
char error_string[BUFSIZ];
int length_of_error_string, error_class, rank, size;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
fprintf(stderr, "\n\n*** MEL::ABORT ***\nRank %d / %d: %s\n", rank, size, message.c_str());
MPI_Error_class(ierr, &error_class);
MPI_Error_string(error_class, error_string, &length_of_error_string);
fprintf(stderr, "Rank %d / %d: %s\n", rank, size, error_string);
MPI_Error_string(ierr, error_string, &length_of_error_string);
fprintf(stderr, "Rank %d / %d: %s\n", rank, size, error_string);
MPI_Abort(MPI_COMM_WORLD, ierr);
};
/// Setup and teardown
/**
* \ingroup Utils
* Tests if MPI_Init has been successfully called
*
* \see MPI_Initialized
*
* \return Returns whether MPI is initialized as a bool
*/
inline bool IsInitialized() {
int init;
MEL_THROW( MPI_Initialized(&init), "Initialized" );
return init != 0;
};
/**
* \ingroup Utils
* Tests if MPI_Finalize has been successfully called
*
* \see MPI_Finalized
*
* \return Returns whether MPI is finalized as a bool
*/
inline bool IsFinalized() {
int fin;
MEL_THROW( MPI_Finalized(&fin), "Finalized" );
return fin != 0;
};
/**
* \ingroup Utils
* Call MPI_Init and setup default error handling
*
* \see MPI_Init, MPI_Comm_set_errhandler
*
* \param[in] argc Forwarded from program main
* \param[in] argv Forwarded from program main
*/
inline void Init(int &argc, char **&argv) {
if (!IsInitialized()) {
MEL_THROW( MPI_Init(&argc, &argv), "Init" );
}
/// Allows MEL::Abort to be called properly
MEL_THROW( MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN), "Initialize::SetErrorHandler" );
};
/**
* \ingroup Utils
* Call MPI_Finalize
*
* \see MPI_Finalize
*/
inline void Finalize() {
if (!IsFinalized()) {
MEL_THROW( MPI_Finalize(), "Finalize");
}
};
/**
* \ingroup Errors
* MEL alternative to std::exit
*
* \param[in] errcode The error code to exit with
*/
inline void Exit(const int errcode) {
MEL::Abort(errcode, "EXIT");
};
/**
* \ingroup Errors
* MEL alternative to std::exit
*
* \param[in] errcode The error code to exit with
* \param[in] msg A message to print to stderr as the program exits
*/
inline void Exit(const int errcode, const std::string &msg) {
std::cerr << msg << std::endl;
MEL::Abort(errcode, "EXIT");
};
/**
* \ingroup Utils
* Gets the current wall time since epoch in seconds
*
* \see MPI_Wtime
*
* \return Returns the current wall time as a double
*/
inline double Wtime() {
return MPI_Wtime();
};
/**
* \ingroup Utils
* Gets the current system tick
*
* \see MPI_Wtick
*
* \return Returns the current system tick as a double
*/
inline double Wtick() {
return MPI_Wtick();
};
struct ErrorHandler {
static const ErrorHandler ERRHANDLER_NULL;
MPI_Errhandler errHandl;
explicit ErrorHandler(const MPI_Errhandler &_e) : errHandl(_e) {};
inline ErrorHandler& operator=(const MPI_Errhandler &_e) {
errHandl = _e;
return *this;
};
explicit operator MPI_Errhandler() const {
return errHandl;
};
};
#ifdef MEL_IMPLEMENTATION
const ErrorHandler ErrorHandler::ERRHANDLER_NULL = ErrorHandler(MPI_ERRHANDLER_NULL);
#endif
typedef void(*ErrorHandlerFunc)(MPI_Comm*, int*, ...);
/**
* \ingroup Errors
* A default error handler that can be attached to MPI objects to give basic error catching
*
* \see MPI_Comm_rank, MPI_Comm_size, MPI_Error_class, MPI_Error_string, MPI_Abort
*
* \param[in] comm Comm world in which the error occured
* \param[in] ierr The error code that was thrown
*/
inline void DefaultErrorHandler(MPI_Comm *comm, int *ierr, ...) {
char error_string[BUFSIZ];
int length_of_error_string, error_class, rank, size;
MPI_Comm_rank(*comm, &rank);
MPI_Comm_size(*comm, &size);
fprintf(stderr, "\n\n*** MEL::DefaultErrorHandler ***\nRank %d / %d\n", rank, size);
MPI_Error_class(*ierr, &error_class);
MPI_Error_string(error_class, error_string, &length_of_error_string);
fprintf(stderr, "Rank %d / %d: %s\n", rank, size, error_string);
MPI_Error_string(*ierr, error_string, &length_of_error_string);
fprintf(stderr, "Rank %d / %d: %s\n", rank, size, error_string);
MPI_Abort(*comm, *ierr);
};
/**
* \ingroup Errors
* Add an error class for MPI to reference
*
* \see MPI_Add_error_class
*
* \return Returns the new error class code that was added
*/
inline int AddErrorClass() {
int err;
MEL_THROW( MPI_Add_error_class(&err), "ErrorHandler::AddErrorClass" );
return err;
};
/**
* \ingroup Errors
* Add an error code to an exisiting error class for MPI to reference
*
* \see MPI_Add_error_code
*
* \param[in] errClass The error class to add the error code to
* \return Returns the new error code that was added
*/
inline int AddErrorCode(const int errClass) {
int err;
MEL_THROW( MPI_Add_error_code(errClass, &err), "ErrorHandler::AddErrorCode" );
return err;
};
/**
* \ingroup Errors
* Add an error code to a new error class for MPI to reference
*
* \return Returns the new error code that was added
*/
inline int AddErrorCode() {
return AddErrorCode(AddErrorClass());
};
/**
* \ingroup Errors
* Add an error string to an existing error code for MPI to reference
*
* \see MPI_Add_error_string
*
* \param[in] err The error code to bind the string to
* \param[in] str The error string
*/
inline void AddErrorString(const int err, const std::string &str) {
MEL_THROW( MPI_Add_error_string(err, str.c_str()), "ErrorHandler::AddErrorString" );
};
/**
* \ingroup Errors
* Add an error string to a new existing error code for MPI to reference
*
* \param[in] str The error string
* \return Returns the new error code added
*/
inline int AddErrorString(const std::string &str) {
const int err = AddErrorCode();
AddErrorString(err, str);
return err;
};
/**
* \ingroup Errors
* Get the error class code of the given error code
*
* \see MPI_Error_class
*
* \param[in] errCode The error code
* \return Returns the error class
*/
inline int GetErrorClass(const int errCode) {
int err;
MEL_THROW( MPI_Error_class(errCode, &err), "ErrorHandler::GetErrorClass" );
return err;
};
/**
* \ingroup Errors
* Get the error class code of the given error code
*
* \see MPI_Error_string
*
* \param[in] errCode The error code
* \return Returns the error class
*/
inline std::string GetErrorString(const int errCode) {
std::string str; str.resize(BUFSIZ); int len;
MEL_THROW( MPI_Error_string(errCode, &str[0], &len), "ErrorHandler::GetErrorString" );
str.resize(len);
return str;
};
/**
* \ingroup Errors
* Free an error handler that was previously added
*
* \see MPI_Errhandler_free
*
* \param[in] errHndl The error handler object that references the bound function
*/
inline void ErrorHandlerFree(ErrorHandler &errHndl) {
MEL_THROW( MPI_Errhandler_free((MPI_Errhandler*) &errHndl), "ErrorHandler::Free" );
//errHndl = MEL::ErrorHandler::ERRHANDLER_NULL;
};
/**
* \ingroup Errors
* Free a vector Error Handlers
*
* \param[in] errHndls A std::vector of Error Handlers
*/
inline void ErrorHandlerFree(std::vector<ErrorHandler> &errHndls) {
for (auto &d : errHndls) ErrorHandlerFree(d);
};
/**
* \ingroup Errors
* Free the varadic set of error handlers provided
*
* \param[in] d0 The first error handler to free
* \param[in] d1 The second error handler to free
* \param[in] args The varadic set of remaining error handlers to free
*/
template<typename T0, typename T1, typename ...Args>
inline void ErrorHandlerFree(T0 &d0, T1 &d1, Args &&...args) {
ErrorHandlerFree(d0);
ErrorHandlerFree(d1, args...);
};
/**
* \ingroup Mem
* Allocate a block of memory for 'size' number of type T
*
* \see MPI_Alloc_mem
*
* \param[in] size The number of elements of type T to allocate
* \return Returns the pointer to the allocated memory
*/
template<typename T>
inline T* MemAlloc(const Aint size) {
T *ptr;
MEL_THROW( MPI_Alloc_mem(size * sizeof(T), MPI_INFO_NULL, &ptr), "Mem::Alloc" );
return ptr;
};
/**
* \ingroup Mem
* Allocate a block of memory for 'size' number of type T and assign a default value
*
* \param[in] size The number of elements of type T to allocate
* \param[in] val The value to set each element equal to
* \return Returns the pointer to the allocated memory
*/
template<typename T>
inline T* MemAlloc(const Aint size, const T &val) {
T *ptr = MemAlloc<T>(size);
for (Aint i = 0; i < size; ++i) ptr[i] = val;
return ptr;
};
/**
* \ingroup Mem
* Allocate a single object of type T and construct it with the set of varadic arguments
*
* \param[in] args The set of varadic arguments to construct the object with
* \return Returns the pointer to the allocated memory
*/
template<typename T, typename ...Args>
inline T* MemConstruct(Args &&...args) {
T *ptr = MemAlloc<T>(1);
new (ptr) T(args...);
return ptr;
};
/**
* \ingroup Mem
* Free a pointer allocated with MPI_Alloc or the MEL equivilant functions
*
* \see MPI_Free_mem
*
* \param[in] ptr The pointer to free
*/
template<typename T>
inline void MemFree(T *&ptr) {
if (ptr != nullptr) {
MEL_THROW( MPI_Free_mem(ptr), "Mem::Free" );
ptr = nullptr;
}
};
/**
* \ingroup Mem
* Free the varadic set of pointers provided
*
* \param[in] d0 The first pointer to free
* \param[in] d1 The second pointer to free
* \param[in] args The varadic set of remaining pointers to free
*/
template<typename T0, typename T1, typename ...Args>
inline void MemFree(T0 &d0, T1 &d1, Args &&...args) {
MemFree(d0);
MemFree(d1, args...);
};
/**
* \ingroup Mem
* Call the destructor for each element of the given array and then free the memory
*
* \param[in] ptr The pointer to the memory to be destructed
* \param[in] len The length of the array
*/
template<typename T>
inline void MemDestruct(T *&ptr, const Aint len = 1) {
if (ptr == nullptr) return;
for (Aint i = 0; i < len; ++i) {
(&ptr[i])->~T();
}
MemFree(ptr);
};
enum {
PROC_NULL = MPI_PROC_NULL,
ANY_SOURCE = MPI_ANY_SOURCE,
ANY_TAG = MPI_ANY_TAG
};
struct Comm {
static const Comm WORLD, SELF, COMM_NULL;
MPI_Comm comm;
Comm() : comm(MPI_COMM_NULL) {};
explicit Comm(const MPI_Comm &_e) : comm(_e) {};
inline Comm& operator=(const MPI_Comm &_e) {
comm = _e;
return *this;
};
explicit operator MPI_Comm() const {
return comm;
};
};
struct Group {
static const Group GROUP_NULL;
MPI_Group group;
Group() : group(MPI_GROUP_NULL) {};
explicit Group(const MPI_Group &_e) : group(_e) {};
inline Group& operator=(const MPI_Group &_e) {
group = _e;
return *this;
};
explicit operator MPI_Group() const {
return group;
};
};
struct Request {
static const Request REQUEST_NULL;
MPI_Request request;
Request() : request(MPI_REQUEST_NULL) {};
explicit Request(const MPI_Request &_e) : request(_e) {};
inline Request& operator=(const MPI_Request &_e) {
request = _e;
return *this;
};
explicit operator MPI_Request() const {
return request;
};
};
#ifdef MEL_IMPLEMENTATION
const Comm Comm::WORLD = Comm(MPI_COMM_WORLD);
const Comm Comm::SELF = Comm(MPI_COMM_SELF);
const Comm Comm::COMM_NULL = Comm(MPI_COMM_NULL);
const Group Group::GROUP_NULL = Group(MPI_GROUP_NULL);
const Request Request::REQUEST_NULL = Request(MPI_REQUEST_NULL);
#endif
typedef MPI_Status Status;
typedef MPI_Info Info;
/**
* \ingroup Comm
* Create a Comm error handler by directly passing the function to use
*
* \see MPI_Comm_create_errhandler
*
* \param[in] func The function to use as an error handler
* \return Returns an object that MPI can use to reference the error handler
*/
inline ErrorHandler CommCreateErrorHandler(ErrorHandlerFunc func) {
MPI_Errhandler errHndl;
MEL_THROW( MPI_Comm_create_errhandler((MPI_Comm_errhandler_function*) func, &errHndl), "Comm::CreateErrorHandler" );
return ErrorHandler(errHndl);
};
/**
* \ingroup Comm
* Set a Comm error handler by passing the a error handler reference
*
* \see MPI_Comm_set_errhandler
*
* \param[in] comm The comm world to attach the error handler to
* \param[in] errHndl The reference to a bound error handler
*/
inline void CommSetErrorHandler(const Comm &comm, const ErrorHandler &errHndl) {
MEL_THROW( MPI_Comm_set_errhandler((MPI_Comm) comm, (MPI_Errhandler) errHndl), "Comm::SetErrorHandler" );
};
/**
* \ingroup Comm
* Set a Comm error handler by directly passing the function to use
*
* \param[in] comm The comm world to attach the error handler to
* \param[in] func The function to use as an error handler
*/
inline void CommSetErrorHandler(const Comm &comm, ErrorHandlerFunc func) {
CommSetErrorHandler(comm, CommCreateErrorHandler(func));
};
/**
* \ingroup Comm
* Get the Comm error handler attached to a comm world
*
* \see MPI_Comm_get_errhandler
*
* \param[in] comm The comm world to get the error handler of
* \return Returns a reference to a bound error handler
*/
inline ErrorHandler CommGetErrorHandler(const Comm &comm) {
MPI_Errhandler errHndl;
MEL_THROW( MPI_Comm_get_errhandler((MPI_Comm) comm, &errHndl), "Comm::GetErrorHandler");
return ErrorHandler(errHndl);
};
/**
* \ingroup Comm
* Get the Comm rank of the process
*
* \see MPI_Comm_rank
*
* \param[in] comm The comm world to get the rank in
* \return Returns the rank or the process within comm
*/
inline int CommRank(const Comm &comm) {
int r;
MEL_THROW( MPI_Comm_rank((MPI_Comm) comm, &r), "Comm::Rank" );
return r;
};
/**
* \ingroup Comm
* Get the Comm world size
*
* \see MPI_Comm_size
*
* \param[in] comm The comm world to get the size of
* \return Returns the size of the comm world
*/
inline int CommSize(const Comm &comm) {
int s;
MEL_THROW( MPI_Comm_size((MPI_Comm) comm, &s), "Comm::Size" );
return s;
};
/**
* \ingroup Comm
* Get the Comm world remote size
*
* \see MPI_Comm_remote_size
*
* \param[in] comm The comm world to get the remote size of
* \return Returns the remote size of the comm world
*/
inline int CommRemoteSize(const Comm &comm) {
int s;
MEL_THROW( MPI_Comm_remote_size((MPI_Comm) comm, &s), "Comm::RemoteSize" );
return s;
};
/**
* \ingroup Comm
* Split a comm world into seperate comms. Processes with the same colour will end up in the same comm world
*
* \see MPI_Comm_split
*
* \param[in] comm The comm world to split
* \param[in] colour The group that this process will end up in in the new comm world
* \return Returns a new comm world
*/
inline Comm CommSplit(const Comm &comm, int colour) {
MPI_Comm out_comm;
MEL_THROW( MPI_Comm_split((MPI_Comm) comm, colour, CommRank(comm), &out_comm), "Comm::Split" );
return Comm(out_comm);
};
/**
* \ingroup Comm
* Duplicate a comm world so that it can be handled independently.
*
* \see MPI_Comm_dup
*
* \param[in] comm The comm world to duplicate
* \return Returns a new comm world
*/
inline Comm CommDuplicate(const Comm &comm) {
MPI_Comm out_comm;
MEL_THROW( MPI_Comm_dup((MPI_Comm) comm, &out_comm), "Comm::Duplicate" );
return Comm(out_comm);
};
#ifdef MEL_3
/**
* \ingroup Comm
* Non-Blocking. Duplicate a comm world so that it can be handled independently.
*
* \see MPI_Comm_idup
*
* \param[in] comm The comm world to duplicate
* \param[out] rq A request object that will signify when the comm world has been fully duplicated
* \return Returns a new comm world
*/
inline Comm CommIduplicate(const Comm &comm, Request &rq) {
MPI_Comm out_comm;
MEL_THROW( MPI_Comm_idup((MPI_Comm) comm, &out_comm, (MPI_Request*) &rq), "Comm::Iduplicate" );
return Comm(out_comm);
};
/**
* \ingroup Comm
* Non-Blocking. Duplicate a comm world so that it can be handled independently.
*
* \param[in] comm The comm world to duplicate
* \return Returns a std::pair of the new comm world and a request object
*/
inline std::pair<Comm, Request> CommIduplicate(const Comm &comm) {
Request rq;
Comm out_comm = CommIduplicate(comm, rq);
return std::make_pair(out_comm, rq);
};
#endif
/**
* \ingroup Comm
* Get the group of a comm world
*
* \see MPI_Comm_group
*
* \param[in] comm The comm world to get the group of
* \return Returns a Group object representing the processes in comm
*/
inline Group CommGetGroup(const Comm &comm) {
MPI_Group group;
MEL_THROW( MPI_Comm_group((MPI_Comm) comm, &group), "Comm::GetGroup" );
return Group(group);
};
/**
* \ingroup Comm
* Create a comm object from an existing comm object and a group object
*
* \see MPI_Comm_create
*
* \param[in] comm The comm world to build off of
* \param[in] group The group to use to build the new comm object
* \return Returns a new comm object
*/
inline Comm CommCreateFromGroup(const Comm &comm, const Group &group) {
MPI_Comm out_comm;
MEL_THROW( MPI_Comm_create((MPI_Comm) comm, (MPI_Group) group, &out_comm), "Comm::CreateFromGroup" );
return Comm(out_comm);
};
#ifdef MEL_3
/**
* \ingroup Comm
* Create a comm object from an existing comm object and a group object. This is a non-collective version
*
* \see MPI_Comm_create_group
*
* \param[in] comm The comm world to build off of
* \param[in] group The group to use to build the new comm object
* \param[in] tag The tag to use
* \return Returns a new comm object
*/
inline Comm CommCreateFromGroup(const Comm &comm, const Group &group, const int tag) {
MPI_Comm out_comm;
MEL_THROW( MPI_Comm_create_group((MPI_Comm) comm, (MPI_Group) group, tag, &out_comm), "Comm::CreateFromGroup" );
return Comm(out_comm);
};
#endif
/**
* \ingroup Comm
* Free a comm world
*
* \see MPI_Comm_disconnect
*
* \param[in] comm The comm world to free
*/
inline void CommFree(Comm &comm) {
MEL_THROW( MPI_Comm_disconnect((MPI_Comm*) &comm), "Comm::Free" );
comm = Comm::COMM_NULL;
};
/**
* \ingroup Comm
* Free a vector comm world
*
* \param[in] comms A std::vector of comm world
*/
inline void CommFree(std::vector<Comm> &comms) {
for (auto &d : comms) CommFree(d);
};
/**
* \ingroup Comm
* Free the varadic set of comm worlds provided
*
* \param[in] d0 The first comm world to free
* \param[in] d1 The second comm world to free
* \param[in] args The varadic set of remaining comm worlds to free
*/
template<typename T0, typename T1, typename ...Args>
inline void CommFree(T0 &d0, T1 &d1, Args &&...args) {
CommFree(d0);
CommFree(d1, args...);
};
/**
* \ingroup Comm
* Test if a comm world is the null comm world
*
* \param[in] comm The comm world to test
* \return Returns true if comm is the null comm world
*/
inline bool CommIsNULL(const Comm &comm) {
return (MPI_Comm) comm == MPI_COMM_NULL;
};
/**
* \ingroup Sync
* Collective operation that forces all processes to wait until they are all at the barrier
*
* \see MPI_Barrier
*
* \param[in] comm The comm world to synchronize
*/
inline void Barrier(const Comm &comm) {
MEL_THROW( MPI_Barrier((MPI_Comm) comm), "Comm::Barrier" );
};
#ifdef MEL_3
/**
* \ingroup Sync
* Collective operation that forces all processes to wait until they are all at the barrier
*
* \see MPI_Ibarrier
*
* \param[in] comm The comm world to synchronize
* \param[out] rq A reference to a request object used to determine when the barrier has been reached by all processes in comm
*/
inline void Ibarrier(const Comm &comm, Request &rq) {
MEL_THROW( MPI_Ibarrier((MPI_Comm) comm, (MPI_Request*) &rq), "Comm::IBarrier" );
};
/**
* \ingroup Sync
* Collective operation that forces all processes to wait until they are all at the barrier
*
* \param[in] comm The comm world to synchronize
* \return Returns a request object used to determine when the barrier has been reached by all processes in comm
*/
inline Request Ibarrier(const Comm &comm) {
Request rq{};
Ibarrier(comm, rq);
return rq;
};
#endif
/**
* \ingroup Sync
* Blocking operation to wait until a request object has completed
*
* \see MPI_Wait
*
* \param[in] rq The request object to wait for
*/
inline void Wait(Request &rq) {
MEL_THROW( MPI_Wait((MPI_Request*) &rq, MPI_STATUS_IGNORE), "Comm::Wait" );
};
/**
* \ingroup Sync
* Non-Blocking operation to test if a request object has completed
*
* \see MPI_Test
*
* \param[in] rq The request object to test
*/
inline bool Test(Request &rq) {
int f;
MEL_THROW( MPI_Test((MPI_Request*) &rq, &f, MPI_STATUS_IGNORE), "Comm::Test" );
return f != 0;
};
/**
* \ingroup Sync
* Blocking operation to wait until all request objects in an array have completed
*
* \see MPI_Waitall
*
* \param[in] ptr Pointer to the array of request objects
* \param[in] num The length of the array
*/
inline void Waitall(Request *ptr, int num) {
MEL_THROW( MPI_Waitall(num, (MPI_Request*) ptr, MPI_STATUS_IGNORE), "Comm::Waitall" );
};
/**
* \ingroup Sync
* Blocking operation to wait until all request objects in an array have completed
*
* \param[in] rqs A std::vector of request objects to wait for
*/
inline void Waitall(std::vector<Request> &rqs) {
Waitall(&rqs[0], rqs.size());
};
/**
* \ingroup Sync
* Non-Blocking operation to test if all request objects in an array have completed
*
* \see MPI_Testall
*
* \param[in] ptr Pointer to the array of request objects
* \param[in] num The length of the array
*/
inline bool Testall(Request *ptr, int num) {
int f;
MEL_THROW( MPI_Testall(num, (MPI_Request*) ptr, &f, MPI_STATUS_IGNORE), "Comm::Testall" );
return f != 0;
};
/**
* \ingroup Sync
* Non-Blocking operation to test if all request objects in an array have completed
*
* \param[in] rqs A std::vector of request objects to wait for
*/
inline bool Testall(std::vector<Request> &rqs) {
return Testall(&rqs[0], rqs.size());
};
/**
* \ingroup Sync
* Blocking operation to wait until any of the request objects in an array have completed
*
* \see MPI_Waitany
*
* \param[in] ptr Pointer to the array of request objects
* \param[in] num The length of the array
* \return Returns the index of the completed request
*/
inline int Waitany(Request *ptr, int num) {
int idx;
MEL_THROW( MPI_Waitany(num, (MPI_Request*) ptr, &idx, MPI_STATUS_IGNORE), "Comm::Waitany" );
return idx;
};
/**
* \ingroup Sync
* Blocking operation to wait until any of the request objects in an array have completed