decomposedBlockData.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2017-2019 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "decomposedBlockData.H"
27 #include "OPstream.H"
28 #include "IPstream.H"
29 #include "PstreamBuffers.H"
30 #include "OFstream.H"
31 #include "IFstream.H"
32 #include "IStringStream.H"
33 #include "dictionary.H"
34 #include "objectRegistry.H"
35 #include "SubList.H"
36 #include "labelPair.H"
38 
39 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
40 
41 namespace Foam
42 {
43  defineTypeNameAndDebug(decomposedBlockData, 0);
44 }
45 
46 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
47 
49 (
50  const label comm,
51  const IOobject& io,
52  const UPstream::commsTypes commsType
53 )
54 :
55  regIOobject(io),
56  commsType_(commsType),
57  comm_(comm)
58 {
59  // Temporary warning
61  {
63  << "decomposedBlockData " << name()
64  << " constructed with IOobject::MUST_READ_IF_MODIFIED"
65  " but decomposedBlockData does not support automatic rereading."
66  << endl;
67  }
68  if
69  (
70  (
73  )
74  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
75  )
76  {
77  read();
78  }
79 }
80 
81 
83 (
84  const label comm,
85  const IOobject& io,
86  const UList<char>& list,
87  const UPstream::commsTypes commsType
88 )
89 :
90  regIOobject(io),
91  commsType_(commsType),
92  comm_(comm)
93 {
94  // Temporary warning
96  {
98  << "decomposedBlockData " << name()
99  << " constructed with IOobject::MUST_READ_IF_MODIFIED"
100  " but decomposedBlockData does not support automatic rereading."
101  << endl;
102  }
103 
104  if
105  (
106  (
109  )
110  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
111  )
112  {
113  read();
114  }
115  else
116  {
117  List<char>::operator=(list);
118  }
119 }
120 
121 
123 (
124  const label comm,
125  const IOobject& io,
126  List<char>&& list,
127  const UPstream::commsTypes commsType
128 )
129 :
130  regIOobject(io),
131  List<char>(move(list)),
132  commsType_(commsType),
133  comm_(comm)
134 {
135  // Temporary warning
137  {
139  << "decomposedBlockData " << name()
140  << " constructed with IOobject::MUST_READ_IF_MODIFIED"
141  " but decomposedBlockData does not support automatic rereading."
142  << endl;
143  }
144 
145  if
146  (
147  (
150  )
151  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
152  )
153  {
154  read();
155  }
156 }
157 
158 
159 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
160 
162 {}
163 
164 
165 // * * * * * * * * * * * * * * * Members Functions * * * * * * * * * * * * * //
166 
168 {
169  if (debug)
170  {
171  Pout<< "decomposedBlockData::readMasterHeader:"
172  << " stream:" << is.name() << endl;
173  }
174 
175  // Master-only reading of header
176  is.fatalCheck("read(Istream&)");
177 
178  List<char> data(is);
179  is.fatalCheck("read(Istream&) : reading entry");
180  string buf(data.begin(), data.size());
181  IStringStream str(is.name(), buf);
182 
183  return io.readHeader(str);
184 }
185 
186 
188 (
189  Ostream& os,
190  const IOstream::versionNumber version,
191  const IOstream::streamFormat format,
192  const word& type,
193  const string& note,
194  const fileName& location,
195  const word& name
196 )
197 {
199  << "FoamFile\n{\n"
200  << " version " << version << ";\n"
201  << " format " << format << ";\n"
202  << " class " << type << ";\n";
203  if (note.size())
204  {
205  os << " note " << note << ";\n";
206  }
207 
208  if (location.size())
209  {
210  os << " location " << location << ";\n";
211  }
212 
213  os << " object " << name << ";\n"
214  << "}" << nl;
215 
216  IOobject::writeDivider(os) << nl;
217 }
218 
219 
221 (
222  const label blocki,
223  Istream& is,
224  IOobject& headerIO
225 )
226 {
227  if (debug)
228  {
229  Pout<< "decomposedBlockData::readBlock:"
230  << " stream:" << is.name() << " attempt to read block " << blocki
231  << endl;
232  }
233 
234  is.fatalCheck("read(Istream&)");
235 
236  List<char> data;
237  autoPtr<ISstream> realIsPtr;
238 
239  if (blocki == 0)
240  {
241  is >> data;
242  is.fatalCheck("read(Istream&) : reading entry");
243 
244  string buf(data.begin(), data.size());
245  realIsPtr = new IStringStream(is.name(), buf);
246 
247  // Read header
248  if (!headerIO.readHeader(realIsPtr()))
249  {
250  FatalIOErrorInFunction(realIsPtr())
251  << "problem while reading header for object "
252  << is.name() << exit(FatalIOError);
253  }
254  }
255  else
256  {
257  // Read master for header
258  is >> data;
259  is.fatalCheck("read(Istream&) : reading entry");
260 
263  {
264  string buf(data.begin(), data.size());
265  IStringStream headerStream(is.name(), buf);
266 
267  // Read header
268  if (!headerIO.readHeader(headerStream))
269  {
270  FatalIOErrorInFunction(headerStream)
271  << "problem while reading header for object "
272  << is.name() << exit(FatalIOError);
273  }
274  ver = headerStream.version();
275  fmt = headerStream.format();
276  }
277 
278  for (label i = 1; i < blocki+1; i++)
279  {
280  // Read data, override old data
281  is >> data;
282  is.fatalCheck("read(Istream&) : reading entry");
283  }
284  string buf(data.begin(), data.size());
285  realIsPtr = new IStringStream(is.name(), buf);
286 
287  // Apply master stream settings to realIsPtr
288  realIsPtr().format(fmt);
289  realIsPtr().version(ver);
290  }
291  return realIsPtr;
292 }
293 
294 
296 (
297  const label comm,
298  autoPtr<ISstream>& isPtr,
299  List<char>& data,
300  const UPstream::commsTypes commsType
301 )
302 {
303  if (debug)
304  {
305  Pout<< "decomposedBlockData::readBlocks:"
306  << " stream:" << (isPtr.valid() ? isPtr().name() : "invalid")
307  << " commsType:" << Pstream::commsTypeNames[commsType]
308  << " comm:" << comm << endl;
309  }
310 
311  bool ok = false;
312 
313  if (commsType == UPstream::commsTypes::scheduled)
314  {
315  if (UPstream::master(comm))
316  {
317  Istream& is = isPtr();
318  is.fatalCheck("read(Istream&)");
319 
320  // Read master data
321  {
322  is >> data;
323  is.fatalCheck("read(Istream&) : reading entry");
324  }
325 
326  // Read slave data
327  for
328  (
329  label proci = 1;
330  proci < UPstream::nProcs(comm);
331  proci++
332  )
333  {
334  List<char> elems(is);
335  is.fatalCheck("read(Istream&) : reading entry");
336 
337  OPstream os
338  (
340  proci,
341  0,
343  comm
344  );
345  os << elems;
346  }
347 
348  ok = is.good();
349  }
350  else
351  {
352  IPstream is
353  (
356  0,
358  comm
359  );
360  is >> data;
361  }
362  }
363  else
364  {
365  PstreamBuffers pBufs
366  (
369  comm
370  );
371 
372  if (UPstream::master(comm))
373  {
374  Istream& is = isPtr();
375  is.fatalCheck("read(Istream&)");
376 
377  // Read master data
378  {
379  is >> data;
380  is.fatalCheck("read(Istream&) : reading entry");
381  }
382 
383  // Read slave data
384  for
385  (
386  label proci = 1;
387  proci < UPstream::nProcs(comm);
388  proci++
389  )
390  {
391  List<char> elems(is);
392  is.fatalCheck("read(Istream&) : reading entry");
393 
394  UOPstream os(proci, pBufs);
395  os << elems;
396  }
397  }
398 
399  labelList recvSizes;
400  pBufs.finishedSends(recvSizes);
401 
402  if (!UPstream::master(comm))
403  {
404  UIPstream is(UPstream::masterNo(), pBufs);
405  is >> data;
406  }
407  }
408 
409  Pstream::scatter(ok, Pstream::msgType(), comm);
410 
411  return ok;
412 }
413 
414 
416 (
417  const label comm,
418  const fileName& fName,
419  autoPtr<ISstream>& isPtr,
420  IOobject& headerIO,
421  const UPstream::commsTypes commsType
422 )
423 {
424  if (debug)
425  {
426  Pout<< "decomposedBlockData::readBlocks:"
427  << " stream:" << (isPtr.valid() ? isPtr().name() : "invalid")
428  << " commsType:" << Pstream::commsTypeNames[commsType] << endl;
429  }
430 
431  bool ok = false;
432 
433  List<char> data;
434  autoPtr<ISstream> realIsPtr;
435 
436  if (commsType == UPstream::commsTypes::scheduled)
437  {
438  if (UPstream::master(comm))
439  {
440  Istream& is = isPtr();
441  is.fatalCheck("read(Istream&)");
442 
443  // Read master data
444  {
445  is >> data;
446  is.fatalCheck("read(Istream&) : reading entry");
447 
448  string buf(data.begin(), data.size());
449  realIsPtr = new IStringStream(fName, buf);
450 
451  // Read header
452  if (!headerIO.readHeader(realIsPtr()))
453  {
454  FatalIOErrorInFunction(realIsPtr())
455  << "problem while reading header for object "
456  << is.name() << exit(FatalIOError);
457  }
458  }
459 
460  // Read slave data
461  for
462  (
463  label proci = 1;
464  proci < UPstream::nProcs(comm);
465  proci++
466  )
467  {
468  is >> data;
469  is.fatalCheck("read(Istream&) : reading entry");
470 
471  OPstream os
472  (
474  proci,
475  0,
477  comm
478  );
479  os << data;
480  }
481 
482  ok = is.good();
483  }
484  else
485  {
486  IPstream is
487  (
490  0,
492  comm
493  );
494  is >> data;
495 
496  string buf(data.begin(), data.size());
497  realIsPtr = new IStringStream(fName, buf);
498  }
499  }
500  else
501  {
502  PstreamBuffers pBufs
503  (
506  comm
507  );
508 
509  if (UPstream::master(comm))
510  {
511  Istream& is = isPtr();
512  is.fatalCheck("read(Istream&)");
513 
514  // Read master data
515  {
516  is >> data;
517  is.fatalCheck("read(Istream&) : reading entry");
518 
519  string buf(data.begin(), data.size());
520  realIsPtr = new IStringStream(fName, buf);
521 
522  // Read header
523  if (!headerIO.readHeader(realIsPtr()))
524  {
525  FatalIOErrorInFunction(realIsPtr())
526  << "problem while reading header for object "
527  << is.name() << exit(FatalIOError);
528  }
529  }
530 
531  // Read slave data
532  for
533  (
534  label proci = 1;
535  proci < UPstream::nProcs(comm);
536  proci++
537  )
538  {
539  List<char> elems(is);
540  is.fatalCheck("read(Istream&) : reading entry");
541 
542  UOPstream os(proci, pBufs);
543  os << elems;
544  }
545 
546  ok = is.good();
547  }
548 
549  labelList recvSizes;
550  pBufs.finishedSends(recvSizes);
551 
552  if (!UPstream::master(comm))
553  {
554  UIPstream is(UPstream::masterNo(), pBufs);
555  is >> data;
556 
557  string buf(data.begin(), data.size());
558  realIsPtr = new IStringStream(fName, buf);
559  }
560  }
561 
562  Pstream::scatter(ok, Pstream::msgType(), comm);
563 
564  // version
565  string versionString(realIsPtr().version().str());
566  Pstream::scatter(versionString, Pstream::msgType(), comm);
567  realIsPtr().version(IStringStream(versionString)());
568 
569  // stream
570  {
571  OStringStream os;
572  os << realIsPtr().format();
573  string formatString(os.str());
574  Pstream::scatter(formatString, Pstream::msgType(), comm);
575  realIsPtr().format(formatString);
576  }
577 
578  word name(headerIO.name());
579  Pstream::scatter(name, Pstream::msgType(), comm);
580  headerIO.rename(name);
582  Pstream::scatter(headerIO.note(), Pstream::msgType(), comm);
583  // Pstream::scatter(headerIO.instance(), Pstream::msgType(), comm);
584  // Pstream::scatter(headerIO.local(), Pstream::msgType(), comm);
585 
586  return realIsPtr;
587 }
588 
589 
591 (
592  const label comm,
593  const label data,
594  labelList& datas
595 )
596 {
597  const label nProcs = UPstream::nProcs(comm);
598  datas.setSize(nProcs);
599 
600  char* data0Ptr = reinterpret_cast<char*>(datas.begin());
601 
602  List<int> recvOffsets;
603  List<int> recvSizes;
604  if (UPstream::master(comm))
605  {
606  recvOffsets.setSize(nProcs);
607  forAll(recvOffsets, proci)
608  {
609  // Note: truncating long int to int since UPstream::gather limited
610  // to ints
611  recvOffsets[proci] =
612  int(reinterpret_cast<char*>(&datas[proci]) - data0Ptr);
613  }
614  recvSizes.setSize(nProcs, sizeof(label));
615  }
616 
618  (
619  reinterpret_cast<const char*>(&data),
620  sizeof(label),
621  data0Ptr,
622  recvSizes,
623  recvOffsets,
624  comm
625  );
626 }
627 
628 
630 (
631  const label comm,
632  const UList<char>& data,
633  const labelUList& recvSizes,
634 
635  const label startProc,
636  const label nProcs,
637 
638  List<int>& sliceOffsets,
639  List<char>& recvData
640 )
641 {
642  // Calculate master data
643  List<int> sliceSizes;
644  if (UPstream::master(comm))
645  {
646  const label numProcs = UPstream::nProcs(comm);
647 
648  sliceSizes.setSize(numProcs, 0);
649  sliceOffsets.setSize(numProcs+1, 0);
650 
651  int totalSize = 0;
652  label proci = startProc;
653  for (label i = 0; i < nProcs; i++)
654  {
655  sliceSizes[proci] = int(recvSizes[proci]);
656  sliceOffsets[proci] = totalSize;
657  totalSize += sliceSizes[proci];
658  proci++;
659  }
660  sliceOffsets[proci] = totalSize;
661  recvData.setSize(totalSize);
662  }
663 
664  int nSend = 0;
665  if
666  (
667  !UPstream::master(comm)
668  && (UPstream::myProcNo(comm) >= startProc)
669  && (UPstream::myProcNo(comm) < startProc+nProcs)
670  )
671  {
672  // Note: UPstream::gather limited to int
673  nSend = int(data.byteSize());
674  }
675 
677  (
678  data.begin(),
679  nSend,
680 
681  recvData.begin(),
682  sliceSizes,
683  sliceOffsets,
684  comm
685  );
686 }
687 
688 
690 (
691  const label comm,
692  const off_t maxBufferSize,
693  const labelUList& recvSizes,
694  const label startProci
695 )
696 {
697  const label nProcs = UPstream::nProcs(comm);
698 
699  label nSendProcs = -1;
700  if (UPstream::master(comm))
701  {
702  off_t totalSize = recvSizes[startProci];
703  label proci = startProci+1;
704  while (proci < nProcs && (totalSize+recvSizes[proci] < maxBufferSize))
705  {
706  totalSize += recvSizes[proci];
707  proci++;
708  }
709 
710  nSendProcs = proci-startProci;
711  }
712 
713  // Scatter nSendProcs
714  label n;
716  (
717  reinterpret_cast<const char*>(&nSendProcs),
718  List<int>(nProcs, sizeof(nSendProcs)),
719  List<int>(nProcs, 0),
720  reinterpret_cast<char*>(&n),
721  sizeof(n),
722  comm
723  );
724 
725  return n;
726 }
727 
728 
730 (
731  const label comm,
732  autoPtr<OSstream>& osPtr,
733  List<std::streamoff>& start,
734  const UList<char>& data,
735 
736  const labelUList& recvSizes,
737  const PtrList<SubList<char>>& slaveData,
738 
739  const UPstream::commsTypes commsType,
740  const bool syncReturnState
741 )
742 {
743  if (debug)
744  {
745  Pout<< "decomposedBlockData::writeBlocks:"
746  << " stream:" << (osPtr.valid() ? osPtr().name() : "invalid")
747  << " data:" << data.size()
748  << " (master only) slaveData:" << slaveData.size()
749  << " commsType:" << Pstream::commsTypeNames[commsType] << endl;
750  }
751 
752  const label nProcs = UPstream::nProcs(comm);
753 
754  bool ok = true;
755 
756  if (slaveData.size())
757  {
758  // Already have gathered the slave data. communicator only used to
759  // check who is the master
760 
761  if (UPstream::master(comm))
762  {
763  OSstream& os = osPtr();
764 
765  start.setSize(nProcs);
766 
767  // Write master data
768  {
769  os << nl << "// Processor" << UPstream::masterNo() << nl;
770  start[UPstream::masterNo()] = os.stdStream().tellp();
771  os << data;
772  }
773 
774  // Write slaves
775 
776  label slaveOffset = 0;
777 
778  for (label proci = 1; proci < nProcs; proci++)
779  {
780  os << nl << nl << "// Processor" << proci << nl;
781  start[proci] = os.stdStream().tellp();
782 
783  os << slaveData[proci];
784  slaveOffset += recvSizes[proci];
785  }
786 
787  ok = os.good();
788  }
789  }
790  else if (commsType == UPstream::commsTypes::scheduled)
791  {
792  if (UPstream::master(comm))
793  {
794  start.setSize(nProcs);
795 
796  OSstream& os = osPtr();
797 
798  // Write master data
799  {
800  os << nl << "// Processor" << UPstream::masterNo() << nl;
801  start[UPstream::masterNo()] = os.stdStream().tellp();
802  os << data;
803  }
804  // Write slaves
805  List<char> elems;
806  for (label proci = 1; proci < nProcs; proci++)
807  {
808  elems.setSize(recvSizes[proci]);
810  (
812  proci,
813  elems.begin(),
814  elems.size(),
816  comm
817  );
818 
819  os << nl << nl << "// Processor" << proci << nl;
820  start[proci] = os.stdStream().tellp();
821  os << elems;
822  }
823 
824  ok = os.good();
825  }
826  else
827  {
829  (
832  data.begin(),
833  data.byteSize(),
835  comm
836  );
837  }
838  }
839  else
840  {
841  // Write master data
842  if (UPstream::master(comm))
843  {
844  start.setSize(nProcs);
845 
846  OSstream& os = osPtr();
847 
848  os << nl << "// Processor" << UPstream::masterNo() << nl;
849  start[UPstream::masterNo()] = os.stdStream().tellp();
850  os << data;
851  }
852 
853 
854  // Find out how many processor can be received into
855  // maxMasterFileBufferSize
856 
857  // Starting slave processor and number of processors
858  label startProc = 1;
859  label nSendProcs = nProcs-1;
860 
861  while (nSendProcs > 0 && startProc < nProcs)
862  {
863  nSendProcs = calcNumProcs
864  (
865  comm,
866  off_t
867  (
869  maxMasterFileBufferSize
870  ),
871  recvSizes,
872  startProc
873  );
874 
875  if (nSendProcs == 0)
876  {
877  break;
878  }
879 
880 
881  // Gather data from (a slice of) the slaves
882  List<int> sliceOffsets;
883  List<char> recvData;
884  gatherSlaveData
885  (
886  comm,
887  data,
888  recvSizes,
889 
890  startProc, // startProc,
891  nSendProcs, // nProcs,
892 
893  sliceOffsets,
894  recvData
895  );
896 
897  if (UPstream::master(comm))
898  {
899  OSstream& os = osPtr();
900 
901  // Write slaves
902  for
903  (
904  label proci = startProc;
905  proci < startProc+nSendProcs;
906  proci++
907  )
908  {
909  os << nl << nl << "// Processor" << proci << nl;
910  start[proci] = os.stdStream().tellp();
911 
912  os <<
914  (
915  recvData,
916  sliceOffsets[proci+1]-sliceOffsets[proci],
917  sliceOffsets[proci]
918  );
919  }
920  }
921 
922  startProc += nSendProcs;
923  }
924 
925  if (UPstream::master(comm))
926  {
927  ok = osPtr().good();
928  }
929  }
930 
931  if (syncReturnState)
932  {
933  //- Enable to get synchronised error checking. Is the one that keeps
934  // slaves as slow as the master (which does all the writing)
935  Pstream::scatter(ok, Pstream::msgType(), comm);
936  }
937 
938  return ok;
939 }
940 
941 
943 {
944  autoPtr<ISstream> isPtr;
945  fileName objPath(fileHandler().filePath(false, *this, word::null));
946  if (UPstream::master(comm_))
947  {
948  isPtr.reset(new IFstream(objPath));
949  IOobject::readHeader(isPtr());
950  }
951 
952  List<char>& data = *this;
953  return readBlocks(comm_, isPtr, data, commsType_);
954 }
955 
956 
958 {
959  const List<char>& data = *this;
960 
961  string str
962  (
963  reinterpret_cast<const char*>(data.cbegin()),
964  data.byteSize()
965  );
966 
967  IOobject io(*this);
968  if (Pstream::master(comm_))
969  {
970  IStringStream is(name(), str);
971  io.readHeader(is);
972  }
973 
974  // Scatter header information
975 
976  // version
977  string versionString(os.version().str());
978  Pstream::scatter(versionString, Pstream::msgType(), comm_);
979 
980  // stream
981  string formatString;
982  {
983  OStringStream os;
984  os << os.format();
985  formatString = os.str();
986  Pstream::scatter(formatString, Pstream::msgType(), comm_);
987  }
988 
989  // word masterName(name());
990  // Pstream::scatter(masterName, Pstream::msgType(), comm_);
991 
992  Pstream::scatter(io.headerClassName(), Pstream::msgType(), comm_);
993  Pstream::scatter(io.note(), Pstream::msgType(), comm_);
994  // Pstream::scatter(io.instance(), Pstream::msgType(), comm);
995  // Pstream::scatter(io.local(), Pstream::msgType(), comm);
996 
997  fileName masterLocation(instance()/db().dbDir()/local());
998  Pstream::scatter(masterLocation, Pstream::msgType(), comm_);
999 
1000  if (!Pstream::master(comm_))
1001  {
1002  writeHeader
1003  (
1004  os,
1005  IOstream::versionNumber(IStringStream(versionString)()),
1006  IOstream::formatEnum(formatString),
1007  io.headerClassName(),
1008  io.note(),
1009  masterLocation,
1010  name()
1011  );
1012  }
1013 
1014  os.writeQuoted(str, false);
1015 
1016  if (!Pstream::master(comm_))
1017  {
1019  }
1020 
1021  return os.good();
1022 }
1023 
1024 
1030  const bool write
1031 ) const
1032 {
1033  autoPtr<OSstream> osPtr;
1034  if (UPstream::master(comm_))
1035  {
1036  // Note: always write binary. These are strings so readable
1037  // anyway. They have already be tokenised on the sending side.
1038  osPtr.reset(new OFstream(objectPath(), IOstream::BINARY, ver, cmp));
1039  IOobject::writeHeader(osPtr());
1040  }
1041 
1042  labelList recvSizes;
1043  gather(comm_, label(this->byteSize()), recvSizes);
1044 
1045  List<std::streamoff> start;
1046  PtrList<SubList<char>> slaveData; // dummy slave data
1047  return writeBlocks
1048  (
1049  comm_,
1050  osPtr,
1051  start,
1052  *this,
1053  recvSizes,
1054  slaveData,
1055  commsType_
1056  );
1057 }
1058 
1059 
1061 {
1062  label nBlocks = 0;
1063 
1064  IFstream is(fName);
1065  is.fatalCheck("decomposedBlockData::numBlocks(const fileName&)");
1066 
1067  if (!is.good())
1068  {
1069  return nBlocks;
1070  }
1071 
1072  // Skip header
1073  token firstToken(is);
1074 
1075  if
1076  (
1077  is.good()
1078  && firstToken.isWord()
1079  && firstToken.wordToken() == "FoamFile"
1080  )
1081  {
1082  dictionary headerDict(is);
1083  is.version(headerDict.lookup("version"));
1084  is.format(headerDict.lookup("format"));
1085  }
1086 
1087  List<char> data;
1088  while (is.good())
1089  {
1090  token sizeToken(is);
1091  if (!sizeToken.isLabel())
1092  {
1093  return nBlocks;
1094  }
1095  is.putBack(sizeToken);
1096 
1097  is >> data;
1098  nBlocks++;
1099  }
1100 
1101  return nBlocks;
1102 }
1103 
1104 
1105 // ************************************************************************* //
bool isLabel() const
Definition: tokenI.H:271
decomposedBlockData(const label comm, const IOobject &, const UPstream::commsTypes=UPstream::commsTypes::scheduled)
Construct given an IOobject.
virtual ~decomposedBlockData()
Destructor.
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.
Generic output stream.
Definition: OSstream.H:51
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
bool isWord() const
Definition: tokenI.H:230
static streamFormat formatEnum(const word &)
Return stream format of given format name.
Definition: IOstream.C:39
virtual bool read()
Read object.
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:59
static int masterNo()
Process index of the master.
Definition: UPstream.H:417
const word & name() const
Return name.
Definition: IOobject.H:295
A class for handling file names.
Definition: fileName.H:79
static Stream & writeBanner(Stream &os, bool noHint=false)
Write the standard OpenFOAM file/dictionary banner.
Definition: IOobjectI.H:45
virtual bool writeData(Ostream &) const
Write separated content. Assumes content is the serialised data.
void finishedSends(const bool block=true)
Mark all sends as having been done. This will start receives.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
void reset(T *=nullptr)
If object pointer already set, delete object and set to given.
Definition: autoPtrI.H:114
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:111
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:158
commsTypes
Types of communications.
Definition: UPstream.H:64
const word & wordToken() const
Definition: tokenI.H:235
Output to file stream.
Definition: OFstream.H:82
static Stream & writeDivider(Stream &os)
Write the standard file section divider.
Definition: IOobjectI.H:93
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
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.
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
virtual void rename(const word &newName)
Rename.
Definition: IOobject.H:325
A token holds items read from Istream.
Definition: token.H:69
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:429
const_iterator cbegin() const
Return const_iterator to begin traversing the constant UList.
Definition: UListI.H:230
void putBack(const token &)
Put back token.
Definition: Istream.C:30
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
static label numBlocks(const fileName &)
Detect number of blocks in a file.
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:423
static int & msgType()
Message tag of standard messages.
Definition: UPstream.H:476
versionNumber version() const
Return the stream version.
Definition: IOstream.H:399
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
virtual Ostream & writeQuoted(const std::string &, const bool quoted=true)=0
Write std::string surrounded by quotes.
Input inter-processor communications stream.
Definition: IPstream.H:50
Input inter-processor communications stream operating on external buffer.
Definition: UIPstream.H:53
bool readHeader(Istream &)
Read header.
string & note()
Return non-constant access to the optional note.
Definition: IOobject.H:313
A List obtained as a section of another List.
Definition: SubList.H:53
bool read(const char *, int32_t &)
Definition: int32IO.C:85
A class for handling words, derived from string.
Definition: word.H:59
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:297
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.
iterator begin()
Return an iterator to begin traversing the UList.
Definition: UListI.H:216
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
static const word null
An empty word.
Definition: word.H:77
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool write) const
Write using given format, version and compression.
const fileOperation & fileHandler()
Get current file handler.
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
bool valid() const
Return true if the autoPtr valid (ie, the pointer is set)
Definition: autoPtrI.H:83
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:105
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:60
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
compressionType
Enumeration for the format of data in the stream.
Definition: IOstream.H:193
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
Output inter-processor communications stream operating on external buffer.
Definition: UOPstream.H:54
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:79
static const char nl
Definition: Ostream.H:265
defineTypeNameAndDebug(combustionModel, 0)
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:34
Input from file stream.
Definition: IFstream.H:81
static const NamedEnum< commsTypes, 3 > commsTypeNames
Definition: UPstream.H:71
Output inter-processor communications stream.
Definition: OPstream.H:50
Buffers for inter-processor communications streams (UOPstream, UIPstream).
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.
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
void operator=(const UList< T > &)
Assignment to UList operator. Takes linear time.
Definition: List.C:376
static label calcNumProcs(const label comm, const off_t maxBufferSize, const labelUList &recvSizes, const label startProci)
Helper: determine number of processors whose recvSizes fits.
virtual ostream & stdStream()
Access to underlying std::ostream.
Definition: OSstream.H:175
void setSize(const label)
Reset size of List.
Definition: List.C:281
static autoPtr< ISstream > readBlock(const label blocki, Istream &is, IOobject &headerIO)
Read selected block (non-seeking) + header information.
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:411
static Stream & writeEndDivider(Stream &os)
Write the standard end file divider.
Definition: IOobjectI.H:103
static bool readMasterHeader(IOobject &, Istream &)
Read header. Call only on master.
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:96
#define WarningInFunction
Report a warning using Foam::Warning.
static const versionNumber currentVersion
Current version number.
Definition: IOstream.H:206
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
string str() const
Return the versionNumber as a character string.
Definition: IOstream.C:116
A templated 1D list of pointers to objects of type <T>, where the size of the array is known and used...
Definition: List.H:70
std::streamsize byteSize() const
Return the binary size in number of characters of the UList.
Definition: UList.C:100
Input from memory buffer stream.
Definition: IStringStream.H:49
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
string str() const
Return the string.
Version number type.
Definition: IOstream.H:96
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:55
bool writeHeader(Ostream &) const
Write header.
label n
label size() const
Return the number of elements in the UList.
Definition: UListI.H:299
readOption readOpt() const
Definition: IOobject.H:345
const word & headerClassName() const
Return name of the class name read from header.
Definition: IOobject.H:301
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
Output to memory buffer stream.
Definition: OStringStream.H:49
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:583
static void gather(const label comm, const label data, labelList &datas)
Helper: gather single label. Note: using native Pstream.
IOerror FatalIOError