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-2021 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
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 "boolList.H"
35 #include <algorithm>
36 
37 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
38 
39 void Foam::masterOFstream::checkWrite
40 (
41  const fileName& fName,
42  const char* str,
43  std::streamsize len
44 )
45 {
46  if (!len)
47  {
48  // Can probably skip all of this if there is nothing to write
49  return;
50  }
51 
52  mkDir(fName.path());
53 
54  OFstream os
55  (
56  fName,
58  append_
59  );
60  if (!os.good())
61  {
63  << "Could not open file " << fName << nl
64  << exit(FatalIOError);
65  }
66 
67  // Use writeRaw() instead of writeQuoted(string,false) to output
68  // characters directly.
69 
70  os.writeRaw(str, len);
71 
72  if (!os.good())
73  {
75  << "Failed writing to " << fName << nl
76  << exit(FatalIOError);
77  }
78 }
79 
80 
81 void Foam::masterOFstream::checkWrite
82 (
83  const fileName& fName,
84  const std::string& s
85 )
86 {
87  checkWrite(fName, &s[0], s.length());
88 }
89 
90 
91 void Foam::masterOFstream::commit()
92 {
93  if (Pstream::parRun())
94  {
95  List<fileName> filePaths(Pstream::nProcs());
96  filePaths[Pstream::myProcNo()] = pathName_;
97  Pstream::gatherList(filePaths);
98 
99  bool uniform =
101  (
102  filePaths
103  );
104 
105  Pstream::scatter(uniform);
106 
107  if (uniform)
108  {
109  if (Pstream::master() && valid_)
110  {
111  checkWrite(pathName_, this->str());
112  }
113 
114  this->reset();
115  return;
116  }
117 
118  boolList valid(Pstream::nProcs());
119  valid[Pstream::myProcNo()] = valid_;
120  Pstream::gatherList(valid);
121 
122 
123  // Different files
124  PstreamBuffers pBufs(Pstream::commsTypes::nonBlocking);
125 
126  // Send my buffer to master
127  if (!Pstream::master())
128  {
129  UOPstream os(Pstream::masterNo(), pBufs);
130  string s(this->str());
131  this->reset();
132 
133  os.write(&s[0], s.length());
134  }
135 
136  labelList recvSizes;
137  pBufs.finishedSends(recvSizes);
138 
139  if (Pstream::master())
140  {
141  // Write master data
142  if (valid[Pstream::masterNo()])
143  {
144  checkWrite(filePaths[Pstream::masterNo()], this->str());
145  }
146  this->reset();
147 
148  // Find the max slave size
149  recvSizes[Pstream::masterNo()] = 0;
150  List<char> buf
151  (
152  *std::max_element(recvSizes.cbegin(), recvSizes.cend())
153  );
154 
155  for (const int proci : Pstream::subProcs())
156  {
157  UIPstream is(proci, pBufs);
158 
159  const std::streamsize count(recvSizes[proci]);
160  is.read(buf.data(), count);
161 
162  if (valid[proci])
163  {
164  checkWrite(filePaths[proci], buf.cdata(), count);
165  }
166  }
167  }
168  }
169  else
170  {
171  checkWrite(pathName_, this->str());
172  this->reset();
173  }
174 
175  // This method is only called once (internally)
176  // so no need to clear/flush old buffered data
177 }
178 
179 
180 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
181 
183 (
184  const fileName& pathName,
185  IOstreamOption streamOpt,
186  const bool append,
187  const bool valid
188 )
189 :
190  OStringStream(streamOpt),
191  pathName_(pathName),
192  compression_(streamOpt.compression()),
193  append_(append),
194  valid_(valid)
195 {}
196 
197 
198 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
199 
201 {
202  commit();
203 }
204 
205 
206 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:67
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::UPstream::masterNo
static constexpr int masterNo() noexcept
Process index of the master (always 0)
Definition: UPstream.H:451
boolList.H
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
s
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))
Definition: gmvOutputSpray.H:25
Foam::fileOperations::masterUncollatedFileOperation::uniformFile
static bool uniformFile(const fileNameList &)
Same file?
Definition: masterUncollatedFileOperation.C:478
Foam::IOstreamOption::IOstreamOption
constexpr IOstreamOption(streamFormat fmt=streamFormat::ASCII, compressionType comp=compressionType::UNCOMPRESSED) noexcept
Definition: IOstreamOption.H:193
masterOFstream.H
Foam::UPstream::master
static bool master(const label communicator=worldComm)
Am I the master process.
Definition: UPstream.H:457
Foam::Pstream::scatter
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
Definition: gatherScatter.C:150
Foam::boolList
List< bool > boolList
A List of bools.
Definition: List.H:65
Foam::FatalIOError
IOerror FatalIOError
Foam::masterOFstream::~masterOFstream
~masterOFstream()
Destructor - commits buffered information to file.
Definition: masterOFstream.C:200
append
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
Foam::OBJstream::write
virtual Ostream & write(const char c)
Write character.
Definition: OBJstream.C:78
OFstream.H
Foam::UPstream::subProcs
static rangeType subProcs(const label communicator=worldComm)
Range of process indices for sub-processes.
Definition: UPstream.H:515
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
Foam::masterOFstream::masterOFstream
masterOFstream(const fileName &pathname, IOstreamOption streamOpt=IOstreamOption(), const bool append=false, const bool valid=true)
Construct from pathname and set stream status.
Definition: masterOFstream.C:183
Foam::IOstreamOption::version
versionNumber version() const noexcept
Get the stream version.
Definition: IOstreamOption.H:338
os
OBJstream os(runTime.globalPath()/outputName)
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
reset
meshPtr reset(new Foam::fvMesh(Foam::IOobject(regionName, runTime.timeName(), runTime, Foam::IOobject::MUST_READ), false))
Foam::Detail::StringStreamAllocator< std::ostringstream >::str
Foam::string str() const
Get the string - as Foam::string rather than std::string.
Definition: StringStream.H:88
Foam::IOstreamOption::BINARY
"binary"
Definition: IOstreamOption.H:73
masterUncollatedFileOperation.H
Foam::Pstream::gatherList
static void gatherList(const List< commsStruct > &comms, List< T > &Values, const int tag, const label comm)
Gather data but keep individual values separate.
Definition: gatherScatterList.C:52
Foam::UPstream::commsTypes::nonBlocking
Foam::UPstream::myProcNo
static int myProcNo(const label communicator=worldComm)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:463
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:77
Foam::UPstream::parRun
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:433
Foam::OStringStream
Output to string buffer, using a OSstream. Always UNCOMPRESSED.
Definition: StringStream.H:227
PstreamBuffers.H
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::IOstreamOption::compression
compressionType compression() const noexcept
Get the stream compression.
Definition: IOstreamOption.H:312
Foam::mkDir
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:507
Foam::UPstream::nProcs
static label nProcs(const label communicator=worldComm)
Number of processes in parallel run, and 1 for serial run.
Definition: UPstream.H:445