7#include <kernel/util/dist.hpp>
8#include <kernel/util/math.hpp>
18#if defined(FEAT_HAVE_MPI) || defined(DOXYGEN)
20#ifdef FEAT_OVERRIDE_MPI_OPS
25 static inline void eval(T_& x, T_& y)
35 static inline void eval(T_& x, T_& y)
46 static inline void eval(T_& x, T_& y)
56 template<
typename Op_,
typename T_>
57 inline void func_op_t(
void* iv,
void* iov,
int* n)
59 T_* y =
reinterpret_cast<T_*
>(iv);
60 T_* x =
reinterpret_cast<T_*
>(iov);
61 for(
int i(0); i < *n; ++i)
63 Op_::eval(x[i], y[i]);
68 template<
typename Op_>
69 void op_callback(
void* iv,
void* iov,
int* n, MPI_Datatype* dt)
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
95#ifdef FEAT_HAVE_HALFMATH
96 if(*dt ==
dt__half.
dt) func_op_t<Op_, __half> (iv, iov, n);
else
100 MPI_Abort(MPI_COMM_WORLD, 1);
107 int already_initialized(0);
108 if (::MPI_Initialized(&already_initialized) != MPI_SUCCESS)
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;
115 int required = MPI_THREAD_SERIALIZED;
117 int provided = MPI_THREAD_SINGLE;
119 if (!already_initialized && ::MPI_Init_thread(&argc, &argv, required, &provided) != MPI_SUCCESS)
122 if (!already_initialized && (provided < required))
125#ifdef FEAT_HAVE_QUADMATH
128 MPI_Type_contiguous(
int(
sizeof(__float128)), MPI_BYTE, &dt_f128.
dt);
129 MPI_Type_commit(&dt_f128.
dt);
131#ifdef FEAT_HAVE_HALFMATH
134 MPI_Type_contiguous(
int(
sizeof(__half)), MPI_BYTE, &dt_half.
dt);
135 MPI_Type_commit(&dt_half.
dt);
138#ifdef FEAT_OVERRIDE_MPI_OPS
151#ifdef FEAT_OVERRIDE_MPI_OPS
153 MPI_Op_free(&my_op_sum.
op);
156#ifdef FEAT_HAVE_QUADMATH
158 MPI_Type_free(&dt_f128.
dt);
161#ifdef FEAT_HAVE_HALFMATH
163 MPI_Type_free(&dt_half.
dt);
169 int already_finalized(0);
170 ::MPI_Finalized(&already_finalized);
171 if (!already_finalized)
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));
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));
200 const Datatype
dt_packed (MPI_PACKED,
sizeof(
char));
201#ifdef FEAT_HAVE_QUADMATH
205#ifdef FEAT_HAVE_HALFMATH
207 const Datatype
dt__half (0,
sizeof(__half));
211 const Operation
op_sum(MPI_SUM);
212 const Operation
op_max(MPI_MAX);
213 const Operation
op_min(MPI_MIN);
222 request(MPI_REQUEST_NULL)
232 request(other.request)
234 other.request = MPI_REQUEST_NULL;
243 other.request = MPI_REQUEST_NULL;
250 return request == MPI_REQUEST_NULL;
255 if(
request != MPI_REQUEST_NULL)
261 if(
request != MPI_REQUEST_NULL)
274 if(
request == MPI_REQUEST_NULL)
289 MPI_Testall(_isize(), _reqs_array(), &flag, _stats_array());
297 MPI_Testany(_isize(), _reqs_array(), &i, &flag, status.
mpi_status());
300 idx = std::size_t(i);
329 MPI_Waitall(_isize(), _reqs_array(), _stats_array());
335 MPI_Waitany(_isize(), _reqs_array(), &i, status.
mpi_status());
336 if(i == MPI_UNDEFINED)
338 idx = std::size_t(i);
387 if(comm_ != MPI_COMM_NULL)
400 if((other.comm != MPI_COMM_WORLD) && (other.comm != MPI_COMM_SELF))
402 other.comm = MPI_COMM_NULL;
403 other._rank = other._size = 0;
416 if((other.comm != MPI_COMM_WORLD) && (other.comm != MPI_COMM_SELF))
418 other.comm = MPI_COMM_NULL;
419 other._rank = other._size = 0;
428 if((
comm != MPI_COMM_WORLD) && (
comm != MPI_COMM_SELF) && (
comm != MPI_COMM_NULL))
429 MPI_Comm_free(&
comm);
434 return Comm(MPI_COMM_WORLD);
439 return Comm(MPI_COMM_SELF);
444 return Comm(MPI_COMM_NULL);
449 return (
comm == MPI_COMM_WORLD);
454 return (
comm == MPI_COMM_SELF);
459 return (
comm == MPI_COMM_NULL);
465 if((
comm == MPI_COMM_WORLD) || (
comm == MPI_COMM_SELF) || (
comm == MPI_COMM_NULL))
469 MPI_Comm newcomm = MPI_COMM_NULL;
470 MPI_Comm_dup(
comm, &newcomm);
472 return Comm(newcomm);
482 MPI_Group group = MPI_GROUP_NULL;
483 MPI_Comm_group(
comm, &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);
491 MPI_Comm newcomm = MPI_COMM_NULL;
492 MPI_Comm_create(
comm, newgroup, &newcomm);
495 MPI_Group_free(&newgroup);
496 MPI_Group_free(&group);
498 return Comm(newcomm);
507 MPI_Group group = MPI_GROUP_NULL;
508 MPI_Comm_group(
comm, &group);
511 MPI_Group newgroup = MPI_GROUP_NULL;
512 MPI_Group_incl(group, n, ranks, &newgroup);
515 MPI_Comm newcomm = MPI_COMM_NULL;
516 MPI_Comm_create(
comm, newgroup, &newcomm);
519 MPI_Group_free(&newgroup);
520 MPI_Group_free(&group);
522 return Comm(newcomm);
527 MPI_Comm newcomm = MPI_COMM_NULL;
528 MPI_Comm_split(
comm, color, key, &newcomm);
529 return Comm(newcomm);
539 MPI_Request req(MPI_REQUEST_NULL);
540 MPI_Ibarrier(
comm, &req);
546 MPI_Bcast(buffer,
int(count), datatype.
dt, root,
comm);
551 MPI_Request req(MPI_REQUEST_NULL);
552 MPI_Ibcast(buffer,
int(count), datatype.
dt, root,
comm, &req);
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
558 if((sendbuf == recvbuf) && (root ==
_rank))
559 MPI_Gather(MPI_IN_PLACE, 0, recvtype.
dt, recvbuf,
int(recvcount), recvtype.
dt, root,
comm);
561 MPI_Gather(sendbuf,
int(sendcount), sendtype.
dt, recvbuf,
int(recvcount), recvtype.
dt, root,
comm);
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);
570 MPI_Igather(sendbuf,
int(sendcount), sendtype.
dt, recvbuf,
int(recvcount), recvtype.
dt, root,
comm, &req);
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
576 if((sendbuf == recvbuf) && (root ==
_rank))
577 MPI_Scatter(sendbuf,
int(sendcount), sendtype.
dt, MPI_IN_PLACE, 0, sendtype.
dt, root,
comm);
579 MPI_Scatter(sendbuf,
int(sendcount), sendtype.
dt, recvbuf,
int(recvcount), recvtype.
dt, root,
comm);
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);
588 MPI_Iscatter(sendbuf,
int(sendcount), sendtype.
dt, recvbuf,
int(recvcount), recvtype.
dt, root,
comm, &req);
594 if(sendbuf == recvbuf)
595 MPI_Allgather(MPI_IN_PLACE, 0, recvtype.
dt, recvbuf,
int(recvcount), recvtype.
dt,
comm);
597 MPI_Allgather(sendbuf,
int(sendcount), sendtype.
dt, recvbuf,
int(recvcount), recvtype.
dt,
comm);
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);
606 MPI_Iallgather(sendbuf,
int(sendcount), sendtype.
dt, recvbuf,
int(recvcount), recvtype.
dt,
comm, &req);
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
612 if(sendbuf == recvbuf)
613 MPI_Allgatherv(MPI_IN_PLACE, 0, recvtype.
dt, recvbuf, recvcounts, displs, recvtype.
dt,
comm);
615 MPI_Allgatherv(sendbuf,
int(sendcount), sendtype.
dt, recvbuf, recvcounts, displs, recvtype.
dt,
comm);
620 if(sendbuf == recvbuf)
621 MPI_Alltoall(MPI_IN_PLACE, 0, recvtype.
dt, recvbuf,
int(recvcount), recvtype.
dt,
comm);
623 MPI_Alltoall(sendbuf,
int(sendcount), sendtype.
dt, recvbuf,
int(recvcount), recvtype.
dt,
comm);
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);
632 MPI_Ialltoall(sendbuf,
int(sendcount), sendtype.
dt, recvbuf,
int(recvcount), recvtype.
dt,
comm, &req);
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
638 if(sendbuf == recvbuf)
639 MPI_Alltoallv(MPI_IN_PLACE, 0, 0, recvtype.
dt, recvbuf, recvcounts, rdispls, recvtype.
dt,
comm);
641 MPI_Alltoallv(sendbuf, sendcounts, sdispls, sendtype.
dt, recvbuf, recvcounts, rdispls, recvtype.
dt,
comm);
647 MPI_Reduce((sendbuf == recvbuf) && (
_rank == root) ? MPI_IN_PLACE : sendbuf, recvbuf,
int(count), datatype.
dt, op.
op, root,
comm);
652 MPI_Request req(MPI_REQUEST_NULL);
654 MPI_Ireduce((sendbuf == recvbuf) && (
_rank == root) ? MPI_IN_PLACE : sendbuf, recvbuf,
int(count), datatype.
dt, op.
op, root,
comm, &req);
660 MPI_Allreduce(sendbuf == recvbuf ? MPI_IN_PLACE : sendbuf, recvbuf,
int(count), datatype.
dt, op.
op,
comm);
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);
672 MPI_Scan(sendbuf == recvbuf ? MPI_IN_PLACE : sendbuf, recvbuf,
int(count), datatype.
dt, op.
op,
comm);
688 MPI_Exscan(sendbuf == recvbuf ? MPI_IN_PLACE : sendbuf, recvbuf,
int(count), datatype.
dt, op.
op,
comm);
702 void Comm::send(
const void* buffer, std::size_t count,
const Datatype& datatype,
int dest,
int tag)
const
704 MPI_Send(buffer,
int(count), datatype.
dt, dest, tag,
comm);
709 MPI_Request req(MPI_REQUEST_NULL);
710 MPI_Isend(buffer,
int(count), datatype.
dt, dest, tag,
comm, &req);
716 MPI_Recv(buffer,
int(count), datatype.
dt, source, tag,
comm, status.
mpi_status());
721 MPI_Request req(MPI_REQUEST_NULL);
722 MPI_Irecv(buffer,
int(count), datatype.
dt, source, tag,
comm, &req);
726 void Comm::pack(
const void* inbuf, std::size_t incount,
const Datatype& datatype,
void* outbuf, std::size_t outsize,
int* position)
const
728 MPI_Pack(inbuf, (
int)incount, datatype.
dt, outbuf, (
int)outsize, position,
comm);
731 void Comm::unpack(
const void* inbuf, std::size_t insize,
int* position,
void* outbuf, std::size_t outcount,
const Datatype& datatype)
const
733 MPI_Unpack(inbuf, (
int)insize, position, outbuf, (
int)outcount, datatype.
dt,
comm);
738 MPI_Pack_size((
int)incount, datatype.
dt,
comm,
size);
754 bcast(&len, std::size_t(1), root);
757 if(len == std::size_t(0))
761 std::vector<char> buf(len + std::size_t(1));
765 std::strcpy(buf.data(), str.c_str());
768 bcast(buf.data(), buf.size(), root);
772 stream << std::string(buf.data(), len);
778 std::vector<char>& data = stream.
container();
786 bcast(&len, std::size_t(1), root);
789 if(len == std::size_t(0))
797 bcast(data.data(), data.size(), root);
802 XASSERTM((0 <= root) && (root <
_size),
"invalid root rank argument");
809 XASSERTM((0 <= root) && (root <
_size),
"invalid root rank argument");
814 const std::size_t n = std::size_t(
_size);
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);
823 for(
int i(0); i <
_size; ++i)
826 std::size_t length = lengths.at(std::size_t(i));
827 if(length == std::size_t(0))
837 std::vector<char> msgbuf(length);
838 this->
recv(msgbuf.data(), length, i);
839 message.assign(msgbuf.data(), length);
849 for(
auto it = lines.begin(); it != lines.end(); ++it)
850 os <<
'[' << prefix <<
"] " << (*it) <<
"\n";
856 std::size_t dummy(0), mylen = msg.size();
857 this->
gather(&mylen, std::size_t(1), &dummy, std::size_t(1), root);
860 if(mylen > std::size_t(0))
861 this->
send(msg.data(), mylen, root);
867 XASSERTM((0 <= root) && (root <
_size),
"invalid root rank argument");
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));
907 const Datatype
dt_float (31,
sizeof(
float));
908 const Datatype
dt_double (32,
sizeof(
double));
910#ifdef FEAT_HAVE_QUADMATH
913#ifdef FEAT_HAVE_HALFMATH
914 const Datatype
dt__half (35,
sizeof(__half));
924 const Datatype
dt_packed (55,
sizeof(
char));
927 const Operation
op_sum(1);
928 const Operation
op_max(2);
929 const Operation
op_min(3);
944 request(
other.request)
1001 for(std::size_t i(0); i <
_reqs.size(); ++i)
1003 if(!
_reqs.at(i).is_null())
1146 void Comm::bcast(
void*, std::size_t,
const Datatype&,
int)
const
1151 Request
Comm::ibcast(
void*, std::size_t,
const Datatype&,
int)
const
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
1160 alltoall(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
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
1166 alltoall(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
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
1173 alltoall(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
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
1179 alltoall(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
1183 void Comm::allgather(
const void* sendbuf, std::size_t sendcount,
const Datatype& sendtype,
void* recvbuf, std::size_t recvcount,
const Datatype& recvtype)
const
1185 alltoall(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
1188 Request
Comm::iallgather(
const void* sendbuf, std::size_t sendcount,
const Datatype& sendtype,
void* recvbuf, std::size_t recvcount,
const Datatype& recvtype)
const
1190 alltoall(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
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
1197 alltoall(sendbuf, sendcount, sendtype, recvbuf, std::size_t(recvcounts[0]), recvtype);
1200 void Comm::alltoall(
const void* sendbuf, std::size_t sendcount,
const Datatype& sendtype,
void* recvbuf, std::size_t recvcount,
const Datatype& recvtype)
const
1202 XASSERT(sendcount == recvcount);
1203 XASSERT(sendtype == recvtype);
1206 if((recvbuf != sendbuf) && (recvbuf !=
nullptr))
1207 memcpy(recvbuf, sendbuf, sendcount * sendtype.size());
1210 Request
Comm::ialltoall(
const void* sendbuf, std::size_t sendcount,
const Datatype& sendtype,
void* recvbuf, std::size_t recvcount,
const Datatype& recvtype)
const
1212 alltoall(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype);
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
1220 alltoall(sendbuf, std::size_t(sendcounts[0]), sendtype, recvbuf, std::size_t(recvcounts[0]), recvtype);
1223 void Comm::reduce(
const void* sendbuf,
void* recvbuf, std::size_t count,
const Datatype& datatype,
const Operation& op,
int root)
const
1226 allreduce(sendbuf, recvbuf, count, datatype, op);
1229 Request
Comm::ireduce(
const void* sendbuf,
void* recvbuf, std::size_t count,
const Datatype& datatype,
const Operation& op,
int root)
const
1232 allreduce(sendbuf, recvbuf, count, datatype, op);
1236 void Comm::allreduce(
const void* sendbuf,
void* recvbuf, std::size_t count,
const Datatype& datatype,
const Operation&)
const
1240 if((recvbuf != sendbuf) && (recvbuf !=
nullptr))
1241 memcpy(recvbuf, sendbuf, count * datatype.size());
1244 Request
Comm::iallreduce(
const void* sendbuf,
void* recvbuf, std::size_t count,
const Datatype& datatype,
const Operation& op)
const
1246 allreduce(sendbuf, recvbuf, count, datatype, op);
1250 void Comm::scan(
const void* sendbuf,
void* recvbuf, std::size_t count,
const Datatype& datatype,
const Operation& op)
const
1252 allreduce(sendbuf, recvbuf, count, datatype, op);
1261 void Comm::exscan(
const void*,
void*, std::size_t,
const Datatype&,
const Operation&)
const
1272 void Comm::send(
const void*, std::size_t,
const Datatype&,
int,
int)
const
1277 Request
Comm::isend(
const void*, std::size_t,
const Datatype&,
int,
int)
const
1283 void Comm::recv(
void*, std::size_t,
const Datatype&,
int,
int, Status&)
const
1288 Request
Comm::irecv(
void*, std::size_t,
const Datatype&,
int,
int)
const
1294 void Comm::pack(
const void* inbuf, std::size_t incount,
const Datatype& datatype,
void* outbuf, std::size_t ,
int* position)
const
1296 if(inbuf != outbuf && outbuf !=
nullptr)
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;
1305 void Comm::unpack(
const void* inbuf, std::size_t ,
int* position,
void* outbuf, std::size_t outcount,
const Datatype& datatype)
const
1307 if(inbuf != outbuf && outbuf !=
nullptr)
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;
1316 void Comm::pack_size(std::size_t incount,
const Datatype& datatype,
int* size)
const
1318 *
size = (int)(datatype.size() * incount);
1331 void Comm::print(std::ostream& os,
const String& msg,
int)
const
1336 void Comm::allprint(std::ostream& os,
const String& msg,
int)
const
#define XASSERT(expr)
Assertion macro definition.
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
std::vector< char > & container()
Returns a reference to the internal vector container.
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.
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.
void reduce(const void *sendbuf, void *recvbuf, std::size_t count, const Datatype &datatype, const Operation &op, int root) const
Blocking Reduce.
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.
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.
void allreduce(const void *sendbuf, void *recvbuf, std::size_t count, const Datatype &datatype, const Operation &op) const
Blocking All-Reduce.
void barrier() const
Blocking barrier.
Request ibcast(void *buffer, std::size_t count, const Datatype &datatype, int root) const
Nonblocking broadcast.
int size() const
Returns the size of this communicator.
void exscan(const void *sendbuf, void *recvbuf, std::size_t count, const Datatype &datatype, const Operation &op) const
Blocking Exclusive Scan.
Comm comm_create_incl(int n, const int *ranks) const
Creates a new sub-communicator for a given set of ranks.
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.
static Comm null()
Returns a null communicator.
Request ibarrier() const
Nonblocking barrier.
virtual ~Comm()
virtual destructor
Request iallreduce(const void *sendbuf, void *recvbuf, std::size_t count, const Datatype &datatype, const Operation &op) const
Nonblocking All-Reduce.
Request irecv(void *buffer, std::size_t count, const Datatype &datatype, int source, int tag=0) const
Nonblocking Receive.
Comm()
Standard constructor.
Comm comm_split(int color, int key) const
Creates a new sub-communicator by splitting this communicator.
bool is_world() const
Checks whether this communicator is the world communicator.
void bcast_binarystream(BinaryStream &stream, int root=0) const
Blocking broadcast of a BinaryStream.
void recv(void *buffer, std::size_t count, const Datatype &datatype, int source, int tag, Status &status) const
Blocking Receive.
Request isend(const void *buffer, std::size_t count, const Datatype &datatype, int dest, int tag=0) const
Nonblocking Send.
int _size
the communicator size
MPI_Comm comm
our MPI communicator handle
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.
void unpack(const void *inbuf, std::size_t insize, int *position, void *outbuf, std::size_t outcount, const Datatype &datatype) const
Unpack message.
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.
static Comm self()
Returns a copy of the self communicator.
bool is_self() const
Checks whether this communicator is the self communicator.
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.
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.
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.
void send(const void *buffer, std::size_t count, const Datatype &datatype, int dest, int tag=0) const
Blocking Send.
void pack(const void *inbuf, std::size_t incount, const Datatype &datatype, void *outbuf, std::size_t outsize, int *position) const
Pack message.
void print_flush(std::ostream &os, int root=0) const
Explicitly flushes the output stream.
Comm comm_dup() const
Creates a copy of this communicator.
void allprint(std::ostream &os, const String &msg, int root=0) const
Prints the ordered messages of all processes to an output stream.
Request ireduce(const void *sendbuf, void *recvbuf, std::size_t count, const Datatype &datatype, const Operation &op, int root) const
Nonblocking Reduce.
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.
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.
int rank() const
Returns the rank of this process in this communicator.
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.
bool is_null() const
Checks whether this communicator is a null communicator.
static Comm world()
Returns a copy of the world communicator.
void print(std::ostream &os, const String &msg, int root=0) const
Prints a message line to an output stream.
void scan(const void *sendbuf, void *recvbuf, std::size_t count, const Datatype &datatype, const Operation &op) const
Blocking Inclusive Scan.
void bcast_stringstream(std::stringstream &stream, int root=0) const
Blocking broadcast of a std::stringstream.
Communication Request class.
MPI_Request request
our internal MPI request handle
Request()
Standard constructor.
bool wait()
Blocks until the request is fulfilled (or null).
bool test()
Tests whether the request is fulfilled (or null).
void free()
Frees the request.
void cancel()
Cancels the request.
Request & operator=(const Request &)=delete
Request objects are non-copyable.
bool is_null() const
Checks whether this request is null.
bool test_all()
Tests whether all active requests are fulfilled (or null).
bool test_any(std::size_t &idx, Status &status)
Tests whether one of the active requests has been fulfilled.
std::vector< Request > _reqs
internal vector of Request objects
void free()
Frees all remaining active requests.
bool wait_any(std::size_t &idx, Status &status)
Blocks until one of the active requests has been fulfilled.
void wait_all()
Blocks until all active requests are fulfilled.
Communication Status class.
MPI_Status * mpi_status()
void free()
Frees all commited Datatypes.
static TypeRegistry & instance() noexcept
Singleton accessor.
String class implementation.
std::deque< String > split_by_charset(const String &charset) const
Splits the string by a delimiter charset.
String pad_front(size_type len, char c=' ') const
Pads the front of the string up to a desired length.
bool initialize(int &argc, char **&argv)
Initializes the distributed communication system.
const Datatype dt_byte(MPI_BYTE, sizeof(char))
Datatype wrapper for MPI_BYTE.
const Datatype dt_wchar(MPI_WCHAR, sizeof(wchar_t))
Datatype wrapper for MPI_WCHAR.
const Datatype dt_signed_long(MPI_LONG, sizeof(long))
Datatype wrapper for MPI_LONG.
const Datatype dt_float(MPI_FLOAT, sizeof(float))
Datatype wrapper for MPI_FLOAT.
const Datatype dt_signed_char(MPI_SIGNED_CHAR, sizeof(signed char))
Datatype wrapper for MPI_SIGNED_CHAR.
const Datatype dt_signed_short(MPI_SHORT, sizeof(short))
Datatype wrapper for MPI_SHORT.
const Datatype dt_unsigned_long_long(MPI_UNSIGNED_LONG_LONG, sizeof(unsigned long long))
Datatype wrapper for MPI_UNSIGNED_LONG_LONG.
const Datatype dt_signed_int32(MPI_INT32_T, sizeof(std::int32_t))
Datatype wrapper for MPI_INT32_T.
const Datatype dt_unsigned_int32(MPI_UINT32_T, sizeof(std::uint32_t))
Datatype wrapper for MPI_UINT32_T.
void finalize()
Finalizes the distributed communication system.
const Datatype dt_signed_int8(MPI_INT8_T, sizeof(std::int8_t))
Datatype wrapper for MPI_INT8_T.
const Datatype dt_char(MPI_CHAR, sizeof(char))
Datatype wrapper for MPI_CHAR.
const Operation op_min(MPI_MIN)
Operation wrapper for MPI_MIN.
const Datatype dt_unsigned_int(MPI_UNSIGNED, sizeof(unsigned int))
Datatype wrapper for MPI_UNSIGNED.
const Datatype dt_unsigned_int16(MPI_UINT16_T, sizeof(std::uint16_t))
Datatype wrapper for MPI_UINT16_T.
const Datatype dt_unsigned_int8(MPI_UINT8_T, sizeof(std::uint8_t))
Datatype wrapper for MPI_UINT8_T.
const Datatype dt__half
custom Datatype for __half
const Datatype dt_signed_int(MPI_INT, sizeof(int))
Datatype wrapper for MPI_INT.
const Datatype dt_signed_long_long(MPI_LONG_LONG, sizeof(long long))
Datatype wrapper for MPI_LONG_LONG.
const Operation op_max(MPI_MAX)
Operation wrapper for MPI_MAX.
const Datatype dt_double(MPI_DOUBLE, sizeof(double))
Datatype wrapper for MPI_DOUBLE.
const Datatype dt_unsigned_char(MPI_UNSIGNED_CHAR, sizeof(unsigned char))
Datatype wrapper for MPI_UNSIGNED_CHAR.
const Datatype dt_packed(MPI_PACKED, sizeof(char))
Datatype wrapper for MPI_PACKED.
const Datatype dt_signed_int16(MPI_INT16_T, sizeof(std::int16_t))
Datatype wrapper for MPI_INT16_T.
const Datatype dt_unsigned_short(MPI_UNSIGNED_SHORT, sizeof(unsigned short))
Datatype wrapper for MPI_UNSIGNED_SHORT.
const Datatype dt_unsigned_int64(MPI_UINT64_T, sizeof(std::uint64_t))
Datatype wrapper for MPI_UINT64_T.
const Datatype dt_long_double(MPI_LONG_DOUBLE, sizeof(long double))
Datatype wrapper for MPI_LONG_DOUBLE.
const Operation op_sum(MPI_SUM)
Operation wrapper for MPI_SUM.
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.
const Datatype dt_signed_int64(MPI_INT64_T, sizeof(std::int64_t))
Datatype wrapper for MPI_INT64_T.
@ 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.
String stringify(const T_ &item)
Converts an item into a String.
Communication Datatype class.
MPI_Datatype dt
the MPI datatype handle
Communication Operation class.
MPI_Op op
the MPI operation handle