OFstreamCollator.H
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-2018 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 Class
27  Foam::OFstreamCollator
28 
29 Description
30  Threaded file writer.
31 
32  Collects all data from all processors and writes as single
33  'decomposedBlockData' file. The operation is determined by the
34  buffer size (maxThreadFileBufferSize setting):
35  - local size of data is larger than buffer: receive and write processor
36  by processor (i.e. 'scheduled'). Does not use a thread, no file size
37  limit.
38  - total size of data is larger than buffer (but local is not):
39  thread does all the collecting and writing of the processors. No file
40  size limit.
41  - total size of data is less than buffer:
42  collecting is done locally; the thread only does the writing
43  (since the data has already been collected)
44 
45 
46 Operation determine
47 
48 SourceFiles
49  OFstreamCollator.C
50 
51 \*---------------------------------------------------------------------------*/
52 
53 #ifndef OFstreamCollator_H
54 #define OFstreamCollator_H
55 
56 #include <thread>
57 #include <mutex>
58 #include "IOstream.H"
59 #include "labelList.H"
60 #include "FIFOStack.H"
61 #include "SubList.H"
62 
63 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
64 
65 namespace Foam
66 {
67 
68 /*---------------------------------------------------------------------------*\
69  Class OFstreamCollator Declaration
70 \*---------------------------------------------------------------------------*/
71 
72 class OFstreamCollator
73 {
74  // Private class
75 
76  class writeData
77  {
78  public:
79 
80  const label comm_;
81  const word typeName_;
82  const fileName pathName_;
83  const string data_;
84  const labelList sizes_;
85  PtrList<List<char>> slaveData_;
86  const IOstream::streamFormat format_;
87  const IOstream::versionNumber version_;
88  const IOstream::compressionType compression_;
89  const bool append_;
90 
91  writeData
92  (
93  const label comm,
94  const word& typeName,
95  const fileName& pathName,
96  const string& data,
97  const labelList& sizes,
100  IOstream::compressionType compression,
101  const bool append
102  )
103  :
104  comm_(comm),
105  typeName_(typeName),
106  pathName_(pathName),
107  data_(data),
108  sizes_(sizes),
109  slaveData_(0),
110  format_(format),
111  version_(version),
112  compression_(compression),
113  append_(append)
114  {}
115 
116  //- (approximate) size of master + any optional slave data
117  off_t size() const
118  {
119  off_t sz = data_.size();
120  forAll(slaveData_, i)
121  {
122  if (slaveData_.set(i))
123  {
124  sz += slaveData_[i].size();
125  }
126  }
127  return sz;
128  }
129  };
130 
131 
132  // Private data
133 
134  //- Total amount of storage to use for object stack below
135  const off_t maxBufferSize_;
136 
137  mutable std::mutex mutex_;
138 
139  autoPtr<std::thread> thread_;
140 
141  //- Stack of files to write + contents
142  FIFOStack<writeData*> objects_;
143 
144  //- Whether thread is running (and not exited)
145  bool threadRunning_;
146 
147  //- Communicator to use for all parallel ops (in simulation thread)
148  label localComm_;
149 
150  //- Communicator to use for all parallel ops (in write thread)
151  label threadComm_;
152 
153 
154  // Private Member Functions
155 
156  //- Write actual file
157  static bool writeFile
158  (
159  const label comm,
160  const word& typeName,
161  const fileName& fName,
162  const string& masterData,
163  const labelUList& recvSizes,
164  const PtrList<SubList<char>>& slaveData,
168  const bool append
169  );
170 
171  //- Write all files in stack
172  static void* writeAll(void *threadarg);
173 
174  //- Wait for total size of objects_ (master + optional slave data)
175  // to be wantedSize less than overall maxBufferSize.
176  void waitForBufferSpace(const off_t wantedSize) const;
177 
178 
179 public:
180 
181  // Declare name of the class and its debug switch
182  TypeName("OFstreamCollator");
183 
184 
185  // Constructors
186 
187  //- Construct from buffer size. 0 = do not use thread
188  OFstreamCollator(const off_t maxBufferSize);
189 
190  //- Construct from buffer size (0 = do not use thread) and local
191  // thread
192  OFstreamCollator(const off_t maxBufferSize, const label comm);
193 
194 
195  //- Destructor
196  virtual ~OFstreamCollator();
197 
198 
199  // Member functions
200 
201  //- Write file with contents. Blocks until writethread has space
202  // available (total file sizes < maxBufferSize)
203  bool write
204  (
205  const word& typeName,
206  const fileName&,
207  const string& data,
211  const bool append,
212  const bool useThread = true
213  );
214 
215  //- Wait for all thread actions to have finished
216  void waitAll();
217 };
218 
219 
220 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
221 
222 } // End namespace Foam
223 
224 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
225 
226 #endif
227 
228 // ************************************************************************* //
FIFOStack.H
SubList.H
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::SubList
A List obtained as a section of another List.
Definition: SubList.H:53
Foam::OFstreamCollator::OFstreamCollator
OFstreamCollator(const off_t maxBufferSize)
Construct from buffer size. 0 = do not use thread.
Definition: OFstreamCollator.C:297
Foam::PtrList::set
const T * set(const label i) const
Return const pointer to element (can be nullptr),.
Definition: PtrList.H:138
append
rAUs append(new volScalarField(IOobject::groupName("rAU", phase1.name()), 1.0/(U1Eqn.A()+byDt(max(phase1.residualAlpha() - alpha1, scalar(0)) *rho1))))
Foam::OFstreamCollator::write
bool write(const word &typeName, const fileName &, const string &data, IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool append, const bool useThread=true)
Write file with contents. Blocks until writethread has space.
Definition: OFstreamCollator.C:357
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::OFstreamCollator::TypeName
TypeName("OFstreamCollator")
format
word format(conversionProperties.get< word >("format"))
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:85
labelList.H
IOstream.H
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:62
Foam::writeData
static void writeData(Ostream &os, const Type &val)
Definition: rawSurfaceWriterImpl.C:39
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:70
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::OFstreamCollator::waitAll
void waitAll()
Wait for all thread actions to have finished.
Definition: OFstreamCollator.C:600
Foam::foamVersion::version
const std::string version
OpenFOAM version (name or stringified number) as a std::string.
Foam::OFstreamCollator
Threaded file writer.
Definition: OFstreamCollator.H:71
Foam::autoPtr< std::thread >
Foam::List< label >
Foam::UList< label >
Foam::IOstreamOption::compressionType
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
Definition: IOstreamOption.H:77
Foam::FIFOStack< writeData * >
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:55
Foam::OFstreamCollator::~OFstreamCollator
virtual ~OFstreamCollator()
Destructor.
Definition: OFstreamCollator.C:335