Go to the documentation of this file.
44 #define MPI_SCALAR MPI_FLOAT
45 #define MPI_SOLVESCALAR MPI_FLOAT
46 #elif defined(WM_SPDP)
47 #define MPI_SCALAR MPI_FLOAT
48 #define MPI_SOLVESCALAR MPI_DOUBLE
50 #define MPI_SCALAR MPI_DOUBLE
51 #define MPI_SOLVESCALAR MPI_DOUBLE
83 if (str.empty() || !
Foam::read(str, len) || len <= 0)
95 Foam::Pout<<
"UPstream::init : buffer-size " << len <<
'\n';
98 char* buf =
new char[len];
100 if (MPI_SUCCESS != MPI_Buffer_attach(buf, len))
103 Foam::Pout<<
"UPstream::init : could not attach buffer\n";
125 if (MPI_SUCCESS == MPI_Buffer_detach(&buf, &len) && len)
141 validParOptions.insert(
"np",
"");
142 validParOptions.insert(
"p4pg",
"PI file");
143 validParOptions.insert(
"p4wd",
"directory");
144 validParOptions.insert(
"p4amslave",
"");
145 validParOptions.insert(
"p4yourname",
"hostname");
146 validParOptions.insert(
"machinefile",
"machine file");
154 MPI_Finalized(&flag);
159 <<
"MPI was already finalized - cannot perform MPI_Init\n"
165 MPI_Initialized(&flag);
170 Pout<<
"UPstream::initNull : was already initialized\n";
196 int numprocs = 0, myRank = 0;
197 int provided_thread_support = 0;
200 MPI_Finalized(&flag);
205 <<
"MPI was already finalized - cannot perform MPI_Init" <<
endl
211 MPI_Initialized(&flag);
220 <<
"MPI was already initialized - cannot perform MPI_Init" <<
nl
221 <<
"This could indicate an application programming error!"
228 Pout<<
"UPstream::init : was already initialized\n";
239 ? MPI_THREAD_MULTIPLE
242 &provided_thread_support
249 label worldIndex = -1;
251 for (
int argi = 1; argi < argc; ++argi)
253 if (strcmp(argv[argi],
"-world") == 0)
259 <<
"Missing world name to argument \"world\""
268 if (worldIndex != -1)
270 for (label i = worldIndex+2; i < argc; i++)
277 MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
278 MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
282 Pout<<
"UPstream::init : procs:" << numprocs
283 <<
" rank:" << myRank
284 <<
" world:" << world <<
endl;
287 if (worldIndex == -1 && numprocs <= 1)
290 <<
"attempt to run parallel on 1 processor"
295 setParRun(numprocs, provided_thread_support == MPI_THREAD_MULTIPLE);
297 if (worldIndex != -1)
307 DynamicList<word> allWorlds(numprocs);
308 for (
const auto& world : worlds)
310 if (!allWorlds.found(world))
312 allWorlds.append(world);
315 allWorlds_ = std::move(allWorlds);
317 worldIDs_.setSize(numprocs);
320 const word& world = worlds[proci];
321 worldIDs_[proci] = allWorlds_.find(world);
327 DynamicList<label> subRanks;
332 subRanks.append(proci);
337 const label subComm = allocateCommunicator(0, subRanks,
true);
347 int subNProcs, subRank;
359 Pout<<
"UPstream::init : in world:" << world
360 <<
" using local communicator:" << subComm
361 <<
" with procs:" << subNProcs
362 <<
" and rank:" << subRank
367 Pout.
prefix() =
'[' + world +
'/' +
name(myProcNo(subComm)) +
"] ";
368 Perr.
prefix() =
'[' + world +
'/' +
name(myProcNo(subComm)) +
"] ";
373 worldIDs_.setSize(numprocs, 0);
386 Pout<<
"UPstream::shutdown\n";
391 MPI_Initialized(&flag);
398 MPI_Finalized(&flag);
405 <<
"MPI was already finalized (by a connected program?)\n";
409 Pout<<
"UPstream::shutdown : was already finalized\n";
420 label nOutstanding = 0;
435 <<
"There were still " << nOutstanding
436 <<
" outstanding MPI_Requests." <<
nl
437 <<
"Which means your code exited before doing a "
438 <<
" UPstream::waitRequests()." <<
nl
439 <<
"This should not happen for a normal code exit."
445 forAll(myProcNo_, communicator)
447 if (myProcNo_[communicator] != -1)
449 freePstreamCommunicator(communicator);
460 <<
"Finalizing MPI, but was initialized elsewhere\n";
470 MPI_Abort(MPI_COMM_WORLD, errNo);
485 MPI_Abort(MPI_COMM_WORLD, 1);
492 const sumOp<scalar>& bop,
494 const label communicator
499 Pout<<
"** reducing:" << Value <<
" with comm:" << communicator
504 allReduce(Value, 1, MPI_SCALAR, MPI_SUM, bop, tag, communicator);
511 const minOp<scalar>& bop,
513 const label communicator
518 Pout<<
"** reducing:" << Value <<
" with comm:" << communicator
523 allReduce(Value, 1, MPI_SCALAR, MPI_MIN, bop, tag, communicator);
530 const sumOp<vector2D>& bop,
532 const label communicator
537 Pout<<
"** reducing:" << Value <<
" with comm:" << communicator
542 allReduce(Value, 2, MPI_SCALAR, MPI_SUM, bop, tag, communicator);
551 const label communicator
556 Pout<<
"** sumReduce:" << Value <<
" with comm:" << communicator
561 vector2D twoScalars(Value, scalar(Count));
562 reduce(twoScalars, sumOp<vector2D>(), tag, communicator);
564 Value = twoScalars.x();
565 Count = twoScalars.y();
572 const sumOp<scalar>& bop,
574 const label communicator,
578 iallReduce<scalar>(&Value, 1, MPI_SCALAR, MPI_SUM, communicator, requestID);
586 const sumOp<scalar>& bop,
588 const label communicator,
608 const sumOp<solveScalar>& bop,
610 const label communicator
615 Pout<<
"** reducing:" << Value <<
" with comm:" << communicator
620 allReduce(Value, 1, MPI_SOLVESCALAR, MPI_SUM, bop, tag, communicator);
627 const minOp<solveScalar>& bop,
629 const label communicator
634 Pout<<
"** reducing:" << Value <<
" with comm:" << communicator
639 allReduce(Value, 1, MPI_SOLVESCALAR, MPI_MIN, bop, tag, communicator);
645 Vector2D<solveScalar>& Value,
646 const sumOp<Vector2D<solveScalar>>& bop,
648 const label communicator
653 Pout<<
"** reducing:" << Value <<
" with comm:" << communicator
658 allReduce(Value, 2, MPI_SOLVESCALAR, MPI_SUM, bop, tag, communicator);
667 const label communicator
672 Pout<<
"** reducing:" << Value <<
" with comm:" << communicator
677 Vector2D<solveScalar> twoScalars(Value, solveScalar(Count));
678 reduce(twoScalars, sumOp<Vector2D<solveScalar>>(), tag, communicator);
680 Value = twoScalars.x();
681 Count = twoScalars.y();
688 const sumOp<solveScalar>& bop,
690 const label communicator,
694 iallReduce<solveScalar>
710 const sumOp<solveScalar>& bop,
712 const label communicator,
716 iallReduce<solveScalar>
733 const label communicator
736 const label np = nProcs(communicator);
740 Pout<<
"** allToAll :"
742 <<
" sendData:" << sendData.size()
743 <<
" with comm:" << communicator
749 if (sendData.size() != np || recvData.size() != np)
752 <<
"Size of sendData " << sendData.size()
753 <<
" or size of recvData " << recvData.size()
754 <<
" is not equal to the number of processors in the domain "
761 recvData.deepCopy(sendData);
773 const_cast<label*
>(sendData.begin()),
784 <<
"MPI_Alltoall failed for " << sendData
785 <<
" on communicator " << communicator
796 const char* sendData,
811 Pout<<
"** allToAll :"
812 <<
" sendSizes:" << sendSizes
813 <<
" sendOffsets:" << sendOffsets
822 sendSizes.
size() != np
823 || sendOffsets.
size() != np
824 || recvSizes.
size() != np
825 || recvOffsets.
size() != np
829 <<
"Size of sendSize " << sendSizes.
size()
830 <<
", sendOffsets " << sendOffsets.
size()
831 <<
", recvSizes " << recvSizes.
size()
832 <<
" or recvOffsets " << recvOffsets.
size()
833 <<
" is not equal to the number of processors in the domain "
840 if (recvSizes[0] != sendSizes[0])
843 <<
"Bytes to send " << sendSizes[0]
844 <<
" does not equal bytes to receive " << recvSizes[0]
847 std::memmove(recvData, &sendData[sendOffsets[0]], recvSizes[0]);
857 const_cast<char*
>(sendData),
858 const_cast<int*
>(sendSizes.
begin()),
859 const_cast<int*
>(sendOffsets.
begin()),
862 const_cast<int*
>(recvSizes.
begin()),
863 const_cast<int*
>(recvOffsets.
begin()),
870 <<
"MPI_Alltoallv failed for sendSizes " << sendSizes
871 <<
" recvSizes " << recvSizes
883 const char* sendData,
889 const label communicator
892 const label np = nProcs(communicator);
896 Pout<<
"** allToAll :"
898 <<
" recvSizes:" << recvSizes
899 <<
" recvOffsets:" << recvOffsets
900 <<
" with comm:" << communicator
909 && (recvSizes.
size() != np || recvOffsets.
size() < np)
916 <<
"Size of recvSizes " << recvSizes.
size()
917 <<
" or recvOffsets " << recvOffsets.
size()
918 <<
" is not equal to the number of processors in the domain "
925 std::memmove(recvData, sendData, sendSize);
935 const_cast<char*
>(sendData),
939 const_cast<int*
>(recvSizes.
begin()),
940 const_cast<int*
>(recvOffsets.
begin()),
948 <<
"MPI_Gatherv failed for sendSize " << sendSize
949 <<
" recvSizes " << recvSizes
950 <<
" communicator " << communicator
961 const char* sendData,
962 const UList<int>& sendSizes,
963 const UList<int>& sendOffsets,
967 const label communicator
970 const label np = nProcs(communicator);
974 Pout<<
"** allToAll :"
976 <<
" sendSizes:" << sendSizes
977 <<
" sendOffsets:" << sendOffsets
978 <<
" with comm:" << communicator
987 && (sendSizes.size() != np || sendOffsets.size() != np)
991 <<
"Size of sendSizes " << sendSizes.size()
992 <<
" or sendOffsets " << sendOffsets.size()
993 <<
" is not equal to the number of processors in the domain "
1000 std::memmove(recvData, sendData, recvSize);
1010 const_cast<char*
>(sendData),
1011 const_cast<int*
>(sendSizes.begin()),
1012 const_cast<int*
>(sendOffsets.begin()),
1023 <<
"MPI_Scatterv failed for sendSizes " << sendSizes
1024 <<
" sendOffsets " << sendOffsets
1025 <<
" communicator " << communicator
1034 void Foam::UPstream::allocatePstreamCommunicator
1036 const label parentIndex,
1043 MPI_Group newGroup = MPI_GROUP_NULL;
1045 MPI_Comm newComm = MPI_COMM_NULL;
1051 <<
"PstreamGlobals out of sync with UPstream data. Problem."
1056 if (parentIndex == -1)
1063 <<
"world communicator should always be index "
1080 procIDs_[index].setSize(numProcs);
1081 forAll(procIDs_[index], i)
1083 procIDs_[index][i] = i;
1092 procIDs_[index].size(),
1093 procIDs_[index].
begin(),
1107 myProcNo_[index] = -1;
1122 <<
" when allocating communicator at " << index
1123 <<
" from ranks " << procIDs_[index]
1124 <<
" of parent " << parentIndex
1125 <<
" cannot find my own rank"
1133 void Foam::UPstream::freePstreamCommunicator(
const label communicator)
1135 if (communicator != 0)
1170 Pout<<
"UPstream::waitRequests : starting wait for "
1172 <<
" outstanding requests starting at " << start <<
endl;
1177 SubList<MPI_Request> waitRequests
1190 waitRequests.size(),
1191 waitRequests.begin(),
1197 <<
"MPI_Waitall returned with error" <<
Foam::endl;
1202 resetRequests(start);
1207 Pout<<
"UPstream::waitRequests : finished wait." <<
endl;
1216 Pout<<
"UPstream::waitRequest : starting wait for request:" << i
1224 <<
" outstanding send requests and you are asking for i=" << i
1226 <<
"Maybe you are mixing blocking/non-blocking comms?"
1242 <<
"MPI_Wait returned with error" <<
Foam::endl;
1251 Pout<<
"UPstream::waitRequest : finished wait for request:" << i
1261 Pout<<
"UPstream::finishedRequest : checking request:" << i
1269 <<
" outstanding send requests and you are asking for i=" << i
1271 <<
"Maybe you are mixing blocking/non-blocking comms?"
1285 Pout<<
"UPstream::finishedRequest : finished request:" << i
1313 Pout<<
"UPstream::allocateTag " <<
s
1342 Pout<<
"UPstream::allocateTag " <<
s
1361 Pout<<
"UPstream::freeTag " <<
s <<
" tag:" << tag <<
endl;
1377 Pout<<
"UPstream::freeTag " <<
s <<
" tag:" << tag <<
endl;
int debug
Static debugging option.
static int allocateTag(const char *)
static label warnComm
Debugging: warn for use of any communicator differing from warnComm.
static void resetRequests(const label sz)
Truncate number of outstanding requests.
static void addWaitTime()
Add time increment to waitTime.
static void printStack(Ostream &os)
Helper function to print a stack.
A class for handling words, derived from Foam::string.
constexpr auto begin(C &c) -> decltype(c.begin())
Return iterator to the beginning of the container c.
DynamicList< label > freedRequests_
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
static void scatterList(const List< commsStruct > &comms, List< T > &Values, const int tag, const label comm)
Scatter data. Reverse of gatherList.
bool read(const char *buf, int32_t &val)
Same as readInt32.
static bool & parRun()
Test if this a parallel run, or allow modify access.
static void waitRequests(const label start=0)
Wait until all requests (from start onwards) have finished.
static bool master(const label communicator=worldComm)
Am I the master process.
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
static void beginTiming()
Update timer prior to measurement.
DynamicList< MPI_Request > outstandingRequests_
Outstanding non-blocking operations.
static void abort()
Call MPI_Abort with no other checks or cleanup.
Ostream & endl(Ostream &os)
Add newline and flush stream.
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
iterator begin()
Return an iterator to begin traversing the UList.
string getEnv(const std::string &envName)
Get environment value for given envName.
#define forAll(list, i)
Loop across all elements in list.
static void addScatterTime()
Add time increment to scatterTime.
DynamicList< MPI_Comm > MPICommunicators_
List< word > wordList
A List of words.
void allReduce(Type &Value, int count, MPI_Datatype MPIType, MPI_Op op, const BinaryOp &bop, const int tag, const label communicator)
DynamicList< MPI_Group > MPIGroups_
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
static const int mpiBufferSize
MPI buffer-size (bytes)
word name(const complex &c)
Return string representation of complex.
static void addGatherTime()
Add time increment to gatherTime.
DynamicList< T, SizeMin > & append(const T &val)
Append an element to the end of this list.
static void waitRequest(const label i)
Wait until request i has finished.
static void addValidParOptions(HashTable< string > &validParOptions)
errorManip< error > abort(error &err)
static void detachOurBuffers()
Inter-processor communication reduction functions.
Various functions to wrap MPI_Allreduce.
errorManipArg< error, int > exit(error &err, const int errNo=1)
prefixOSstream Perr
OSstream wrapped stderr (std::cerr) with parallel prefix.
static void addAllToAllTime()
Add time increment to allToAllTime.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
static void gatherList(const List< commsStruct > &comms, List< T > &Values, const int tag, const label comm)
Gather data but keep individual values separate.
static label nRequests()
Get number of outstanding requests.
static int myProcNo(const label communicator=worldComm)
Number of this process (starting from masterNo() = 0)
static void scatter(const char *sendData, const UList< int > &sendSizes, const UList< int > &sendOffsets, char *recvData, int recvSize, const label communicator=worldComm)
Send data to all processors from the root of the communicator.
Vector2D< scalar > vector2D
A 2D vector of scalars obtained from the generic Vector2D.
static bool initNull()
Special purpose initialisation function.
static label worldComm
Default communicator (all processors)
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
static void shutdown(int errNo=0)
Shutdown (finalize) MPI as required.
static bool finishedRequest(const label i)
Non-blocking comms: has request i finished?
T remove()
Remove and return the last element. Fatal on an empty list.
DynamicList< int > freedTags_
Free'd message tags.
void size(const label n) noexcept
Override size to be inconsistent with allocated storage.
static void attachOurBuffers()
void sumReduce(T &Value, label &Count, const int tag=Pstream::msgType(), const label comm=UPstream::worldComm)
Helper class for allocating/freeing communicators.
UList< label > labelUList
A UList of labels.
static bool init(int &argc, char **&argv, const bool needsThread)
Initialisation function called from main.
static void allToAll(const labelUList &sendData, labelUList &recvData, const label communicator=worldComm)
Exchange label with all processors (in the communicator).
int nTags_
Max outstanding message tag operations.
#define WarningInFunction
Report a warning using Foam::Warning.
const string & prefix() const
Return the stream prefix.
static void gather(const char *sendData, int sendSize, char *recvData, const UList< int > &recvSizes, const UList< int > &recvOffsets, const label communicator=worldComm)
Receive data from all processors on the master.
static void exit(int errNo=1)
Shutdown (finalize) MPI as required and exit program with errNo.
static void freeTag(const char *, const int tag)