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 -------------------------------------------------------------------------------
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 "masterOFstream.H"
29 #include "OFstream.H"
30 #include "OSspecific.H"
31 #include "PstreamBuffers.H"
33 #include "boolList.H"
34 
35 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
36 
37 void Foam::masterOFstream::checkWrite
38 (
39  const fileName& fName,
40  const string& str
41 )
42 {
43  mkDir(fName.path());
44 
45  OFstream os
46  (
47  fName,
48  IOstream::BINARY, //format(),
49  version(),
50  compression_,
51  append_
52  );
53  if (!os.good())
54  {
56  << "Could not open file " << fName
57  << exit(FatalIOError);
58  }
59 
60  os.writeQuoted(str, false);
61  if (!os.good())
62  {
64  << "Failed writing to " << fName
65  << exit(FatalIOError);
66  }
67 }
68 
69 
70 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
71 
73 (
74  const fileName& pathName,
77  compressionType compression,
78  const bool append,
79  const bool valid
80 )
81 :
83  pathName_(pathName),
84  compression_(compression),
85  append_(append),
86  valid_(valid)
87 {}
88 
89 
90 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
91 
93 {
94  if (Pstream::parRun())
95  {
96  List<fileName> filePaths(Pstream::nProcs());
97  filePaths[Pstream::myProcNo()] = pathName_;
98  Pstream::gatherList(filePaths);
99 
100  bool uniform =
102  (
103  filePaths
104  );
105 
107 
108  if (uniform)
109  {
110  if (Pstream::master() && valid_)
111  {
112  checkWrite(pathName_, str());
113  }
114  return;
115  }
116  boolList valid(Pstream::nProcs());
117  valid[Pstream::myProcNo()] = valid_;
118  Pstream::gatherList(valid);
119 
120 
121  // Different files
123 
124  // Send my buffer to master
125  if (!Pstream::master())
126  {
127  UOPstream os(Pstream::masterNo(), pBufs);
128  string s(this->str());
129  os.write(&s[0], s.size());
130  }
131 
132  labelList recvSizes;
133  pBufs.finishedSends(recvSizes);
134 
135  if (Pstream::master())
136  {
137  // Write my own data
138  {
139  if (valid[Pstream::myProcNo()])
140  {
141  checkWrite(filePaths[Pstream::myProcNo()], str());
142  }
143  }
144 
145  for (label proci = 1; proci < Pstream::nProcs(); proci++)
146  {
147  UIPstream is(proci, pBufs);
148  List<char> buf(recvSizes[proci]);
149 
150  is.read(buf.begin(), buf.size());
151 
152  if (valid[proci])
153  {
154  checkWrite
155  (
156  filePaths[proci],
157  string(buf.begin(), buf.size())
158  );
159  }
160  }
161  }
162  }
163  else
164  {
165  checkWrite(pathName_, str());
166  }
167 }
168 
169 
170 // ************************************************************************* //
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::UPstream::commsTypes::nonBlocking
Foam::masterOFstream::masterOFstream
masterOFstream(const fileName &pathname, streamFormat format=ASCII, versionNumber version=currentVersion, compressionType compression=UNCOMPRESSED, const bool append=false, const bool valid=true)
Construct and set stream status.
Definition: masterOFstream.C:73
Foam::UPstream::masterNo
static constexpr int masterNo() noexcept
Process index of the master.
Definition: UPstream.H:432
boolList.H
Foam::UOPstream
Output inter-processor communications stream operating on external buffer.
Definition: UOPstream.H:57
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
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:477
masterOFstream.H
Foam::UPstream::nProcs
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:426
Foam::PstreamBuffers
Buffers for inter-processor communications streams (UOPstream, UIPstream).
Definition: PstreamBuffers.H:88
Foam::UPstream::parRun
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:414
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::FatalIOError
IOerror FatalIOError
Foam::masterOFstream::~masterOFstream
~masterOFstream()
Destructor.
Definition: masterOFstream.C:92
OFstream.H
Foam::UIPstream::read
static label read(const commsTypes commsType, const int fromProcNo, char *buf, const std::streamsize bufSize, const int tag=UPstream::msgType(), const label communicator=0)
Read into given buffer from given processor and return the.
Definition: UIPread.C:81
Foam::uniform
Definition: uniform.H:50
format
word format(conversionProperties.get< word >("format"))
Foam::UOPstream::write
static bool write(const commsTypes commsType, const int toProcNo, const char *buf, const std::streamsize bufSize, const int tag=UPstream::msgType(), const label communicator=0)
Write given buffer to given processor.
Definition: UOPwrite.C:36
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:79
Foam::PstreamBuffers::finishedSends
void finishedSends(const bool block=true)
Mark all sends as having been done. This will start receives.
Definition: PstreamBuffers.C:80
Foam::IOstreamOption::version
versionNumber version() const noexcept
Get the stream version.
Definition: IOstreamOption.H:321
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:64
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::foamVersion::version
const std::string version
OpenFOAM version (name or stringified number) as a std::string.
Foam::UPstream::myProcNo
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:444
Foam::UPstream::master
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:438
Foam::Detail::StringStreamAllocator< std::ostringstream >::str
Foam::string str() const
Get the string - as Foam::string rather than std::string.
Definition: StringStream.H:92
Foam::IOstreamOption::BINARY
"binary"
Definition: IOstreamOption.H:67
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::List< fileName >
Foam::OStringStream
Output to string buffer, using a OSstream.
Definition: StringStream.H:189
PstreamBuffers.H
Foam::IOstreamOption::compressionType
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
Definition: IOstreamOption.H:71
append
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:375
Foam::UIPstream
Input inter-processor communications stream operating on external buffer.
Definition: UIPstream.H:56
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