UPstream.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2014 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 Class
25  Foam::UPstream
26 
27 Description
28  Inter-processor communications stream
29 
30 SourceFiles
31  UPstream.C
32  UPstreamCommsStruct.C
33  gatherScatter.C
34  combineGatherScatter.C
35  gatherScatterList.C
36 
37 \*---------------------------------------------------------------------------*/
38 
39 #ifndef UPstream_H
40 #define UPstream_H
41 
42 #include "labelList.H"
43 #include "DynamicList.H"
44 #include "HashTable.H"
45 #include "string.H"
46 #include "NamedEnum.H"
47 #include "ListOps.H"
48 #include "LIFOStack.H"
49 
50 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
51 
52 namespace Foam
53 {
54 
55 /*---------------------------------------------------------------------------*\
56  Class UPstream Declaration
57 \*---------------------------------------------------------------------------*/
58 
59 class UPstream
60 {
61 
62 public:
63 
64  //- Types of communications
65  enum commsTypes
66  {
70  };
71 
73 
74  // Public classes
75 
76  //- Structure for communicating between processors
77  class commsStruct
78  {
79  // Private data
80 
81  //- procID of above processor
82  label above_;
83 
84  //- procIDs of processors directly below me
86 
87  //- procIDs of all processors below (so not just directly below)
89 
90  //- procIDs of all processors not below. (inverse set of
91  // allBelow_ and minus myProcNo)
93 
94 
95  public:
96 
97  // Constructors
98 
99  //- Construct null
100  commsStruct();
101 
102  //- Construct from components
104  (
105  const label,
106  const labelList&,
107  const labelList&,
108  const labelList&
109  );
110 
111  //- Construct from components; construct allNotBelow_
113  (
114  const label nProcs,
115  const label myProcID,
116  const label,
117  const labelList&,
118  const labelList&
119  );
120 
121 
122  // Member Functions
123 
124  // Access
125 
126  label above() const
127  {
128  return above_;
129  }
130 
131  const labelList& below() const
132  {
133  return below_;
134  }
135 
136  const labelList& allBelow() const
137  {
138  return allBelow_;
139  }
140 
141  const labelList& allNotBelow() const
142  {
143  return allNotBelow_;
144  }
145 
146 
147  // Member operators
148 
149  bool operator==(const commsStruct&) const;
150 
151  bool operator!=(const commsStruct&) const;
152 
153 
154  // Ostream Operator
155 
156  friend Ostream& operator<<(Ostream&, const commsStruct&);
157  };
158 
159 
160  //- combineReduce operator for lists. Used for counting.
161  class listEq
162  {
163 
164  public:
165 
166  template<class T>
167  void operator()(T& x, const T& y) const
168  {
169  forAll(y, i)
170  {
171  if (y[i].size())
172  {
173  x[i] = y[i];
174  }
175  }
176  }
177  };
178 
179 
180 private:
181 
182  // Private data
183 
184  static bool parRun_;
185  static int msgType_;
186 
187  // Communicator specific data
188 
193 
196 
197 
198  // Private Member Functions
199 
200  //- Calculate linear communication schedule
202 
203  //- Calculate tree communication schedule
205 
206  //- Helper function for tree communication schedule determination
207  // Collects all processorIDs below a processor
208  static void collectReceives
209  (
210  const label procID,
211  const List<DynamicList<label> >& receives,
212  DynamicList<label>& allReceives
213  );
214 
215  //- Allocate a communicator with index
216  static void allocatePstreamCommunicator
217  (
218  const label parentIndex,
219  const label index
220  );
221 
222  //- Free a communicator
223  static void freePstreamCommunicator
224  (
225  const label index
226  );
227 
228 protected:
229 
230  // Protected data
231 
232  //- Communications type of this stream
234 
235 public:
236 
237  // Declare name of the class and its debug switch
238  ClassName("UPstream");
239 
240 
241  // Static data
242 
243  //- Should compact transfer be used in which floats replace doubles
244  // reducing the bandwidth requirement at the expense of some loss
245  // in accuracy
246  static bool floatTransfer;
247 
248  //- Number of processors at which the sum algorithm changes from linear
249  // to tree
250  static int nProcsSimpleSum;
251 
252  //- Default commsType
254 
255  //- Number of polling cycles in processor updates
256  static int nPollProcInterfaces;
257 
258  //- Default communicator (all processors)
259  static label worldComm;
260 
261  //- Debugging: warn for use of any communicator differing from warnComm
262  static label warnComm;
263 
264 
265  // Constructors
266 
267  //- Construct given optional buffer size
269  :
271  {}
272 
273 
274  // Member functions
275 
276  //- Allocate a new communicator
278  (
279  const label parent,
280  const labelList& subRanks,
281  const bool doPstream = true
282  );
283 
284  //- Free a previously allocated communicator
285  static void freeCommunicator
286  (
287  const label communicator,
288  const bool doPstream = true
289  );
290 
291  //- Free all communicators
292  static void freeCommunicators(const bool doPstream);
293 
294  //- Helper class for allocating/freeing communicators
295  class communicator
296  {
297  label comm_;
298 
299  //- Disallow copy and assignment
300  communicator(const communicator&);
301  void operator=(const communicator&);
302 
303  public:
304 
306  (
307  const label parent,
308  const labelList& subRanks,
309  const bool doPstream
310  )
311  :
312  comm_(allocateCommunicator(parent, subRanks, doPstream))
313  {}
314 
315  ~communicator()
316  {
318  }
319 
320  operator label() const
321  {
322  return comm_;
323  }
324  };
325 
326  //- Return physical processor number (i.e. processor number in
327  // worldComm) given communicator and procssor
328  static int baseProcNo(const label myComm, const int procID);
329 
330  //- Return processor number in communicator (given physical processor
331  // number) (= reverse of baseProcNo)
332  static label procNo(const label comm, const int baseProcID);
333 
334  //- Return processor number in communicator (given processor number
335  // and communicator)
336  static label procNo
337  (
338  const label myComm,
339  const label currentComm,
340  const int currentProcID
341  );
342 
343  //- Add the valid option this type of communications library
344  // adds/requires on the command line
345  static void addValidParOptions(HashTable<string>& validParOptions);
346 
347  //- Initialisation function called from main
348  // Spawns slave processes and initialises inter-communication
349  static bool init(int& argc, char**& argv);
350 
351  // Non-blocking comms
352 
353  //- Get number of outstanding requests
354  static label nRequests();
355 
356  //- Truncate number of outstanding requests
357  static void resetRequests(const label sz);
358 
359  //- Wait until all requests (from start onwards) have finished.
360  static void waitRequests(const label start = 0);
361 
362  //- Wait until request i has finished.
363  static void waitRequest(const label i);
364 
365  //- Non-blocking comms: has request i finished?
366  static bool finishedRequest(const label i);
367 
368  static int allocateTag(const char*);
369 
370  static int allocateTag(const word&);
371 
372  static void freeTag(const char*, const int tag);
373 
374  static void freeTag(const word&, const int tag);
375 
376 
377  //- Is this a parallel run?
378  static bool& parRun()
379  {
380  return parRun_;
381  }
382 
383  //- Set data for parallel running. Special case nProcs=0 to switch off
384  // parallel running
385  static void setParRun(const label nProcs);
386 
387  //- Number of processes in parallel run
388  static label nProcs(const label communicator = 0)
389  {
390  return procIDs_[communicator].size();
391  }
392 
393  //- Process index of the master
394  static int masterNo()
395  {
396  return 0;
397  }
398 
399  //- Am I the master process
400  static bool master(const label communicator = 0)
401  {
402  return myProcNo_[communicator] == masterNo();
403  }
404 
405  //- Number of this process (starting from masterNo() = 0)
406  static int myProcNo(const label communicator = 0)
407  {
408  return myProcNo_[communicator];
409  }
410 
411  static label parent(const label communicator)
412  {
414  }
415 
416  //- Process ID of given process index
418  {
419  return procIDs_[communicator];
420  }
421 
422  //- Process index of first slave
423  static int firstSlave()
424  {
425  return 1;
426  }
427 
428  //- Process index of last slave
429  static int lastSlave(const label communicator = 0)
430  {
431  return nProcs(communicator) - 1;
432  }
433 
434  //- Communication schedule for linear all-to-master (proc 0)
436  (
437  const label communicator = 0
438  )
439  {
441  }
442 
443  //- Communication schedule for tree all-to-master (proc 0)
445  (
446  const label communicator = 0
447  )
448  {
450  }
451 
452  //- Message tag of standard messages
453  static int& msgType()
454  {
455  return msgType_;
456  }
457 
458 
459  //- Get the communications type of the stream
460  commsTypes commsType() const
461  {
462  return commsType_;
463  }
464 
465  //- Set the communications type of the stream
467  {
468  commsTypes oldCommsType = commsType_;
469  commsType_ = ct;
470  return oldCommsType;
471  }
472 
473 
474  //- Exit program
475  static void exit(int errnum = 1);
476 
477  //- Abort program
478  static void abort();
479 
480 
481 };
482 
483 
484 Ostream& operator<<(Ostream&, const UPstream::commsStruct&);
485 
486 
487 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
488 
489 } // End namespace Foam
490 
491 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
492 
493 #endif
494 
495 // ************************************************************************* //
Foam::UPstream::allocateTag
static int allocateTag(const char *)
Definition: UPstream.C:547
Foam::UPstream::procIDs_
static DynamicList< List< int > > procIDs_
Definition: UPstream.H:190
Foam::UPstream::warnComm
static label warnComm
Debugging: warn for use of any communicator differing from warnComm.
Definition: UPstream.H:261
Foam::UPstream::resetRequests
static void resetRequests(const label sz)
Truncate number of outstanding requests.
Definition: UPstream.C:102
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
HashTable.H
Foam::UPstream::communicator::~communicator
~communicator()
Definition: UPstream.H:314
Foam::UPstream::commsStruct::commsStruct
commsStruct()
Construct null.
Definition: UPstreamCommsStruct.C:31
Foam::UPstream::baseProcNo
static int baseProcNo(const label myComm, const int procID)
Return physical processor number (i.e. processor number in.
Definition: UPstream.C:353
Foam::UPstream::scheduled
@ scheduled
Definition: UPstream.H:67
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::DynamicList< int >
Foam::UPstream::exit
static void exit(int errnum=1)
Exit program.
Definition: UPstream.C:46
Foam::UPstream::communicator::communicator
communicator(const communicator &)
Disallow copy and assignment.
Foam::UPstream::msgType_
static int msgType_
Definition: UPstream.H:184
Foam::UPstream::nProcs
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:387
NamedEnum.H
Foam::UPstream::parRun
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:377
Foam::UPstream::linearCommunication_
static DynamicList< List< commsStruct > > linearCommunication_
Definition: UPstream.H:193
Foam::UPstream::commsStruct::operator<<
friend Ostream & operator<<(Ostream &, const commsStruct &)
Foam::UPstream::commsTypeNames
static const NamedEnum< commsTypes, 3 > commsTypeNames
Definition: UPstream.H:71
Foam::UPstream::freeComms_
static LIFOStack< label > freeComms_
Definition: UPstream.H:188
Foam::UPstream::waitRequests
static void waitRequests(const label start=0)
Wait until all requests (from start onwards) have finished.
Definition: UPstream.C:106
string.H
Foam::UPstream::abort
static void abort()
Abort program.
Definition: UPstream.C:52
Foam::UPstream::freePstreamCommunicator
static void freePstreamCommunicator(const label index)
Free a communicator.
Definition: UPstream.C:92
Foam::UPstream::defaultCommsType
static commsTypes defaultCommsType
Default commsType.
Definition: UPstream.H:252
Foam::UPstream::commsStruct::above_
label above_
procID of above processor
Definition: UPstream.H:81
Foam::UPstream::UPstream
UPstream(const commsTypes commsType)
Construct given optional buffer size.
Definition: UPstream.H:267
Foam::UPstream::communicator::operator=
void operator=(const communicator &)
Foam::UPstream::allocatePstreamCommunicator
static void allocatePstreamCommunicator(const label parentIndex, const label index)
Allocate a communicator with index.
Definition: UPstream.C:85
Foam::UPstream::commsStruct::operator==
bool operator==(const commsStruct &) const
Definition: UPstreamCommsStruct.C:93
Foam::UPstream::commsStruct::below_
labelList below_
procIDs of processors directly below me
Definition: UPstream.H:84
Foam::UPstream::commsStruct::allBelow
const labelList & allBelow() const
Definition: UPstream.H:135
Foam::UPstream::blocking
@ blocking
Definition: UPstream.H:66
Foam::UPstream::listEq::operator()
void operator()(T &x, const T &y) const
Definition: UPstream.H:166
Foam::UPstream::commsStruct::allBelow_
labelList allBelow_
procIDs of all processors below (so not just directly below)
Definition: UPstream.H:87
Foam::UPstream::allocateCommunicator
static label allocateCommunicator(const label parent, const labelList &subRanks, const bool doPstream=true)
Allocate a new communicator.
Definition: UPstream.C:248
Foam::UPstream::nonBlocking
@ nonBlocking
Definition: UPstream.H:68
Foam::UPstream::setParRun
static void setParRun(const label nProcs)
Set data for parallel running. Special case nProcs=0 to switch off.
Definition: UPstream.C:58
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:59
Foam::UPstream
Inter-processor communications stream.
Definition: UPstream.H:58
labelList.H
Foam::UPstream::lastSlave
static int lastSlave(const label communicator=0)
Process index of last slave.
Definition: UPstream.H:428
Foam::UPstream::floatTransfer
static bool floatTransfer
Should compact transfer be used in which floats replace doubles.
Definition: UPstream.H:245
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:55
Foam::UPstream::waitRequest
static void waitRequest(const label i)
Wait until request i has finished.
Definition: UPstream.C:110
Foam::UPstream::commsType
commsTypes commsType() const
Get the communications type of the stream.
Definition: UPstream.H:459
Foam::UPstream::commsStruct::allNotBelow_
labelList allNotBelow_
procIDs of all processors not below. (inverse set of
Definition: UPstream.H:91
Foam::UPstream::addValidParOptions
static void addValidParOptions(HashTable< string > &validParOptions)
Add the valid option this type of communications library.
Definition: UPstream.C:31
Foam::UPstream::procID
static List< int > & procID(label communicator)
Process ID of given process index.
Definition: UPstream.H:416
Foam::UPstream::freeCommunicator
static void freeCommunicator(const label communicator, const bool doPstream=true)
Free a previously allocated communicator.
Definition: UPstream.C:314
Foam::UPstream::masterNo
static int masterNo()
Process index of the master.
Definition: UPstream.H:393
Foam::operator<<
Ostream & operator<<(Ostream &, const edgeMesh &)
Definition: edgeMeshIO.C:130
Foam::UPstream::parRun_
static bool parRun_
Definition: UPstream.H:183
Foam::UPstream::commsStruct::allNotBelow
const labelList & allNotBelow() const
Definition: UPstream.H:140
Foam::UPstream::communicator::comm_
label comm_
Definition: UPstream.H:296
Foam::UPstream::firstSlave
static int firstSlave()
Process index of first slave.
Definition: UPstream.H:422
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::UPstream::commsStruct::above
label above() const
Definition: UPstream.H:125
Foam::UPstream::commsStruct
Structure for communicating between processors.
Definition: UPstream.H:76
Foam::UPstream::myProcNo
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:405
Foam::UPstream::master
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:399
Foam::UPstream::nProcsSimpleSum
static int nProcsSimpleSum
Number of processors at which the sum algorithm changes from linear.
Definition: UPstream.H:249
Foam::HashTable
An STL-conforming hash table.
Definition: HashTable.H:61
Foam::UPstream::listEq
combineReduce operator for lists. Used for counting.
Definition: UPstream.H:160
Foam::UPstream::msgType
static int & msgType()
Message tag of standard messages.
Definition: UPstream.H:452
Foam::UPstream::commsTypes
commsTypes
Types of communications.
Definition: UPstream.H:64
Foam::UPstream::nRequests
static label nRequests()
Get number of outstanding requests.
Definition: UPstream.C:96
Foam::UPstream::collectReceives
static void collectReceives(const label procID, const List< DynamicList< label > > &receives, DynamicList< label > &allReceives)
Helper function for tree communication schedule determination.
Definition: UPstream.C:139
Foam::UPstream::commsType_
commsTypes commsType_
Communications type of this stream.
Definition: UPstream.H:232
Foam::UPstream::worldComm
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:258
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:59
Foam::UPstream::procNo
static label procNo(const label comm, const int baseProcID)
Return processor number in communicator (given physical processor.
Definition: UPstream.C:369
Foam::UPstream::ClassName
ClassName("UPstream")
Foam::UPstream::commsType
commsTypes commsType(const commsTypes ct)
Set the communications type of the stream.
Definition: UPstream.H:465
Foam::UPstream::finishedRequest
static bool finishedRequest(const label i)
Non-blocking comms: has request i finished?
Definition: UPstream.C:114
x
x
Definition: LISASMDCalcMethod2.H:52
Foam::UPstream::freeCommunicators
static void freeCommunicators(const bool doPstream)
Free all communicators.
Definition: UPstream.C:341
Foam::UPstream::calcLinearComm
static List< commsStruct > calcLinearComm(const label nProcs)
Calculate linear communication schedule.
Definition: UPstream.C:99
Foam::UPstream::myProcNo_
static DynamicList< int > myProcNo_
Definition: UPstream.H:189
DynamicList.H
ListOps.H
Various functions to operate on Lists.
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::UPstream::parent
static label parent(const label communicator)
Definition: UPstream.H:410
Foam::UPstream::communicator
Helper class for allocating/freeing communicators.
Definition: UPstream.H:294
Foam::UPstream::linearCommunication
static const List< commsStruct > & linearCommunication(const label communicator=0)
Communication schedule for linear all-to-master (proc 0)
Definition: UPstream.H:435
Foam::LIFOStack< label >
Foam::UPstream::parentCommunicator_
static DynamicList< label > parentCommunicator_
Definition: UPstream.H:191
Foam::UPstream::commsStruct::operator!=
bool operator!=(const commsStruct &) const
Definition: UPstreamCommsStruct.C:105
Foam::UPstream::calcTreeComm
static List< commsStruct > calcTreeComm(const label nProcs)
Calculate tree communication schedule.
Definition: UPstream.C:182
LIFOStack.H
Foam::NamedEnum< commsTypes, 3 >
Foam::UPstream::nPollProcInterfaces
static int nPollProcInterfaces
Number of polling cycles in processor updates.
Definition: UPstream.H:255
Foam::UPstream::commsStruct::below
const labelList & below() const
Definition: UPstream.H:130
Foam::UPstream::treeCommunication
static const List< commsStruct > & treeCommunication(const label communicator=0)
Communication schedule for tree all-to-master (proc 0)
Definition: UPstream.H:444
Foam::UPstream::init
static bool init(int &argc, char **&argv)
Initialisation function called from main.
Definition: UPstream.C:35
y
scalar y
Definition: LISASMDCalcMethod1.H:14
Foam::UPstream::treeCommunication_
static DynamicList< List< commsStruct > > treeCommunication_
Definition: UPstream.H:194
Foam::UPstream::freeTag
static void freeTag(const char *, const int tag)
Definition: UPstream.C:605