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-2018 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  const Xfer<List<char>>& list,
127  const UPstream::commsTypes commsType
128 )
129 :
130  regIOobject(io),
131  commsType_(commsType),
132  comm_(comm)
133 {
134  // Temporary warning
136  {
138  << "decomposedBlockData " << name()
139  << " constructed with IOobject::MUST_READ_IF_MODIFIED"
140  " but decomposedBlockData does not support automatic rereading."
141  << endl;
142  }
143 
144  List<char>::transfer(list());
145 
146  if
147  (
148  (
151  )
152  || (io.readOpt() == IOobject::READ_IF_PRESENT && headerOk())
153  )
154  {
155  read();
156  }
157 }
158 
159 
160 // * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * * //
161 
163 {}
164 
165 
166 // * * * * * * * * * * * * * * * Members Functions * * * * * * * * * * * * * //
167 
169 {
170  if (debug)
171  {
172  Pout<< "decomposedBlockData::readMasterHeader:"
173  << " stream:" << is.name() << endl;
174  }
175 
176  // Master-only reading of header
177  is.fatalCheck("read(Istream&)");
178 
179  List<char> data(is);
180  is.fatalCheck("read(Istream&) : reading entry");
181  string buf(data.begin(), data.size());
182  IStringStream str(is.name(), buf);
183 
184  return io.readHeader(str);
185 }
186 
187 
189 (
190  Ostream& os,
191  const IOstream::versionNumber version,
192  const IOstream::streamFormat format,
193  const word& type,
194  const string& note,
195  const fileName& location,
196  const word& name
197 )
198 {
200  << "FoamFile\n{\n"
201  << " version " << version << ";\n"
202  << " format " << format << ";\n"
203  << " class " << type << ";\n";
204  if (note.size())
205  {
206  os << " note " << note << ";\n";
207  }
208 
209  if (location.size())
210  {
211  os << " location " << location << ";\n";
212  }
213 
214  os << " object " << name << ";\n"
215  << "}" << nl;
216 
217  IOobject::writeDivider(os) << nl;
218 }
219 
220 
222 (
223  const label blocki,
224  Istream& is,
225  IOobject& headerIO
226 )
227 {
228  if (debug)
229  {
230  Pout<< "decomposedBlockData::readBlock:"
231  << " stream:" << is.name() << " attempt to read block " << blocki
232  << endl;
233  }
234 
235  is.fatalCheck("read(Istream&)");
236 
237  List<char> data;
238  autoPtr<ISstream> realIsPtr;
239 
240  if (blocki == 0)
241  {
242  is >> data;
243  is.fatalCheck("read(Istream&) : reading entry");
244 
245  string buf(data.begin(), data.size());
246  realIsPtr = new IStringStream(is.name(), buf);
247 
248  // Read header
249  if (!headerIO.readHeader(realIsPtr()))
250  {
251  FatalIOErrorInFunction(realIsPtr())
252  << "problem while reading header for object "
253  << is.name() << exit(FatalIOError);
254  }
255  }
256  else
257  {
258  // Read master for header
259  is >> data;
260  is.fatalCheck("read(Istream&) : reading entry");
261 
264  {
265  string buf(data.begin(), data.size());
266  IStringStream headerStream(is.name(), buf);
267 
268  // Read header
269  if (!headerIO.readHeader(headerStream))
270  {
271  FatalIOErrorInFunction(headerStream)
272  << "problem while reading header for object "
273  << is.name() << exit(FatalIOError);
274  }
275  ver = headerStream.version();
276  fmt = headerStream.format();
277  }
278 
279  for (label i = 1; i < blocki+1; i++)
280  {
281  // Read data, override old data
282  is >> data;
283  is.fatalCheck("read(Istream&) : reading entry");
284  }
285  string buf(data.begin(), data.size());
286  realIsPtr = new IStringStream(is.name(), buf);
287 
288  // Apply master stream settings to realIsPtr
289  realIsPtr().format(fmt);
290  realIsPtr().version(ver);
291  }
292  return realIsPtr;
293 }
294 
295 
297 (
298  const label comm,
299  autoPtr<ISstream>& isPtr,
300  List<char>& data,
301  const UPstream::commsTypes commsType
302 )
303 {
304  if (debug)
305  {
306  Pout<< "decomposedBlockData::readBlocks:"
307  << " stream:" << (isPtr.valid() ? isPtr().name() : "invalid")
308  << " commsType:" << Pstream::commsTypeNames[commsType]
309  << " comm:" << comm << endl;
310  }
311 
312  bool ok = false;
313 
314  if (commsType == UPstream::commsTypes::scheduled)
315  {
316  if (UPstream::master(comm))
317  {
318  Istream& is = isPtr();
319  is.fatalCheck("read(Istream&)");
320 
321  // Read master data
322  {
323  is >> data;
324  is.fatalCheck("read(Istream&) : reading entry");
325  }
326 
327  // Read slave data
328  for
329  (
330  label proci = 1;
331  proci < UPstream::nProcs(comm);
332  proci++
333  )
334  {
335  List<char> elems(is);
336  is.fatalCheck("read(Istream&) : reading entry");
337 
338  OPstream os
339  (
341  proci,
342  0,
344  comm
345  );
346  os << elems;
347  }
348 
349  ok = is.good();
350  }
351  else
352  {
353  IPstream is
354  (
357  0,
359  comm
360  );
361  is >> data;
362  }
363  }
364  else
365  {
366  PstreamBuffers pBufs
367  (
370  comm
371  );
372 
373  if (UPstream::master(comm))
374  {
375  Istream& is = isPtr();
376  is.fatalCheck("read(Istream&)");
377 
378  // Read master data
379  {
380  is >> data;
381  is.fatalCheck("read(Istream&) : reading entry");
382  }
383 
384  // Read slave data
385  for
386  (
387  label proci = 1;
388  proci < UPstream::nProcs(comm);
389  proci++
390  )
391  {
392  List<char> elems(is);
393  is.fatalCheck("read(Istream&) : reading entry");
394 
395  UOPstream os(proci, pBufs);
396  os << elems;
397  }
398  }
399 
400  labelList recvSizes;
401  pBufs.finishedSends(recvSizes);
402 
403  if (!UPstream::master(comm))
404  {
405  UIPstream is(UPstream::masterNo(), pBufs);
406  is >> data;
407  }
408  }
409 
410  Pstream::scatter(ok, Pstream::msgType(), comm);
411 
412  return ok;
413 }
414 
415 
417 (
418  const label comm,
419  const fileName& fName,
420  autoPtr<ISstream>& isPtr,
421  IOobject& headerIO,
422  const UPstream::commsTypes commsType
423 )
424 {
425  if (debug)
426  {
427  Pout<< "decomposedBlockData::readBlocks:"
428  << " stream:" << (isPtr.valid() ? isPtr().name() : "invalid")
429  << " commsType:" << Pstream::commsTypeNames[commsType] << endl;
430  }
431 
432  bool ok = false;
433 
434  List<char> data;
435  autoPtr<ISstream> realIsPtr;
436 
437  if (commsType == UPstream::commsTypes::scheduled)
438  {
439  if (UPstream::master(comm))
440  {
441  Istream& is = isPtr();
442  is.fatalCheck("read(Istream&)");
443 
444  // Read master data
445  {
446  is >> data;
447  is.fatalCheck("read(Istream&) : reading entry");
448 
449  string buf(data.begin(), data.size());
450  realIsPtr = new IStringStream(fName, buf);
451 
452  // Read header
453  if (!headerIO.readHeader(realIsPtr()))
454  {
455  FatalIOErrorInFunction(realIsPtr())
456  << "problem while reading header for object "
457  << is.name() << exit(FatalIOError);
458  }
459  }
460 
461  // Read slave data
462  for
463  (
464  label proci = 1;
465  proci < UPstream::nProcs(comm);
466  proci++
467  )
468  {
469  is >> data;
470  is.fatalCheck("read(Istream&) : reading entry");
471 
472  OPstream os
473  (
475  proci,
476  0,
478  comm
479  );
480  os << data;
481  }
482 
483  ok = is.good();
484  }
485  else
486  {
487  IPstream is
488  (
491  0,
493  comm
494  );
495  is >> data;
496 
497  string buf(data.begin(), data.size());
498  realIsPtr = new IStringStream(fName, buf);
499  }
500  }
501  else
502  {
503  PstreamBuffers pBufs
504  (
507  comm
508  );
509 
510  if (UPstream::master(comm))
511  {
512  Istream& is = isPtr();
513  is.fatalCheck("read(Istream&)");
514 
515  // Read master data
516  {
517  is >> data;
518  is.fatalCheck("read(Istream&) : reading entry");
519 
520  string buf(data.begin(), data.size());
521  realIsPtr = new IStringStream(fName, buf);
522 
523  // Read header
524  if (!headerIO.readHeader(realIsPtr()))
525  {
526  FatalIOErrorInFunction(realIsPtr())
527  << "problem while reading header for object "
528  << is.name() << exit(FatalIOError);
529  }
530  }
531 
532  // Read slave data
533  for
534  (
535  label proci = 1;
536  proci < UPstream::nProcs(comm);
537  proci++
538  )
539  {
540  List<char> elems(is);
541  is.fatalCheck("read(Istream&) : reading entry");
542 
543  UOPstream os(proci, pBufs);
544  os << elems;
545  }
546 
547  ok = is.good();
548  }
549 
550  labelList recvSizes;
551  pBufs.finishedSends(recvSizes);
552 
553  if (!UPstream::master(comm))
554  {
555  UIPstream is(UPstream::masterNo(), pBufs);
556  is >> data;
557 
558  string buf(data.begin(), data.size());
559  realIsPtr = new IStringStream(fName, buf);
560  }
561  }
562 
563  Pstream::scatter(ok, Pstream::msgType(), comm);
564 
565  // version
566  string versionString(realIsPtr().version().str());
567  Pstream::scatter(versionString, Pstream::msgType(), comm);
568  realIsPtr().version(IStringStream(versionString)());
569 
570  // stream
571  {
572  OStringStream os;
573  os << realIsPtr().format();
574  string formatString(os.str());
575  Pstream::scatter(formatString, Pstream::msgType(), comm);
576  realIsPtr().format(formatString);
577  }
578 
579  word name(headerIO.name());
580  Pstream::scatter(name, Pstream::msgType(), comm);
581  headerIO.rename(name);
583  Pstream::scatter(headerIO.note(), Pstream::msgType(), comm);
584  // Pstream::scatter(headerIO.instance(), Pstream::msgType(), comm);
585  // Pstream::scatter(headerIO.local(), Pstream::msgType(), comm);
586 
587  return realIsPtr;
588 }
589 
590 
592 (
593  const label comm,
594  const label data,
595  labelList& datas
596 )
597 {
598  const label nProcs = UPstream::nProcs(comm);
599  datas.setSize(nProcs);
600 
601  char* data0Ptr = reinterpret_cast<char*>(datas.begin());
602 
603  List<int> recvOffsets;
604  List<int> recvSizes;
605  if (UPstream::master(comm))
606  {
607  recvOffsets.setSize(nProcs);
608  forAll(recvOffsets, proci)
609  {
610  // Note: truncating long int to int since UPstream::gather limited
611  // to ints
612  recvOffsets[proci] =
613  int(reinterpret_cast<char*>(&datas[proci]) - data0Ptr);
614  }
615  recvSizes.setSize(nProcs, sizeof(label));
616  }
617 
619  (
620  reinterpret_cast<const char*>(&data),
621  sizeof(label),
622  data0Ptr,
623  recvSizes,
624  recvOffsets,
625  comm
626  );
627 }
628 
629 
631 (
632  const label comm,
633  const UList<char>& data,
634  const labelUList& recvSizes,
635 
636  const label startProc,
637  const label nProcs,
638 
639  List<int>& sliceOffsets,
640  List<char>& recvData
641 )
642 {
643  // Calculate master data
644  List<int> sliceSizes;
645  if (UPstream::master(comm))
646  {
647  const label numProcs = UPstream::nProcs(comm);
648 
649  sliceSizes.setSize(numProcs, 0);
650  sliceOffsets.setSize(numProcs+1, 0);
651 
652  int totalSize = 0;
653  label proci = startProc;
654  for (label i = 0; i < nProcs; i++)
655  {
656  sliceSizes[proci] = int(recvSizes[proci]);
657  sliceOffsets[proci] = totalSize;
658  totalSize += sliceSizes[proci];
659  proci++;
660  }
661  sliceOffsets[proci] = totalSize;
662  recvData.setSize(totalSize);
663  }
664 
665  int nSend = 0;
666  if
667  (
668  !UPstream::master(comm)
669  && (UPstream::myProcNo(comm) >= startProc)
670  && (UPstream::myProcNo(comm) < startProc+nProcs)
671  )
672  {
673  // Note: UPstream::gather limited to int
674  nSend = int(data.byteSize());
675  }
676 
678  (
679  data.begin(),
680  nSend,
681 
682  recvData.begin(),
683  sliceSizes,
684  sliceOffsets,
685  comm
686  );
687 }
688 
689 
691 (
692  const label comm,
693  const off_t maxBufferSize,
694  const labelUList& recvSizes,
695  const label startProci
696 )
697 {
698  const label nProcs = UPstream::nProcs(comm);
699 
700  label nSendProcs = -1;
701  if (UPstream::master(comm))
702  {
703  off_t totalSize = recvSizes[startProci];
704  label proci = startProci+1;
705  while (proci < nProcs && (totalSize+recvSizes[proci] < maxBufferSize))
706  {
707  totalSize += recvSizes[proci];
708  proci++;
709  }
710 
711  nSendProcs = proci-startProci;
712  }
713 
714  // Scatter nSendProcs
715  label n;
717  (
718  reinterpret_cast<const char*>(&nSendProcs),
719  List<int>(nProcs, sizeof(nSendProcs)),
720  List<int>(nProcs, 0),
721  reinterpret_cast<char*>(&n),
722  sizeof(n),
723  comm
724  );
725 
726  return n;
727 }
728 
729 
731 (
732  const label comm,
733  autoPtr<OSstream>& osPtr,
734  List<std::streamoff>& start,
735  const UList<char>& data,
736 
737  const labelUList& recvSizes,
738  const PtrList<SubList<char>>& slaveData,
739 
740  const UPstream::commsTypes commsType,
741  const bool syncReturnState
742 )
743 {
744  if (debug)
745  {
746  Pout<< "decomposedBlockData::writeBlocks:"
747  << " stream:" << (osPtr.valid() ? osPtr().name() : "invalid")
748  << " data:" << data.size()
749  << " (master only) slaveData:" << slaveData.size()
750  << " commsType:" << Pstream::commsTypeNames[commsType] << endl;
751  }
752 
753  const label nProcs = UPstream::nProcs(comm);
754 
755  bool ok = true;
756 
757  if (slaveData.size())
758  {
759  // Already have gathered the slave data. communicator only used to
760  // check who is the master
761 
762  if (UPstream::master(comm))
763  {
764  OSstream& os = osPtr();
765 
766  start.setSize(nProcs);
767 
768  // Write master data
769  {
770  os << nl << "// Processor" << UPstream::masterNo() << nl;
771  start[UPstream::masterNo()] = os.stdStream().tellp();
772  os << data;
773  }
774 
775  // Write slaves
776 
777  label slaveOffset = 0;
778 
779  for (label proci = 1; proci < nProcs; proci++)
780  {
781  os << nl << nl << "// Processor" << proci << nl;
782  start[proci] = os.stdStream().tellp();
783 
784  os << slaveData[proci];
785  slaveOffset += recvSizes[proci];
786  }
787 
788  ok = os.good();
789  }
790  }
791  else if (commsType == UPstream::commsTypes::scheduled)
792  {
793  if (UPstream::master(comm))
794  {
795  start.setSize(nProcs);
796 
797  OSstream& os = osPtr();
798 
799  // Write master data
800  {
801  os << nl << "// Processor" << UPstream::masterNo() << nl;
802  start[UPstream::masterNo()] = os.stdStream().tellp();
803  os << data;
804  }
805  // Write slaves
806  List<char> elems;
807  for (label proci = 1; proci < nProcs; proci++)
808  {
809  elems.setSize(recvSizes[proci]);
811  (
813  proci,
814  elems.begin(),
815  elems.size(),
817  comm
818  );
819 
820  os << nl << nl << "// Processor" << proci << nl;
821  start[proci] = os.stdStream().tellp();
822  os << elems;
823  }
824 
825  ok = os.good();
826  }
827  else
828  {
830  (
833  data.begin(),
834  data.byteSize(),
836  comm
837  );
838  }
839  }
840  else
841  {
842  // Write master data
843  if (UPstream::master(comm))
844  {
845  start.setSize(nProcs);
846 
847  OSstream& os = osPtr();
848 
849  os << nl << "// Processor" << UPstream::masterNo() << nl;
850  start[UPstream::masterNo()] = os.stdStream().tellp();
851  os << data;
852  }
853 
854 
855  // Find out how many processor can be received into
856  // maxMasterFileBufferSize
857 
858  // Starting slave processor and number of processors
859  label startProc = 1;
860  label nSendProcs = nProcs-1;
861 
862  while (nSendProcs > 0 && startProc < nProcs)
863  {
864  nSendProcs = calcNumProcs
865  (
866  comm,
867  off_t
868  (
870  maxMasterFileBufferSize
871  ),
872  recvSizes,
873  startProc
874  );
875 
876  if (nSendProcs == 0)
877  {
878  break;
879  }
880 
881 
882  // Gather data from (a slice of) the slaves
883  List<int> sliceOffsets;
884  List<char> recvData;
885  gatherSlaveData
886  (
887  comm,
888  data,
889  recvSizes,
890 
891  startProc, // startProc,
892  nSendProcs, // nProcs,
893 
894  sliceOffsets,
895  recvData
896  );
897 
898  if (UPstream::master(comm))
899  {
900  OSstream& os = osPtr();
901 
902  // Write slaves
903  for
904  (
905  label proci = startProc;
906  proci < startProc+nSendProcs;
907  proci++
908  )
909  {
910  os << nl << nl << "// Processor" << proci << nl;
911  start[proci] = os.stdStream().tellp();
912 
913  os <<
915  (
916  recvData,
917  sliceOffsets[proci+1]-sliceOffsets[proci],
918  sliceOffsets[proci]
919  );
920  }
921  }
922 
923  startProc += nSendProcs;
924  }
925 
926  if (UPstream::master(comm))
927  {
928  ok = osPtr().good();
929  }
930  }
931 
932  if (syncReturnState)
933  {
934  //- Enable to get synchronised error checking. Is the one that keeps
935  // slaves as slow as the master (which does all the writing)
936  Pstream::scatter(ok, Pstream::msgType(), comm);
937  }
938 
939  return ok;
940 }
941 
942 
944 {
945  autoPtr<ISstream> isPtr;
946  fileName objPath(fileHandler().filePath(false, *this, word::null));
947  if (UPstream::master(comm_))
948  {
949  isPtr.reset(new IFstream(objPath));
950  IOobject::readHeader(isPtr());
951  }
952 
953  List<char>& data = *this;
954  return readBlocks(comm_, isPtr, data, commsType_);
955 }
956 
957 
959 {
960  const List<char>& data = *this;
961 
962  string str
963  (
964  reinterpret_cast<const char*>(data.cbegin()),
965  data.byteSize()
966  );
967 
968  IOobject io(*this);
969  if (Pstream::master(comm_))
970  {
971  IStringStream is(name(), str);
972  io.readHeader(is);
973  }
974 
975  // Scatter header information
976 
977  // version
978  string versionString(os.version().str());
979  Pstream::scatter(versionString, Pstream::msgType(), comm_);
980 
981  // stream
982  string formatString;
983  {
984  OStringStream os;
985  os << os.format();
986  formatString = os.str();
987  Pstream::scatter(formatString, Pstream::msgType(), comm_);
988  }
989 
990  // word masterName(name());
991  // Pstream::scatter(masterName, Pstream::msgType(), comm_);
992 
993  Pstream::scatter(io.headerClassName(), Pstream::msgType(), comm_);
994  Pstream::scatter(io.note(), Pstream::msgType(), comm_);
995  // Pstream::scatter(io.instance(), Pstream::msgType(), comm);
996  // Pstream::scatter(io.local(), Pstream::msgType(), comm);
997 
998  fileName masterLocation(instance()/db().dbDir()/local());
999  Pstream::scatter(masterLocation, Pstream::msgType(), comm_);
1000 
1001  if (!Pstream::master(comm_))
1002  {
1003  writeHeader
1004  (
1005  os,
1006  IOstream::versionNumber(IStringStream(versionString)()),
1007  IOstream::formatEnum(formatString),
1008  io.headerClassName(),
1009  io.note(),
1010  masterLocation,
1011  name()
1012  );
1013  }
1014 
1015  os.writeQuoted(str, false);
1016 
1017  if (!Pstream::master(comm_))
1018  {
1020  }
1021 
1022  return os.good();
1023 }
1024 
1025 
1031  const bool valid
1032 ) const
1033 {
1034  autoPtr<OSstream> osPtr;
1035  if (UPstream::master(comm_))
1036  {
1037  // Note: always write binary. These are strings so readable
1038  // anyway. They have already be tokenised on the sending side.
1039  osPtr.reset(new OFstream(objectPath(), IOstream::BINARY, ver, cmp));
1040  IOobject::writeHeader(osPtr());
1041  }
1042 
1043  labelList recvSizes;
1044  gather(comm_, label(this->byteSize()), recvSizes);
1045 
1046  List<std::streamoff> start;
1047  PtrList<SubList<char>> slaveData; // dummy slave data
1048  return writeBlocks
1049  (
1050  comm_,
1051  osPtr,
1052  start,
1053  *this,
1054  recvSizes,
1055  slaveData,
1056  commsType_
1057  );
1058 }
1059 
1060 
1062 {
1063  label nBlocks = 0;
1064 
1065  IFstream is(fName);
1066  is.fatalCheck("decomposedBlockData::numBlocks(const fileName&)");
1067 
1068  if (!is.good())
1069  {
1070  return nBlocks;
1071  }
1072 
1073  // Skip header
1074  token firstToken(is);
1075 
1076  if
1077  (
1078  is.good()
1079  && firstToken.isWord()
1080  && firstToken.wordToken() == "FoamFile"
1081  )
1082  {
1083  dictionary headerDict(is);
1084  is.version(headerDict.lookup("version"));
1085  is.format(headerDict.lookup("format"));
1086  }
1087 
1088  List<char> data;
1089  while (is.good())
1090  {
1091  token sizeToken(is);
1092  if (!sizeToken.isLabel())
1093  {
1094  return nBlocks;
1095  }
1096  is.putBack(sizeToken);
1097 
1098  is >> data;
1099  nBlocks++;
1100  }
1101 
1102  return nBlocks;
1103 }
1104 
1105 
1106 // ************************************************************************* //
bool isLabel() const
Definition: tokenI.H:271
decomposedBlockData(const label comm, const IOobject &, const UPstream::commsTypes=UPstream::commsTypes::scheduled)
Construct given an IOobject.
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
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:428
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:415
const word & name() const
Return name.
Definition: IOobject.H:297
A class for handling file names.
Definition: fileName.H:69
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:137
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:327
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:427
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:421
static int & msgType()
Message tag of standard messages.
Definition: UPstream.H:474
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:315
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
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:61
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
virtual bool writeObject(IOstream::streamFormat, IOstream::versionNumber, IOstream::compressionType, const bool valid) const
Write using given format, version and compression.
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:181
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:409
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:63
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:65
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:359
const word & headerClassName() const
Return name of the class name read from header.
Definition: IOobject.H:303
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
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Definition: List.C:342
Namespace for OpenFOAM.
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:576
static void gather(const label comm, const label data, labelList &datas)
Helper: gather single label. Note: using native Pstream.
IOerror FatalIOError