masterOFstream.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) 2017 OpenFOAM Foundation
9 Copyright (C) 2020-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include "masterOFstream.H"
30#include "OFstream.H"
31#include "OSspecific.H"
32#include "PstreamBuffers.H"
34#include <algorithm>
35
36// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
37
38void Foam::masterOFstream::checkWrite
39(
40 const fileName& fName,
41 const char* str,
42 std::streamsize len
43)
44{
45 if (!len)
46 {
47 // Can probably skip all of this if there is nothing to write
48 return;
49 }
50
51 mkDir(fName.path());
52
53 OFstream os
54 (
55 fName,
57 append_
58 );
59 if (!os.good())
60 {
62 << "Could not open file " << fName << nl
64 }
65
66 // Use writeRaw() instead of writeQuoted(string,false) to output
67 // characters directly.
68
69 os.writeRaw(str, len);
70
71 if (!os.good())
72 {
74 << "Failed writing to " << fName << nl
76 }
77}
78
79
80void Foam::masterOFstream::checkWrite
81(
82 const fileName& fName,
83 const std::string& s
84)
85{
86 checkWrite(fName, &s[0], s.length());
87}
88
89
90void Foam::masterOFstream::commit()
91{
92 if (Pstream::parRun())
93 {
94 List<fileName> filePaths(Pstream::nProcs());
95 filePaths[Pstream::myProcNo()] = pathName_;
96 Pstream::gatherList(filePaths);
97
98 bool uniform =
100 (
101 filePaths
102 );
103
104 Pstream::broadcast(uniform);
105
106 if (uniform)
107 {
108 if (Pstream::master() && valid_)
109 {
110 checkWrite(pathName_, this->str());
111 }
112
113 this->reset();
114 return;
115 }
116
117 boolList procValid(UPstream::listGatherValues<bool>(valid_));
118
119 // Different files
120 PstreamBuffers pBufs(Pstream::commsTypes::nonBlocking);
121
122 // Send my buffer to master
123 if (!Pstream::master())
124 {
125 UOPstream os(Pstream::masterNo(), pBufs);
126 string s(this->str());
127 this->reset();
128
129 os.write(&s[0], s.length());
130 }
131
132 labelList recvSizes;
133 pBufs.finishedGathers(recvSizes);
134
135 if (Pstream::master())
136 {
137 // Write master data
138 if (procValid[Pstream::masterNo()])
139 {
140 checkWrite(filePaths[Pstream::masterNo()], this->str());
141 }
142 this->reset();
143
144 // Find the max receive size
145 recvSizes[Pstream::masterNo()] = 0;
146 List<char> buf
147 (
148 *std::max_element(recvSizes.cbegin(), recvSizes.cend())
149 );
150
151 for (const int proci : Pstream::subProcs())
152 {
153 UIPstream is(proci, pBufs);
154
155 const std::streamsize count(recvSizes[proci]);
156 is.read(buf.data(), count);
157
158 if (procValid[proci])
159 {
160 checkWrite(filePaths[proci], buf.cdata(), count);
161 }
162 }
163 }
164 }
165 else
166 {
167 checkWrite(pathName_, this->str());
168 this->reset();
169 }
170
171 // This method is only called once (internally)
172 // so no need to clear/flush old buffered data
173}
174
175
176// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
177
179(
180 const fileName& pathName,
181 IOstreamOption streamOpt,
182 const bool append,
183 const bool valid
184)
185:
186 OStringStream(streamOpt),
187 pathName_(pathName),
188 compression_(streamOpt.compression()),
189 append_(append),
190 valid_(valid)
191{}
192
193
194// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
195
197{
198 commit();
199}
200
201
202// ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::string str() const
Get the string - as Foam::string rather than std::string.
Definition: StringStream.H:88
The IOstreamOption is a simple container for options an IOstream can normally have.
versionNumber version() const noexcept
Get the stream version.
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
virtual Ostream & write(const char c)
Write character.
Definition: OBJstream.C:78
virtual Ostream & writeRaw(const char *data, std::streamsize count)
Low-level raw binary output.
Definition: OSstream.C:251
Output to string buffer, using a OSstream. Always UNCOMPRESSED.
Definition: StringStream.H:231
label nProcs() const noexcept
Number of ranks associated with PstreamBuffers.
UPstream::rangeType subProcs() const noexcept
Range of sub-processes indices associated with PstreamBuffers.
static void gatherList(const List< commsStruct > &comms, List< T > &values, const int tag, const label comm)
static void broadcast(Type &value, const label comm=UPstream::worldComm)
@ nonBlocking
"nonBlocking"
static constexpr int masterNo() noexcept
Process index of the master (always 0)
Definition: UPstream.H:451
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:433
A class for handling file names.
Definition: fileName.H:76
static bool uniformFile(const fileNameList &)
Same file?
Master-only drop-in replacement for OFstream.
~masterOFstream()
Destructor - commits buffered information to file.
int myProcNo() const noexcept
Return processor number.
splitCell * master() const
Definition: splitCell.H:113
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
OBJstream os(runTime.globalPath()/outputName)
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:78
List< label > labelList
A List of labels.
Definition: List.H:66
bool mkDir(const fileName &pathName, mode_t mode=0777)
Make a directory and return an error if it could not be created.
Definition: MSwindows.C:515
IOerror FatalIOError
List< bool > boolList
A List of bools.
Definition: List.H:64
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53