collatedFileOperation.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 OpenFOAM Foundation
9  Copyright (C) 2019-2023 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 Class
28  Foam::fileOperations::collatedFileOperation
29 
30 Description
31  Version of masterUncollatedFileOperation that collates regIOobjects
32  into a container in the processors/ subdirectory.
33 
34  Uses threading if maxThreadFileBufferSize != 0.
35  > 0 : Can use mpi inside thread to collect data if buffer is not
36  large enough. Does need full thread support inside MPI.
37 
38  < 0 : special : -maxThreadFileBufferSize is guaranteed large enough
39  for all writing. Initialises MPI without thread support.
40 
41 See also
42  masterUncollatedFileOperation
43 
44 SourceFiles
45  collatedFileOperation.C
46 
47 \*---------------------------------------------------------------------------*/
48 
49 #ifndef Foam_fileOperations_collatedFileOperation_H
50 #define Foam_fileOperations_collatedFileOperation_H
51 
54 #include "OFstreamCollator.H"
55 
56 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
57 
58 namespace Foam
59 {
60 namespace fileOperations
61 {
62 
63 /*---------------------------------------------------------------------------*\
64  Class collatedFileOperation Declaration
65 \*---------------------------------------------------------------------------*/
66 
68 :
70 {
71  // Private Data
72 
73  //- Communicator allocated/managed by us
74  mutable label managedComm_;
75 
76 
77  // Private Member Functions
78 
79  //- Any initialisation steps after constructing
80  void init(bool verbose);
81 
82 
83 protected:
84 
85  // Protected Data
86 
87  //- Threaded writer
88  mutable OFstreamCollator writer_;
89 
90 
91  // Protected Member Functions
92 
93  //- Print banner information, optionally with io ranks
94  void printBanner(const bool withRanks = false) const;
95 
96  //- Append to processorsNN/ file
97  bool appendObject
98  (
99  const regIOobject& io,
100  const fileName& pathName,
101  IOstreamOption streamOpt
102  ) const;
103 
104 
105 public:
106 
107  //- Runtime type information
108  TypeName("collated");
109 
110 
111  // Static Data
112 
113  //- Max size of thread buffer size. This is the overall size of
114  // all files to be written. Starts blocking if not enough size.
115  // Read as float to enable easy specification of large sizes.
116  static float maxThreadFileBufferSize;
117 
118 
119  // Constructors
120 
121  //- Default construct
122  explicit collatedFileOperation(bool verbose = false);
123 
124  //- Construct from communicator with specified io-ranks
125  explicit collatedFileOperation
126  (
127  const Tuple2<label, labelList>& commAndIORanks,
128  const bool distributedRoots,
129  bool verbose = false
130  );
131 
132 
133  //- Destructor
134  virtual ~collatedFileOperation();
135 
136 
137  // Member Functions
138 
139  //- Transfer ownership of communicator to this fileOperation.
140  //- Use with caution
141  virtual void storeComm() const;
142 
143 
144  // (reg)IOobject functionality
145 
146  //- Generate disk file name for object. Opposite of filePath.
147  virtual fileName objectPath
148  (
149  const IOobject& io,
150  const word& typeName
151  ) const;
152 
153  //- Writes a regIOobject (so header, contents and divider).
154  // Returns success state.
155  virtual bool writeObject
156  (
157  const regIOobject&,
158  IOstreamOption streamOpt = IOstreamOption(),
159  const bool writeOnProc = true
160  ) const;
161 
162 
163  // Other
164 
165  //- Forcibly wait until all output done. Flush any cached data
166  virtual void flush() const;
167 
168  //- Actual name of processors dir
169  virtual word processorsDir(const IOobject&) const;
170 
171  //- Actual name of processors dir
172  virtual word processorsDir(const fileName&) const;
173 };
174 
175 
176 /*---------------------------------------------------------------------------*\
177  Class fileOperationInitialise_collated Declaration
178 \*---------------------------------------------------------------------------*/
179 
180 //- A fileOperation initialiser for collated file handlers.
181 //- Requires threading for non-zero maxThreadFileBufferSize.
183 :
185 {
186 public:
187 
188  // Constructors
189 
190  //- Construct from components
191  fileOperationInitialise_collated(int& argc, char**& argv)
192  :
193  fileOperationInitialise(argc, argv)
194  {}
195 
196 
197  //- Destructor
198  virtual ~fileOperationInitialise_collated() = default;
199 
200 
201  // Member Functions
202 
203  //- The (MPI) threading requirement depends on buffering
204  virtual bool needsThreading() const
205  {
207  }
208 };
209 
210 
211 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
212 
213 } // End namespace fileOperations
214 } // End namespace Foam
215 
216 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
217 
218 #endif
219 
220 // ************************************************************************* //
Threaded file writer.
A class for handling file names.
Definition: fileName.H:72
virtual bool needsThreading() const
The (MPI) threading requirement depends on buffering.
A fileOperation initialiser for collated file handlers. Requires threading for non-zero maxThreadFile...
A 2-tuple for storing two objects of dissimilar types. The container is similar in purpose to std::pa...
Definition: stringOps.H:54
TypeName("collated")
Runtime type information.
OFstreamCollator writer_
Threaded writer.
A simple container for options an IOstream can normally have.
bool appendObject(const regIOobject &io, const fileName &pathName, IOstreamOption streamOpt) const
Append to processorsNN/ file.
collatedFileOperation(bool verbose=false)
Default construct.
virtual fileName objectPath(const IOobject &io, const word &typeName) const
Generate disk file name for object. Opposite of filePath.
A class for handling words, derived from Foam::string.
Definition: word.H:63
void printBanner(const bool withRanks=false) const
Print banner information, optionally with io ranks.
virtual void storeComm() const
Transfer ownership of communicator to this fileOperation. Use with caution.
virtual void flush() const
Forcibly wait until all output done. Flush any cached data.
virtual bool writeObject(const regIOobject &, IOstreamOption streamOpt=IOstreamOption(), const bool writeOnProc=true) const
Writes a regIOobject (so header, contents and divider).
static float maxThreadFileBufferSize
Max size of thread buffer size. This is the overall size of.
fileOperationInitialise_collated(int &argc, char **&argv)
Construct from components.
Version of masterUncollatedFileOperation that collates regIOobjects into a container in the processor...
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:66
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
virtual ~fileOperationInitialise_collated()=default
Destructor.
fileOperations that performs all file operations on the master processor. Requires the calls to be pa...
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:172
virtual word processorsDir(const IOobject &) const
Actual name of processors dir.
Namespace for OpenFOAM.