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-2017 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 class commsTypes
66  {
67  blocking,
68  scheduled,
69  nonBlocking
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
85  labelList below_;
86 
87  //- procIDs of all processors below (so not just directly below)
88  labelList allBelow_;
89 
90  //- procIDs of all processors not below. (inverse set of
91  // allBelow_ and minus myProcNo)
92  labelList allNotBelow_;
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
126  label above() const
127  {
128  return above_;
129  }
131  const labelList& below() const
132  {
133  return below_;
134  }
136  const labelList& allBelow() const
137  {
138  return allBelow_;
139  }
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  //- By default this is not a parallel run
185  static bool parRun_;
186 
187  //- Standard transfer message type
188  static int msgType_;
189 
190  // Communicator specific data
191 
192  //- Free communicators
193  static LIFOStack<label> freeComms_;
194 
195  //- My processor number
196  static DynamicList<int> myProcNo_;
197 
198  //- List of process IDs
199  static DynamicList<List<int>> procIDs_;
200 
201  //- Parent communicator
202  static DynamicList<label> parentCommunicator_;
203 
204  //- Linear communication schedule
205  static DynamicList<List<commsStruct>> linearCommunication_;
206 
207  //- Multi level communication schedule
208  static DynamicList<List<commsStruct>> treeCommunication_;
209 
210 
211  // Private Member Functions
212 
213  //- Set data for parallel running
214  static void setParRun(const label nProcs);
215 
216  //- Calculate linear communication schedule
217  static List<commsStruct> calcLinearComm(const label nProcs);
218 
219  //- Calculate tree communication schedule
220  static List<commsStruct> calcTreeComm(const label nProcs);
221 
222  //- Helper function for tree communication schedule determination
223  // Collects all processorIDs below a processor
224  static void collectReceives
225  (
226  const label procID,
227  const List<DynamicList<label>>& receives,
228  DynamicList<label>& allReceives
229  );
230 
231  //- Allocate a communicator with index
232  static void allocatePstreamCommunicator
233  (
234  const label parentIndex,
235  const label index
236  );
237 
238  //- Free a communicator
239  static void freePstreamCommunicator
240  (
241  const label index
242  );
243 
244 
245 protected:
246 
247  // Protected data
248 
249  //- Communications type of this stream
251 
252 public:
253 
254  // Declare name of the class and its debug switch
255  ClassName("UPstream");
256 
257 
258  // Static data
259 
260  //- Should compact transfer be used in which floats replace doubles
261  // reducing the bandwidth requirement at the expense of some loss
262  // in accuracy
263  static bool floatTransfer;
264 
265  //- Number of processors at which the sum algorithm changes from linear
266  // to tree
267  static int nProcsSimpleSum;
268 
269  //- Default commsType
271 
272  //- Number of polling cycles in processor updates
273  static int nPollProcInterfaces;
274 
275  //- Default communicator (all processors)
276  static label worldComm;
277 
278  //- Debugging: warn for use of any communicator differing from warnComm
279  static label warnComm;
280 
281 
282  // Constructors
283 
284  //- Construct given optional buffer size
286  :
287  commsType_(commsType)
288  {}
289 
290 
291  // Member functions
292 
293  //- Allocate a new communicator
295  (
296  const label parent,
297  const labelList& subRanks,
298  const bool doPstream = true
299  );
300 
301  //- Free a previously allocated communicator
302  static void freeCommunicator
303  (
304  const label communicator,
305  const bool doPstream = true
306  );
307 
308  //- Free all communicators
309  static void freeCommunicators(const bool doPstream);
310 
311  //- Helper class for allocating/freeing communicators
312  class communicator
313  {
314  label comm_;
315 
316  //- Disallow copy and assignment
317  communicator(const communicator&);
318  void operator=(const communicator&);
319 
320  public:
321 
322  communicator
323  (
324  const label parent,
325  const labelList& subRanks,
326  const bool doPstream
327  )
328  :
329  comm_(allocateCommunicator(parent, subRanks, doPstream))
330  {}
332  ~communicator()
333  {
334  freeCommunicator(comm_);
335  }
337  operator label() const
338  {
339  return comm_;
340  }
341  };
342 
343  //- Return physical processor number (i.e. processor number in
344  // worldComm) given communicator and procssor
345  static int baseProcNo(const label myComm, const int procID);
346 
347  //- Return processor number in communicator (given physical processor
348  // number) (= reverse of baseProcNo)
349  static label procNo(const label comm, const int baseProcID);
350 
351  //- Return processor number in communicator (given processor number
352  // and communicator)
353  static label procNo
354  (
355  const label myComm,
356  const label currentComm,
357  const int currentProcID
358  );
359 
360  //- Add the valid option this type of communications library
361  // adds/requires on the command line
362  static void addValidParOptions(HashTable<string>& validParOptions);
363 
364  //- Initialisation function called from main
365  // Spawns slave processes and initialises inter-communication
366  static bool init(int& argc, char**& argv);
367 
368  // Non-blocking comms
369 
370  //- Get number of outstanding requests
371  static label nRequests();
372 
373  //- Truncate number of outstanding requests
374  static void resetRequests(const label sz);
375 
376  //- Wait until all requests (from start onwards) have finished.
377  static void waitRequests(const label start = 0);
378 
379  //- Wait until request i has finished.
380  static void waitRequest(const label i);
381 
382  //- Non-blocking comms: has request i finished?
383  static bool finishedRequest(const label i);
384 
385  static int allocateTag(const char*);
386 
387  static int allocateTag(const word&);
388 
389  static void freeTag(const char*, const int tag);
390 
391  static void freeTag(const word&, const int tag);
392 
393 
394  //- Is this a parallel run?
395  static bool& parRun()
396  {
397  return parRun_;
398  }
399 
400  //- Number of processes in parallel run
401  static label nProcs(const label communicator = 0)
402  {
403  return procIDs_[communicator].size();
404  }
405 
406  //- Process index of the master
407  static int masterNo()
408  {
409  return 0;
410  }
411 
412  //- Am I the master process
413  static bool master(const label communicator = 0)
414  {
415  return myProcNo_[communicator] == masterNo();
416  }
417 
418  //- Number of this process (starting from masterNo() = 0)
419  static int myProcNo(const label communicator = 0)
420  {
421  return myProcNo_[communicator];
422  }
424  static label parent(const label communicator)
425  {
426  return parentCommunicator_(communicator);
427  }
428 
429  //- Process ID of given process index
430  static List<int>& procID(label communicator)
431  {
432  return procIDs_[communicator];
433  }
434 
435  //- Process index of first slave
436  static int firstSlave()
437  {
438  return 1;
439  }
440 
441  //- Process index of last slave
442  static int lastSlave(const label communicator = 0)
443  {
444  return nProcs(communicator) - 1;
445  }
446 
447  //- Communication schedule for linear all-to-master (proc 0)
449  (
450  const label communicator = 0
451  )
452  {
453  return linearCommunication_[communicator];
454  }
455 
456  //- Communication schedule for tree all-to-master (proc 0)
458  (
459  const label communicator = 0
460  )
461  {
462  return treeCommunication_[communicator];
463  }
464 
465  //- Message tag of standard messages
466  static int& msgType()
467  {
468  return msgType_;
469  }
470 
471 
472  //- Get the communications type of the stream
473  commsTypes commsType() const
474  {
475  return commsType_;
476  }
477 
478  //- Set the communications type of the stream
480  {
481  commsTypes oldCommsType = commsType_;
482  commsType_ = ct;
483  return oldCommsType;
484  }
485 
486 
487  //- Exit program
488  static void exit(int errnum = 1);
489 
490  //- Abort program
491  static void abort();
492 
493  //- Exchange label with all processors (in the communicator).
494  // sendData[proci] is the label to send to proci.
495  // After return recvData contains the data from the other processors.
496  static void allToAll
497  (
498  const labelUList& sendData,
499  labelUList& recvData,
500  const label communicator = 0
501  );
502 };
503 
504 
506 
507 
508 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
509 
510 } // End namespace Foam
511 
512 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
513 
514 #endif
515 
516 // ************************************************************************* //
static bool floatTransfer
Should compact transfer be used in which floats replace doubles.
Definition: UPstream.H:262
commsTypes commsType() const
Get the communications type of the stream.
Definition: UPstream.H:472
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
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:406
commsTypes
Types of communications.
Definition: UPstream.H:64
static int firstSlave()
Process index of first slave.
Definition: UPstream.H:435
commsTypes commsType_
Communications type of this stream.
Definition: UPstream.H:249
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:418
static int nProcsSimpleSum
Number of processors at which the sum algorithm changes from linear.
Definition: UPstream.H:266
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:412
static bool finishedRequest(const label i)
Non-blocking comms: has request i finished?
Definition: UPstream.C:125
static label procNo(const label comm, const int baseProcID)
Return processor number in communicator (given physical processor.
Definition: UPstream.C:371
static label nRequests()
Get number of outstanding requests.
Definition: UPstream.C:107
static int & msgType()
Message tag of standard messages.
Definition: UPstream.H:465
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:275
static const List< commsStruct > & linearCommunication(const label communicator=0)
Communication schedule for linear all-to-master (proc 0)
Definition: UPstream.H:448
ClassName("UPstream")
Various functions to operate on Lists.
static void freeCommunicators(const bool doPstream)
Free all communicators.
Definition: UPstream.C:343
scalar y
static int nPollProcInterfaces
Number of polling cycles in processor updates.
Definition: UPstream.H:272
static label parent(const label communicator)
Definition: UPstream.H:423
static int allocateTag(const char *)
Definition: UPstream.C:614
static void freeTag(const char *, const int tag)
Definition: UPstream.C:672
tmp< fvMatrix< Type > > operator==(const fvMatrix< Type > &, const fvMatrix< Type > &)
A class for handling words, derived from string.
Definition: word.H:59
static void resetRequests(const label sz)
Truncate number of outstanding requests.
Definition: UPstream.C:113
static bool init(int &argc, char **&argv)
Initialisation function called from main.
Definition: UPstream.C:35
static const List< commsStruct > & treeCommunication(const label communicator=0)
Communication schedule for tree all-to-master (proc 0)
Definition: UPstream.H:457
combineReduce operator for lists. Used for counting.
Definition: UPstream.H:160
An STL-conforming hash table.
Definition: HashTable.H:62
static label warnComm
Debugging: warn for use of any communicator differing from warnComm.
Definition: UPstream.H:278
Structure for communicating between processors.
Definition: UPstream.H:76
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
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
static void exit(int errnum=1)
Exit program.
Definition: UPstream.C:46
static int baseProcNo(const label myComm, const int procID)
Return physical processor number (i.e. processor number in.
Definition: UPstream.C:355
static const NamedEnum< commsTypes, 3 > commsTypeNames
Definition: UPstream.H:71
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
static void waitRequests(const label start=0)
Wait until all requests (from start onwards) have finished.
Definition: UPstream.C:117
static void abort()
Abort program.
Definition: UPstream.C:52
static commsTypes defaultCommsType
Default commsType.
Definition: UPstream.H:269
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:394
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:400
UPstream(const commsTypes commsType)
Construct given optional buffer size.
Definition: UPstream.H:284
Ostream & operator<<(Ostream &, const ensightPart &)
static void allToAll(const labelUList &sendData, labelUList &recvData, const label communicator=0)
Exchange label with all processors (in the communicator).
Definition: UPstream.C:85
static label allocateCommunicator(const label parent, const labelList &subRanks, const bool doPstream=true)
Allocate a new communicator.
Definition: UPstream.C:250
bool operator!=(const particle &, const particle &)
Definition: particle.C:1106
static void waitRequest(const label i)
Wait until request i has finished.
Definition: UPstream.C:121
Inter-processor communications stream.
Definition: UPstream.H:58
static List< int > & procID(label communicator)
Process ID of given process index.
Definition: UPstream.H:429
static void addValidParOptions(HashTable< string > &validParOptions)
Add the valid option this type of communications library.
Definition: UPstream.C:31
static void freeCommunicator(const label communicator, const bool doPstream=true)
Free a previously allocated communicator.
Definition: UPstream.C:316
Namespace for OpenFOAM.
static int lastSlave(const label communicator=0)
Process index of last slave.
Definition: UPstream.H:441