UOPstreamWrite.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2019-2023 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "UOPstream.H"
30 #include "PstreamGlobals.H"
31 #include "profilingPstream.H"
32 
33 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
34 
35 bool Foam::UOPstream::bufferIPCsend()
36 {
37  return UOPstream::write
38  (
39  commsType(),
40  toProcNo_,
41  sendBuf_.cdata(),
42  sendBuf_.size(),
43  tag_,
44  comm_
45  );
46 }
47 
48 
49 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
50 
52 (
53  const UPstream::commsTypes commsType,
54  const int toProcNo,
55  const char* buf,
56  const std::streamsize bufSize,
57  const int tag,
58  const label communicator,
59  UPstream::Request* req,
60  const UPstream::sendModes sendMode
61 )
62 {
64 
65  // TODO: some corrective action, at least when not nonBlocking
66  #if 0
67  if (bufSize > std::streamsize(INT_MAX))
68  {
69  Perr<< "UOPstream::write() : to rank " << toProcNo
70  << " exceeds INT_MAX bytes" << Foam::endl;
72  }
73  #endif
74 
75  if (UPstream::warnComm >= 0 && communicator != UPstream::warnComm)
76  {
77  Perr<< "UOPstream::write : starting write to:" << toProcNo
78  << " size:" << label(bufSize)
79  << " tag:" << tag << " comm:" << communicator
80  << " commType:" << UPstream::commsTypeNames[commsType]
81  << " warnComm:" << UPstream::warnComm
82  << Foam::endl;
84  }
85  else if (UPstream::debug)
86  {
87  Perr<< "UOPstream::write : starting write to:" << toProcNo
88  << " size:" << label(bufSize)
89  << " tag:" << tag << " comm:" << communicator
90  << " commType:" << UPstream::commsTypeNames[commsType]
91  << Foam::endl;
92  }
93 
94  PstreamGlobals::checkCommunicator(communicator, toProcNo);
95 
96  int returnCode = MPI_ERR_UNKNOWN;
97 
99 
100  if (commsType == UPstream::commsTypes::buffered)
101  {
102  returnCode = MPI_Bsend
103  (
104  const_cast<char*>(buf),
105  bufSize,
106  MPI_BYTE,
107  toProcNo,
108  tag,
110  );
111 
112  // Assume these are from scatters ...
114 
115  if (UPstream::debug)
116  {
117  Perr<< "UOPstream::write : finished buffered send to:"
118  << toProcNo
119  << " size:" << label(bufSize) << " tag:" << tag
120  << Foam::endl;
121  }
122  }
123  else if (commsType == UPstream::commsTypes::scheduled)
124  {
125  if (UPstream::sendModes::sync == sendMode)
126  {
127  returnCode = MPI_Ssend
128  (
129  const_cast<char*>(buf),
130  bufSize,
131  MPI_BYTE,
132  toProcNo,
133  tag,
135  );
136  }
137  else
138  {
139  returnCode = MPI_Send
140  (
141  const_cast<char*>(buf),
142  bufSize,
143  MPI_BYTE,
144  toProcNo,
145  tag,
147  );
148  }
149 
150  // Assume these are from scatters ...
152 
153  if (UPstream::debug)
154  {
155  Perr<< "UOPstream::write : finished send to:"
156  << toProcNo
157  << " size:" << label(bufSize) << " tag:" << tag
158  << Foam::endl;
159  }
160  }
161  else if (commsType == UPstream::commsTypes::nonBlocking)
162  {
163  MPI_Request request;
164 
165  if (UPstream::sendModes::sync == sendMode)
166  {
167  returnCode = MPI_Issend
168  (
169  const_cast<char*>(buf),
170  bufSize,
171  MPI_BYTE,
172  toProcNo,
173  tag,
174  PstreamGlobals::MPICommunicators_[communicator],
175  &request
176  );
177  }
178  else
179  {
180  returnCode = MPI_Isend
181  (
182  const_cast<char*>(buf),
183  bufSize,
184  MPI_BYTE,
185  toProcNo,
186  tag,
187  PstreamGlobals::MPICommunicators_[communicator],
188  &request
189  );
190  }
191 
192  if (UPstream::debug)
193  {
194  Perr<< "UOPstream::write : started non-blocking send to:"
195  << toProcNo
196  << " size:" << label(bufSize) << " tag:" << tag
197  << " request:" <<
198  (req ? label(-1) : PstreamGlobals::outstandingRequests_.size())
199  << Foam::endl;
200  }
201 
202  PstreamGlobals::push_request(request, req);
204  }
205  else
206  {
208  << "Unsupported communications type " << int(commsType)
210  }
211 
212  return (returnCode == MPI_SUCCESS);
213 }
214 
215 
216 // ************************************************************************* //
prefixOSstream Perr
OSstream wrapped stderr (std::cerr) with parallel prefix.
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
sendModes
Different MPI-send modes (ignored for commsTypes::buffered)
Definition: UPstream.H:94
DynamicList< MPI_Request > outstandingRequests_
Outstanding non-blocking operations.
static const Enum< commsTypes > commsTypeNames
Enumerated names for the communication types.
Definition: UPstream.H:89
commsTypes
Communications types.
Definition: UPstream.H:77
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:608
const int comm_
The communicator index.
Definition: UOPstream.H:117
DynamicList< MPI_Comm > MPICommunicators_
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
void reset_request(UPstream::Request *requestPtr, label *requestIdx=nullptr)
Reset UPstream::Request to null and/or the index of the outstanding request to -1.
void checkCommunicator(const label comm, const label toProcNo)
Fatal if comm is outside the allocated range.
"scheduled" (MPI standard) : (MPI_Send, MPI_Recv)
const int toProcNo_
Destination rank for the data.
Definition: UOPstream.H:107
DynamicList< char > & sendBuf_
Reference to the send buffer data.
Definition: UOPstream.H:127
static void addScatterTime()
Add time increment to scatter time.
static label warnComm
Debugging: warn for use of any communicator differing from warnComm.
Definition: UPstream.H:426
errorManip< error > abort(error &err)
Definition: errorManip.H:139
commsTypes commsType() const noexcept
Get the communications type of the stream.
Definition: UPstream.H:1284
(MPI_Ssend, MPI_Issend)
static void beginTiming()
Update timer prior to measurement.
int debug
Static debugging option.
static void addRequestTime()
Add time increment to request time.
void push_request(MPI_Request request, UPstream::Request *requestPtr=nullptr, label *requestIdx=nullptr)
Transcribe MPI_Request to UPstream::Request (does not affect the stack of outstanding requests) or el...
static bool write(const UPstream::commsTypes commsType, const int toProcNo, const char *buf, const std::streamsize bufSize, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm, UPstream::Request *req=nullptr, const UPstream::sendModes sendMode=UPstream::sendModes::normal)
Write buffer contents to given processor.
"nonBlocking" (immediate) : (MPI_Isend, MPI_Irecv)
static void printStack(Ostream &os, int size=-1)
Helper function to print a stack, with optional upper limit.
const T * cdata() const noexcept
Return pointer to the underlying array serving as data storage.
Definition: UListI.H:258
const int tag_
Message tag for communication.
Definition: UOPstream.H:112
"buffered" : (MPI_Bsend, MPI_Recv)
Namespace for OpenFOAM.