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 public:
161 
162  //- Declare type-name, virtual type (with debug switch)
163  TypeName("decomposedBlockData");
164 
165 
166  // Constructors
167 
168  //- Construct given an IOobject
170  (
171  const label comm,
172  const IOobject& io,
174  );
175 
176 
177  //- Destructor
178  virtual ~decomposedBlockData() = default;
179 
180 
181  // Member Functions
182 
183  //- Read object
184  virtual bool read();
185 
186  //- Write separated content (assumes content is the serialised data)
187  // The serialised master data should also contain a FoamFile header
188  virtual bool writeData(Ostream& os) const;
189 
190  //- Write using stream options
191  virtual bool writeObject
192  (
193  IOstreamOption streamOpt,
194  const bool writeOnProc
195  ) const;
196 
197 
198  // Helpers
199 
200  //- True if object type is a known collated type
201  static bool isCollatedType(const word& objectType);
202 
203  //- True if object header class is a known collated type
204  static bool isCollatedType(const IOobject& io);
205 
206  //- Extract number of decomposedBlockData block entries, optionally
207  //- with an upper limit.
208  //- The input stream should be in a rewound state
209  //- (or only have read the header) before calling.
210  static label getNumBlocks(Istream& is, const label maxNumBlocks = -1);
211 
212  //- True if the given block number (starts at 0) has a corresponding
213  //- decomposedBlockData block entry.
214  //- The input stream should be in a rewound state
215  //- (or only have read the header) before calling.
216  // This will be faster than checking against getNumBlocks()
217  // since it can potentially exit without scanning the entire file.
218  static bool hasBlock(Istream& is, const label blockNumber);
219 
220  //- Read header as per IOobject with additional handling of
221  //- decomposedBlockData
222  static bool readHeader(IOobject& io, Istream& is);
223 
224  //- Helper: write FoamFile IOobject header
225  static void writeHeader
226  (
227  Ostream& os,
228  IOstreamOption streamOptContainer,
229  const word& objectType,
230  const string& note,
231  const fileName& location,
232  const word& objectName,
233  const dictionary& extraEntries
234  );
235 
236  //- Helper: write FoamFile IOobject header
237  static void writeHeader
238  (
239  Ostream& os,
240  IOstreamOption streamOptData,
241  const IOobject& io
242  );
243 
244  //- Helper: generate additional entries for FoamFile header
245  static void writeExtraHeaderContent
246  (
247  dictionary& dict,
248  IOstreamOption streamOptData,
249  const IOobject& io
250  );
251 
252  //- Helper: read block of (binary) character data
253  static bool readBlockEntry
254  (
255  Istream& is,
256  List<char>& charData
257  );
258 
259  //- Helper: write block of (binary) character data
260  static std::streamoff writeBlockEntry
261  (
262  OSstream& os,
263  const label blocki,
264  const char* str,
265  const size_t len
266  );
267 
268  //- Helper: write block of (binary) character data
269  static std::streamoff writeBlockEntry
270  (
271  OSstream& os,
272  const label blocki,
273  const UList<char>& s
274  )
275  {
276  return writeBlockEntry(os, blocki, s.cdata(), s.size_bytes());
277  }
278 
279  //- Helper: write block of (binary) character content
280  static std::streamoff writeBlockEntry
281  (
282  OSstream& os,
283  const label blocki,
284  const std::string& s
285  )
286  {
287  return writeBlockEntry(os, blocki, s.data(), s.size());
288  }
289 
290  //- Helper: write block of (binary) character data
291  // \return -1 on error
292  static std::streamoff writeBlockEntry
293  (
294  OSstream& os,
295  IOstreamOption streamOptData,
296  const regIOobject& io,
297  const label blocki,
298  const bool withLocalHeader
299  );
300 
301  //- Read selected block (non-seeking) + header information
302  static autoPtr<ISstream> readBlock
303  (
304  const label blocki,
305  ISstream& is,
306  IOobject& headerIO
307  );
308 
309  //- Read master header information (into headerIO) and return
310  //- data in stream. Note: isPtr is only valid on master.
311  static autoPtr<ISstream> readBlocks
312  (
313  const label comm,
314  const fileName& fName,
316  IOobject& headerIO,
317  const UPstream::commsTypes commsType
318  );
319 
320  //- Helper: gather single label. Note: using native Pstream.
321  // datas sized with num procs but undefined contents on
322  // slaves
323  static void gather
324  (
325  const label comm,
326  const label data,
327  labelList& datas
328  );
329 
330  //- Helper: gather data from (subset of) slaves.
331  //
332  // Returns:
333  // - recvData : received data
334  // - recvOffsets : offset in data. recvOffsets is nProcs+1
335  static void gatherSlaveData
336  (
337  const label comm,
338  const UList<char>& data,
339  const labelUList& recvSizes,
340 
341  const labelRange& fromProcs,
342 
343  List<int>& recvOffsets,
344  DynamicList<char>& recvData
345  );
346 
347  //- Write *this. Ostream only valid on master.
348  // Returns offsets of processor blocks in blockOffset
349  static bool writeBlocks
350  (
351  const label comm,
352  autoPtr<OSstream>& osPtr,
353  List<std::streamoff>& blockOffset,
354 
355  const UList<char>& masterData,
356 
357  const labelUList& recvSizes,
358 
359  // Optional slave data (on master)
360  const UPtrList<SubList<char>>& slaveData,
361 
362  const UPstream::commsTypes commsType,
363  const bool syncReturnState = true
364  );
365 };
366 
367 
368 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
369 
370 } // End namespace Foam
371 
372 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
373 
374 #endif
375 
376 // ************************************************************************* //
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:72
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:72
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
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:52
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
regIOobject(const IOobject &io, const bool isTimeObject=false)
Construct from IOobject. The optional flag adds special handling if the object is the top-level regIO...
Definition: regIOobject.C:43
const string & note() const noexcept
Return the optional note.
Definition: IOobjectI.H:225
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 std::streamoff writeBlockEntry(OSstream &os, const label blocki, const char *str, const size_t len)
Helper: write block of (binary) character data.
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)
static bool hasBlock(Istream &is, const label blockNumber)
True if the given block number (starts at 0) has a corresponding decomposedBlockData block entry...
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 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:106
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:56
OBJstream os(runTime.globalPath()/outputName)
virtual ~decomposedBlockData()=default
Destructor.
static label getNumBlocks(Istream &is, const label maxNumBlocks=-1)
Extract number of decomposedBlockData block entries, optionally with an upper limit. The input stream should be in a rewound state (or only have read the header) before calling.
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.
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:66
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)
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))
Defines the attributes of an object for which implicit objectRegistry management is supported...
Definition: IOobject.H:172
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.