Loading...
Searching...
No Matches
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-2025 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::decomposedBlockData
29
30Description
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
46FoamFile
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 data.format ascii; // optional
55 data.class vectorField; // optional
56}
57
58// processor0
59NCHARS
60(FoamFile
61{
62 version 2.0;
63 format ascii;
64 arch "LSB;label=32;scalar=64";
65 class vectorField;
66 location "constant/polyMesh";
67 object points;
68}
69...content...
70)
71
72// processor1
73NCHARS
74(...content...)
75
76...
77\endverbatim
78
79
80SourceFiles
81 decomposedBlockData.C
82 decomposedBlockDataHeader.C
83
84\*---------------------------------------------------------------------------*/
85
86#ifndef Foam_decomposedBlockData_H
87#define Foam_decomposedBlockData_H
88
89#include "regIOobject.H"
90#include "ISstream.H"
91#include "OSstream.H"
92#include "UPstream.H"
93
94// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
95
96namespace Foam
97{
98
99// Forward Declarations
100class dictionary;
102/*---------------------------------------------------------------------------*\
103 Class decomposedBlockData Declaration
104\*---------------------------------------------------------------------------*/
105
107:
108 public regIOobject
109{
110 // Private Functions
111
112 //- Helper: write content for FoamFile IOobject header
113 static void writeHeaderContent
114 (
115 Ostream& os,
116 IOstreamOption streamOptContainer,
117 const word& objectType,
118 const string& note,
119 const fileName& location,
120 const word& objectName
121 );
122
123
124protected:
125
126 // Protected Data
127
128 //- Type to use for gather
130
131 //- Communicator for all parallel comms
132 const label comm_;
134 //- The block content
136
137
138 // Protected Member Functions
139
140 //- Read data (on master) and transmit.
141 static bool readBlocks
142 (
143 const label comm,
144 // [in] The input stream (only valid on master)
145 autoPtr<ISstream>& isPtr,
146 // [out] The processor local data
147 List<char>& localData,
148 const UPstream::commsTypes commsType /* unused */
149 );
150
151 //- Helper: skip a block of (binary) character data
152 static bool skipBlockEntry(Istream& is);
153
154public:
155
156 //- Declare type-name, virtual type (with debug switch)
157 TypeName("decomposedBlockData");
158
159
160 // Constructors
161
162 //- Construct given an IOobject
164 (
165 const label comm,
166 const IOobject& io,
168 );
169
170
171 //- Destructor
172 virtual ~decomposedBlockData() = default;
173
174
175 // Member Functions
176
177 //- Read object
178 virtual bool read();
179
180 //- Write separated content (assumes content is the serialised data)
181 // The serialised master data should also contain a FoamFile header
182 virtual bool writeData(Ostream& os) const;
183
184 //- Write using stream options
185 virtual bool writeObject
186 (
187 IOstreamOption streamOpt,
188 const bool writeOnProc
189 ) const;
190
191
192 // Helpers
193
194 //- True if object type is a known collated type
195 static bool isCollatedType(const word& objectType);
196
197 //- True if object header class is a known collated type
198 static bool isCollatedType(const IOobject& io);
199
200 //- Extract number of decomposedBlockData block entries, optionally
201 //- with an upper limit.
202 //- The input stream should be in a rewound state
203 //- (or only have read the header) before calling.
204 static label getNumBlocks(Istream& is, const label maxNumBlocks = -1);
205
206 //- True if the given block number (starts at 0) has a corresponding
207 //- decomposedBlockData block entry.
208 //- The input stream should be in a rewound state
209 //- (or only have read the header) before calling.
210 // This will be faster than checking against getNumBlocks()
211 // since it can potentially exit without scanning the entire file.
212 static bool hasBlock(Istream& is, const label blockNumber);
213
214 //- Read header as per IOobject with additional handling of
215 //- decomposedBlockData
216 static bool readHeader(IOobject& io, Istream& is);
217
218 //- Helper: write FoamFile IOobject header
219 static void writeHeader
220 (
221 Ostream& os,
222 IOstreamOption streamOptContainer,
223 const word& objectType,
224 const string& note,
225 const fileName& location,
226 const word& objectName,
227 const dictionary& extraEntries
228 );
229
230 //- Helper: write FoamFile IOobject header
231 static void writeHeader
232 (
233 Ostream& os,
234 IOstreamOption streamOptData,
235 const IOobject& io
236 );
237
238 //- Helper: generate additional entries for FoamFile header
239 static void writeExtraHeaderContent
240 (
242 IOstreamOption streamOptData,
243 const IOobject& io
244 );
245
246 //- Helper: read block of (binary) character data
247 static bool readBlockEntry
248 (
249 Istream& is,
250 List<char>& charData
251 );
252
253 //- Helper: write block of (binary) character data
254 static std::streamoff writeBlockEntry
255 (
256 OSstream& os,
257 const label blocki,
258 const char* str,
259 const size_t len
260 );
261
262 //- Helper: write block of (binary) character data
263 static std::streamoff writeBlockEntry
264 (
265 OSstream& os,
266 const label blocki,
267 const UList<char>& s
268 )
269 {
270 return writeBlockEntry(os, blocki, s.cdata(), s.size_bytes());
271 }
272
273 //- Helper: write block of (binary) character content
274 // Housekeeping
275 static std::streamoff writeBlockEntry
276 (
277 OSstream& os,
278 const label blocki,
279 std::string_view sv
280 )
281 {
282 return writeBlockEntry(os, blocki, sv.data(), sv.size());
283 }
284
285 //- Helper: write block of (binary) character data
286 // \return -1 on error
287 static std::streamoff writeBlockEntry
288 (
289 OSstream& os,
290 IOstreamOption streamOptData,
291 const regIOobject& io,
292 const label blocki,
293 const bool withLocalHeader
294 );
295
296 //- Read selected block (non-seeking) + header information
297 static autoPtr<ISstream> readBlock
298 (
299 const label blocki,
300 ISstream& is,
301 IOobject& headerIO
302 );
303
304 //- Read master header information (into headerIO) and return
305 //- data in stream.
307 (
308 const label comm,
309 const fileName& fName,
311 autoPtr<ISstream>& isPtr,
313 IOobject& headerIO,
314 const UPstream::commsTypes commsType /* unused */
315 );
316
317 //- Helper: gather data from (subset of) sub-ranks.
318 // In non-blocking mode it sets up send/recv for non-empty content.
319 // In blocking/scheduled mode it uses MPI_Gatherv to collect data.
320 //
321 // Returns:
322 // - recvData : the received data
323 // - recvOffsets : offset in data. recvOffsets is nProcs+1
324 static void gatherProcData
325 (
326 const label comm,
327 const UList<char>& localData,
328 const labelUList& recvSizes,
329
330 const labelRange& whichProcs,
331
332 List<int>& recvOffsets,
333 DynamicList<char>& recvData,
334
335 const UPstream::commsTypes commsType
336 );
337
338 //- Write *this. Ostream only valid on master.
339 // Returns offsets of processor blocks in blockOffset
340 static bool writeBlocks
341 (
342 const label comm,
343
345 autoPtr<OSstream>& osPtr,
348 List<std::streamoff>& blockOffset,
349
350 const UList<char>& localData,
351 const labelUList& recvSizes,
352
356 const UList<std::string_view>& procData,
357
358 const UPstream::commsTypes commsType,
359 const bool syncReturnState = true
360 );
361
362
363 // Housekeeping
364
365 //- Write *this. Ostream only valid on master.
366 // Returns offsets of processor blocks in blockOffset
367 FOAM_DEPRECATED_FOR(2023-09, "write with std::string_view instead")
368 static bool writeBlocks
369 (
370 const label comm,
371 autoPtr<OSstream>& osPtr,
372 List<std::streamoff>& blockOffset,
373
374 const UList<char>& localData, // [in] required on all procs
375 const labelUList& recvSizes, // [in] only required on master
376
377 // Optional proc data (only written on master)
378 // but \b must also be symmetrically defined (empty/non-empty)
379 // on all ranks
380 const UPtrList<SubList<char>>& procData,
381
382 const UPstream::commsTypes commsType,
383 const bool syncReturnState = true
384 )
385 {
386 // Transcribe to string_view
387 List<std::string_view> spans(procData.size());
388 forAll(procData, proci)
389 {
390 const auto* ptr = procData.get(proci);
391 if (ptr && !ptr->empty())
392 {
393 spans[proci] = std::string_view(ptr->cdata(), ptr->size());
394 }
395 }
396
398 (
399 comm,
400 osPtr,
401 blockOffset,
402 localData,
403 recvSizes,
404 spans,
405 commsType,
406 syncReturnState
407 );
408 return ok;
409 }
410
411 //- Deprecated(2023-09) - consider UPstream::listGatherValue
412 // The only difference is that this gather also resizes the output
413 // on the non-master procs
414 // \deprecated(2023-09) - consider UPstream::listGatherValue
415 FOAM_DEPRECATED_FOR(2023-09, "consider UPstream::listGatherValue()")
416 static void gather
417 (
418 const label comm,
419 const label localValue,
420 labelList& allValues
421 )
422 {
423 allValues.resize_nocopy(UPstream::nProcs(comm));
424
426 (
427 reinterpret_cast<const char*>(&localValue),
428 allValues.data_bytes(),
429 sizeof(label), // The send/recv size per rank
430 comm
431 );
432 }
433};
434
435
436// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
437
438} // End namespace Foam
439
440// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
441
442#endif
443
444// ************************************************************************* //
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition DynamicList.H:68
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition IOobject.H:191
const string & note() const noexcept
Return the optional note.
Definition IOobjectI.H:235
A simple container for options an IOstream can normally have.
Generic input stream using a standard (STL) stream.
Definition ISstream.H:54
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
Generic output stream using a standard (STL) stream.
Definition OSstream.H:53
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
A non-owning sub-view of a List (allocated or unallocated storage).
Definition SubList.H:61
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition UList.H:89
Inter-processor communications stream.
Definition UPstream.H:69
commsTypes
Communications types.
Definition UPstream.H:81
@ scheduled
"scheduled" (MPI standard) : (MPI_Send, MPI_Recv)
Definition UPstream.H:83
static void mpiGather(const Type *sendData, Type *recvData, int count, const int communicator=UPstream::worldComm)
Receive identically-sized (contiguous) data from all ranks.
static label nProcs(const label communicator=worldComm)
Number of ranks in parallel run (for given communicator). It is 1 for serial run.
Definition UPstream.H:1697
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition UPtrList.H:101
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
virtual bool writeObject(IOstreamOption streamOpt, const bool writeOnProc) const
Write using stream options.
static std::streamoff writeBlockEntry(OSstream &os, const label blocki, std::string_view sv)
Helper: write block of (binary) character content.
static bool readBlockEntry(Istream &is, List< char > &charData)
Helper: read block of (binary) character data.
static bool hasBlock(Istream &is, const label blockNumber)
True if the given block number (starts at 0) has a corresponding decomposedBlockData block entry....
static void gatherProcData(const label comm, const UList< char > &localData, const labelUList &recvSizes, const labelRange &whichProcs, List< int > &recvOffsets, DynamicList< char > &recvData, const UPstream::commsTypes commsType)
Helper: gather data from (subset of) sub-ranks.
virtual bool writeData(Ostream &os) const
Write separated content (assumes content is the serialised data).
TypeName("decomposedBlockData")
Declare type-name, virtual type (with debug switch).
static autoPtr< ISstream > readBlock(const label blocki, ISstream &is, IOobject &headerIO)
Read selected block (non-seeking) + header information.
const label comm_
Communicator for all parallel comms.
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.
static bool writeBlocks(const label comm, autoPtr< OSstream > &osPtr, List< std::streamoff > &blockOffset, const UList< char > &localData, const labelUList &recvSizes, const UList< std::string_view > &procData, const UPstream::commsTypes commsType, const bool syncReturnState=true)
Write *this. Ostream only valid on master.
static std::streamoff writeBlockEntry(OSstream &os, const label blocki, const char *str, const size_t len)
Helper: write block of (binary) character data.
static void writeExtraHeaderContent(dictionary &dict, IOstreamOption streamOptData, const IOobject &io)
Helper: generate additional entries for FoamFile header.
virtual ~decomposedBlockData()=default
Destructor.
static bool isCollatedType(const word &objectType)
True if object type is a known collated type.
decomposedBlockData(const label comm, const IOobject &io, const UPstream::commsTypes=UPstream::commsTypes::scheduled)
Construct given an IOobject.
List< char > contentData_
The block content.
static bool readBlocks(const label comm, autoPtr< ISstream > &isPtr, List< char > &localData, const UPstream::commsTypes commsType)
Read data (on master) and transmit.
static bool skipBlockEntry(Istream &is)
Helper: skip a block of (binary) character data.
static void gather(const label comm, const label localValue, labelList &allValues)
Deprecated(2023-09) - consider UPstream::listGatherValue.
static bool readHeader(IOobject &io, Istream &is)
Read header as per IOobject with additional handling of decomposedBlockData.
static std::streamoff writeBlockEntry(OSstream &os, const label blocki, const UList< char > &s)
Helper: write block of (binary) character data.
static label getNumBlocks(Istream &is, const label maxNumBlocks=-1)
Extract number of decomposedBlockData block entries, optionally with an upper limit....
virtual bool read()
Read object.
const UPstream::commsTypes commsType_
Type to use for gather.
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
A class for handling file names.
Definition fileName.H:75
A range or interval of labels defined by a start and a size.
Definition labelRange.H:66
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition regIOobject.H:71
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
A class for handling words, derived from Foam::string.
Definition word.H:66
OBJstream os(runTime.globalPath()/outputName)
const auto & io
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))
Namespace for OpenFOAM.
List< label > labelList
A List of labels.
Definition List.H:62
UList< label > labelUList
A UList of labels.
Definition UList.H:75
dictionary dict
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299
#define FOAM_DEPRECATED_FOR(since, replacement)
Definition stdFoam.H:43
#define TypeName(TypeNameString)
Declare a ClassName() with extra virtual type info.
Definition typeInfo.H:68