FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
dist.cpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6// includes, FEAT
7#include <kernel/util/dist.hpp>
8#include <kernel/util/math.hpp> // for ilog10
9
10// includes, system
11#include <cstring> // for strcpy, memcpy
12#include <cstdint>
13
14namespace FEAT
15{
16 namespace Dist
17 {
18#if defined(FEAT_HAVE_MPI) || defined(DOXYGEN)
19
20#ifdef FEAT_OVERRIDE_MPI_OPS
21 // operation: x <- x + y
22 struct OpSum
23 {
24 template<typename T_>
25 static inline void eval(T_& x, T_& y)
26 {
27 x += y;
28 }
29 };
30
32 struct OpMax
33 {
34 template<typename T_>
35 static inline void eval(T_& x, T_& y)
36 {
37 if(x < y)
38 x = y;
39 }
40 };
41
43 struct OpMin
44 {
45 template<typename T_>
46 static inline void eval(T_& x, T_& y)
47 {
48 if(y < x)
49 x = y;
50 }
51 };
52
53 // Helper function:
54 // Interprets iv and iov as arrays of type T_ and
55 // applies Op_ on each pair of array elements
56 template<typename Op_, typename T_>
57 inline void func_op_t(void* iv, void* iov, int* n)
58 {
59 T_* y = reinterpret_cast<T_*>(iv);
60 T_* x = reinterpret_cast<T_*>(iov);
61 for(int i(0); i < *n; ++i)
62 {
63 Op_::eval(x[i], y[i]);
64 }
65 }
66
67 // Callback function for MPI operation overrides
68 template<typename Op_>
69 void op_callback(void* iv, void* iov, int* n, MPI_Datatype* dt)
70 {
71 if(*dt == MPI_SIGNED_CHAR) func_op_t<Op_, signed char> (iv, iov, n); else
72 if(*dt == MPI_SHORT) func_op_t<Op_, short> (iv, iov, n); else
73 if(*dt == MPI_INT) func_op_t<Op_, int> (iv, iov, n); else
74 if(*dt == MPI_LONG) func_op_t<Op_, long> (iv, iov, n); else
75 if(*dt == MPI_LONG_LONG) func_op_t<Op_, long long> (iv, iov, n); else
76 if(*dt == MPI_UNSIGNED_CHAR) func_op_t<Op_, unsigned char> (iv, iov, n); else
77 if(*dt == MPI_UNSIGNED_SHORT) func_op_t<Op_, unsigned short> (iv, iov, n); else
78 if(*dt == MPI_UNSIGNED) func_op_t<Op_, unsigned int> (iv, iov, n); else
79 if(*dt == MPI_UNSIGNED_LONG) func_op_t<Op_, unsigned long> (iv, iov, n); else
80 if(*dt == MPI_UNSIGNED_LONG_LONG) func_op_t<Op_, unsigned long long>(iv, iov, n); else
81 if(*dt == MPI_FLOAT) func_op_t<Op_, float> (iv, iov, n); else
82 if(*dt == MPI_DOUBLE) func_op_t<Op_, double> (iv, iov, n); else
83 if(*dt == MPI_LONG_DOUBLE) func_op_t<Op_, long double> (iv, iov, n); else
84 if(*dt == MPI_INT8_T) func_op_t<Op_, std::int8_t> (iv, iov, n); else
85 if(*dt == MPI_INT16_T) func_op_t<Op_, std::int16_t> (iv, iov, n); else
86 if(*dt == MPI_INT32_T) func_op_t<Op_, std::int32_t> (iv, iov, n); else
87 if(*dt == MPI_INT64_T) func_op_t<Op_, std::int64_t> (iv, iov, n); else
88 if(*dt == MPI_UINT8_T) func_op_t<Op_, std::uint8_t> (iv, iov, n); else
89 if(*dt == MPI_UINT16_T) func_op_t<Op_, std::uint16_t> (iv, iov, n); else
90 if(*dt == MPI_UINT32_T) func_op_t<Op_, std::uint32_t> (iv, iov, n); else
91 if(*dt == MPI_UINT64_T) func_op_t<Op_, std::uint64_t> (iv, iov, n); else
92#ifdef FEAT_HAVE_QUADMATH
93 if(*dt == dt__float128.dt) func_op_t<Op_, __float128> (iv, iov, n); else
94#endif
95#ifdef FEAT_HAVE_HALFMATH
96 if(*dt == dt__half.dt) func_op_t<Op_, __half> (iv, iov, n); else
97#endif
98 {
99 // unsupported datatype
100 MPI_Abort(MPI_COMM_WORLD, 1);
101 }
102 }
103#endif // FEAT_OVERRIDE_MPI_OPS
104
105 bool initialize(int& argc, char**& argv)
106 {
107 int already_initialized(0);
108 if (::MPI_Initialized(&already_initialized) != MPI_SUCCESS)
109 return false;
110
111#ifdef FEAT_MPI_THREAD_MULTIPLE
112 XASSERTM((bool)!already_initialized, "MPI was already initialized by another library, we cannot ensure MPI_THREAD_MULTIPLE support!");
113 int required = MPI_THREAD_MULTIPLE;
114#else
115 int required = MPI_THREAD_SERIALIZED;
116#endif //FEAT_MPI_THREAD_MULTIPLE
117 int provided = MPI_THREAD_SINGLE;
118 // if mpi is not already initialized, check if we are able to
119 if (!already_initialized && ::MPI_Init_thread(&argc, &argv, required, &provided) != MPI_SUCCESS)
120 return false;
121 // also, if we initialize ourselves, check if we at least get our required features
122 if (!already_initialized && (provided < required))
123 return false;
124
125#ifdef FEAT_HAVE_QUADMATH
126 // Create a custom MPI datatype for '__float128'
127 Datatype& dt_f128 = const_cast<Datatype&>(Dist::dt__float128);
128 MPI_Type_contiguous(int(sizeof(__float128)), MPI_BYTE, &dt_f128.dt);
129 MPI_Type_commit(&dt_f128.dt);
130#endif // FEAT_HAVE_QUADMATH
131#ifdef FEAT_HAVE_HALFMATH
132 // Create a custom MPI datatype for '__half'
133 Datatype& dt_half = const_cast<Datatype&>(Dist::dt__half);
134 MPI_Type_contiguous(int(sizeof(__half)), MPI_BYTE, &dt_half.dt);
135 MPI_Type_commit(&dt_half.dt);
136#endif // FEAT_HAVE_HALFMATH
137
138#ifdef FEAT_OVERRIDE_MPI_OPS
139 // override MPI operations
140 MPI_Op_create(op_callback<OpSum>, 1, &const_cast<Operation&>(Dist::op_sum).op);
141 MPI_Op_create(op_callback<OpMax>, 1, &const_cast<Operation&>(Dist::op_max).op);
142 MPI_Op_create(op_callback<OpMin>, 1, &const_cast<Operation&>(Dist::op_min).op);
143#endif // FEAT_OVERRIDE_MPI_OPS
144
145 return true;
146 }
147
148 void finalize()
149 {
150
151#ifdef FEAT_OVERRIDE_MPI_OPS
152 Operation& my_op_sum = const_cast<Operation&>(Dist::op_sum);
153 MPI_Op_free(&my_op_sum.op);
154#endif // FEAT_OVERRIDE_MPI_OPS
155
156#ifdef FEAT_HAVE_QUADMATH
157 Datatype& dt_f128 = const_cast<Datatype&>(Dist::dt__float128);
158 MPI_Type_free(&dt_f128.dt);
159#endif // FEAT_HAVE_QUADMATH
160
161#ifdef FEAT_HAVE_HALFMATH
162 Datatype& dt_half = const_cast<Datatype&>(Dist::dt__half);
163 MPI_Type_free(&dt_half.dt);
164#endif // FEAT_HAVE_HALFMATH
165
167
168 // finalize MPI
169 int already_finalized(0);
170 ::MPI_Finalized(&already_finalized);
171 if (!already_finalized)
172 MPI_Finalize();
173 }
174
175 // datatypes
176 const Datatype dt_byte (MPI_BYTE, sizeof(char));
177 const Datatype dt_char (MPI_CHAR, sizeof(char));
178 const Datatype dt_wchar (MPI_WCHAR, sizeof(wchar_t));
179 const Datatype dt_signed_char (MPI_SIGNED_CHAR, sizeof(signed char));
180 const Datatype dt_signed_short (MPI_SHORT, sizeof(short));
181 const Datatype dt_signed_int (MPI_INT, sizeof(int));
182 const Datatype dt_signed_long (MPI_LONG, sizeof(long));
183 const Datatype dt_signed_long_long (MPI_LONG_LONG, sizeof(long long));
184 const Datatype dt_unsigned_char (MPI_UNSIGNED_CHAR, sizeof(unsigned char));
185 const Datatype dt_unsigned_short (MPI_UNSIGNED_SHORT, sizeof(unsigned short));
186 const Datatype dt_unsigned_int (MPI_UNSIGNED, sizeof(unsigned int));
187 const Datatype dt_unsigned_long (MPI_UNSIGNED_LONG, sizeof(unsigned long));
188 const Datatype dt_unsigned_long_long (MPI_UNSIGNED_LONG_LONG, sizeof(unsigned long long));
189 const Datatype dt_float (MPI_FLOAT, sizeof(float));
190 const Datatype dt_double (MPI_DOUBLE, sizeof(double));
191 const Datatype dt_long_double (MPI_LONG_DOUBLE, sizeof(long double));
192 const Datatype dt_signed_int8 (MPI_INT8_T, sizeof(std::int8_t));
193 const Datatype dt_signed_int16 (MPI_INT16_T, sizeof(std::int16_t));
194 const Datatype dt_signed_int32 (MPI_INT32_T, sizeof(std::int32_t));
195 const Datatype dt_signed_int64 (MPI_INT64_T, sizeof(std::int64_t));
196 const Datatype dt_unsigned_int8 (MPI_UINT8_T, sizeof(std::uint8_t));
197 const Datatype dt_unsigned_int16 (MPI_UINT16_T, sizeof(std::uint16_t));
198 const Datatype dt_unsigned_int32 (MPI_UINT32_T, sizeof(std::uint32_t));
199 const Datatype dt_unsigned_int64 (MPI_UINT64_T, sizeof(std::uint64_t));
200 const Datatype dt_packed (MPI_PACKED, sizeof(char));
201#ifdef FEAT_HAVE_QUADMATH
202 // This needs to initialized by Dist::initialize() !
203 const Datatype dt__float128 (0, sizeof(__float128));
204#endif
205#ifdef FEAT_HAVE_HALFMATH
206 // This needs to initialized by Dist::initialize() !
207 const Datatype dt__half (0, sizeof(__half));
208#endif
209
210 // operations
211 const Operation op_sum(MPI_SUM);
212 const Operation op_max(MPI_MAX);
213 const Operation op_min(MPI_MIN);
214
215 /* ***************************************************************************************** */
216 /* ***************************************************************************************** */
217 /* MPI Request wrapper implementation */
218 /* ***************************************************************************************** */
219 /* ***************************************************************************************** */
220
222 request(MPI_REQUEST_NULL)
223 {
224 }
225
226 Request::Request(MPI_Request req_) :
227 request(req_)
228 {
229 }
230
232 request(other.request)
233 {
234 other.request = MPI_REQUEST_NULL;
235 }
236
238 {
239 if(this != &other)
240 {
241 XASSERT(is_null());
242 this->request = other.request;
243 other.request = MPI_REQUEST_NULL;
244 }
245 return *this;
246 }
247
248 bool Request::is_null() const
249 {
250 return request == MPI_REQUEST_NULL;
251 }
252
254 {
255 if(request != MPI_REQUEST_NULL)
256 MPI_Request_free(&request);
257 }
258
260 {
261 if(request != MPI_REQUEST_NULL)
262 MPI_Cancel(&request);
263 }
264
265 bool Request::test(Status& status)
266 {
267 int flag(0);
268 MPI_Test(&request, &flag, status.mpi_status());
269 return flag != 0;
270 }
271
272 bool Request::wait(Status& status)
273 {
274 if(request == MPI_REQUEST_NULL)
275 return false;
276 MPI_Wait(&request, status.mpi_status());
277 return true;
278 }
279
280 /* ***************************************************************************************** */
281 /* ***************************************************************************************** */
282 /* MPI RequestVector implementation */
283 /* ***************************************************************************************** */
284 /* ***************************************************************************************** */
285
287 {
288 int flag = 0;
289 MPI_Testall(_isize(), _reqs_array(), &flag, _stats_array());
290 return flag != 0;
291 }
292
293 bool RequestVector::test_any(std::size_t& idx, Status& status)
294 {
295 int i = 0;
296 int flag = 0;
297 MPI_Testany(_isize(), _reqs_array(), &i, &flag, status.mpi_status());
298 if(flag == 0)
299 return false;
300 idx = std::size_t(i);
301 return true;
302 }
303
304 /*std::size_t RequestVector::test_some(std::size_t* indices, Status* statuses)
305 {
306 static_assert(sizeof(int) <= sizeof(std::size_t), "size_t must be at least size of int");
307 XASSERT(indices != nullptr);
308 XASSERT(statuses != nullptr);
309
310 int outcount = -1;
311 int* idx = reinterpret_cast<int*>(indices);
312 MPI_Testsome(_isize(), _reqs_array(), &outcount, idx, (MPI_Status*)statuses);
313
314 // unpack int to size_t ?
315 if(sizeof(int) < sizeof(std::size_t))
316 {
317 for(std::size_t k = std::size_t(outcount); k > 0;)
318 {
319 --k;
320 indices[k] = std::size_t(idx[k]);
321 }
322 }
323
324 return std::size_t(outcount);
325 }*/
326
328 {
329 MPI_Waitall(_isize(), _reqs_array(), _stats_array());
330 }
331
332 bool RequestVector::wait_any(std::size_t& idx, Status& status)
333 {
334 int i = -1;
335 MPI_Waitany(_isize(), _reqs_array(), &i, status.mpi_status());
336 if(i == MPI_UNDEFINED)
337 return false;
338 idx = std::size_t(i);
339 return true;
340 }
341
342 /*std::size_t RequestVector::wait_some(std::size_t* indices, Status* statuses)
343 {
344 static_assert(sizeof(int) <= sizeof(std::size_t), "size_t must be at least size of int");
345 XASSERT(indices != nullptr);
346 XASSERT(statuses != nullptr);
347
348 int outcount = -1;
349 int* idx = reinterpret_cast<int*>(indices);
350 MPI_Waitsome(_isize(), _reqs_array(), &outcount, idx, (MPI_Status*)statuses);
351
352 // unpack int to size_t ?
353 if(sizeof(int) < sizeof(std::size_t))
354 {
355 for(std::size_t k = std::size_t(outcount); k > 0;)
356 {
357 --k;
358 indices[k] = std::size_t(idx[k]);
359 }
360 }
361
362 // store statuses
363 for(std::size_t k(0); k < std::size_t(outcount); ++k)
364 _stats[indices[k]] = statuses[k];
365
366 return std::size_t(outcount);
367 }*/
368
369 /* ***************************************************************************************** */
370 /* ***************************************************************************************** */
371 /* MPI Comm wrapper implementation */
372 /* ***************************************************************************************** */
373 /* ***************************************************************************************** */
374
376 comm(MPI_COMM_NULL),
377 _rank(0),
378 _size(0)
379 {
380 }
381
382 Comm::Comm(MPI_Comm comm_) :
383 comm(comm_),
384 _rank(0),
385 _size(0)
386 {
387 if(comm_ != MPI_COMM_NULL)
388 {
389 MPI_Comm_rank(comm, &_rank);
390 MPI_Comm_size(comm, &_size);
391 }
392 }
393
394 Comm::Comm(Comm&& other) :
395 comm(other.comm),
396 _rank(other._rank),
397 _size(other._size)
398 {
399 // do not free the world and self comms
400 if((other.comm != MPI_COMM_WORLD) && (other.comm != MPI_COMM_SELF))
401 {
402 other.comm = MPI_COMM_NULL;
403 other._rank = other._size = 0;
404 }
405 }
406
408 {
409 if(this != &other)
410 {
411 XASSERT(comm == MPI_COMM_NULL);
412 comm = other.comm;
413 _rank = other._rank;
414 _size = other._size;
415 // do not free the world and self comms
416 if((other.comm != MPI_COMM_WORLD) && (other.comm != MPI_COMM_SELF))
417 {
418 other.comm = MPI_COMM_NULL;
419 other._rank = other._size = 0;
420 }
421 }
422 return *this;
423 }
424
426 {
427 // do not free the world, self and null comms
428 if((comm != MPI_COMM_WORLD) && (comm != MPI_COMM_SELF) && (comm != MPI_COMM_NULL))
429 MPI_Comm_free(&comm);
430 }
431
433 {
434 return Comm(MPI_COMM_WORLD);
435 }
436
438 {
439 return Comm(MPI_COMM_SELF);
440 }
441
443 {
444 return Comm(MPI_COMM_NULL);
445 }
446
447 bool Comm::is_world() const
448 {
449 return (comm == MPI_COMM_WORLD);
450 }
451
452 bool Comm::is_self() const
453 {
454 return (comm == MPI_COMM_SELF);
455 }
456
457 bool Comm::is_null() const
458 {
459 return (comm == MPI_COMM_NULL);
460 }
461
463 {
464 // do not duplicate world, self and null comms
465 if((comm == MPI_COMM_WORLD) || (comm == MPI_COMM_SELF) || (comm == MPI_COMM_NULL))
466 return Comm(comm);
467
468 // create a real duplicate
469 MPI_Comm newcomm = MPI_COMM_NULL;
470 MPI_Comm_dup(comm, &newcomm);
471
472 return Comm(newcomm);
473 }
474
475 Comm Comm::comm_create_range_incl(int count, int first, int stride) const
476 {
477 XASSERT(count > 0);
478 XASSERT((first >= 0) && (first < _size));
479 XASSERT(stride > 0);
480
481 // get this comm's group
482 MPI_Group group = MPI_GROUP_NULL;
483 MPI_Comm_group(comm, &group);
484
485 // create sub-group
486 MPI_Group newgroup = MPI_GROUP_NULL;
487 int ranges[3] = {first, first + (count-1)*stride, stride};
488 MPI_Group_range_incl(group, 1, &ranges, &newgroup);
489
490 // create new comm from group
491 MPI_Comm newcomm = MPI_COMM_NULL;
492 MPI_Comm_create(comm, newgroup, &newcomm);
493
494 // free group handles
495 MPI_Group_free(&newgroup);
496 MPI_Group_free(&group);
497
498 return Comm(newcomm);
499 }
500
501 Comm Comm::comm_create_incl(int n, const int* ranks) const
502 {
503 XASSERT(n > 0);
504 XASSERT(ranks != nullptr);
505
506 // get this comm's group
507 MPI_Group group = MPI_GROUP_NULL;
508 MPI_Comm_group(comm, &group);
509
510 // create sub-group
511 MPI_Group newgroup = MPI_GROUP_NULL;
512 MPI_Group_incl(group, n, ranks, &newgroup);
513
514 // create new comm from group
515 MPI_Comm newcomm = MPI_COMM_NULL;
516 MPI_Comm_create(comm, newgroup, &newcomm);
517
518 // free group handles
519 MPI_Group_free(&newgroup);
520 MPI_Group_free(&group);
521
522 return Comm(newcomm);
523 }
524
525 Comm Comm::comm_split(int color, int key) const
526 {
527 MPI_Comm newcomm = MPI_COMM_NULL;
528 MPI_Comm_split(comm, color, key, &newcomm);
529 return Comm(newcomm);
530 }
531
532 void Comm::barrier() const
533 {
534 MPI_Barrier(comm);
535 }
536
538 {
539 MPI_Request req(MPI_REQUEST_NULL);
540 MPI_Ibarrier(comm, &req);
541 return Request(req);
542 }
543
544 void Comm::bcast(void* buffer, std::size_t count, const Datatype& datatype, int root) const
545 {
546 MPI_Bcast(buffer, int(count), datatype.dt, root, comm);
547 }
548
549 Request Comm::ibcast(void* buffer, std::size_t count, const Datatype& datatype, int root) const
550 {
551 MPI_Request req(MPI_REQUEST_NULL);
552 MPI_Ibcast(buffer, int(count), datatype.dt, root, comm, &req);
553 return Request(req);
554 }
555
556 void Comm::gather(const void* sendbuf, std::size_t sendcount, const Datatype& sendtype, void* recvbuf, std::size_t recvcount, const Datatype& recvtype, int root) const
557 {
558 if((sendbuf == recvbuf) && (root == _rank))
559 MPI_Gather(MPI_IN_PLACE, 0, recvtype.dt, recvbuf, int(recvcount), recvtype.dt, root, comm);
560 else
561 MPI_Gather(sendbuf, int(sendcount), sendtype.dt, recvbuf, int(recvcount), recvtype.dt, root, comm);
562 }
563
564 Request Comm::igather(const void* sendbuf, std::size_t sendcount, const Datatype& sendtype, void* recvbuf, std::size_t recvcount, const Datatype& recvtype, int root) const
565 {
566 MPI_Request req(MPI_REQUEST_NULL);
567 if((sendbuf == recvbuf) && (root == _rank))
568 MPI_Igather(MPI_IN_PLACE, 0, recvtype.dt, recvbuf, int(recvcount), recvtype.dt, root, comm, &req);
569 else
570 MPI_Igather(sendbuf, int(sendcount), sendtype.dt, recvbuf, int(recvcount), recvtype.dt, root, comm, &req);
571 return Request(req);
572 }
573
574 void Comm::scatter(const void* sendbuf, std::size_t sendcount, const Datatype& sendtype, void* recvbuf, std::size_t recvcount, const Datatype& recvtype, int root) const
575 {
576 if((sendbuf == recvbuf) && (root == _rank))
577 MPI_Scatter(sendbuf, int(sendcount), sendtype.dt, MPI_IN_PLACE, 0, sendtype.dt, root, comm);
578 else
579 MPI_Scatter(sendbuf, int(sendcount), sendtype.dt, recvbuf, int(recvcount), recvtype.dt, root, comm);
580 }
581
582 Request Comm::iscatter(const void* sendbuf, std::size_t sendcount, const Datatype& sendtype, void* recvbuf, std::size_t recvcount, const Datatype& recvtype, int root) const
583 {
584 MPI_Request req(MPI_REQUEST_NULL);
585 if((sendbuf == recvbuf) && (root == _rank))
586 MPI_Iscatter(sendbuf, int(sendcount), sendtype.dt, MPI_IN_PLACE, 0, sendtype.dt, root, comm, &req);
587 else
588 MPI_Iscatter(sendbuf, int(sendcount), sendtype.dt, recvbuf, int(recvcount), recvtype.dt, root, comm, &req);
589 return Request(req);
590 }
591
592 void Comm::allgather(const void* sendbuf, std::size_t sendcount, const Datatype& sendtype, void* recvbuf, std::size_t recvcount, const Datatype& recvtype) const
593 {
594 if(sendbuf == recvbuf)
595 MPI_Allgather(MPI_IN_PLACE, 0, recvtype.dt, recvbuf, int(recvcount), recvtype.dt, comm);
596 else
597 MPI_Allgather(sendbuf, int(sendcount), sendtype.dt, recvbuf, int(recvcount), recvtype.dt, comm);
598 }
599
600 Request Comm::iallgather(const void* sendbuf, std::size_t sendcount, const Datatype& sendtype, void* recvbuf, std::size_t recvcount, const Datatype& recvtype) const
601 {
602 MPI_Request req(MPI_REQUEST_NULL);
603 if(sendbuf == recvbuf)
604 MPI_Iallgather(MPI_IN_PLACE, 0, recvtype.dt, recvbuf, int(recvcount), recvtype.dt, comm, &req);
605 else
606 MPI_Iallgather(sendbuf, int(sendcount), sendtype.dt, recvbuf, int(recvcount), recvtype.dt, comm, &req);
607 return Request(req);
608 }
609
610 void Comm::allgatherv(const void* sendbuf, std::size_t sendcount, const Datatype& sendtype, void* recvbuf, const int* recvcounts, const int* displs, const Datatype& recvtype) const
611 {
612 if(sendbuf == recvbuf)
613 MPI_Allgatherv(MPI_IN_PLACE, 0, recvtype.dt, recvbuf, recvcounts, displs, recvtype.dt, comm);
614 else
615 MPI_Allgatherv(sendbuf, int(sendcount), sendtype.dt, recvbuf, recvcounts, displs, recvtype.dt, comm);
616 }
617
618 void Comm::alltoall(const void* sendbuf, std::size_t sendcount, const Datatype& sendtype, void* recvbuf, std::size_t recvcount, const Datatype& recvtype) const
619 {
620 if(sendbuf == recvbuf)
621 MPI_Alltoall(MPI_IN_PLACE, 0, recvtype.dt, recvbuf, int(recvcount), recvtype.dt, comm);
622 else
623 MPI_Alltoall(sendbuf, int(sendcount), sendtype.dt, recvbuf, int(recvcount), recvtype.dt, comm);
624 }
625
626 Request Comm::ialltoall(const void* sendbuf, std::size_t sendcount, const Datatype& sendtype, void* recvbuf, std::size_t recvcount, const Datatype& recvtype) const
627 {
628 MPI_Request req(MPI_REQUEST_NULL);
629 if(sendbuf == recvbuf)
630 MPI_Ialltoall(MPI_IN_PLACE, 0, recvtype.dt, recvbuf, int(recvcount), recvtype.dt, comm, &req);
631 else
632 MPI_Ialltoall(sendbuf, int(sendcount), sendtype.dt, recvbuf, int(recvcount), recvtype.dt, comm, &req);
633 return Request(req);
634 }
635
636 void Comm::alltoallv(const void* sendbuf, const int* sendcounts, const int* sdispls, const Datatype& sendtype, void* recvbuf, const int* recvcounts, const int* rdispls, const Datatype& recvtype) const
637 {
638 if(sendbuf == recvbuf)
639 MPI_Alltoallv(MPI_IN_PLACE, 0, 0, recvtype.dt, recvbuf, recvcounts, rdispls, recvtype.dt, comm);
640 else
641 MPI_Alltoallv(sendbuf, sendcounts, sdispls, sendtype.dt, recvbuf, recvcounts, rdispls, recvtype.dt, comm);
642 }
643
644 void Comm::reduce(const void* sendbuf, void* recvbuf, std::size_t count, const Datatype& datatype, const Operation& op, int root) const
645 {
646 // MPI_IN_PLACE is only allowed on root process
647 MPI_Reduce((sendbuf == recvbuf) && (_rank == root) ? MPI_IN_PLACE : sendbuf, recvbuf, int(count), datatype.dt, op.op, root, comm);
648 }
649
650 Request Comm::ireduce(const void* sendbuf, void* recvbuf, std::size_t count, const Datatype& datatype, const Operation& op, int root) const
651 {
652 MPI_Request req(MPI_REQUEST_NULL);
653 // MPI_IN_PLACE is only allowed on root process
654 MPI_Ireduce((sendbuf == recvbuf) && (_rank == root) ? MPI_IN_PLACE : sendbuf, recvbuf, int(count), datatype.dt, op.op, root, comm, &req);
655 return Request(req);
656 }
657
658 void Comm::allreduce(const void* sendbuf, void* recvbuf, std::size_t count, const Datatype& datatype, const Operation& op) const
659 {
660 MPI_Allreduce(sendbuf == recvbuf ? MPI_IN_PLACE : sendbuf, recvbuf, int(count), datatype.dt, op.op, comm);
661 }
662
663 Request Comm::iallreduce(const void* sendbuf, void* recvbuf, std::size_t count, const Datatype& datatype, const Operation& op) const
664 {
665 MPI_Request req(MPI_REQUEST_NULL);
666 MPI_Iallreduce(sendbuf == recvbuf ? MPI_IN_PLACE : sendbuf, recvbuf, int(count), datatype.dt, op.op, comm, &req);
667 return Request(req);
668 }
669
670 void Comm::scan(const void* sendbuf, void* recvbuf, std::size_t count, const Datatype& datatype, const Operation& op) const
671 {
672 MPI_Scan(sendbuf == recvbuf ? MPI_IN_PLACE : sendbuf, recvbuf, int(count), datatype.dt, op.op, comm);
673 }
674
675 /*Request Comm::iscan(const void* sendbuf, void* recvbuf, std::size_t count, const Datatype& datatype, const Operation& op) const
676 {
677 MPI_Request req(MPI_REQUEST_NULL);
678#ifdef MSMPI_VER
679 MPI_Scan(sendbuf == recvbuf ? MPI_IN_PLACE : sendbuf, recvbuf, int(count), datatype.dt, op.op, comm);
680#else
681 MPI_Iscan(sendbuf == recvbuf ? MPI_IN_PLACE : sendbuf, recvbuf, int(count), datatype.dt, op.op, comm, &req);
682#endif
683 return Request(req);
684 }*/
685
686 void Comm::exscan(const void* sendbuf, void* recvbuf, std::size_t count, const Datatype& datatype, const Operation& op) const
687 {
688 MPI_Exscan(sendbuf == recvbuf ? MPI_IN_PLACE : sendbuf, recvbuf, int(count), datatype.dt, op.op, comm);
689 }
690
691 /*Request Comm::iexscan(const void* sendbuf, void* recvbuf, std::size_t count, const Datatype& datatype, const Operation& op) const
692 {
693 MPI_Request req(MPI_REQUEST_NULL);
694#ifdef MSMPI_VER
695 MPI_Exscan(sendbuf == recvbuf ? MPI_IN_PLACE : sendbuf, recvbuf, int(count), datatype.dt, op.op, comm);
696#else
697 MPI_Iexscan(sendbuf == recvbuf ? MPI_IN_PLACE : sendbuf, recvbuf, int(count), datatype.dt, op.op, comm, &req);
698#endif
699 return Request(req);
700 }*/
701
702 void Comm::send(const void* buffer, std::size_t count, const Datatype& datatype, int dest, int tag) const
703 {
704 MPI_Send(buffer, int(count), datatype.dt, dest, tag, comm);
705 }
706
707 Request Comm::isend(const void* buffer, std::size_t count, const Datatype& datatype, int dest, int tag) const
708 {
709 MPI_Request req(MPI_REQUEST_NULL);
710 MPI_Isend(buffer, int(count), datatype.dt, dest, tag, comm, &req);
711 return Request(req);
712 }
713
714 void Comm::recv(void* buffer, std::size_t count, const Datatype& datatype, int source, int tag, Status& status) const
715 {
716 MPI_Recv(buffer, int(count), datatype.dt, source, tag, comm, status.mpi_status());
717 }
718
719 Request Comm::irecv(void* buffer, std::size_t count, const Datatype& datatype, int source, int tag) const
720 {
721 MPI_Request req(MPI_REQUEST_NULL);
722 MPI_Irecv(buffer, int(count), datatype.dt, source, tag, comm, &req);
723 return Request(req);
724 }
725
726 void Comm::pack(const void* inbuf, std::size_t incount, const Datatype& datatype, void* outbuf, std::size_t outsize, int* position) const
727 {
728 MPI_Pack(inbuf, (int)incount, datatype.dt, outbuf, (int)outsize, position, comm);
729 }
730
731 void Comm::unpack(const void* inbuf, std::size_t insize, int* position, void* outbuf, std::size_t outcount, const Datatype& datatype) const
732 {
733 MPI_Unpack(inbuf, (int)insize, position, outbuf, (int)outcount, datatype.dt, comm);
734 }
735
736 void Comm::pack_size(std::size_t incount, const Datatype& datatype, int* size) const
737 {
738 MPI_Pack_size((int)incount, datatype.dt, comm, size);
739 }
740
741 void Comm::bcast_stringstream(std::stringstream& stream, int root) const
742 {
743 std::string str;
744 std::size_t len(0);
745
746 // Get size to broadcast
747 if(rank() == root)
748 {
749 str = stream.str();
750 len = str.length();
751 }
752
753 // broadcast string length
754 bcast(&len, std::size_t(1), root);
755
756 // empty string?
757 if(len == std::size_t(0))
758 return;
759
760 // allocate buffer
761 std::vector<char> buf(len + std::size_t(1));
762
763 // fill buffer on root
764 if(rank() == root)
765 std::strcpy(buf.data(), str.c_str());
766
767 // broadcast buffer
768 bcast(buf.data(), buf.size(), root);
769
770 // convert
771 if(rank() != root)
772 stream << std::string(buf.data(), len);
773 }
774
775 void Comm::bcast_binarystream(BinaryStream& stream, int root) const
776 {
777 // get the stream's internal data container
778 std::vector<char>& data = stream.container();
779 std::size_t len(0);
780
781 // Get size to broadcast
782 if(rank() == root)
783 len = data.size();
784
785 // broadcast vector length
786 bcast(&len, std::size_t(1), root);
787
788 // empty vector?
789 if(len == std::size_t(0))
790 return;
791
792 // reallocate vector
793 if(rank() != root)
794 data.resize(len);
795
796 // broadcast data
797 bcast(data.data(), data.size(), root);
798 }
799
800 void Comm::print(std::ostream& os, const String& msg, int root) const
801 {
802 XASSERTM((0 <= root) && (root < _size), "invalid root rank argument");
803 if(root == _rank)
804 os << msg << "\n";
805 }
806
807 void Comm::allprint(std::ostream& os, const String& msg, int root) const
808 {
809 XASSERTM((0 <= root) && (root < _size), "invalid root rank argument");
810
811 if(_rank == root)
812 {
813 // determine the rank padding size first:
814 const std::size_t n = std::size_t(_size);
815 const std::size_t ndig = Math::ilog10(n);
816
817 // gather message lengths
818 std::size_t mylen = msg.size();
819 std::vector<std::size_t> lengths(n);
820 this->gather(&mylen, std::size_t(1), lengths.data(), std::size_t(1), root);
821
822 // receive and print messages
823 for(int i(0); i < _size; ++i)
824 {
825 // allocate message buffer
826 std::size_t length = lengths.at(std::size_t(i));
827 if(length == std::size_t(0))
828 continue;
829
830 // get the message to be printed
831 String message;
832 if(i == root)
833 message = msg;
834 else
835 {
836 // receive message
837 std::vector<char> msgbuf(length);
838 this->recv(msgbuf.data(), length, i);
839 message.assign(msgbuf.data(), length);
840 }
841
842 // split message into single lines
843 std::deque<String> lines = message.split_by_charset("\n");
844
845 // set line prefix
846 String prefix = stringify(i).pad_front(ndig);
847
848 // print all lines prefixed
849 for(auto it = lines.begin(); it != lines.end(); ++it)
850 os << '[' << prefix << "] " << (*it) << "\n";
851 }
852 }
853 else // rank != root
854 {
855 // send the message length via gather
856 std::size_t dummy(0), mylen = msg.size();
857 this->gather(&mylen, std::size_t(1), &dummy, std::size_t(1), root);
858
859 // send the message itself
860 if(mylen > std::size_t(0))
861 this->send(msg.data(), mylen, root);
862 }
863 }
864
865 void Comm::print_flush(std::ostream& os, int root) const
866 {
867 XASSERTM((0 <= root) && (root < _size), "invalid root rank argument");
868 if(root == _rank)
869 os << std::flush;
870 }
871
872 /* ######################################################################################### */
873 /* ######################################################################################### */
874 /* ######################################################################################### */
875#else // non-MPI build
876 /* ######################################################################################### */
877 /* ######################################################################################### */
878 /* ######################################################################################### */
879
880 bool initialize(int& /*argc*/, char**& /*argv*/)
881 {
882 // nothing to do here
883 return true;
884 }
885
886 void finalize()
887 {
889 }
890
891 // datatypes
892 // Note: The numbers for the datatypes are arbitrary; it is just important that each datatype
893 // has its own unique number.
894 const Datatype dt_byte ( 1, sizeof(char));
895 const Datatype dt_char ( 2, sizeof(char));
896 const Datatype dt_wchar ( 3, sizeof(wchar_t));
897 const Datatype dt_signed_char (11, sizeof(signed char));
898 const Datatype dt_signed_short (12, sizeof(short));
899 const Datatype dt_signed_int (13, sizeof(int));
900 const Datatype dt_signed_long (14, sizeof(long));
901 const Datatype dt_signed_long_long (15, sizeof(long long));
902 const Datatype dt_unsigned_char (21, sizeof(unsigned char));
903 const Datatype dt_unsigned_short (22, sizeof(unsigned short));
904 const Datatype dt_unsigned_int (23, sizeof(unsigned int));
905 const Datatype dt_unsigned_long (24, sizeof(unsigned long));
906 const Datatype dt_unsigned_long_long (25, sizeof(unsigned long long));
907 const Datatype dt_float (31, sizeof(float));
908 const Datatype dt_double (32, sizeof(double));
909 const Datatype dt_long_double (33, sizeof(long double));
910#ifdef FEAT_HAVE_QUADMATH
911 const Datatype dt__float128 (34, sizeof(__float128));
912#endif
913#ifdef FEAT_HAVE_HALFMATH
914 const Datatype dt__half (35, sizeof(__half));
915#endif
916 const Datatype dt_signed_int8 (41, sizeof(std::int8_t));
917 const Datatype dt_signed_int16 (42, sizeof(std::int16_t));
918 const Datatype dt_signed_int32 (43, sizeof(std::int32_t));
919 const Datatype dt_signed_int64 (44, sizeof(std::int64_t));
920 const Datatype dt_unsigned_int8 (51, sizeof(std::uint8_t));
921 const Datatype dt_unsigned_int16 (52, sizeof(std::uint16_t));
922 const Datatype dt_unsigned_int32 (53, sizeof(std::uint32_t));
923 const Datatype dt_unsigned_int64 (54, sizeof(std::uint64_t));
924 const Datatype dt_packed (55, sizeof(char));
925
926 // operations
927 const Operation op_sum(1);
928 const Operation op_max(2);
929 const Operation op_min(3);
930
931 /* ***************************************************************************************** */
932 /* ***************************************************************************************** */
933 /* Dummy Request wrapper implementation */
934 /* ***************************************************************************************** */
935 /* ***************************************************************************************** */
936
938 request(0)
939 {
940 }
941
942
943 Request::Request(Request&& other) :
944 request(other.request)
945 {
946 other.request = 0;
947 }
948
949 Request& Request::operator=(Request&& other)
950 {
951 if(this != &other)
952 {
953 this->request = other.request;
954 other.request = 0;
955 }
956 return *this;
957 }
958
959 bool Request::is_null() const
960 {
961 return request == 0;
962 }
963
964 void Request::free()
965 {
966 request = 0;
967 }
968
969 void Request::cancel()
970 {
971 }
972
973 bool Request::test(Status&)
974 {
975 request = 0;
976 return true;
977 }
978
979 bool Request::wait(Status&)
980 {
981 bool ret = (request != 0);
982 request = 0;
983 return ret;
984 }
985
986 /* ***************************************************************************************** */
987 /* ***************************************************************************************** */
988 /* MPI RequestVector implementation */
989 /* ***************************************************************************************** */
990 /* ***************************************************************************************** */
991
993 {
994 free();
995 return true;
996 }
997
998 bool RequestVector::test_any(std::size_t& idx, Status&)
999 {
1000 // try to find one non-null request
1001 for(std::size_t i(0); i < _reqs.size(); ++i)
1002 {
1003 if(!_reqs.at(i).is_null())
1004 {
1005 idx = i;
1006 _reqs.at(i).free();
1007 return true;
1008 }
1009 }
1010 // no active requests in vector
1011 return false;
1012 }
1013
1014 /*std::size_t RequestVector::test_some(std::size_t* indices, Status* statuses)
1015 {
1016 XASSERT(indices != nullptr);
1017 XASSERT(statuses != nullptr);
1018
1019 // return all non-null requests
1020 std::size_t k = 0;
1021 for(std::size_t i(0); i < _reqs.size(); ++i)
1022 {
1023 if(!_reqs.at(i).is_null())
1024 {
1025 indices[k++] = i;
1026 _reqs.at(i).free();
1027 }
1028 }
1029 return k;
1030 }*/
1031
1033 {
1034 free();
1035 }
1036
1037 bool RequestVector::wait_any(std::size_t& idx, Status& status)
1038 {
1039 return test_any(idx, status);
1040 }
1041
1042 /*std::size_t RequestVector::wait_some(std::size_t* indices, Status* statuses)
1043 {
1044 return test_some(indices, statuses);
1045 }*/
1046
1047 /* ***************************************************************************************** */
1048 /* ***************************************************************************************** */
1049 /* Dummy Comm wrapper implementation */
1050 /* ***************************************************************************************** */
1051 /* ***************************************************************************************** */
1052
1053 Comm::Comm() :
1054 _rank(0),
1055 _size(0)
1056 {
1057 }
1058
1059 Comm::Comm(int) :
1060 _rank(0),
1061 _size(1)
1062 {
1063 }
1064
1065 Comm::Comm(Comm&& other) :
1066 _rank(other._rank),
1067 _size(other._size)
1068 {
1069 }
1070
1071 Comm& Comm::operator=(Comm&& other)
1072 {
1073 if(this != &other)
1074 {
1075 _rank = other._rank;
1076 _size = other._size;
1077 }
1078 return *this;
1079 }
1080
1081 Comm::~Comm()
1082 {
1083 }
1084
1085 Comm Comm::world()
1086 {
1087 return Comm(1);
1088 }
1089
1090 Comm Comm::self()
1091 {
1092 return Comm(1);
1093 }
1094
1095 Comm Comm::null()
1096 {
1097 return Comm();
1098 }
1099
1100 bool Comm::is_null() const
1101 {
1102 return _size == 0;
1103 }
1104
1105 bool Comm::is_world() const
1106 {
1107 return _size == 1;
1108 }
1109
1110 bool Comm::is_self() const
1111 {
1112 return _size == 1;
1113 }
1114
1115 Comm Comm::comm_dup() const
1116 {
1117 return (_size == 0) ? Comm() : Comm(0);
1118 }
1119
1120 Comm Comm::comm_create_range_incl(int, int, int) const
1121 {
1122 return Comm(1);
1123 }
1124
1125 Comm Comm::comm_create_incl(int, const int*) const
1126 {
1127 return Comm(1);
1128 }
1129
1130 Comm Comm::comm_split(int, int) const
1131 {
1132 return Comm(1);
1133 }
1134
1135 void Comm::barrier() const
1136 {
1137 // nothing to do
1138 }
1139
1140 Request Comm::ibarrier() const
1141 {
1142 // nothing to do
1143 return Request();
1144 }
1145
1146 void Comm::bcast(void*, std::size_t, const Datatype&, int) const
1147 {
1148 // nothing to do
1149 }
1150
1151 Request Comm::ibcast(void*, std::size_t, const Datatype&, int) const
1152 {
1153 // nothing to do
1154 return Request();
1155 }
1156
1157 void Comm::gather(const void* sendbuf, std::size_t sendcount, const Datatype& sendtype, void* recvbuf, std::size_t recvcount, const Datatype& recvtype, int root) const
1158 {
1159 XASSERT(root == 0);
1160 alltoall(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
1161 }
1162
1163 Request Comm::igather(const void* sendbuf, std::size_t sendcount, const Datatype& sendtype, void* recvbuf, std::size_t recvcount, const Datatype& recvtype, int root) const
1164 {
1165 XASSERT(root == 0);
1166 alltoall(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
1167 return Request();
1168 }
1169
1170 void Comm::scatter(const void* sendbuf, std::size_t sendcount, const Datatype& sendtype, void* recvbuf, std::size_t recvcount, const Datatype& recvtype, int root) const
1171 {
1172 XASSERT(root == 0);
1173 alltoall(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
1174 }
1175
1176 Request Comm::iscatter(const void* sendbuf, std::size_t sendcount, const Datatype& sendtype, void* recvbuf, std::size_t recvcount, const Datatype& recvtype, int root) const
1177 {
1178 XASSERT(root == 0);
1179 alltoall(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
1180 return Request();
1181 }
1182
1183 void Comm::allgather(const void* sendbuf, std::size_t sendcount, const Datatype& sendtype, void* recvbuf, std::size_t recvcount, const Datatype& recvtype) const
1184 {
1185 alltoall(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
1186 }
1187
1188 Request Comm::iallgather(const void* sendbuf, std::size_t sendcount, const Datatype& sendtype, void* recvbuf, std::size_t recvcount, const Datatype& recvtype) const
1189 {
1190 alltoall(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
1191 return Request();
1192 }
1193
1194 void Comm::allgatherv(const void* sendbuf, std::size_t sendcount, const Datatype& sendtype, void* recvbuf, const int* recvcounts, const int* displs, const Datatype& recvtype) const
1195 {
1196 XASSERT(displs[0] == 0);
1197 alltoall(sendbuf, sendcount, sendtype, recvbuf, std::size_t(recvcounts[0]), recvtype);
1198 }
1199
1200 void Comm::alltoall(const void* sendbuf, std::size_t sendcount, const Datatype& sendtype, void* recvbuf, std::size_t recvcount, const Datatype& recvtype) const
1201 {
1202 XASSERT(sendcount == recvcount);
1203 XASSERT(sendtype == recvtype);
1204 // gcc 9.2.0 checks for non-nullptr receive buffer at compile-time,
1205 // which may issue false-positive warnings for code that is never executed at run-time
1206 if((recvbuf != sendbuf) && (recvbuf != nullptr))
1207 memcpy(recvbuf, sendbuf, sendcount * sendtype.size());
1208 }
1209
1210 Request Comm::ialltoall(const void* sendbuf, std::size_t sendcount, const Datatype& sendtype, void* recvbuf, std::size_t recvcount, const Datatype& recvtype) const
1211 {
1212 alltoall(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
1213 return Request();
1214 }
1215
1216 void Comm::alltoallv(const void* sendbuf, const int* sendcounts, const int* sdispls, const Datatype& sendtype, void* recvbuf, const int* recvcounts, const int* rdispls, const Datatype& recvtype) const
1217 {
1218 XASSERT(sdispls[0] == 0);
1219 XASSERT(rdispls[0] == 0);
1220 alltoall(sendbuf, std::size_t(sendcounts[0]), sendtype, recvbuf, std::size_t(recvcounts[0]), recvtype);
1221 }
1222
1223 void Comm::reduce(const void* sendbuf, void* recvbuf, std::size_t count, const Datatype& datatype, const Operation& op, int root) const
1224 {
1225 XASSERT(root == 0);
1226 allreduce(sendbuf, recvbuf, count, datatype, op);
1227 }
1228
1229 Request Comm::ireduce(const void* sendbuf, void* recvbuf, std::size_t count, const Datatype& datatype, const Operation& op, int root) const
1230 {
1231 XASSERT(root == 0);
1232 allreduce(sendbuf, recvbuf, count, datatype, op);
1233 return Request();
1234 }
1235
1236 void Comm::allreduce(const void* sendbuf, void* recvbuf, std::size_t count, const Datatype& datatype, const Operation&) const
1237 {
1238 // gcc 9.2.0 checks for non-nullptr receive buffer at compile-time,
1239 // which may issue false-positive warnings for code that is never executed at run-time
1240 if((recvbuf != sendbuf) && (recvbuf != nullptr))
1241 memcpy(recvbuf, sendbuf, count * datatype.size());
1242 }
1243
1244 Request Comm::iallreduce(const void* sendbuf, void* recvbuf, std::size_t count, const Datatype& datatype, const Operation& op) const
1245 {
1246 allreduce(sendbuf, recvbuf, count, datatype, op);
1247 return Request();
1248 }
1249
1250 void Comm::scan(const void* sendbuf, void* recvbuf, std::size_t count, const Datatype& datatype, const Operation& op) const
1251 {
1252 allreduce(sendbuf, recvbuf, count, datatype, op);
1253 }
1254
1255 /*Request Comm::iscan(const void* sendbuf, void* recvbuf, std::size_t count, const Datatype& datatype, const Operation& op) const
1256 {
1257 allreduce(sendbuf, recvbuf, count, datatype, op);
1258 return Request();
1259 }*/
1260
1261 void Comm::exscan(const void*, void*, std::size_t, const Datatype&, const Operation&) const
1262 {
1263 // nothing to do
1264 }
1265
1266 /*Request Comm::iexscan(const void*, void*, std::size_t, const Datatype&, const Operation&) const
1267 {
1268 // nothing to do
1269 return Request();
1270 }*/
1271
1272 void Comm::send(const void*, std::size_t, const Datatype&, int, int) const
1273 {
1274 // nothing to do
1275 }
1276
1277 Request Comm::isend(const void*, std::size_t, const Datatype&, int, int) const
1278 {
1279 // nothing to do
1280 return Request();
1281 }
1282
1283 void Comm::recv(void*, std::size_t, const Datatype&, int, int, Status&) const
1284 {
1285 // nothing to do
1286 }
1287
1288 Request Comm::irecv(void*, std::size_t, const Datatype&, int, int) const
1289 {
1290 // nothing to do
1291 return Request();
1292 }
1293
1294 void Comm::pack(const void* inbuf, std::size_t incount, const Datatype& datatype, void* outbuf, std::size_t /*outsize*/, int* position) const
1295 {
1296 if(inbuf != outbuf && outbuf != nullptr)
1297 {
1298 void* out_ptr = static_cast<char*>(outbuf) + *position;
1299 auto length = datatype.size() * incount;
1300 memcpy(out_ptr, inbuf, length);
1301 *position += (int)length;
1302 }
1303 }
1304
1305 void Comm::unpack(const void* inbuf, std::size_t /*insize*/, int* position, void* outbuf, std::size_t outcount, const Datatype& datatype) const
1306 {
1307 if(inbuf != outbuf && outbuf != nullptr)
1308 {
1309 void* out_ptr = static_cast<char*>(outbuf) + *position;
1310 auto length = datatype.size() * outcount;
1311 memcpy(out_ptr, inbuf, length);
1312 *position += (int)length;
1313 }
1314 }
1315
1316 void Comm::pack_size(std::size_t incount, const Datatype& datatype, int* size) const
1317 {
1318 *size = (int)(datatype.size() * incount);
1319 }
1320
1321 void Comm::bcast_stringstream(std::stringstream&, int) const
1322 {
1323 // nothing to do
1324 }
1325
1326 void Comm::bcast_binarystream(BinaryStream&, int) const
1327 {
1328 // nothing to do
1329 }
1330
1331 void Comm::print(std::ostream& os, const String& msg, int) const
1332 {
1333 os << msg << "\n";
1334 }
1335
1336 void Comm::allprint(std::ostream& os, const String& msg, int) const
1337 {
1338 os << msg << "\n";
1339 }
1340
1341 void Comm::print_flush(std::ostream& os, int) const
1342 {
1343 os << std::flush;
1344 }
1345#endif // FEAT_HAVE_MPI
1346 } // namespace Dist
1347} // namespace FEAT
#define XASSERT(expr)
Assertion macro definition.
Definition: assertion.hpp:262
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
Binary Stream class.
std::vector< char > & container()
Returns a reference to the internal vector container.
Communicator class.
Definition: dist.hpp:1657
void scatter(const void *sendbuf, std::size_t sendcount, const Datatype &sendtype, void *recvbuf, std::size_t recvcount, const Datatype &recvtype, int root) const
Blocking scatter.
Definition: dist.cpp:574
Comm & operator=(const Comm &)=delete
communicators are non-copyable
void bcast(void *buffer, std::size_t count, const Datatype &datatype, int root) const
Blocking broadcast.
Definition: dist.cpp:544
void reduce(const void *sendbuf, void *recvbuf, std::size_t count, const Datatype &datatype, const Operation &op, int root) const
Blocking Reduce.
Definition: dist.cpp:644
Request iallgather(const void *sendbuf, std::size_t sendcount, const Datatype &sendtype, void *recvbuf, std::size_t recvcount, const Datatype &recvtype) const
Nonblocking gather-to-all.
Definition: dist.cpp:600
Comm comm_create_range_incl(int count, int first=0, int stride=1) const
Creates a new sub-communicator from a strided range of ranks.
Definition: dist.cpp:475
void allreduce(const void *sendbuf, void *recvbuf, std::size_t count, const Datatype &datatype, const Operation &op) const
Blocking All-Reduce.
Definition: dist.cpp:658
void barrier() const
Blocking barrier.
Definition: dist.cpp:532
Request ibcast(void *buffer, std::size_t count, const Datatype &datatype, int root) const
Nonblocking broadcast.
Definition: dist.cpp:549
int size() const
Returns the size of this communicator.
Definition: dist.hpp:1814
void exscan(const void *sendbuf, void *recvbuf, std::size_t count, const Datatype &datatype, const Operation &op) const
Blocking Exclusive Scan.
Definition: dist.cpp:686
Comm comm_create_incl(int n, const int *ranks) const
Creates a new sub-communicator for a given set of ranks.
Definition: dist.cpp:501
Request igather(const void *sendbuf, std::size_t sendcount, const Datatype &sendtype, void *recvbuf, std::size_t recvcount, const Datatype &recvtype, int root) const
Nonblocking gather.
Definition: dist.cpp:564
static Comm null()
Returns a null communicator.
Definition: dist.cpp:442
Request ibarrier() const
Nonblocking barrier.
Definition: dist.cpp:537
int _rank
our rank
Definition: dist.hpp:1666
virtual ~Comm()
virtual destructor
Definition: dist.cpp:425
Request iallreduce(const void *sendbuf, void *recvbuf, std::size_t count, const Datatype &datatype, const Operation &op) const
Nonblocking All-Reduce.
Definition: dist.cpp:663
Request irecv(void *buffer, std::size_t count, const Datatype &datatype, int source, int tag=0) const
Nonblocking Receive.
Definition: dist.cpp:719
Comm()
Standard constructor.
Definition: dist.cpp:375
Comm comm_split(int color, int key) const
Creates a new sub-communicator by splitting this communicator.
Definition: dist.cpp:525
bool is_world() const
Checks whether this communicator is the world communicator.
Definition: dist.cpp:447
void bcast_binarystream(BinaryStream &stream, int root=0) const
Blocking broadcast of a BinaryStream.
Definition: dist.cpp:775
void recv(void *buffer, std::size_t count, const Datatype &datatype, int source, int tag, Status &status) const
Blocking Receive.
Definition: dist.cpp:714
Request isend(const void *buffer, std::size_t count, const Datatype &datatype, int dest, int tag=0) const
Nonblocking Send.
Definition: dist.cpp:707
int _size
the communicator size
Definition: dist.hpp:1668
MPI_Comm comm
our MPI communicator handle
Definition: dist.hpp:1661
Request iscatter(const void *sendbuf, std::size_t sendcount, const Datatype &sendtype, void *recvbuf, std::size_t recvcount, const Datatype &recvtype, int root) const
Nonblocking scatter.
Definition: dist.cpp:582
void unpack(const void *inbuf, std::size_t insize, int *position, void *outbuf, std::size_t outcount, const Datatype &datatype) const
Unpack message.
Definition: dist.cpp:731
void gather(const void *sendbuf, std::size_t sendcount, const Datatype &sendtype, void *recvbuf, std::size_t recvcount, const Datatype &recvtype, int root) const
Blocking gather.
Definition: dist.cpp:556
static Comm self()
Returns a copy of the self communicator.
Definition: dist.cpp:437
bool is_self() const
Checks whether this communicator is the self communicator.
Definition: dist.cpp:452
void alltoallv(const void *sendbuf, const int *sendcounts, const int *sdispls, const Datatype &sendtype, void *recvbuf, const int *recvcounts, const int *rdispls, const Datatype &recvtype) const
Blocking All-to-All Scatter/Gather.
Definition: dist.cpp:636
void pack_size(std::size_t incount, const Datatype &datatype, int *size) const
Determine upper bound on increment of position by call to pack/unpack.
Definition: dist.cpp:736
void alltoall(const void *sendbuf, std::size_t sendcount, const Datatype &sendtype, void *recvbuf, std::size_t recvcount, const Datatype &recvtype) const
Blocking All-to-All Scatter/Gather.
Definition: dist.cpp:618
void send(const void *buffer, std::size_t count, const Datatype &datatype, int dest, int tag=0) const
Blocking Send.
Definition: dist.cpp:702
void pack(const void *inbuf, std::size_t incount, const Datatype &datatype, void *outbuf, std::size_t outsize, int *position) const
Pack message.
Definition: dist.cpp:726
void print_flush(std::ostream &os, int root=0) const
Explicitly flushes the output stream.
Definition: dist.cpp:865
Comm comm_dup() const
Creates a copy of this communicator.
Definition: dist.cpp:462
void allprint(std::ostream &os, const String &msg, int root=0) const
Prints the ordered messages of all processes to an output stream.
Definition: dist.cpp:807
Request ireduce(const void *sendbuf, void *recvbuf, std::size_t count, const Datatype &datatype, const Operation &op, int root) const
Nonblocking Reduce.
Definition: dist.cpp:650
void allgatherv(const void *sendbuf, std::size_t sendcount, const Datatype &sendtype, void *recvbuf, const int *recvcounts, const int *displs, const Datatype &recvtype) const
Blocking gather-to-all.
Definition: dist.cpp:610
Request ialltoall(const void *sendbuf, std::size_t sendcount, const Datatype &sendtype, void *recvbuf, std::size_t recvcount, const Datatype &recvtype) const
Nonblocking All-to-All Scatter/Gather.
Definition: dist.cpp:626
int rank() const
Returns the rank of this process in this communicator.
Definition: dist.hpp:1802
void allgather(const void *sendbuf, std::size_t sendcount, const Datatype &sendtype, void *recvbuf, std::size_t recvcount, const Datatype &recvtype) const
Blocking gather-to-all.
Definition: dist.cpp:592
bool is_null() const
Checks whether this communicator is a null communicator.
Definition: dist.cpp:457
static Comm world()
Returns a copy of the world communicator.
Definition: dist.cpp:432
void print(std::ostream &os, const String &msg, int root=0) const
Prints a message line to an output stream.
Definition: dist.cpp:800
void scan(const void *sendbuf, void *recvbuf, std::size_t count, const Datatype &datatype, const Operation &op) const
Blocking Inclusive Scan.
Definition: dist.cpp:670
void bcast_stringstream(std::stringstream &stream, int root=0) const
Blocking broadcast of a std::stringstream.
Definition: dist.cpp:741
Communication Request class.
Definition: dist.hpp:731
MPI_Request request
our internal MPI request handle
Definition: dist.hpp:735
Request()
Standard constructor.
Definition: dist.cpp:221
bool wait()
Blocks until the request is fulfilled (or null).
Definition: dist.hpp:896
bool test()
Tests whether the request is fulfilled (or null).
Definition: dist.hpp:864
void free()
Frees the request.
Definition: dist.cpp:253
void cancel()
Cancels the request.
Definition: dist.cpp:259
Request & operator=(const Request &)=delete
Request objects are non-copyable.
bool is_null() const
Checks whether this request is null.
Definition: dist.cpp:248
bool test_all()
Tests whether all active requests are fulfilled (or null).
Definition: dist.cpp:286
bool test_any(std::size_t &idx, Status &status)
Tests whether one of the active requests has been fulfilled.
Definition: dist.cpp:293
std::vector< Request > _reqs
internal vector of Request objects
Definition: dist.hpp:951
void free()
Frees all remaining active requests.
Definition: dist.hpp:1282
bool wait_any(std::size_t &idx, Status &status)
Blocks until one of the active requests has been fulfilled.
Definition: dist.cpp:332
void wait_all()
Blocks until all active requests are fulfilled.
Definition: dist.cpp:327
Communication Status class.
Definition: dist.hpp:595
MPI_Status * mpi_status()
Definition: dist.hpp:614
void free()
Frees all commited Datatypes.
Definition: dist.hpp:481
static TypeRegistry & instance() noexcept
Singleton accessor.
Definition: dist.hpp:370
String class implementation.
Definition: string.hpp:47
std::deque< String > split_by_charset(const String &charset) const
Splits the string by a delimiter charset.
Definition: string.hpp:468
String pad_front(size_type len, char c=' ') const
Pads the front of the string up to a desired length.
Definition: string.hpp:393
bool initialize(int &argc, char **&argv)
Initializes the distributed communication system.
Definition: dist.cpp:105
const Datatype dt_byte(MPI_BYTE, sizeof(char))
Datatype wrapper for MPI_BYTE.
Definition: dist.hpp:129
const Datatype dt_wchar(MPI_WCHAR, sizeof(wchar_t))
Datatype wrapper for MPI_WCHAR.
Definition: dist.hpp:133
const Datatype dt_signed_long(MPI_LONG, sizeof(long))
Datatype wrapper for MPI_LONG.
Definition: dist.hpp:141
const Datatype dt_float(MPI_FLOAT, sizeof(float))
Datatype wrapper for MPI_FLOAT.
Definition: dist.hpp:155
const Datatype dt_signed_char(MPI_SIGNED_CHAR, sizeof(signed char))
Datatype wrapper for MPI_SIGNED_CHAR.
Definition: dist.hpp:135
const Datatype dt_signed_short(MPI_SHORT, sizeof(short))
Datatype wrapper for MPI_SHORT.
Definition: dist.hpp:137
const Datatype dt_unsigned_long_long(MPI_UNSIGNED_LONG_LONG, sizeof(unsigned long long))
Datatype wrapper for MPI_UNSIGNED_LONG_LONG.
Definition: dist.hpp:153
const Datatype dt_signed_int32(MPI_INT32_T, sizeof(std::int32_t))
Datatype wrapper for MPI_INT32_T.
Definition: dist.hpp:165
const Datatype dt_unsigned_int32(MPI_UINT32_T, sizeof(std::uint32_t))
Datatype wrapper for MPI_UINT32_T.
Definition: dist.hpp:173
void finalize()
Finalizes the distributed communication system.
Definition: dist.cpp:148
const Datatype dt_signed_int8(MPI_INT8_T, sizeof(std::int8_t))
Datatype wrapper for MPI_INT8_T.
Definition: dist.hpp:161
const Datatype dt_char(MPI_CHAR, sizeof(char))
Datatype wrapper for MPI_CHAR.
Definition: dist.hpp:131
const Operation op_min(MPI_MIN)
Operation wrapper for MPI_MIN.
Definition: dist.hpp:575
const Datatype dt_unsigned_int(MPI_UNSIGNED, sizeof(unsigned int))
Datatype wrapper for MPI_UNSIGNED.
Definition: dist.hpp:149
const Datatype dt_unsigned_int16(MPI_UINT16_T, sizeof(std::uint16_t))
Datatype wrapper for MPI_UINT16_T.
Definition: dist.hpp:171
const Datatype dt_unsigned_int8(MPI_UINT8_T, sizeof(std::uint8_t))
Datatype wrapper for MPI_UINT8_T.
Definition: dist.hpp:169
const Datatype dt__half
custom Datatype for __half
const Datatype dt_signed_int(MPI_INT, sizeof(int))
Datatype wrapper for MPI_INT.
Definition: dist.hpp:139
const Datatype dt_signed_long_long(MPI_LONG_LONG, sizeof(long long))
Datatype wrapper for MPI_LONG_LONG.
Definition: dist.hpp:143
const Operation op_max(MPI_MAX)
Operation wrapper for MPI_MAX.
Definition: dist.hpp:573
const Datatype dt_double(MPI_DOUBLE, sizeof(double))
Datatype wrapper for MPI_DOUBLE.
Definition: dist.hpp:157
const Datatype dt_unsigned_char(MPI_UNSIGNED_CHAR, sizeof(unsigned char))
Datatype wrapper for MPI_UNSIGNED_CHAR.
Definition: dist.hpp:145
const Datatype dt_packed(MPI_PACKED, sizeof(char))
Datatype wrapper for MPI_PACKED.
Definition: dist.hpp:177
const Datatype dt_signed_int16(MPI_INT16_T, sizeof(std::int16_t))
Datatype wrapper for MPI_INT16_T.
Definition: dist.hpp:163
const Datatype dt_unsigned_short(MPI_UNSIGNED_SHORT, sizeof(unsigned short))
Datatype wrapper for MPI_UNSIGNED_SHORT.
Definition: dist.hpp:147
const Datatype dt_unsigned_int64(MPI_UINT64_T, sizeof(std::uint64_t))
Datatype wrapper for MPI_UINT64_T.
Definition: dist.hpp:175
const Datatype dt_long_double(MPI_LONG_DOUBLE, sizeof(long double))
Datatype wrapper for MPI_LONG_DOUBLE.
Definition: dist.hpp:159
const Operation op_sum(MPI_SUM)
Operation wrapper for MPI_SUM.
Definition: dist.hpp:571
const Datatype dt__float128
custom Datatype for __float128
const Datatype dt_unsigned_long(MPI_UNSIGNED_LONG, sizeof(unsigned long))
Datatype wrapper for MPI_UNSIGNED_LONG.
Definition: dist.hpp:151
const Datatype dt_signed_int64(MPI_INT64_T, sizeof(std::int64_t))
Datatype wrapper for MPI_INT64_T.
Definition: dist.hpp:167
@ other
generic/other permutation strategy
T_ ilog10(T_ x)
Computes the integral base-10 logarithm of an integer, i.e. its number of non-zero decimal digits.
Definition: math.hpp:231
FEAT namespace.
Definition: adjactor.hpp:12
String stringify(const T_ &item)
Converts an item into a String.
Definition: string.hpp:993
Communication Datatype class.
Definition: dist.hpp:80
MPI_Datatype dt
the MPI datatype handle
Definition: dist.hpp:83
Communication Operation class.
Definition: dist.hpp:537
MPI_Op op
the MPI operation handle
Definition: dist.hpp:540