UOPBstreamWrite.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 "UOPstream.H"
29 #include "PstreamGlobals.H"
30 
31 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
32 
33 bool Foam::UOPBstream::bufferIPCsend()
34 {
35  // Uses double broadcast
36  // 1. for the data size
37  // 2. for the data itself
38  // With this information, can determine and resize receive buffer
39 
41 
42  // Same type must be expected in UIPBstream::bufferIPCrecv()
43  label bufSize(sendBuf_.size());
44 
45  // Broadcast #1 - data size
46  if
47  (
49  (
50  reinterpret_cast<char*>(&bufSize),
51  sizeof(label),
52  comm_,
53  toProcNo_ //< is actually rootProcNo
54  )
55  )
56  {
58  << "MPI_Bcast failure sending buffer size:" << bufSize << nl
60  return false;
61  }
62 
63  // Broadcast #2 - data content
64  // - skip if there is no data to send
65  if (bufSize)
66  {
67  if
68  (
70  (
71  sendBuf_.data(),
72  sendBuf_.size(), // same as bufSize
73  comm_,
74  toProcNo_ //< is actually rootProcNo
75  )
76  )
77  {
79  << "MPI_Bcast failure sending buffer data:" << bufSize << nl
81  return false;
82  }
83  }
84 
85  return true;
86 }
87 
88 
89 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
90 
92 (
93  const int rootProcNo,
94  const char* buf,
95  const std::streamsize bufSize,
96  const label comm
97 )
98 {
99  if
100  (
101  !UPstream::broadcast(const_cast<char*>(buf), bufSize, comm, rootProcNo)
102  )
103  {
105  << "MPI_Bcast failure sending buffer data:" << label(bufSize) << nl
107  return false;
108  }
109 
110  return true;
111 }
112 
113 
114 // ************************************************************************* //
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:578
const int comm_
The communicator index.
Definition: UOPstream.H:114
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition: UListI.H:229
void checkCommunicator(const label comm, const label toProcNo)
Fatal if comm is outside the allocated range.
static bool write(const int rootProcNo, const char *buf, const std::streamsize bufSize, const label comm=UPstream::worldComm)
Wrapped version of UPstream::broadcast with const-cast.
const int toProcNo_
Destination rank for the data.
Definition: UOPstream.H:104
DynamicList< char > & sendBuf_
Reference to the send buffer data.
Definition: UOPstream.H:124
errorManip< error > abort(error &err)
Definition: errorManip.H:139
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...