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 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
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
34void 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 (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
114Foam::label Foam::UIPBstream::read
115(
116 const commsTypes commsType,
117 const int rootProcNo,
118 char* buf,
119 const std::streamsize bufSize,
120 const int tag,
121 const label comm
122)
123{
124 if
125 (
126 !UPstream::broadcast(buf, bufSize, comm, rootProcNo)
127 )
128 {
130 << "MPI_Bcast failure receiving data:" << label(bufSize) << nl
132 return 0;
133 }
134
135 return bufSize;
136}
137
138
139// ************************************************************************* //
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
label capacity() const noexcept
Size of the underlying storage.
Definition: DynamicListI.H:287
void resize(const label len)
Definition: DynamicListI.H:353
void setEof() noexcept
Set stream state as reached 'eof'.
Definition: IOstream.H:357
virtual bool read()
Re-read model coefficients if they have changed.
const label comm_
Definition: UIPstream.H:95
DynamicList< char > & recvBuf_
Definition: UIPstream.H:89
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition: UListI.H:237
static bool broadcast(char *buf, const std::streamsize bufSize, const label communicator=worldComm, const int rootProcNo=masterNo())
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
errorManip< error > abort(error &err)
Definition: errorManip.H:144
error FatalError
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53