decomposedBlockData.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  Copyright (C) 2020-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::decomposedBlockData
29 
30 Description
31  The decomposedBlockData comprise a \c List<char> for each output
32  processor, typically with IO on the master processor only.
33 
34  For decomposedBlockData, we make a distinction between the container
35  description and the individual block contents.
36 
37  The \b FoamFile header specifies the container characteristics and thus
38  has \c class = \c %decomposedBlockData and normally \c format = \c binary.
39  This description refers to the \em entire file container, not the
40  individual blocks.
41 
42  Each processor block is simply a binary chunk of characters and the
43  first block also contains the header description for all of the blocks.
44  For example,
45 \verbatim
46 FoamFile
47 {
48  version 2.0;
49  format binary;
50  arch "LSB;label=32;scalar=64";
51  class decomposedBlockData;
52  location "constant/polyMesh";
53  object points;
54 }
55 
56 // processor0
57 NCHARS
58 (FoamFile
59 {
60  version 2.0;
61  format ascii;
62  arch "LSB;label=32;scalar=64";
63  class vectorField;
64  location "constant/polyMesh";
65  object points;
66 }
67 ...content...
68 )
69 
70 // processor1
71 NCHARS
72 (...content...)
73 
74 ...
75 \endverbatim
76 
77 
78 SourceFiles
79  decomposedBlockData.C
80  decomposedBlockDataHeader.C
81 
82 \*---------------------------------------------------------------------------*/
83 
84 #ifndef Foam_decomposedBlockData_H
85 #define Foam_decomposedBlockData_H
86 
87 #include "regIOobject.H"
88 #include "ISstream.H"
89 #include "OSstream.H"
90 #include "UPstream.H"
91 
92 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
93 
94 namespace Foam
95 {
96 
97 // Forward Declarations
98 class dictionary;
99 
100 /*---------------------------------------------------------------------------*\
101  Class decomposedBlockData Declaration
102 \*---------------------------------------------------------------------------*/
103 
105 :
106  public regIOobject
107 {
108  // Private Functions
109 
110  //- Helper: write content for FoamFile IOobject header
111  static void writeHeaderContent
112  (
113  Ostream& os,
114  IOstreamOption streamOptContainer,
115  const word& objectType,
116  const string& note,
117  const fileName& location,
118  const word& objectName
119  );
120 
121 
122 protected:
123 
124  // Protected Data
125 
126  //- Type to use for gather
128 
129  //- Communicator for all parallel comms
130  const label comm_;
132  //- The block content
134 
135 
136  // Protected Member Functions
137 
138  //- Helper: determine number of processors whose recvSizes fits
139  //- into maxBufferSize
140  static label calcNumProcs
141  (
142  const label comm,
143  const off_t maxBufferSize,
144  const labelUList& recvSizes,
145  const label startProci
146  );
147 
148  //- Read data into *this. ISstream is only valid on master.
149  static bool readBlocks
150  (
151  const label comm,
152  autoPtr<ISstream>& isPtr,
153  List<char>& contentChars,
154  const UPstream::commsTypes commsType
155  );
156 
157  //- Helper: skip a block of (binary) character data
158  static bool skipBlockEntry(Istream& is);
159 
160  //- Extract number of blocks from decomposedBlockData file stream
161  static label getNumBlocks(Istream& is, const bool readHeader);
162 
163 
164 public:
165 
166  //- Declare type-name, virtual type (with debug switch)
167  TypeName("decomposedBlockData");
168 
169 
170  // Constructors
171 
172  //- Construct given an IOobject
174  (
175  const label comm,
176  const IOobject& io,
178  );
179 
180 
181  //- Destructor
182  virtual ~decomposedBlockData() = default;
183 
184 
185  // Member Functions
186 
187  //- Read object
188  virtual bool read();
189 
190  //- Write separated content (assumes content is the serialised data)
191  // The serialised master data should also contain a FoamFile header
192  virtual bool writeData(Ostream& os) const;
193 
194  //- Write using stream options
195  virtual bool writeObject
196  (
197  IOstreamOption streamOpt,
198  const bool writeOnProc
199  ) const;
200 
201 
202  // Helpers
203 
204  //- True if object type is a known collated type
205  static bool isCollatedType(const word& objectType);
206 
207  //- True if object header class is a known collated type
208  static bool isCollatedType(const IOobject& io);
209 
210  //- Read header as per IOobject with additional handling of
211  //- decomposedBlockData
212  static bool readHeader(IOobject& io, Istream& is);
213 
214  //- Helper: write FoamFile IOobject header
215  static void writeHeader
216  (
217  Ostream& os,
218  IOstreamOption streamOptContainer,
219  const word& objectType,
220  const string& note,
221  const fileName& location,
222  const word& objectName,
223  const dictionary& extraEntries
224  );
225 
226  //- Helper: write FoamFile IOobject header
227  static void writeHeader
228  (
229  Ostream& os,
230  IOstreamOption streamOptData,
231  const IOobject& io
232  );
233 
234  //- Helper: generate additional entries for FoamFile header
235  static void writeExtraHeaderContent
236  (
237  dictionary& dict,
238  IOstreamOption streamOptData,
239  const IOobject& io
240  );
241 
242  //- Helper: read block of (binary) character data
243  static bool readBlockEntry
244  (
245  Istream& is,
246  List<char>& charData
247  );
248 
249  //- Helper: write block of (binary) character data
250  static std::streamoff writeBlockEntry
251  (
252  OSstream& os,
253  const label blocki,
254  const UList<char>& charData
255  );
256 
257  //- Helper: write block of (binary) character content
258  static std::streamoff writeBlockEntry
259  (
260  OSstream& os,
261  const label blocki,
262  const std::string& content
263  );
264 
265  //- Helper: write block of (binary) character data
266  // \return -1 on error
267  static std::streamoff writeBlockEntry
268  (
269  OSstream& os,
270  IOstreamOption streamOptData,
271  const regIOobject& io,
272  const label blocki,
273  const bool withLocalHeader
274  );
275 
276  //- Read selected block (non-seeking) + header information
278  (
279  const label blocki,
280  ISstream& is,
281  IOobject& headerIO
282  );
283 
284  //- Read master header information (into headerIO) and return
285  //- data in stream. Note: isPtr is only valid on master.
287  (
288  const label comm,
289  const fileName& fName,
290  autoPtr<ISstream>& isPtr,
291  IOobject& headerIO,
292  const UPstream::commsTypes commsType
293  );
294 
295  //- Helper: gather single label. Note: using native Pstream.
296  // datas sized with num procs but undefined contents on
297  // slaves
298  static void gather
299  (
300  const label comm,
301  const label data,
302  labelList& datas
303  );
304 
305  //- Helper: gather data from (subset of) slaves.
306  //
307  // Returns:
308  // - recvData : received data
309  // - recvOffsets : offset in data. recvOffsets is nProcs+1
310  static void gatherSlaveData
311  (
312  const label comm,
313  const UList<char>& data,
314  const labelUList& recvSizes,
315 
316  const labelRange& fromProcs,
317 
318  List<int>& recvOffsets,
319  DynamicList<char>& recvData
320  );
321 
322  //- Write *this. Ostream only valid on master.
323  // Returns offsets of processor blocks in blockOffset
324  static bool writeBlocks
325  (
326  const label comm,
327  autoPtr<OSstream>& osPtr,
328  List<std::streamoff>& blockOffset,
329 
330  const UList<char>& masterData,
331 
332  const labelUList& recvSizes,
333 
334  // Optional slave data (on master)
335  const UPtrList<SubList<char>>& slaveData,
336 
337  const UPstream::commsTypes commsType,
338  const bool syncReturnState = true
339  );
340 };
341 
342 
343 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
344 
345 } // End namespace Foam
346 
347 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
348 
349 #endif
350 
351 // ************************************************************************* //
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:50
dictionary dict
virtual bool read()
Read object.
A class for handling file names.
Definition: fileName.H:71
static void gatherSlaveData(const label comm, const UList< char > &data, const labelUList &recvSizes, const labelRange &fromProcs, List< int > &recvOffsets, DynamicList< char > &recvData)
Helper: gather data from (subset of) slaves.
const label comm_
Communicator for all parallel comms.
static bool writeBlocks(const label comm, autoPtr< OSstream > &osPtr, List< std::streamoff > &blockOffset, const UList< char > &masterData, const labelUList &recvSizes, const UPtrList< SubList< char >> &slaveData, const UPstream::commsTypes commsType, const bool syncReturnState=true)
Write *this. Ostream only valid on master.
const UPstream::commsTypes commsType_
Type to use for gather.
commsTypes
Communications types.
Definition: UPstream.H:74
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:120
static autoPtr< ISstream > readBlock(const label blocki, ISstream &is, IOobject &headerIO)
Read selected block (non-seeking) + header information.
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:51
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
const string & note() const noexcept
Return the optional note.
Definition: IOobjectI.H:192
static bool readBlocks(const label comm, autoPtr< ISstream > &isPtr, List< char > &contentChars, const UPstream::commsTypes commsType)
Read data into *this. ISstream is only valid on master.
A simple container for options an IOstream can normally have.
virtual bool writeObject(IOstreamOption streamOpt, const bool writeOnProc) const
Write using stream options.
List< char > contentData_
The block content.
static label getNumBlocks(Istream &is, const bool readHeader)
Extract number of blocks from decomposedBlockData file stream.
A List obtained as a section of another List.
Definition: SubList.H:50
static void writeExtraHeaderContent(dictionary &dict, IOstreamOption streamOptData, const IOobject &io)
Helper: generate additional entries for FoamFile header.
"scheduled" : (MPI_Send, MPI_Recv)
A class for handling words, derived from Foam::string.
Definition: word.H:63
static bool readBlockEntry(Istream &is, List< char > &charData)
Helper: read block of (binary) character data.
The decomposedBlockData comprise a List<char> for each output processor, typically with IO on the mas...
static std::streamoff writeBlockEntry(OSstream &os, const label blocki, const UList< char > &charData)
Helper: write block of (binary) character data.
static bool isCollatedType(const word &objectType)
True if object type is a known collated type.
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: HashTable.H:100
static bool readHeader(IOobject &io, Istream &is)
Read header as per IOobject with additional handling of decomposedBlockData.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
OBJstream os(runTime.globalPath()/outputName)
Database for solution data, solver performance and other reduced data.
Definition: data.H:52
virtual ~decomposedBlockData()=default
Destructor.
decomposedBlockData(const label comm, const IOobject &io, const UPstream::commsTypes=UPstream::commsTypes::scheduled)
Construct given an IOobject.
static label calcNumProcs(const label comm, const off_t maxBufferSize, const labelUList &recvSizes, const label startProci)
Helper: determine number of processors whose recvSizes fits into maxBufferSize.
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:51
TypeName("decomposedBlockData")
Declare type-name, virtual type (with debug switch)
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:65
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:171
virtual bool writeData(Ostream &os) const
Write separated content (assumes content is the serialised data)
static bool skipBlockEntry(Istream &is)
Helper: skip a block of (binary) character data.
static void writeHeader(Ostream &os, IOstreamOption streamOptContainer, const word &objectType, const string &note, const fileName &location, const word &objectName, const dictionary &extraEntries)
Helper: write FoamFile IOobject header.
Namespace for OpenFOAM.
static void gather(const label comm, const label data, labelList &datas)
Helper: gather single label. Note: using native Pstream.