masterOFstream.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2017-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "masterOFstream.H"
27 #include "OFstream.H"
28 #include "OSspecific.H"
29 #include "PstreamBuffers.H"
31 #include "boolList.H"
32 
33 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
34 
35 void Foam::masterOFstream::checkWrite
36 (
37  const fileName& fName,
38  const string& str
39 )
40 {
41  mkDir(fName.path());
42 
43  OFstream os
44  (
45  fName,
47  version(),
48  compression_,
49  append_
50  );
51 
52  if (!os.good())
53  {
55  << "Could not open file " << fName
56  << exit(FatalIOError);
57  }
58 
59  os.writeQuoted(str, false);
60  if (!os.good())
61  {
63  << "Failed writing to " << fName
64  << exit(FatalIOError);
65  }
66 }
67 
68 
69 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
70 
72 (
73  const fileName& pathName,
74  streamFormat format,
75  versionNumber version,
76  compressionType compression,
77  const bool append,
78  const bool valid
79 )
80 :
81  OStringStream(format, version),
82  pathName_(pathName),
83  compression_(compression),
84  append_(append),
85  valid_(valid)
86 {}
87 
88 
89 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
90 
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_, str());
112  }
113  return;
114  }
115  boolList valid(Pstream::nProcs());
116  valid[Pstream::myProcNo()] = valid_;
117  Pstream::gatherList(valid);
118 
119 
120  // Different files
122 
123  // Send my buffer to master
124  if (!Pstream::master())
125  {
126  UOPstream os(Pstream::masterNo(), pBufs);
127  string s(this->str());
128  os.write(&s[0], s.size());
129  }
130 
131  labelList recvSizes;
132  pBufs.finishedSends(recvSizes);
133 
134  if (Pstream::master())
135  {
136  // Write my own data
137  {
138  if (valid[Pstream::myProcNo()])
139  {
140  checkWrite(filePaths[Pstream::myProcNo()], str());
141  }
142  }
143 
144  for (label proci = 1; proci < Pstream::nProcs(); proci++)
145  {
146  UIPstream is(proci, pBufs);
147  List<char> buf(recvSizes[proci]);
148 
149  is.read(buf.begin(), buf.size());
150 
151  if (valid[proci])
152  {
153  checkWrite
154  (
155  filePaths[proci],
156  string(buf.begin(), buf.size())
157  );
158  }
159  }
160  }
161  }
162  else
163  {
164  checkWrite(pathName_, str());
165  }
166 }
167 
168 
169 // ************************************************************************* //
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:59
static int masterNo()
Process index of the master.
Definition: UPstream.H:415
A class for handling file names.
Definition: fileName.H:69
void finishedSends(const bool block=true)
Mark all sends as having been done. This will start receives.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:427
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:421
versionNumber version() const
Return the stream version.
Definition: IOstream.H:399
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.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Input inter-processor communications stream operating on external buffer.
Definition: UIPstream.H:53
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().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))
iterator begin()
Return an iterator to begin traversing the UList.
Definition: UListI.H:216
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
OStringStream(streamFormat format=ASCII, versionNumber version=currentVersion)
Construct and set stream status.
Definition: OStringStream.H:60
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
Output inter-processor communications stream operating on external buffer.
Definition: UOPstream.H:54
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:79
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:34
Buffers for inter-processor communications streams (UOPstream, UIPstream).
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:289
static bool uniformFile(const fileNameList &)
Same file?
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:397
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:409
~masterOFstream()
Destructor.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
string str() const
Return the string.
Version number type.
Definition: IOstream.H:96
static void gatherList(const List< commsStruct > &comms, List< T > &Values, const int tag, const label comm)
Gather data but keep individual values separate.
IOerror FatalIOError