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  std::streamsize bufSize(sendBuf_.size());
44 
45  // TODO: some corrective action
46  #if 0
47  if (bufSize > std::streamsize(INT_MAX))
48  {
49  Perr<< "UOPBstream::write() :"
50  << " exceeds INT_MAX bytes" << Foam::endl;
52  }
53  #endif
54 
55  // Broadcast #1 - data size
56  if
57  (
59  (
60  reinterpret_cast<char*>(&bufSize),
61  sizeof(std::streamsize),
62  comm_,
63  toProcNo_ //< is actually rootProcNo
64  )
65  )
66  {
68  << "MPI_Bcast failure sending buffer size:" << bufSize << nl
70  return false;
71  }
72 
73 
74  // Broadcast #2 - data content
75  // - skip if there is no data to send
76  if
77  (
78  (bufSize > 0)
80  (
81  sendBuf_.data(),
82  sendBuf_.size(), // same as bufSize
83  comm_,
84  toProcNo_ //< is actually rootProcNo
85  )
86  )
87  {
89  << "MPI_Bcast failure sending buffer data:"
90  << sendBuf_.size() << nl
92  return false;
93  }
94 
95  return true;
96 }
97 
98 
99 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
100 
102 (
103  const int rootProcNo,
104  const char* buf,
105  const std::streamsize bufSize,
106  const label comm
107 )
108 {
109  if
110  (
111  !UPstream::broadcast(const_cast<char*>(buf), bufSize, comm, rootProcNo)
112  )
113  {
115  << "MPI_Bcast failure sending buffer data:" << label(bufSize) << nl
117  return false;
118  }
119 
120  return true;
121 }
122 
123 
124 // ************************************************************************* //
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
const int comm_
The communicator index.
Definition: UOPstream.H:117
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:265
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
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:107
DynamicList< char > & sendBuf_
Reference to the send buffer data.
Definition: UOPstream.H:127
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...
static void printStack(Ostream &os, int size=-1)
Helper function to print a stack, with optional upper limit.