decomposedBlockData.C
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 \*---------------------------------------------------------------------------*/
27 
28 #include "decomposedBlockData.H"
29 #include "OPstream.H"
30 #include "IPstream.H"
31 #include "PstreamBuffers.H"
32 #include "Fstream.H"
33 #include "StringStream.H"
34 #include "dictionary.H"
35 #include "objectRegistry.H"
36 #include "SubList.H"
37 #include "labelPair.H"
39 #include "IListStream.H"
40 
41 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
42 
43 namespace Foam
44 {
45  defineTypeNameAndDebug(decomposedBlockData, 0);
46 }
47 
48 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
49 
51 (
52  const label comm,
53  const IOobject& io,
54  const UPstream::commsTypes commsType
55 )
56 :
57  regIOobject(io),
58  commsType_(commsType),
59  comm_(comm)
60 {
61  // Temporary warning
62  if (io.readOpt() == IOobject::MUST_READ_IF_MODIFIED)
63  {
65  << "decomposedBlockData " << name()
66  << " constructed with IOobject::MUST_READ_IF_MODIFIED"
67  " but decomposedBlockData does not support automatic rereading."
68  << endl;
69  }
70  if
71  (
72  (
73  io.readOpt() == IOobject::MUST_READ
74  || io.readOpt() == IOobject::MUST_READ_IF_MODIFIED
75  )
76  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
77  )
78  {
79  read();
80  }
81 }
82 
83 
85 (
86  const label comm,
87  const IOobject& io,
88  const UList<char>& list,
89  const UPstream::commsTypes commsType
90 )
91 :
92  regIOobject(io),
93  commsType_(commsType),
94  comm_(comm)
95 {
96  // Temporary warning
97  if (io.readOpt() == IOobject::MUST_READ_IF_MODIFIED)
98  {
100  << "decomposedBlockData " << name()
101  << " constructed with IOobject::MUST_READ_IF_MODIFIED"
102  " but decomposedBlockData does not support automatic rereading."
103  << endl;
104  }
105 
106  if
107  (
108  (
109  io.readOpt() == IOobject::MUST_READ
110  || io.readOpt() == IOobject::MUST_READ_IF_MODIFIED
111  )
112  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
113  )
114  {
115  read();
116  }
117  else
118  {
119  List<char>::operator=(list);
120  }
121 }
122 
123 
125 (
126  const label comm,
127  const IOobject& io,
128  List<char>&& list,
129  const UPstream::commsTypes commsType
130 )
131 :
132  regIOobject(io),
133  commsType_(commsType),
134  comm_(comm)
135 {
136  // Temporary warning
137  if (io.readOpt() == IOobject::MUST_READ_IF_MODIFIED)
138  {
140  << "decomposedBlockData " << name()
141  << " constructed with IOobject::MUST_READ_IF_MODIFIED"
142  " but decomposedBlockData does not support automatic rereading."
143  << endl;
144  }
145 
146  List<char>::transfer(list);
147 
148  if
149  (
150  (
151  io.readOpt() == IOobject::MUST_READ
152  || io.readOpt() == IOobject::MUST_READ_IF_MODIFIED
153  )
154  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
155  )
156  {
157  read();
158  }
159 }
160 
161 
162 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
163 
165 {}
166 
167 
168 // * * * * * * * * * * * * * * * Members Functions * * * * * * * * * * * * * //
169 
171 {
172  if (debug)
173  {
174  Pout<< "decomposedBlockData::readMasterHeader:"
175  << " stream:" << is.name() << endl;
176  }
177 
178  // Master-only reading of header
179  is.fatalCheck("read(Istream&)");
180 
181  List<char> data(is);
182  is.fatalCheck("read(Istream&) : reading entry");
183  IListStream str
184  (
185  std::move(data),
188  is.name()
189  );
190 
191  return io.readHeader(str);
192 }
193 
194 
196 (
197  Ostream& os,
200  const word& type,
201  const string& note,
202  const fileName& location,
203  const word& name
204 )
205 {
207  << "FoamFile\n{\n"
208  << " version " << version << ";\n"
209  << " format " << format << ";\n"
210  << " class " << type << ";\n";
211 
212  // This may be useful to have as well
213  /*
214  if (os.format() == IOstream::BINARY)
215  {
216  os << " arch " << foamVersion::buildArch << ";\n";
217  }
218  */
219 
220  if (Pstream::parRun())
221  {
222  os << " blocks " << Pstream::nProcs() << ";\n";
223  }
224 
225  if (note.size())
226  {
227  os << " note " << note << ";\n";
228  }
229 
230  if (location.size())
231  {
232  os << " location " << location << ";\n";
233  }
234 
235  os << " object " << name << ";\n"
236  << "}" << nl;
237 
238  IOobject::writeDivider(os) << nl;
239 }
240 
241 
243 (
244  const label blocki,
245  Istream& is,
246  IOobject& headerIO
247 )
248 {
249  if (debug)
250  {
251  Pout<< "decomposedBlockData::readBlock:"
252  << " stream:" << is.name() << " attempt to read block " << blocki
253  << endl;
254  }
255 
256  is.fatalCheck("read(Istream&)");
257 
259  autoPtr<ISstream> realIsPtr;
260 
261  if (blocki == 0)
262  {
263  is >> data;
264  is.fatalCheck("read(Istream&) : reading entry");
265 
266  realIsPtr.reset
267  (
268  new IListStream
269  (
270  std::move(data),
273  is.name()
274  )
275  );
276 
277  // Read header
278  if (!headerIO.readHeader(realIsPtr()))
279  {
280  FatalIOErrorInFunction(realIsPtr())
281  << "problem while reading header for object "
282  << is.name() << exit(FatalIOError);
283  }
284  }
285  else
286  {
287  // Read master for header
288  is >> data;
289  is.fatalCheck("read(Istream&) : reading entry");
290 
293  {
294  UIListStream headerStream
295  (
296  data,
299  is.name()
300  );
301 
302  // Read header
303  if (!headerIO.readHeader(headerStream))
304  {
305  FatalIOErrorInFunction(headerStream)
306  << "problem while reading header for object "
307  << is.name() << exit(FatalIOError);
308  }
309  ver = headerStream.version();
310  fmt = headerStream.format();
311  }
312 
313  for (label i = 1; i < blocki+1; i++)
314  {
315  // Read data, override old data
316  is >> data;
317  is.fatalCheck("read(Istream&) : reading entry");
318  }
319  realIsPtr.reset
320  (
321  new IListStream
322  (
323  std::move(data),
326  is.name()
327  )
328  );
329 
330  // Apply master stream settings to realIsPtr
331  realIsPtr().format(fmt);
332  realIsPtr().version(ver);
333  }
334  return realIsPtr;
335 }
336 
337 
339 (
340  const label comm,
341  autoPtr<ISstream>& isPtr,
342  List<char>& data,
343  const UPstream::commsTypes commsType
344 )
345 {
346  if (debug)
347  {
348  Pout<< "decomposedBlockData::readBlocks:"
349  << " stream:" << (isPtr.valid() ? isPtr().name() : "invalid")
350  << " commsType:" << Pstream::commsTypeNames[commsType]
351  << " comm:" << comm << endl;
352  }
353 
354  bool ok = false;
355 
356  if (commsType == UPstream::commsTypes::scheduled)
357  {
358  if (UPstream::master(comm))
359  {
360  Istream& is = isPtr();
361  is.fatalCheck("read(Istream&)");
362 
363  // Read master data
364  {
365  is >> data;
366  is.fatalCheck("read(Istream&) : reading entry");
367  }
368 
369  // Read slave data
370  for
371  (
372  label proci = 1;
373  proci < UPstream::nProcs(comm);
374  ++proci
375  )
376  {
377  List<char> elems(is);
378  is.fatalCheck("read(Istream&) : reading entry");
379 
380  OPstream os
381  (
383  proci,
384  0,
386  comm
387  );
388  os << elems;
389  }
390 
391  ok = is.good();
392  }
393  else
394  {
395  IPstream is
396  (
399  0,
401  comm
402  );
403  is >> data;
404  }
405  }
406  else
407  {
408  PstreamBuffers pBufs
409  (
412  comm
413  );
414 
415  if (UPstream::master(comm))
416  {
417  Istream& is = isPtr();
418  is.fatalCheck("read(Istream&)");
419 
420  // Read master data
421  {
422  is >> data;
423  is.fatalCheck("read(Istream&) : reading entry");
424  }
425 
426  // Read slave data
427  for
428  (
429  label proci = 1;
430  proci < UPstream::nProcs(comm);
431  ++proci
432  )
433  {
434  List<char> elems(is);
435  is.fatalCheck("read(Istream&) : reading entry");
436 
437  UOPstream os(proci, pBufs);
438  os << elems;
439  }
440  }
441 
442  labelList recvSizes;
443  pBufs.finishedSends(recvSizes);
444 
445  if (!UPstream::master(comm))
446  {
447  UIPstream is(UPstream::masterNo(), pBufs);
448  is >> data;
449  }
450  }
451 
452  Pstream::scatter(ok, Pstream::msgType(), comm);
453 
454  return ok;
455 }
456 
457 
459 (
460  const label comm,
461  const fileName& fName,
462  autoPtr<ISstream>& isPtr,
463  IOobject& headerIO,
464  const UPstream::commsTypes commsType
465 )
466 {
467  if (debug)
468  {
469  Pout<< "decomposedBlockData::readBlocks:"
470  << " stream:" << (isPtr.valid() ? isPtr().name() : "invalid")
471  << " commsType:" << Pstream::commsTypeNames[commsType] << endl;
472  }
473 
474  bool ok = false;
475 
477  autoPtr<ISstream> realIsPtr;
478 
479  if (commsType == UPstream::commsTypes::scheduled)
480  {
481  if (UPstream::master(comm))
482  {
483  Istream& is = isPtr();
484  is.fatalCheck("read(Istream&)");
485 
486  // Read master data
487  {
488  is >> data;
489  is.fatalCheck("read(Istream&) : reading entry");
490 
491  realIsPtr.reset
492  (
493  new IListStream
494  (
495  std::move(data),
498  fName
499  )
500  );
501 
502 
503  // Read header
504  if (!headerIO.readHeader(realIsPtr()))
505  {
506  FatalIOErrorInFunction(realIsPtr())
507  << "problem while reading header for object "
508  << is.name() << exit(FatalIOError);
509  }
510  }
511 
512  // Read slave data
513  for
514  (
515  label proci = 1;
516  proci < UPstream::nProcs(comm);
517  ++proci
518  )
519  {
520  is >> data;
521  is.fatalCheck("read(Istream&) : reading entry");
522 
523  OPstream os
524  (
526  proci,
527  0,
529  comm
530  );
531  os << data;
532  }
533 
534  ok = is.good();
535  }
536  else
537  {
538  IPstream is
539  (
542  0,
544  comm
545  );
546  is >> data;
547 
548  realIsPtr.reset
549  (
550  new IListStream
551  (
552  std::move(data),
555  fName
556  )
557  );
558  }
559  }
560  else
561  {
562  PstreamBuffers pBufs
563  (
566  comm
567  );
568 
569  if (UPstream::master(comm))
570  {
571  Istream& is = isPtr();
572  is.fatalCheck("read(Istream&)");
573 
574  // Read master data
575  {
576  is >> data;
577  is.fatalCheck("read(Istream&) : reading entry");
578 
579  realIsPtr.reset
580  (
581  new IListStream
582  (
583  std::move(data),
586  fName
587  )
588  );
589 
590  // Read header
591  if (!headerIO.readHeader(realIsPtr()))
592  {
593  FatalIOErrorInFunction(realIsPtr())
594  << "problem while reading header for object "
595  << is.name() << exit(FatalIOError);
596  }
597  }
598 
599  // Read slave data
600  for
601  (
602  label proci = 1;
603  proci < UPstream::nProcs(comm);
604  ++proci
605  )
606  {
607  List<char> elems(is);
608  is.fatalCheck("read(Istream&) : reading entry");
609 
610  UOPstream os(proci, pBufs);
611  os << elems;
612  }
613 
614  ok = is.good();
615  }
616 
617  labelList recvSizes;
618  pBufs.finishedSends(recvSizes);
619 
620  if (!UPstream::master(comm))
621  {
622  UIPstream is(UPstream::masterNo(), pBufs);
623  is >> data;
624 
625  realIsPtr.reset
626  (
627  new IListStream
628  (
629  std::move(data),
632  fName
633  )
634  );
635  }
636  }
637 
638  Pstream::scatter(ok, Pstream::msgType(), comm);
639 
640  // version
641  string versionString(realIsPtr().version().str());
642  Pstream::scatter(versionString, Pstream::msgType(), comm);
643  realIsPtr().version(IOstream::versionNumber(versionString));
644 
645  // stream
646  {
647  OStringStream os;
648  os << realIsPtr().format();
649  string formatString(os.str());
650  Pstream::scatter(formatString, Pstream::msgType(), comm);
651  realIsPtr().format(formatString);
652  }
653 
654  word name(headerIO.name());
656  headerIO.rename(name);
658  Pstream::scatter(headerIO.note(), Pstream::msgType(), comm);
659  //Pstream::scatter(headerIO.instance(), Pstream::msgType(), comm);
660  //Pstream::scatter(headerIO.local(), Pstream::msgType(), comm);
661 
662  return realIsPtr;
663 }
664 
665 
667 (
668  const label comm,
669  const label data,
670  labelList& datas
671 )
672 {
673  const label nProcs = UPstream::nProcs(comm);
674  datas.setSize(nProcs);
675 
676  char* data0Ptr = reinterpret_cast<char*>(datas.begin());
677 
678  List<int> recvOffsets;
679  List<int> recvSizes;
680  if (UPstream::master(comm))
681  {
682  recvOffsets.setSize(nProcs);
683  forAll(recvOffsets, proci)
684  {
685  // Note: truncating long int to int since UPstream::gather limited
686  // to ints
687  recvOffsets[proci] =
688  int(reinterpret_cast<char*>(&datas[proci]) - data0Ptr);
689  }
690  recvSizes.setSize(nProcs, sizeof(label));
691  }
692 
694  (
695  reinterpret_cast<const char*>(&data),
696  sizeof(label),
697  data0Ptr,
698  recvSizes,
699  recvOffsets,
700  comm
701  );
702 }
703 
704 
706 (
707  const label comm,
708  const UList<char>& data,
709  const labelUList& recvSizes,
710 
711  const label startProc,
712  const label nProcs,
713 
714  List<int>& sliceOffsets,
715  List<char>& recvData
716 )
717 {
718  // Calculate master data
719  List<int> sliceSizes;
720  if (UPstream::master(comm))
721  {
722  const label numProcs = UPstream::nProcs(comm);
723 
724  sliceSizes.setSize(numProcs, 0);
725  sliceOffsets.setSize(numProcs+1, 0);
726 
727  int totalSize = 0;
728  label proci = startProc;
729  for (label i = 0; i < nProcs; i++)
730  {
731  sliceSizes[proci] = int(recvSizes[proci]);
732  sliceOffsets[proci] = totalSize;
733  totalSize += sliceSizes[proci];
734  ++proci;
735  }
736  sliceOffsets[proci] = totalSize;
737  recvData.setSize(totalSize);
738  }
739 
740  int nSend = 0;
741  if
742  (
743  !UPstream::master(comm)
744  && (UPstream::myProcNo(comm) >= startProc)
745  && (UPstream::myProcNo(comm) < startProc+nProcs)
746  )
747  {
748  // Note: UPstream::gather limited to int
749  nSend = int(data.byteSize());
750  }
751 
753  (
754  data.begin(),
755  nSend,
756 
757  recvData.begin(),
758  sliceSizes,
759  sliceOffsets,
760  comm
761  );
762 }
763 
764 
766 (
767  const label comm,
768  const off_t maxBufferSize,
769  const labelUList& recvSizes,
770  const label startProci
771 )
772 {
773  const label nProcs = UPstream::nProcs(comm);
774 
775  label nSendProcs = -1;
776  if (UPstream::master(comm))
777  {
778  off_t totalSize = recvSizes[startProci];
779  label proci = startProci+1;
780  while (proci < nProcs && (totalSize+recvSizes[proci] < maxBufferSize))
781  {
782  totalSize += recvSizes[proci];
783  proci++;
784  }
785 
786  nSendProcs = proci-startProci;
787  }
788 
789  // Scatter nSendProcs
790  label n;
792  (
793  reinterpret_cast<const char*>(&nSendProcs),
794  List<int>(nProcs, sizeof(nSendProcs)),
795  List<int>(nProcs, Zero),
796  reinterpret_cast<char*>(&n),
797  sizeof(n),
798  comm
799  );
800 
801  return n;
802 }
803 
804 
806 (
807  const label comm,
808  autoPtr<OSstream>& osPtr,
810  const UList<char>& data,
811 
812  const labelUList& recvSizes,
813  const PtrList<SubList<char>>& slaveData,
814 
815  const UPstream::commsTypes commsType,
816  const bool syncReturnState
817 )
818 {
819  if (debug)
820  {
821  Pout<< "decomposedBlockData::writeBlocks:"
822  << " stream:" << (osPtr.valid() ? osPtr().name() : "invalid")
823  << " data:" << data.size()
824  << " (master only) slaveData:" << slaveData.size()
825  << " commsType:" << Pstream::commsTypeNames[commsType] << endl;
826  }
827 
828  const label nProcs = UPstream::nProcs(comm);
829 
830  bool ok = true;
831 
832  if (slaveData.size())
833  {
834  // Already have gathered the slave data. communicator only used to
835  // check who is the master
836 
837  if (UPstream::master(comm))
838  {
839  OSstream& os = osPtr();
840 
841  start.setSize(nProcs);
842 
843  // Write master data
844  {
845  os << nl << "// Processor" << UPstream::masterNo() << nl;
846  start[UPstream::masterNo()] = os.stdStream().tellp();
847  os << data;
848  }
849 
850  // Write slaves
851 
852  label slaveOffset = 0;
853 
854  for (label proci = 1; proci < nProcs; ++proci)
855  {
856  os << nl << nl << "// Processor" << proci << nl;
857  start[proci] = os.stdStream().tellp();
858 
859  os << slaveData[proci];
860  slaveOffset += recvSizes[proci];
861  }
862 
863  ok = os.good();
864  }
865  }
866  else if (commsType == UPstream::commsTypes::scheduled)
867  {
868  if (UPstream::master(comm))
869  {
870  start.setSize(nProcs);
871 
872  OSstream& os = osPtr();
873 
874  // Write master data
875  {
876  os << nl << "// Processor" << UPstream::masterNo() << nl;
877  start[UPstream::masterNo()] = os.stdStream().tellp();
878  os << data;
879  }
880  // Write slaves
881  List<char> elems;
882  for (label proci = 1; proci < nProcs; ++proci)
883  {
884  elems.setSize(recvSizes[proci]);
886  (
888  proci,
889  elems.begin(),
890  elems.size(),
892  comm
893  );
894 
895  os << nl << nl << "// Processor" << proci << nl;
896  start[proci] = os.stdStream().tellp();
897  os << elems;
898  }
899 
900  ok = os.good();
901  }
902  else
903  {
905  (
908  data.begin(),
909  data.byteSize(),
911  comm
912  );
913  }
914  }
915  else
916  {
917  // Write master data
918  if (UPstream::master(comm))
919  {
920  start.setSize(nProcs);
921 
922  OSstream& os = osPtr();
923 
924  os << nl << "// Processor" << UPstream::masterNo() << nl;
925  start[UPstream::masterNo()] = os.stdStream().tellp();
926  os << data;
927  }
928 
929 
930  // Find out how many processor can be received into
931  // maxMasterFileBufferSize
932 
933  // Starting slave processor and number of processors
934  label startProc = 1;
935  label nSendProcs = nProcs-1;
936 
937  while (nSendProcs > 0 && startProc < nProcs)
938  {
939  nSendProcs = calcNumProcs
940  (
941  comm,
942  off_t
943  (
945  maxMasterFileBufferSize
946  ),
947  recvSizes,
948  startProc
949  );
950 
951  if (nSendProcs == 0)
952  {
953  break;
954  }
955 
956 
957  // Gather data from (a slice of) the slaves
958  List<int> sliceOffsets;
959  List<char> recvData;
960  gatherSlaveData
961  (
962  comm,
963  data,
964  recvSizes,
965 
966  startProc, // startProc,
967  nSendProcs, // nProcs,
968 
969  sliceOffsets,
970  recvData
971  );
972 
973  if (UPstream::master(comm))
974  {
975  OSstream& os = osPtr();
976 
977  // Write slaves
978  for
979  (
980  label proci = startProc;
981  proci < startProc+nSendProcs;
982  proci++
983  )
984  {
985  os << nl << nl << "// Processor" << proci << nl;
986  start[proci] = os.stdStream().tellp();
987 
988  os <<
990  (
991  recvData,
992  sliceOffsets[proci+1]-sliceOffsets[proci],
993  sliceOffsets[proci]
994  );
995  }
996  }
997 
998  startProc += nSendProcs;
999  }
1000 
1001  if (UPstream::master(comm))
1002  {
1003  ok = osPtr().good();
1004  }
1005  }
1006 
1007  if (syncReturnState)
1008  {
1009  //- Enable to get synchronised error checking. Is the one that keeps
1010  // slaves as slow as the master (which does all the writing)
1011  Pstream::scatter(ok, Pstream::msgType(), comm);
1012  }
1013 
1014  return ok;
1015 }
1016 
1017 
1019 {
1020  autoPtr<ISstream> isPtr;
1021  fileName objPath(fileHandler().filePath(false, *this, word::null));
1022  if (UPstream::master(comm_))
1023  {
1024  isPtr.reset(new IFstream(objPath));
1025  IOobject::readHeader(isPtr());
1026  }
1027 
1028  List<char>& data = *this;
1029  return readBlocks(comm_, isPtr, data, commsType_);
1030 }
1031 
1032 
1034 {
1035  const List<char>& data = *this;
1036 
1037  IOobject io(*this);
1038 
1039  // Re-read my own data to find out the header information
1040  if (Pstream::master(comm_))
1041  {
1042  UIListStream is
1043  (
1044  data,
1047  name()
1048  );
1049  io.readHeader(is);
1050  }
1051 
1052  // Scatter header information
1053 
1054  // version
1055  string versionString(os.version().str());
1056  Pstream::scatter(versionString, Pstream::msgType(), comm_);
1057 
1058  // stream
1059  string formatString;
1060  {
1061  OStringStream os;
1062  os << os.format();
1063  formatString = os.str();
1064  Pstream::scatter(formatString, Pstream::msgType(), comm_);
1065  }
1066 
1067  //word masterName(name());
1068  //Pstream::scatter(masterName, Pstream::msgType(), comm_);
1069 
1071  Pstream::scatter(io.note(), Pstream::msgType(), comm_);
1072  //Pstream::scatter(io.instance(), Pstream::msgType(), comm);
1073  //Pstream::scatter(io.local(), Pstream::msgType(), comm);
1074 
1075  fileName masterLocation(instance()/db().dbDir()/local());
1076  Pstream::scatter(masterLocation, Pstream::msgType(), comm_);
1077 
1078  if (!Pstream::master(comm_))
1079  {
1080  writeHeader
1081  (
1082  os,
1083  IOstream::versionNumber(versionString),
1084  IOstream::formatEnum(formatString),
1085  io.headerClassName(),
1086  io.note(),
1087  masterLocation,
1088  name()
1089  );
1090  }
1091 
1092  string str
1093  (
1094  reinterpret_cast<const char*>(data.cbegin()),
1095  data.byteSize()
1096  );
1097  os.writeQuoted(str, false);
1098 
1099  if (!Pstream::master(comm_))
1100  {
1102  }
1103 
1104  return os.good();
1105 }
1106 
1107 
1113  const bool valid
1114 ) const
1115 {
1116  autoPtr<OSstream> osPtr;
1117  if (UPstream::master(comm_))
1118  {
1119  // Note: always write binary. These are strings so readable
1120  // anyway. They have already be tokenised on the sending side.
1121  osPtr.reset(new OFstream(objectPath(), IOstream::BINARY, ver, cmp));
1122  IOobject::writeHeader(osPtr());
1123  }
1124 
1125  labelList recvSizes;
1126  gather(comm_, label(this->byteSize()), recvSizes);
1127 
1129  PtrList<SubList<char>> slaveData; // dummy slave data
1130  return writeBlocks
1131  (
1132  comm_,
1133  osPtr,
1134  start,
1135  *this,
1136  recvSizes,
1137  slaveData,
1138  commsType_
1139  );
1140 }
1141 
1142 
1144 {
1145  label nBlocks = 0;
1146 
1147  IFstream is(fName);
1148  is.fatalCheck("decomposedBlockData::numBlocks(const fileName&)");
1149 
1150  if (!is.good())
1151  {
1152  return nBlocks;
1153  }
1154 
1155  // FoamFile header
1156  token firstToken(is);
1157 
1158  if
1159  (
1160  is.good()
1161  && firstToken.isWord()
1162  && firstToken.wordToken() == "FoamFile"
1163  )
1164  {
1165  dictionary headerDict(is);
1166  is.version
1167  (
1169  (
1170  headerDict.get<float>("version")
1171  )
1172  );
1173  is.format(headerDict.get<word>("format"));
1174 
1175  // Obtain number of blocks directly
1176  if (headerDict.readIfPresent("blocks", nBlocks))
1177  {
1178  return nBlocks;
1179  }
1180  }
1181 
1182  // Fallback to brute force read of each data block
1183  List<char> data;
1184  while (is.good())
1185  {
1186  token sizeToken(is);
1187  if (!sizeToken.isLabel())
1188  {
1189  return nBlocks;
1190  }
1191  is.putBack(sizeToken);
1192 
1193  is >> data;
1194  nBlocks++;
1195  }
1196 
1197  return nBlocks;
1198 }
1199 
1200 
1201 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::autoPtr::reset
void reset(T *p=nullptr) noexcept
Delete managed object and set to new given pointer.
Definition: autoPtrI.H:158
Foam::UIListStream
Similar to IStringStream but using an externally managed buffer for its input. This allows the input ...
Definition: UIListStream.H:202
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
Foam::IOstream::fatalCheck
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:64
Foam::UPstream::commsTypes::nonBlocking
Foam::UPstream::masterNo
static constexpr int masterNo() noexcept
Process index of the master.
Definition: UPstream.H:432
SubList.H
Foam::IOobject::name
const word & name() const
Return name.
Definition: IOobjectI.H:46
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::UOPstream
Output inter-processor communications stream operating on external buffer.
Definition: UOPstream.H:57
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::Zero
static constexpr const zero Zero
Global zero.
Definition: zero.H:128
Foam::IFstream
Input from file stream, using an ISstream.
Definition: IFstream.H:97
Foam::decomposedBlockData::readBlock
static autoPtr< ISstream > readBlock(const label blocki, Istream &is, IOobject &headerIO)
Read selected block (non-seeking) + header information.
Definition: decomposedBlockData.C:243
Foam::IOobject::writeBanner
static Ostream & writeBanner(Ostream &os, bool noHint=false)
Write the standard OpenFOAM file/dictionary banner.
Definition: IOobjectWriteHeader.C:45
Foam::OPstream
Output inter-processor communications stream.
Definition: OPstream.H:52
Foam::IOobject::writeEndDivider
static Ostream & writeEndDivider(Ostream &os)
Write the standard end file divider.
Definition: IOobjectWriteHeader.C:109
Foam::SubList
A List obtained as a section of another List.
Definition: SubList.H:53
Foam::decomposedBlockData::calcNumProcs
static label calcNumProcs(const label comm, const off_t maxBufferSize, const labelUList &recvSizes, const label startProci)
Helper: determine number of processors whose recvSizes fits.
Definition: decomposedBlockData.C:766
Foam::UPstream::nProcs
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:426
Foam::PstreamBuffers
Buffers for inter-processor communications streams (UOPstream, UIPstream).
Definition: PstreamBuffers.H:88
Foam::Ostream::writeQuoted
virtual Ostream & writeQuoted(const std::string &str, const bool quoted=true)=0
Write std::string surrounded by quotes.
Foam::UPstream::parRun
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:414
objectRegistry.H
Foam::IOstreamOption::format
streamFormat format() const noexcept
Get the current stream format.
Definition: IOstreamOption.H:273
Foam::IOstreamOption::currentVersion
static const versionNumber currentVersion
The current version number.
Definition: IOstreamOption.H:193
OPstream.H
Foam::Pstream::scatter
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
Definition: gatherScatter.C:150
Foam::IListStream
An ISstream with internal List storage.
Definition: IListStream.H:133
StringStream.H
Input/output from string buffers.
Foam::fileHandler
const fileOperation & fileHandler()
Get current file handler.
Definition: fileOperation.C:1181
Foam::autoPtr::valid
bool valid() const noexcept
True if the managed pointer is non-null.
Definition: autoPtrI.H:107
Foam::FatalIOError
IOerror FatalIOError
Foam::decomposedBlockData::writeBlocks
static bool writeBlocks(const label comm, autoPtr< OSstream > &osPtr, List< std::streamoff > &start, const UList< char > &masterData, const labelUList &recvSizes, const PtrList< SubList< char >> &slaveData, const UPstream::commsTypes, const bool syncReturnState=true)
Write *this. Ostream only valid on master. Returns starts of.
Definition: decomposedBlockData.C:806
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::token
A token holds an item read from Istream.
Definition: token.H:69
Foam::dictionary::get
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:81
Foam::writeHeader
static void writeHeader(Ostream &os, const word &fieldName)
Definition: rawSurfaceWriterImpl.C:49
Foam::Pout
prefixOSstream Pout
An Ostream wrapper for parallel output to std::cout.
Foam::decomposedBlockData::~decomposedBlockData
virtual ~decomposedBlockData()
Destructor.
Definition: decomposedBlockData.C:164
Foam::UPstream::gather
static void gather(const char *sendData, int sendSize, char *recvData, const UList< int > &recvSizes, const UList< int > &recvOffsets, const label communicator=0)
Receive data from all processors on the master.
Definition: UPstream.C:111
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:290
Foam::token::isLabel
bool isLabel() const
Token is LABEL.
Definition: tokenI.H:450
Foam::IOobject::readHeader
bool readHeader(Istream &is)
Read header.
Definition: IOobjectReadHeader.C:35
Foam::UIPstream::read
static label read(const commsTypes commsType, const int fromProcNo, char *buf, const std::streamsize bufSize, const int tag=UPstream::msgType(), const label communicator=0)
Read into given buffer from given processor and return the.
Definition: UIPread.C:81
n
label n
Definition: TABSMDCalcMethod2.H:31
format
word format(conversionProperties.get< word >("format"))
Foam::UOPstream::write
static bool write(const commsTypes commsType, const int toProcNo, const char *buf, const std::streamsize bufSize, const int tag=UPstream::msgType(), const label communicator=0)
Write given buffer to given processor.
Definition: UOPwrite.C:36
IPstream.H
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::decomposedBlockData::readMasterHeader
static bool readMasterHeader(IOobject &, Istream &)
Read header. Call only on master.
Definition: decomposedBlockData.C:170
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:79
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::decomposedBlockData::writeHeader
static void writeHeader(Ostream &os, const IOstream::versionNumber version, const IOstream::streamFormat format, const word &type, const string &note, const fileName &location, const word &name)
Helper: write FoamFile IOobject header.
Definition: decomposedBlockData.C:196
Foam::OSstream::stdStream
virtual std::ostream & stdStream()
Access to underlying std::ostream.
Definition: OSstream.H:206
Foam::decomposedBlockData::gather
static void gather(const label comm, const label data, labelList &datas)
Helper: gather single label. Note: using native Pstream.
Definition: decomposedBlockData.C:667
Foam::decomposedBlockData::gatherSlaveData
static void gatherSlaveData(const label comm, const UList< char > &data, const labelUList &recvSizes, const label startProc, const label nProcs, List< int > &recvOffsets, List< char > &recvData)
Helper: gather data from (subset of) slaves. Returns.
Definition: decomposedBlockData.C:706
Foam::decomposedBlockData::writeObject
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool valid) const
Write using given format, version and compression.
Definition: decomposedBlockData.C:1109
Foam::IOstream::name
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.C:39
Foam::OSstream
Generic output stream.
Definition: OSstream.H:54
Foam::PtrList
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers....
Definition: List.H:65
Foam::UPstream::commsTypeNames
static const Enum< commsTypes > commsTypeNames
Names of the communication types.
Definition: UPstream.H:74
Foam::PstreamBuffers::finishedSends
void finishedSends(const bool block=true)
Mark all sends as having been done. This will start receives.
Definition: PstreamBuffers.C:80
Foam::IOstreamOption::version
versionNumber version() const noexcept
Get the stream version.
Definition: IOstreamOption.H:321
Foam::UPstream::commsTypes::scheduled
Foam::blockMeshTools::read
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:33
Foam::UPstream::scatter
static void scatter(const char *sendData, const UList< int > &sendSizes, const UList< int > &sendOffsets, char *recvData, int recvSize, const label communicator=0)
Send data to all processors from the root of the communicator.
Definition: UPstream.C:126
Foam::IOstreamOption::versionNumber::str
std::string str() const
A string representation of major.minor.
Definition: IOstreamOption.H:135
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:64
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::token::isWord
bool isWord() const
Token is WORD.
Definition: tokenI.H:552
Foam::decomposedBlockData::decomposedBlockData
decomposedBlockData(const label comm, const IOobject &io, const UPstream::commsTypes=UPstream::commsTypes::scheduled)
Construct given an IOobject.
Definition: decomposedBlockData.C:51
Foam::decomposedBlockData::writeData
virtual bool writeData(Ostream &) const
Write separated content. Assumes content is the serialised data.
Definition: decomposedBlockData.C:1033
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::IOobject::rename
virtual void rename(const word &newName)
Rename.
Definition: IOobject.H:368
Foam::token::wordToken
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.H:558
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::foamVersion::version
const std::string version
OpenFOAM version (name or stringified number) as a std::string.
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:99
Foam::IOobject::headerClassName
const word & headerClassName() const
Return name of the class name read from header.
Definition: IOobjectI.H:64
Foam::UPstream::myProcNo
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:444
Foam::UPstream::master
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:438
IListStream.H
Foam::Detail::StringStreamAllocator::str
Foam::string str() const
Get the string - as Foam::string rather than std::string.
Definition: StringStream.H:92
Foam::IOobject::note
const string & note() const
Return the optional note.
Definition: IOobjectI.H:76
Foam::autoPtr< Foam::ISstream >
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:67
Foam::fileOperations::masterUncollatedFileOperation
fileOperations that performs all file operations on the master processor. Requires the calls to be pa...
Definition: masterUncollatedFileOperation.H:85
Foam::IOstreamOption::BINARY
"binary"
Definition: IOstreamOption.H:67
masterUncollatedFileOperation.H
Foam::IOstreamOption::ASCII
"ascii"
Definition: IOstreamOption.H:66
Foam::UPstream::msgType
static int & msgType()
Message tag of standard messages.
Definition: UPstream.H:491
Foam::UPstream::commsTypes
commsTypes
Types of communications.
Definition: UPstream.H:66
Foam::decomposedBlockData::readBlocks
static bool readBlocks(const label comm, autoPtr< ISstream > &isPtr, List< char > &data, const UPstream::commsTypes commsType)
Read data into *this. ISstream is only valid on master.
Definition: decomposedBlockData.C:339
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Fstream.H
Input/output from file streams.
Foam::IOstreamOption::formatEnum
static streamFormat formatEnum(const word &formatName)
The stream format enum corresponding to the string.
Definition: IOstreamOption.C:56
Foam::IOobject::writeDivider
static Ostream & writeDivider(Ostream &os)
Write the standard file section divider.
Definition: IOobjectWriteHeader.C:99
decomposedBlockData.H
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:102
Foam::IOobject::readOpt
readOption readOpt() const
The read option.
Definition: IOobjectI.H:141
Foam::OStringStream
Output to string buffer, using a OSstream.
Definition: StringStream.H:189
Foam::Istream::putBack
void putBack(const token &tok)
Put back token.
Definition: Istream.C:53
Foam::start
label ListType::const_reference const label start
Definition: ListOps.H:408
Foam::type
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:590
Foam::UList
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:103
dictionary.H
PstreamBuffers.H
Foam::word::null
static const word null
An empty word.
Definition: word.H:77
Foam::IOstreamOption::compressionType
compressionType
Compression treatment (UNCOMPRESSED | COMPRESSED)
Definition: IOstreamOption.H:71
Foam::IPstream
Input inter-processor communications stream.
Definition: IPstream.H:52
Foam::IOobject::writeHeader
bool writeHeader(Ostream &os) const
Write header.
Definition: IOobjectWriteHeader.C:156
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:375
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::UIPstream
Input inter-processor communications stream operating on external buffer.
Definition: UIPstream.H:56
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:216
Foam::List::setSize
void setSize(const label newSize)
Alias for resize(const label)
Definition: ListI.H:146
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:294
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:54
Foam::dictionary::readIfPresent
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:417
labelPair.H
Foam::decomposedBlockData::read
virtual bool read()
Read object.
Definition: decomposedBlockData.C:1018
Foam::decomposedBlockData::numBlocks
static label numBlocks(const fileName &)
Detect number of blocks in a file.
Definition: decomposedBlockData.C:1143