UIPBstreamRead.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) 2022-2023 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "UIPstream.H"
29 #include "PstreamGlobals.H"
30 #include "IOstreams.H"
31 
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33 
34 void Foam::UIPBstream::bufferIPCrecv()
35 {
36  // Uses double broadcast. Symmetric with UOPBstream::bufferIPCsend()
37  // 1. for the data size
38  // 2. for the data itself
39 
40  // Expected message size, similar to MPI_Probe
41  // Same type must be expected in UOPBstream::bufferIPCsend()
42  label bufSize(0);
43 
44  // Broadcast #1 - data size
45  if
46  (
48  (
49  reinterpret_cast<char*>(&bufSize),
50  sizeof(label),
51  comm_,
52  fromProcNo_ //< is actually rootProcNo
53  )
54  )
55  {
57  << "MPI_Bcast failure receiving buffer size" << nl
59  }
60 
61  if (UPstream::debug)
62  {
63  Pout<< "UOPBstream IPC read buffer :"
64  << " root:" << fromProcNo_
65  << " comm:" << comm_
66  << " probed size:" << bufSize
67  << " wanted size:" << recvBuf_.capacity()
68  << Foam::endl;
69  }
70 
71  // No buffer size allocated/specified
72  if (!recvBuf_.capacity())
73  {
74  recvBuf_.resize(bufSize);
75  }
76 
77  // This is the only real information we can trust
78  messageSize_ = bufSize;
79 
80  // Broadcast #2 - data content
81  // - skip if there is no data to receive
82 
83  if (messageSize_)
84  {
85  if
86  (
88  (
89  recvBuf_.data(),
90  messageSize_, // same as bufSize
91  comm_,
92  fromProcNo_ //< is actually rootProcNo
93  )
94  )
95  {
97  << "MPI_Bcast failure receiving buffer data:" << bufSize << nl
99  }
100  }
101 
102  // Set addressed size. Leave actual allocated memory intact.
104 
105  if (!messageSize_)
106  {
107  setEof();
108  }
109 }
110 
111 
112 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
113 
114 Foam::label Foam::UIPBstream::read
115 (
116  const int rootProcNo,
117  char* buf,
118  const std::streamsize bufSize,
119  const label comm
120 )
121 {
122  if
123  (
124  !UPstream::broadcast(buf, bufSize, comm, rootProcNo)
125  )
126  {
128  << "MPI_Bcast failure receiving data:" << label(bufSize) << nl
130  return 0;
131  }
132 
133  return bufSize;
134 }
135 
136 
137 // ************************************************************************* //
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:598
static label read(const int rootProcNo, char *buf, const std::streamsize bufSize, const label comm=UPstream::worldComm)
Wrapped version of UPstream::broadcast.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition: UListI.H:272
void resize(const label len)
Alter addressable list size, allocating new space if required while recovering old content...
Definition: DynamicListI.H:353
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
const int fromProcNo_
Source rank for the data.
Definition: UIPstream.H:98
int messageSize_
The message size, read on bufferIPCrecv or set directly.
Definition: UIPstream.H:113
label capacity() const noexcept
Size of the underlying storage.
Definition: DynamicList.H:225
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
errorManip< error > abort(error &err)
Definition: errorManip.H:139
int debug
Static debugging option.
DynamicList< char > & recvBuf_
Reference to the receive buffer data.
Definition: UIPstream.H:129
static bool broadcast(char *buf, const std::streamsize bufSize, const label communicator, const int rootProcNo=masterNo())
Broadcast buffer contents to all processes in given communicator. The sizes must match on all process...
void setEof() noexcept
Set stream state as reached &#39;eof&#39;.
Definition: IOstream.H:443
const int comm_
The communicator index.
Definition: UIPstream.H:108
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.