42 #ifndef TEUCHOS_MPI_COMM_HPP 43 #define TEUCHOS_MPI_COMM_HPP 46 #include "Teuchos_Comm.hpp" 47 #include "Teuchos_CommUtilities.hpp" 49 #include "Teuchos_OpaqueWrapper.hpp" 50 #include "Teuchos_MpiReductionOpSetter.hpp" 51 #include "Teuchos_SerializationTraitsHelpers.hpp" 52 #include "Teuchos_Workspace.hpp" 55 #include "Teuchos_Assert.hpp" 63 #ifdef TEUCHOS_MPI_COMM_DUMP 64 # include "Teuchos_VerboseObject.hpp" 88 void safeCommFree (MPI_Comm* comm);
94 int setCommErrhandler (MPI_Comm comm, MPI_Errhandler handler);
98 #ifdef TEUCHOS_MPI_COMM_DUMP 99 template<
typename Ordinal,
typename T>
101 const std::string &funcName,
const std::string &buffName
102 ,
const Ordinal bytes,
const T buff[]
109 <<
"\n" << funcName <<
"::" << buffName <<
":\n";
111 for( Ordinal i = 0; i < bytes; ++i ) {
112 *out << buffName <<
"[" << i <<
"] = '" << buff[i] <<
"'\n";
116 #endif // TEUCHOS_MPI_COMM_DUMP 130 template<
class OrdinalType>
142 OrdinalType
getTag () {
return status_.MPI_TAG; }
145 OrdinalType
getError () {
return status_.MPI_ERROR; }
158 template<
class OrdinalType>
160 mpiCommStatus (MPI_Status rawMpiStatus)
181 template<
class OrdinalType>
186 rawMpiRequest_ (MPI_REQUEST_NULL)
191 rawMpiRequest_ (rawMpiRequest)
203 MPI_Request tmp_rawMpiRequest = rawMpiRequest_;
204 rawMpiRequest_ = MPI_REQUEST_NULL;
205 return tmp_rawMpiRequest;
210 return rawMpiRequest_ == MPI_REQUEST_NULL;
219 MPI_Status rawMpiStatus;
222 const int err = MPI_Wait (&rawMpiRequest_, &rawMpiStatus);
224 err != MPI_SUCCESS, std::runtime_error,
225 "Teuchos: MPI_Wait() failed with error \"" 228 return mpiCommStatus<OrdinalType> (rawMpiStatus);
236 if (rawMpiRequest_ == MPI_REQUEST_NULL) {
240 int err = MPI_Cancel (&rawMpiRequest_);
242 err != MPI_SUCCESS, std::runtime_error,
243 "Teuchos: MPI_Cancel failed with the following error: " 251 err = MPI_Wait (&rawMpiRequest_, &status);
253 "Teuchos::MpiCommStatus::cancel: MPI_Wait failed with the following " 255 return mpiCommStatus<OrdinalType> (status);
261 if (rawMpiRequest_ != MPI_REQUEST_NULL) {
264 const int err = MPI_Cancel (&rawMpiRequest_);
265 if (err == MPI_SUCCESS) {
282 (void) MPI_Wait (&rawMpiRequest_, MPI_STATUS_IGNORE);
289 MPI_Request rawMpiRequest_;
308 template<
class OrdinalType>
321 numBytes_ (numBytesInMessage)
349 template<
class OrdinalType>
351 mpiCommRequest (MPI_Request rawMpiRequest,
372 template<
typename Ordinal>
399 explicit MpiComm (MPI_Comm rawMpiComm);
439 {
return rawMpiComm_;}
467 virtual int getRank()
const;
469 virtual int getSize()
const;
471 virtual void barrier()
const;
473 virtual void broadcast(
474 const int rootRank,
const Ordinal bytes,
char buffer[]
478 gather (
const Ordinal sendBytes,
const char sendBuffer[],
479 const Ordinal recvBytes,
char recvBuffer[],
480 const int root)
const;
482 virtual void gatherAll(
483 const Ordinal sendBytes,
const char sendBuffer[]
484 ,
const Ordinal recvBytes,
char recvBuffer[]
489 ,
const Ordinal bytes,
const char sendBuffer[],
char globalReducts[]
492 virtual void reduceAllAndScatter(
494 ,
const Ordinal sendBytes,
const char sendBuffer[]
495 ,
const Ordinal recvCounts[],
char myGlobalReducts[]
500 ,
const Ordinal bytes,
const char sendBuffer[],
char scanReducts[]
504 const Ordinal bytes,
const char sendBuffer[],
const int destRank
508 send (
const Ordinal bytes,
509 const char sendBuffer[],
511 const int tag)
const;
514 const Ordinal bytes,
const char sendBuffer[],
const int destRank
518 ssend (
const Ordinal bytes,
519 const char sendBuffer[],
521 const int tag)
const;
524 const int sourceRank,
const Ordinal bytes,
char recvBuffer[]
534 const char sendBuffer[],
536 const int tag)
const;
546 const int tag)
const;
555 const int sourceRank,
556 const int tag)
const;
558 virtual void waitAll(
581 std::string description()
const;
587 static int const minTag_ = 26000;
588 static int const maxTag_ = 26099;
600 void setupMembersFromComm();
601 static int tagCounter_;
630 void assertRank(
const int rank,
const std::string &rankName)
const;
635 #ifdef TEUCHOS_MPI_COMM_DUMP 637 static bool show_dump;
638 #endif // TEUCHOS_MPI_COMM_DUMP 656 template<
typename Ordinal>
670 template<
typename Ordinal>
677 template<
typename Ordinal>
683 "Teuchos::MpiComm constructor: The input RCP is null.");
685 std::invalid_argument,
"Teuchos::MpiComm constructor: The given MPI_Comm " 686 "is MPI_COMM_NULL.");
687 rawMpiComm_ = rawMpiComm;
710 const bool makeMpiErrorsReturn =
true;
711 if (makeMpiErrorsReturn) {
714 setErrorHandler (errHandler);
717 setupMembersFromComm();
721 template<
typename Ordinal>
725 std::invalid_argument,
"Teuchos::MpiComm constructor: The given MPI_Comm " 726 "is MPI_COMM_NULL.");
730 rawMpiComm_ = opaqueWrapper<MPI_Comm> (rawMpiComm);
753 const bool makeMpiErrorsReturn =
true;
754 if (makeMpiErrorsReturn) {
757 setErrorHandler (errHandler);
760 setupMembersFromComm();
764 template<
typename Ordinal>
766 rawMpiComm_ (opaqueWrapper<MPI_Comm> (MPI_COMM_NULL))
771 "Teuchos::MpiComm copy constructor: " 772 "The input's getRawMpiComm() method returns null.");
773 MPI_Comm origComm = *origCommPtr;
775 "Teuchos::MpiComm copy constructor: " 776 "The input's raw MPI_Comm is MPI_COMM_NULL.");
784 rawMpiComm_ = origCommPtr;
788 const int err = MPI_Comm_dup (origComm, &newComm);
790 "Teuchos::MpiComm copy constructor: MPI_Comm_dup failed with " 793 rawMpiComm_ = opaqueWrapper (newComm, details::safeCommFree);
796 setupMembersFromComm();
800 template<
typename Ordinal>
803 MPI_Comm_size(*rawMpiComm_, &size_);
804 MPI_Comm_rank(*rawMpiComm_, &rank_);
806 if (tagCounter_ > maxTag_) {
807 tagCounter_ = minTag_;
809 tag_ = tagCounter_++;
811 MPI_Bcast(&tag_, 1, MPI_INT, 0, *rawMpiComm_);
815 template<
typename Ordinal>
821 const int err = details::setCommErrhandler (*
getRawMpiComm (), *errHandler);
823 "Teuchos::MpiComm: Setting the MPI_Comm's error handler failed with " 828 customErrorHandler_ = errHandler;
836 template<
typename Ordinal>
843 template<
typename Ordinal>
850 template<
typename Ordinal>
853 TEUCHOS_COMM_TIME_MONITOR(
856 const int err = MPI_Barrier(*rawMpiComm_);
858 "Teuchos::MpiComm::barrier: MPI_Barrier failed with error \"" 863 template<
typename Ordinal>
865 const int rootRank,
const Ordinal bytes,
char buffer[]
868 TEUCHOS_COMM_TIME_MONITOR(
871 const int err = MPI_Bcast(buffer,bytes,MPI_CHAR,rootRank,*rawMpiComm_);
873 "Teuchos::MpiComm::broadcast: MPI_Bcast failed with error \"" 878 template<
typename Ordinal>
880 const Ordinal sendBytes,
const char sendBuffer[],
881 const Ordinal recvBytes,
char recvBuffer[]
884 TEUCHOS_COMM_TIME_MONITOR(
889 MPI_Allgather (const_cast<char *>(sendBuffer), sendBytes, MPI_CHAR,
890 recvBuffer, sendBytes, MPI_CHAR, *rawMpiComm_);
895 "Teuchos::MpiComm::gatherAll: MPI_Allgather failed with error \"" 900 template<
typename Ordinal>
903 const char sendBuffer[],
904 const Ordinal recvBytes,
906 const int root)
const 908 TEUCHOS_COMM_TIME_MONITOR(
912 MPI_Gather (const_cast<char *> (sendBuffer), sendBytes, MPI_CHAR,
913 recvBuffer, sendBytes, MPI_CHAR, root, *rawMpiComm_);
915 "Teuchos::MpiComm::gather: MPI_Gather failed with error \"" 920 template<
typename Ordinal>
925 const char sendBuffer[],
926 char globalReducts[])
const 928 TEUCHOS_COMM_TIME_MONITOR(
"Teuchos::MpiComm::reduceAll(...)" );
931 MPI_Datatype char_block;
935 MPI_Type_contiguous(bytes, MPI_CHAR, &char_block);
936 MPI_Type_commit(&char_block);
939 MPI_Allreduce (const_cast<char*>(sendBuffer), globalReducts, 1, char_block,
940 op.
mpi_op(), *rawMpiComm_);
942 "Teuchos::MpiComm::reduceAll (custom op): MPI_Allreduce failed with error \"" 947 MPI_Type_free(&char_block);
951 template<
typename Ordinal>
954 ,
const Ordinal sendBytes,
const char sendBuffer[]
955 ,
const Ordinal recvCounts[],
char myGlobalReducts[]
961 TEUCHOS_COMM_TIME_MONITOR(
966 Ordinal sumRecvBytes = 0;
967 for( Ordinal i = 0; i < size_; ++i ) {
968 sumRecvBytes += recvCounts[i];
971 #endif // TEUCHOS_DEBUG 973 #ifdef TEUCHOS_MPI_COMM_DUMP 975 dumpBuffer<Ordinal,char>(
976 "Teuchos::MpiComm<Ordinal>::reduceAllAndScatter(...)",
977 "sendBuffer", sendBytes, sendBuffer );
978 dumpBuffer<Ordinal,Ordinal>(
979 "Teuchos::MpiComm<Ordinal>::reduceAllAndScatter(...)",
980 "recvCounts", as<Ordinal>(size_), recvCounts );
981 dumpBuffer<Ordinal,char>(
982 "Teuchos::MpiComm<Ordinal>::reduceAllAndScatter(...)",
983 "myGlobalReducts", as<char>(recvCounts[rank_]), myGlobalReducts );
985 #endif // TEUCHOS_MPI_COMM_DUMP 989 const bool Ordinal_is_int =
typeid(int)==
typeid(Ordinal);
991 const int *int_recvCounts = 0;
993 int_recvCounts =
reinterpret_cast<const int*
>(recvCounts);
1000 std::copy(recvCounts, recvCounts+size_, &ws_int_recvCounts[0]);
1001 int_recvCounts = &ws_int_recvCounts[0];
1007 const int err = MPI_Reduce_scatter(
1008 const_cast<char*>(sendBuffer), myGlobalReducts,
1009 const_cast<int*>(int_recvCounts),
1015 "Teuchos::MpiComm::reduceAllAndScatter: MPI_Reduce_scatter failed with " 1020 template<
typename Ordinal>
1023 ,
const Ordinal bytes,
const char sendBuffer[],
char scanReducts[]
1026 TEUCHOS_COMM_TIME_MONITOR(
"Teuchos::MpiComm::scan(...)" );
1030 MPI_Scan (const_cast<char*>(sendBuffer), scanReducts, bytes, MPI_CHAR,
1031 op.
mpi_op(), *rawMpiComm_);
1033 "Teuchos::MpiComm::scan: MPI_Scan() failed with error \"" 1038 template<
typename Ordinal>
1041 const char sendBuffer[],
1042 const int destRank)
const 1044 TEUCHOS_COMM_TIME_MONITOR(
"Teuchos::MpiComm::send(...)" );
1046 #ifdef TEUCHOS_MPI_COMM_DUMP 1048 dumpBuffer<Ordinal,char>(
1049 "Teuchos::MpiComm<Ordinal>::send(...)" 1050 ,
"sendBuffer", bytes, sendBuffer
1053 #endif // TEUCHOS_MPI_COMM_DUMP 1055 const int err = MPI_Send (const_cast<char*>(sendBuffer), bytes, MPI_CHAR,
1056 destRank, tag_, *rawMpiComm_);
1058 "Teuchos::MpiComm::send: MPI_Send() failed with error \"" 1063 template<
typename Ordinal>
1066 const char sendBuffer[],
1068 const int tag)
const 1070 TEUCHOS_COMM_TIME_MONITOR(
"Teuchos::MpiComm::send(...)" );
1071 const int err = MPI_Send (const_cast<char*> (sendBuffer), bytes, MPI_CHAR,
1072 destRank, tag, *rawMpiComm_);
1074 "Teuchos::MpiComm::send: MPI_Send() failed with error \"" 1079 template<
typename Ordinal>
1082 const char sendBuffer[],
1083 const int destRank)
const 1085 TEUCHOS_COMM_TIME_MONITOR(
"Teuchos::MpiComm::ssend(...)" );
1087 #ifdef TEUCHOS_MPI_COMM_DUMP 1089 dumpBuffer<Ordinal,char>(
1090 "Teuchos::MpiComm<Ordinal>::send(...)" 1091 ,
"sendBuffer", bytes, sendBuffer
1094 #endif // TEUCHOS_MPI_COMM_DUMP 1096 const int err = MPI_Ssend (const_cast<char*>(sendBuffer), bytes, MPI_CHAR,
1097 destRank, tag_, *rawMpiComm_);
1099 "Teuchos::MpiComm::send: MPI_Ssend() failed with error \"" 1103 template<
typename Ordinal>
1106 const char sendBuffer[],
1108 const int tag)
const 1110 TEUCHOS_COMM_TIME_MONITOR(
"Teuchos::MpiComm::ssend(...)" );
1112 MPI_Ssend (const_cast<char*>(sendBuffer), bytes, MPI_CHAR,
1113 destRank, tag, *rawMpiComm_);
1115 "Teuchos::MpiComm::send: MPI_Ssend() failed with error \"" 1119 template<
typename Ordinal>
1125 TEUCHOS_COMM_TIME_MONITOR(
"Teuchos::MpiComm::readySend" );
1127 #ifdef TEUCHOS_MPI_COMM_DUMP 1129 dumpBuffer<Ordinal,char>(
1130 "Teuchos::MpiComm<Ordinal>::readySend(...)" 1131 ,
"sendBuffer", bytes, sendBuffer
1134 #endif // TEUCHOS_MPI_COMM_DUMP 1137 MPI_Rsend (const_cast<char*>(sendBuffer.
getRawPtr()), sendBuffer.
size(),
1138 MPI_CHAR, destRank, tag_, *rawMpiComm_);
1140 "Teuchos::MpiComm::readySend: MPI_Rsend() failed with error \"" 1145 template<
typename Ordinal>
1148 const char sendBuffer[],
1150 const int tag)
const 1152 TEUCHOS_COMM_TIME_MONITOR(
"Teuchos::MpiComm::readySend" );
1154 MPI_Rsend (const_cast<char*> (sendBuffer), bytes,
1155 MPI_CHAR, destRank, tag, *rawMpiComm_);
1157 "Teuchos::MpiComm::readySend: MPI_Rsend() failed with error \"" 1162 template<
typename Ordinal>
1165 const Ordinal bytes,
1166 char recvBuffer[])
const 1168 TEUCHOS_COMM_TIME_MONITOR(
"Teuchos::MpiComm::receive(...)" );
1173 const int theSrcRank = (sourceRank < 0) ? MPI_ANY_SOURCE : sourceRank;
1176 const int err = MPI_Recv (recvBuffer, bytes, MPI_CHAR, theSrcRank, tag_,
1177 *rawMpiComm_, &status);
1179 "Teuchos::MpiComm::receive: MPI_Recv() failed with error \"" 1182 #ifdef TEUCHOS_MPI_COMM_DUMP 1184 dumpBuffer<Ordinal,char> (
"Teuchos::MpiComm<Ordinal>::receive(...)",
1185 "recvBuffer", bytes, recvBuffer);
1187 #endif // TEUCHOS_MPI_COMM_DUMP 1190 return status.MPI_SOURCE;
1194 template<
typename Ordinal>
1197 const int destRank)
const 1200 TEUCHOS_COMM_TIME_MONITOR(
"Teuchos::MpiComm::isend(...)" );
1202 MPI_Request rawMpiRequest = MPI_REQUEST_NULL;
1204 MPI_Isend (const_cast<char*> (sendBuffer.
getRawPtr ()),
1205 as<Ordinal> (sendBuffer.
size ()), MPI_CHAR,
1206 destRank, tag_, *rawMpiComm_, &rawMpiRequest);
1208 "Teuchos::MpiComm::isend: MPI_Isend() failed with error \"" 1211 return mpiCommRequest<Ordinal> (rawMpiRequest, sendBuffer.
size ());
1215 template<
typename Ordinal>
1220 const int tag)
const 1223 TEUCHOS_COMM_TIME_MONITOR(
"Teuchos::MpiComm::isend(...)" );
1225 MPI_Request rawMpiRequest = MPI_REQUEST_NULL;
1227 MPI_Isend (const_cast<char*> (sendBuffer.
getRawPtr ()),
1228 as<Ordinal> (sendBuffer.
size ()), MPI_CHAR,
1229 destRank, tag, *rawMpiComm_, &rawMpiRequest);
1231 "Teuchos::MpiComm::isend: MPI_Isend() failed with error \"" 1234 return mpiCommRequest<Ordinal> (rawMpiRequest, sendBuffer.
size ());
1238 template<
typename Ordinal>
1241 const int sourceRank)
const 1243 TEUCHOS_COMM_TIME_MONITOR(
"Teuchos::MpiComm::ireceive(...)" );
1248 const int theSrcRank = (sourceRank < 0) ? MPI_ANY_SOURCE : sourceRank;
1250 MPI_Request rawMpiRequest = MPI_REQUEST_NULL;
1252 MPI_Irecv (const_cast<char*>(recvBuffer.
getRawPtr()), recvBuffer.
size(),
1253 MPI_CHAR, theSrcRank, tag_, *rawMpiComm_, &rawMpiRequest);
1255 "Teuchos::MpiComm::ireceive: MPI_Irecv() failed with error \"" 1258 return mpiCommRequest<Ordinal> (rawMpiRequest, recvBuffer.
size());
1261 template<
typename Ordinal>
1264 const int sourceRank,
1265 const int tag)
const 1267 TEUCHOS_COMM_TIME_MONITOR(
"Teuchos::MpiComm::ireceive(...)" );
1272 const int theSrcRank = (sourceRank < 0) ? MPI_ANY_SOURCE : sourceRank;
1274 MPI_Request rawMpiRequest = MPI_REQUEST_NULL;
1276 MPI_Irecv (const_cast<char*> (recvBuffer.
getRawPtr ()), recvBuffer.
size (),
1277 MPI_CHAR, theSrcRank, tag, *rawMpiComm_, &rawMpiRequest);
1279 "Teuchos::MpiComm::ireceive: MPI_Irecv() failed with error \"" 1282 return mpiCommRequest<Ordinal> (rawMpiRequest, recvBuffer.
size ());
1287 template<
typename Ordinal>
1293 const size_type count = requests.
size();
1297 std::logic_error,
"Teuchos::MpiComm's waitAllImpl: rawMpiStatus.size() = " 1298 << rawMpiStatuses.
size() <<
" != requests.size() = " << requests.size()
1299 <<
". Please report this bug to the Tpetra developers.");
1312 bool someNullRequests =
false;
1314 for (
int i = 0; i < count; ++i) {
1326 rawMpiRequests[i] = mpiRequest->releaseRawMpiRequest();
1329 rawMpiRequests[i] = MPI_REQUEST_NULL;
1330 someNullRequests =
true;
1343 const int err = MPI_Waitall (count, rawMpiRequests.
getRawPtr(),
1354 if (err != MPI_SUCCESS) {
1355 if (err == MPI_ERR_IN_STATUS) {
1364 for (size_type k = 0; k < rawMpiStatuses.
size(); ++k) {
1365 const int curErr = rawMpiStatuses[k].MPI_ERROR;
1366 if (curErr != MPI_SUCCESS) {
1367 errorLocationsAndCodes.
push_back (std::make_pair (k, curErr));
1370 const size_type numErrs = errorLocationsAndCodes.
size();
1375 std::ostringstream os;
1376 os <<
"Teuchos::MpiComm::waitAll: MPI_Waitall() failed with error \"" 1378 <<
" total request" << (count != 1 ?
"s" :
"") <<
", " << numErrs
1379 <<
" failed. Here are the indices of the failed requests, and the " 1380 "error codes extracted from their returned MPI_Status objects:" 1382 for (size_type k = 0; k < numErrs; ++k) {
1383 const size_type errInd = errorLocationsAndCodes[k].first;
1384 os <<
"Request " << errInd <<
": MPI_ERROR = " 1388 if (someNullRequests) {
1389 os <<
" On input to MPI_Waitall, there was at least one MPI_" 1390 "Request that was MPI_REQUEST_NULL. MPI_Waitall should not " 1391 "normally fail in that case, but we thought we should let you know " 1400 std::ostringstream os;
1401 os <<
"Teuchos::MpiComm::waitAll: MPI_Waitall() failed with error \"" 1403 if (someNullRequests) {
1404 os <<
" On input to MPI_Waitall, there was at least one MPI_Request " 1405 "that was MPI_REQUEST_NULL. MPI_Waitall should not normally fail in " 1406 "that case, but we thought we should let you know regardless.";
1414 std::fill (requests.begin(), requests.end(), null);
1420 template<
typename Ordinal>
1425 const size_type count = requests.
size ();
1438 bool someNullRequests =
false;
1440 for (
int i = 0; i < count; ++i) {
1452 rawMpiRequests[i] = mpiRequest->releaseRawMpiRequest ();
1455 rawMpiRequests[i] = MPI_REQUEST_NULL;
1456 someNullRequests =
true;
1466 const int err = MPI_Waitall (count, rawMpiRequests.
getRawPtr(),
1467 MPI_STATUSES_IGNORE);
1477 if (err != MPI_SUCCESS) {
1478 std::ostringstream os;
1479 os <<
"Teuchos::MpiComm::waitAll: MPI_Waitall() failed with error \"" 1481 if (someNullRequests) {
1482 os << std::endl <<
"On input to MPI_Waitall, there was at least one " 1483 "MPI_Request that was MPI_REQUEST_NULL. MPI_Waitall should not " 1484 "normally fail in that case, but we thought we should let you know " 1495 std::fill (requests.begin(), requests.end(), null);
1502 template<
typename Ordinal>
1507 TEUCHOS_COMM_TIME_MONITOR(
"Teuchos::MpiComm::waitAll(requests)" );
1510 waitAllImpl<Ordinal> (requests);
1514 template<
typename Ordinal>
1520 TEUCHOS_COMM_TIME_MONITOR(
"Teuchos::MpiComm::waitAll(requests, statuses)" );
1523 const size_type count = requests.
size();
1526 std::invalid_argument,
"Teuchos::MpiComm::waitAll: requests.size() = " 1527 << count <<
" != statuses.size() = " << statuses.size() <<
".");
1530 waitAllImpl<Ordinal> (requests, rawMpiStatuses());
1533 for (size_type i = 0; i < count; ++i) {
1534 statuses[i] = mpiCommStatus<Ordinal> (rawMpiStatuses[i]);
1539 template<
typename Ordinal>
1543 TEUCHOS_COMM_TIME_MONITOR(
"Teuchos::MpiComm::wait(...)" );
1557 template<
typename Ordinal>
1561 MPI_Comm origRawComm = *rawMpiComm_;
1562 MPI_Comm newRawComm = MPI_COMM_NULL;
1563 const int err = MPI_Comm_dup (origRawComm, &newRawComm);
1565 "::MpiComm::duplicate: MPI_Comm_dup failed with the following error: " 1577 template<
typename Ordinal>
1582 const int splitReturn =
1583 MPI_Comm_split (*rawMpiComm_,
1584 color < 0 ? MPI_UNDEFINED : color,
1588 splitReturn != MPI_SUCCESS,
1590 "Teuchos::MpiComm::split: Failed to create communicator with color " 1591 << color <<
"and key " << key <<
". MPI_Comm_split failed with error \"" 1593 if (newComm == MPI_COMM_NULL) {
1598 opaqueWrapper(newComm,MPI_Comm_free))));
1603 template<
typename Ordinal>
1607 int err = MPI_SUCCESS;
1610 MPI_Group thisGroup;
1611 err = MPI_Comm_group (*rawMpiComm_, &thisGroup);
1613 "Failed to obtain the current communicator's group. " 1614 "MPI_Comm_group failed with error \"" 1623 err = MPI_Group_incl (thisGroup, ranks.
size(),
1624 const_cast<int*
> (ranks.
getRawPtr ()), &newGroup);
1626 "Failed to create subgroup. MPI_Group_incl failed with error \"" 1632 err = MPI_Comm_create (*rawMpiComm_, newGroup, &newComm);
1634 "Failed to create subcommunicator. MPI_Comm_create failed with error \"" 1642 (void) MPI_Group_free (&newGroup);
1643 (void) MPI_Group_free (&thisGroup);
1648 err = MPI_Group_free (&newGroup);
1650 "Failed to free subgroup. MPI_Group_free failed with error \"" 1652 err = MPI_Group_free (&thisGroup);
1654 "Failed to free subgroup. MPI_Group_free failed with error \"" 1657 if (newComm == MPI_COMM_NULL) {
1660 using Teuchos::details::safeCommFree;
1663 rcp_implicit_cast<
const ow_type> (opaqueWrapper (newComm, safeCommFree));
1672 template<
typename Ordinal>
1675 std::ostringstream oss;
1681 <<
",rawMpiComm="<<
static_cast<MPI_Comm
>(*rawMpiComm_)
1687 #ifdef TEUCHOS_MPI_COMM_DUMP 1688 template<
typename Ordinal>
1696 template<
typename Ordinal>
1700 ! ( 0 <= rank && rank < size_ ), std::logic_error
1701 ,
"Error, "<<rankName<<
" = " << rank <<
" is not < 0 or is not" 1702 " in the range [0,"<<size_-1<<
"]!" 1710 template<
typename Ordinal>
1712 Teuchos::createMpiComm(
1716 if( rawMpiComm.get()!=NULL && *rawMpiComm != MPI_COMM_NULL )
1718 return Teuchos::null;
1722 #endif // TEUCHOS_MPI_COMM_HPP virtual void ssend(const Ordinal bytes, const char sendBuffer[], const int destRank) const
virtual RCP< Comm< Ordinal > > duplicate() const
RCP< const T > getConst() const
Return an RCP<const T> version of *this.
RCP< CommStatus< OrdinalType > > wait()
Wait on this communication request to complete.
virtual int getRank() const
virtual int receive(const int sourceRank, const Ordinal bytes, char recvBuffer[]) const
RCP< CommStatus< OrdinalType > > cancel()
Cancel the communication request, and return its status.
bool is_null(const boost::shared_ptr< T > &p)
Returns true if p.get()==NULL.
RCP< const MpiReductionOp< Ordinal > > mpiReductionOp(const RCP< const ValueTypeReductionOp< Ordinal, char > > &reductOp)
Create an MpiReductionOp object given an ReductionOp object.
Templated class for workspace creation.
MPI-specific implementation of CommStatus.
virtual void gather(const Ordinal sendBytes, const char sendBuffer[], const Ordinal recvBytes, char recvBuffer[], const int root) const
Gather values from all processes to the root process.
virtual RCP< CommStatus< Ordinal > > wait(const Ptr< RCP< CommRequest< Ordinal > > > &request) const
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
virtual void broadcast(const int rootRank, const Ordinal bytes, char buffer[]) const
size_type size() const
The total number of items in the managed array.
int rank(const Comm< Ordinal > &comm)
Get the process rank.
MpiCommRequest(MPI_Request rawMpiRequest, const ArrayView< char >::size_type numBytesInMessage)
Constructor (from a raw MPI_Request).
virtual ~MpiCommRequest()
Destructor; cancels the request if it is still pending.
virtual void gatherAll(const Ordinal sendBytes, const char sendBuffer[], const Ordinal recvBytes, char recvBuffer[]) const
T * getRawPtr()
Return a raw pointer to beginning of array or NULL if unsized.
std::string description() const
int getTag() const
The current tag.
Tabbing class for helping to create formated, indented output for a basic_FancyOStream object...
RCP< const OpaqueWrapper< MPI_Comm > > getRawMpiComm() const
Return the embedded wrapped opaque MPI_Comm object.
virtual RCP< CommRequest< Ordinal > > isend(const ArrayView< const char > &sendBuffer, const int destRank) const
OrdinalType getSourceRank()
The source rank that sent the message.
virtual ~MpiCommRequestBase()
Destructor; cancels the request if it is still pending.
Base class MPI implementation of CommRequest.
virtual ~MpiCommStatus()
Destructor (declared virtual for memory safety)
virtual RCP< Comm< Ordinal > > createSubcommunicator(const ArrayView< const int > &ranks) const
MPI_Request releaseRawMpiRequest()
Return and relinquish ownership of the raw MPI_Request.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
bool isNull() const
Whether the raw MPI_Request is MPI_REQUEST_NULL.
void setErrorHandler(const RCP< const OpaqueWrapper< MPI_Errhandler > > &errHandler)
Set the MPI error handler for this communicator.
Teuchos implementation details.
Utility class for setting an MPI-compatible reduction object and using it to create an MPI_Op object...
TEUCHOS_DEPRECATED void scan(const Comm< Ordinal > &comm, const EReductionType reductType, const Packet &send, Packet *scanReduct)
Deprecated.
std::string mpiErrorCodeToString(const int err)
Human-readable string version of the given MPI error code.
MPI implementation of CommRequest.
virtual void readySend(const ArrayView< const char > &sendBuffer, const int destRank) const
virtual void waitAll(const ArrayView< RCP< CommRequest< Ordinal > > > &requests) const
static RCP< FancyOStream > getDefaultOStream()
Get the default output stream object.
This structure defines some basic traits for the ordinal field type.
TEUCHOS_DEPRECATED void reduceAll(const Comm< Ordinal > &comm, const EReductionType reductType, const Packet &send, Packet *globalReduct)
Deprecated .
Encapsulation of the result of a receive (blocking or nonblocking).
void send(const Packet sendBuffer[], const Ordinal count, const int destRank, const int tag, const Comm< Ordinal > &comm)
Variant of send() that takes a tag (and restores the correct order of arguments). ...
virtual void send(const Ordinal bytes, const char sendBuffer[], const int destRank) const
Workspace encapsulation class.
T * getRawPtr() const
Return a raw pointer to beginning of array or NULL if unsized.
MPI_Op mpi_op() const
Return the created MPI_Op reduction object that can be used by MPI.
RCP< CommRequest< Ordinal > > ireceive(const ArrayRCP< Packet > &recvBuffer, const int sourceRank, const int tag, const Comm< Ordinal > &comm)
Variant of ireceive that takes a tag argument (and restores the correct order of arguments).
virtual void reduceAllAndScatter(const ValueTypeReductionOp< Ordinal, char > &reductOp, const Ordinal sendBytes, const char sendBuffer[], const Ordinal recvCounts[], char myGlobalReducts[]) const
OrdinalType getError()
The error code of the received message.
MpiCommRequestBase(MPI_Request rawMpiRequest)
Constructor (from a raw MPI_Request).
MpiCommRequestBase()
Default constructor.
virtual void barrier() const
Abstract interface for distributed-memory communication.
void push_back(const value_type &x)
Defines basic traits for the ordinal field type.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
virtual RCP< CommRequest< Ordinal > > ireceive(const ArrayView< char > &Buffer, const int sourceRank) const
MpiCommRequest()
Default constructor.
TypeTo as(const TypeFrom &t)
Convert from one value type to another.
void ssend(const Packet sendBuffer[], const Ordinal count, const int destRank, const int tag, const Comm< Ordinal > &comm)
Variant of ssend() that takes a tag (and restores the correct order of arguments).
ArrayView< char >::size_type numBytes() const
Number of bytes in the nonblocking send or receive request.
virtual RCP< Comm< Ordinal > > split(const int color, const int key) const
OrdinalType getTag()
The tag of the received message.
Smart reference counting pointer class for automatic garbage collection.
virtual int getSize() const
virtual void scan(const ValueTypeReductionOp< Ordinal, char > &reductOp, const Ordinal bytes, const char sendBuffer[], char scanReducts[]) const
Encapsulation of a pending nonblocking communication operation.
#define TEUCHOS_ASSERT_EQUALITY(val1, val2)
This macro is checks that to numbers are equal and if not then throws an exception with a good error ...
virtual void reduceAll(const ValueTypeReductionOp< Ordinal, char > &reductOp, const Ordinal bytes, const char sendBuffer[], char globalReducts[]) const
Implementation of Comm that uses MPI for communication.
Defines basic traits returning the name of a type in a portable and readable way. ...
Definition of Teuchos::as, for conversions between types.
void readySend(const Packet sendBuffer[], const Ordinal count, const int destRank, const int tag, const Comm< Ordinal > &comm)
Variant of readySend() that accepts a message tag.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
This macro is designed to be a short version of TEUCHOS_TEST_FOR_EXCEPTION() that is easier to call...
Simple wrapper class for raw pointers to single objects where no persisting relationship exists...
TEUCHOSCORE_LIB_DLL_EXPORT Teuchos::RCP< WorkspaceStore > get_default_workspace_store()
Get the global workspace object set by set_default_workspace_store().
std::string typeName(const T &t)
Template function for returning the concrete type name of a passed-in object.
Memory-safe templated array class that encapsulates std::vector.
bool is_null() const
Returns true if the underlying pointer is null.