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-2024 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  std::streamsize bufSize(0);
43 
44  // Broadcast #1 - data size
45  if
46  (
48  (
49  reinterpret_cast<char*>(&bufSize),
50  sizeof(std::streamsize),
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  Perr<< "UOPBstream IPC read buffer :"
64  << " root:" << fromProcNo_
65  << " comm:" << comm_
66  << " probed size:" << label(bufSize)
67  << " wanted size:" << recvBuf_.capacity()
68  << Foam::endl;
69  }
70 
71 
72  // Set buffer size, avoiding any copying and resize doubling etc.
73  recvBuf_.clear();
74  if (recvBuf_.capacity() < label(bufSize))
75  {
76  recvBuf_.setCapacity_nocopy(label(bufSize));
77  }
78  recvBuf_.resize_nocopy(label(bufSize));
79 
80  // This is the only real information we can trust
81  messageSize_ = label(bufSize);
82 
83 
84  // Broadcast #2 - data content
85  // - skip if there is no data to receive
86  if
87  (
88  (bufSize > 0)
90  (
91  recvBuf_.data(),
92  recvBuf_.size(), // same as bufSize
93  comm_,
94  fromProcNo_ //< is actually rootProcNo
95  )
96  )
97  {
99  << "MPI_Bcast failure receiving buffer data:"
100  << recvBuf_.size() << nl
102  }
103 
104  if (recvBuf_.empty())
105  {
106  setEof();
107  }
108 }
109 
110 
111 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
112 
113 std::streamsize Foam::UIPBstream::read
114 (
115  const int rootProcNo,
116  char* buf,
117  const std::streamsize bufSize,
118  const label comm
119 )
120 {
121  if
122  (
123  !UPstream::broadcast(buf, bufSize, comm, rootProcNo)
124  )
125  {
127  << "MPI_Bcast failure receiving data:" << label(bufSize) << nl
129  return 0;
130  }
131 
132  return bufSize;
133 }
134 
135 
136 // ************************************************************************* //
prefixOSstream Perr
OSstream wrapped stderr (std::cerr) with parallel prefix.
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
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
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
bool empty() const noexcept
True if List is empty (ie, size() is zero)
Definition: UList.H:675
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition: UListI.H:265
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
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...
label messageSize_
The message size, read on bufferIPCrecv or set directly.
Definition: UIPstream.H:113
static std::streamsize read(const int rootProcNo, char *buf, const std::streamsize bufSize, const label comm=UPstream::worldComm)
Wrapped version of UPstream::broadcast.
errorManip< error > abort(error &err)
Definition: errorManip.H:139
int debug
Static debugging option.
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:405
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 resize_nocopy(const label len)
Alter addressable list size, allocating new space if required without necessarily recovering old cont...
Definition: DynamicListI.H:375
void setEof() noexcept
Set stream state as reached &#39;eof&#39;.
Definition: IOstream.H:453
void setCapacity_nocopy(const label len)
Alter the size of the underlying storage, without retaining old content.
Definition: DynamicListI.H:313
const int comm_
The communicator index.
Definition: UIPstream.H:108