syncObjects.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) 2020-2021 OpenCFD Ltd.
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 "syncObjects.H"
29 #include "Time.H"
30 #include "polyMesh.H"
32 #include "objectRegistry.H"
33 #include "mappedPatchBase.H"
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
39 namespace functionObjects
40 {
41  defineTypeNameAndDebug(syncObjects, 0);
43  (
44  functionObject,
45  syncObjects,
46  dictionary
47  );
48 }
49 }
50 
51 
52 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
53 
54 Foam::functionObjects::syncObjects::syncObjects
55 (
56  const word& name,
57  const Time& runTime,
58  const dictionary& dict
59 )
60 :
62  obr_(runTime)
63 {
64  read(dict);
65 }
66 
67 
68 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
69 
71 {
72  if (debug)
73  {
74  Pout<< type() << " : sync()"
75  << " root:" << root_ << endl;
76  }
77 
78  const label oldWarnComm = UPstream::warnComm;
80 
81  if (!Pstream::parRun())
82  {
83  return;
84  }
85 
86 
87  // Send my data to all other processors
88  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
89 
90  // Note provision of explicit all-world communicator
91  PstreamBuffers pBufs
92  (
95  0
96  );
97 
98 
99  const label nProcs = Pstream::nProcs(pBufs.comm());
100  for (label proci = 0; proci < nProcs; proci++)
101  {
102  // Get database to send
104  (
105  obr_,
106  mappedPatchBase::sendPath(root_, proci)
107  );
108 
109  // Pack into dictionary
110  dictionary sendDataDict;
111  mappedPatchBase::writeDict(sendObr, sendDataDict);
112 
113  if (debug & 2)
114  {
115  Pout<< "** to processor " << proci
116  << " sendObr:" << sendObr.objectPath()
117  << " sending dictionary:" << sendDataDict << endl;
118  }
119  UOPstream os(proci, pBufs);
120  os << sendDataDict;
121  }
122 
123  // Start sending and receiving and block
124  pBufs.finishedSends();
125 
126  for (label proci = 0; proci < nProcs; proci++)
127  {
128  // Get database to receive data into
129  const objectRegistry& receiveObr = mappedPatchBase::subRegistry
130  (
131  obr_,
132  mappedPatchBase::receivePath(root_, proci)
133  );
134  UIPstream is(proci, pBufs);
135  const dictionary fromProcDict(is);
136  if (debug & 2)
137  {
138  Pout<< "** from processor " << proci
139  << " receiveObr:" << receiveObr.objectPath()
140  << " received dictionary:" << fromProcDict << endl;
141  }
143  (
144  fromProcDict,
145  const_cast<objectRegistry&>(receiveObr)
146  );
147  }
148 
149  //if (debug)
150  //{
151  // dictionary allDict;
152  // // Add send subdictionary
153  // dictionary& sendDict = allDict.subDictOrAdd("send");
154  // mappedPatchBase::writeDict
155  // (
156  // mappedPatchBase::subRegistry(obr_, "send"),
157  // sendDict
158  // );
159  // // Add receive subdictionary
160  // dictionary& receiveDict = allDict.subDictOrAdd("receive");
161  // mappedPatchBase::writeDict
162  // (
163  // mappedPatchBase::subRegistry(obr_, "receive"),
164  // receiveDict
165  // );
166  // Pout<< type() << " : after synchronisation:" << allDict << endl;
167  //}
168 
169  UPstream::warnComm = oldWarnComm;
170 }
171 
172 
174 {
175  if (debug)
176  {
177  Pout<< type() << " : read(const dictionary&)" << endl;
178  }
179 
181  root_ = dict.getOrDefault<fileName>("root", fileName::null);
182 
183  if (debug)
184  {
185  Pout<< type() << " : root:" << root_ << endl;
186  }
187 
188  // Make sure that at startup we're doing a sync (execute below only gets
189  // called at end of timeloop)
190  sync();
191 
192  return true;
193 }
194 
195 
197 {
198  if (debug)
199  {
200  Pout<< type() << " : execute()" << endl;
201  }
202 
203  sync();
204 
205  return true;
206 }
207 
208 
210 {
211  if (debug)
212  {
213  Pout<< type() << " : write()" << endl;
214  }
215 
216  return true;
217 }
218 
219 
220 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::functionObjects::syncObjects::read
virtual bool read(const dictionary &)
Read the syncObjects data.
Definition: syncObjects.C:173
Foam::UPstream::warnComm
static label warnComm
Debugging: warn for use of any communicator differing from warnComm.
Definition: UPstream.H:296
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::mappedPatchBase::sendPath
static fileName sendPath(const fileName &root, const label proci)
Helper: return path to store data to be sent to processor i.
Definition: mappedPatchBase.C:1828
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::mappedPatchBase::readDict
static void readDict(const dictionary &d, objectRegistry &obr)
(recursively) construct and register IOFields from dictionary
Definition: mappedPatchBase.C:1901
Foam::mappedPatchBase::subRegistry
static const objectRegistry & subRegistry(const objectRegistry &obr, const wordList &names, const label index)
Definition: mappedPatchBase.C:1195
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
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:73
Foam::PstreamBuffers
Buffers for inter-processor communications streams (UOPstream, UIPstream).
Definition: PstreamBuffers.H:88
objectRegistry.H
Foam::mappedPatchBase::writeDict
static void writeDict(const objectRegistry &obr, dictionary &dict)
Convert objectRegistry contents into dictionary.
Definition: mappedPatchBase.C:1862
Foam::functionObjects::syncObjects::sync
void sync()
Do all: synchronise all IOFields and objectRegistry.
Definition: syncObjects.C:70
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::Pout
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
polyMesh.H
Foam::functionObject
Abstract base-class for Time/database function objects.
Definition: functionObject.H:332
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::blockMeshTools::read
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:57
syncObjects.H
Foam::functionObject::read
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
Definition: functionObject.C:163
Foam::PstreamBuffers::finishedSends
void finishedSends(const bool block=true)
Mark all sends as having been done.
Definition: PstreamBuffers.C:73
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
Foam::mappedPatchBase::receivePath
static fileName receivePath(const fileName &root, const label proci)
Definition: mappedPatchBase.C:1845
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::fileName::null
static const fileName null
An empty fileName.
Definition: fileName.H:101
Time.H
Foam::UPstream::commsTypes::nonBlocking
Foam::UPstream::msgType
static int & msgType() noexcept
Message tag of standard messages.
Definition: UPstream.H:540
Foam::functionObject::debug
static int debug
Flag to execute debug content.
Definition: functionObject.H:367
Foam::functionObjects::addToRunTimeSelectionTable
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
Foam::functionObject::type
virtual const word & type() const =0
Runtime type information.
Foam::UPstream::parRun
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:433
Foam::PstreamBuffers::comm
label comm() const noexcept
Communicator.
Definition: PstreamBuffers.H:147
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::functionObjects::defineTypeNameAndDebug
defineTypeNameAndDebug(ObukhovLength, 0)
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::UIPstream
Input inter-processor communications stream operating on external buffer.
Definition: UIPstream.H:56
Foam::IOobject::objectPath
fileName objectPath() const
The complete path + object name.
Definition: IOobjectI.H:214
Foam::functionObjects::syncObjects::execute
virtual bool execute()
Do nothing.
Definition: syncObjects.C:196
Foam::UPstream::nProcs
static label nProcs(const label communicator=worldComm)
Number of processes in parallel run, and 1 for serial run.
Definition: UPstream.H:445
Foam::functionObjects::syncObjects::write
virtual bool write()
Write the registered objects.
Definition: syncObjects.C:209
mappedPatchBase.H