distributionMapBase.H
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) 2015-2022 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::distributionMapBase
26 
27 Description
28  Class containing processor-to-processor mapping information.
29 
30  We store mapping from the bits-to-send to the complete starting list
31  (subXXXMap) and from the received bits to their location in the new
32  list (constructXXXMap).
33 
34  Schedule is a list of processor pairs (one send, one receive. One of
35  them will be myself) which forms a scheduled (i.e. non-buffered) exchange.
36  See distribute on how to use it.
37  Note2: number of items sent on one processor have to equal the number
38  of items received on the other processor.
39 
40  To aid constructing these maps there are the constructors from global
41  numbering, either with or without transforms.
42 
43  Constructors using compact numbering: layout is
44  - all my own elements first (whether used or not)
45  - followed by used-only remote elements sorted by remote processor.
46  So e.g 4 procs and on proc 1 the compact
47  table will first have all globalIndex.localSize() elements from proc1
48  followed by used-only elements of proc0, proc2, proc3.
49  The constructed distributionMapBase sends the local elements from and
50  receives the remote elements into their compact position.
51  compactMap[proci] is the position of elements from proci in the compact
52  map. compactMap[myProcNo()] is empty since trivial addressing.
53 
54  It rewrites the input global indices into indices into the constructed
55  data.
56 
57  When constructing from components optionally a 'flip' on
58  the maps can be specified. This will interpret the map
59  values as index+flip, similar to e.g. faceProcAddressing. The flip
60  will only be applied to fieldTypes (scalar, vector, .. triad)
61 
62 SourceFiles
63  distributionMapBase.C
64  distributionMapBaseTemplates.C
65 
66 \*---------------------------------------------------------------------------*/
67 
68 #ifndef distributionMapBase_H
69 #define distributionMapBase_H
70 
71 #include "labelList.H"
72 #include "labelPair.H"
73 #include "Pstream.H"
74 #include "boolList.H"
75 #include "Map.H"
76 
77 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
78 
79 namespace Foam
80 {
81 
82 class globalIndex;
83 class PstreamBuffers;
84 
85 // Forward declaration of friend functions and operators
86 
87 class distributionMapBase;
88 
89 Istream& operator>>(Istream&, distributionMapBase&);
90 Ostream& operator<<(Ostream&, const distributionMapBase&);
91 
92 
93 /*---------------------------------------------------------------------------*\
94  Class distributionMapBase Declaration
95 \*---------------------------------------------------------------------------*/
96 
98 {
99 protected:
100 
101  // Protected data
102 
103  //- Size of reconstructed data
105 
106  //- Maps from subsetted data back to original data
108 
109  //- Maps from subsetted data to new reconstructed data
111 
112  //- Whether subMap includes flip or not
113  bool subHasFlip_;
114 
115  //- Whether constructMap includes flip or not
116  bool constructHasFlip_;
117 
118 
119  //- Schedule
121 
122 
123  // Private Member Functions
124 
125  static void checkReceivedSize
126  (
127  const label proci,
128  const label expectedSize,
129  const label receivedSize
130  );
131 
132  //- Construct per processor compact addressing of the global elements
133  // needed. The ones from the local processor are not included since
134  // these are always all needed.
136  (
137  const globalIndex& globalNumbering,
138  const labelList& elements,
139  List<Map<label>>& compactMap
140  ) const;
141 
143  (
144  const globalIndex& globalNumbering,
145  const labelListList& elements,
146  List<Map<label>>& compactMap
147  ) const;
148 
149  void exchangeAddressing
150  (
151  const int tag,
152  const globalIndex& globalNumbering,
153  labelList& elements,
154  List<Map<label>>& compactMap,
155  labelList& compactStart
156  );
157  void exchangeAddressing
158  (
159  const int tag,
160  const globalIndex& globalNumbering,
161  labelListList& elements,
162  List<Map<label>>& compactMap,
163  labelList& compactStart
164  );
165 
166  template<class T, class CombineOp, class negateOp>
167  static void flipAndCombine
168  (
169  const UList<label>& map,
170  const bool hasFlip,
171  const UList<T>& rhs,
172  const CombineOp& cop,
173  const negateOp& negOp,
174  List<T>& lhs
175  );
176 
177  template<class T, class negateOp>
178  static T accessAndFlip
179  (
180  const UList<T>& fld,
181  const label index,
182  const bool hasFlip,
183  const negateOp& negOp
184  );
185 
186 public:
187 
188  // Declare name of the class and its debug switch
189  ClassName("distributionMapBase");
190 
191 
192  // Constructors
193 
194  //- Construct null
196 
197  //- Move construct from components
199  (
200  const label constructSize,
201  const labelListList&& subMap,
202  const labelListList&& constructMap,
203  const bool subHasFlip = false,
204  const bool constructHasFlip = false
205  );
206 
207  //- Construct from reverse addressing: per data item the send
208  // processor and the receive processor. (note: data is not stored
209  // sorted per processor so cannot use printLayout).
211  (
212  const labelList& sendProcs,
213  const labelList& recvProcs
214  );
215 
216  //- Construct from list of (possibly) remote elements in globalIndex
217  // numbering (or -1). Determines compact numbering (see above) and
218  // distribute map to get data into this ordering and renumbers the
219  // elements to be in compact numbering.
221  (
222  const globalIndex&,
223  labelList& elements,
224  List<Map<label>>& compactMap,
225  const int tag = Pstream::msgType()
226  );
227 
228  //- Special variant that works with the info sorted into bins
229  // according to local indices. E.g. think cellCells where
230  // cellCells[localCellI] is a list of global cells
232  (
233  const globalIndex&,
234  labelListList& cellCells,
235  List<Map<label>>& compactMap,
236  const int tag = Pstream::msgType()
237  );
238 
239  //- Construct copy
241 
242  //- Move constructor
244 
245  //- Construct from Istream
247 
248 
249  // Member Functions
250 
251  // Access
252 
253  //- Constructed data size
254  label constructSize() const
255  {
256  return constructSize_;
257  }
258 
259  //- Constructed data size
261  {
262  return constructSize_;
263  }
264 
265  //- From subsetted data back to original data
266  const labelListList& subMap() const
267  {
268  return subMap_;
269  }
270 
271  //- From subsetted data back to original data
273  {
274  return subMap_;
275  }
276 
277  //- From subsetted data to new reconstructed data
278  const labelListList& constructMap() const
279  {
280  return constructMap_;
281  }
282 
283  //- From subsetted data to new reconstructed data
285  {
286  return constructMap_;
287  }
288 
289  //- Does subMap include a sign
290  bool subHasFlip() const
291  {
292  return subHasFlip_;
293  }
294 
295  //- Does subMap include a sign
296  bool& subHasFlip()
297  {
298  return subHasFlip_;
299  }
300 
301  //- Does constructMap include a sign
302  bool constructHasFlip() const
303  {
304  return constructHasFlip_;
305  }
306 
307  //- Does constructMap include a sign
308  bool& constructHasFlip()
309  {
310  return constructHasFlip_;
311  }
312 
313  //- Calculate a schedule. See above.
315  (
316  const labelListList& subMap,
318  const int tag
319  );
320 
321  //- Return a schedule. Demand driven. See above.
322  const List<labelPair>& schedule() const;
323 
324 
325  // Other
326 
327  //- Transfer the contents of the argument and annul the argument.
329 
330  //- Helper for construct from globalIndex. Renumbers element
331  // (in globalIndex numbering) into compact indices.
332  static label renumber
333  (
334  const globalIndex&,
335  const List<Map<label>>& compactMap,
336  const label globalElement
337  );
338 
339  //- Compact maps. Gets per field a bool whether it is used (locally)
340  // and works out itself what this side and sender side can remove
341  // from maps. Only compacts non-local elements (i.e. the stuff
342  // that gets sent over), does not change the local layout
343  void compact
344  (
345  const boolList& elemIsUsed,
346  const int tag = UPstream::msgType()
347  );
348 
349  //- Compact all maps and layout. Returns compaction maps for
350  // subMap and constructMap
351  void compact
352  (
353  const boolList& elemIsUsed,
354  const label localSize, // max index for subMap
355  labelList& oldToNewSub,
356  labelList& oldToNewConstruct,
357  const int tag = UPstream::msgType()
358  );
359 
360  //- Distribute data. Note:schedule only used for
361  // Pstream::commsTypes::scheduled for now, all others just use
362  // send-to-all, receive-from-all.
363  template<class T, class negateOp>
364  static void distribute
365  (
366  const Pstream::commsTypes commsType,
367  const List<labelPair>& schedule,
368  const label constructSize,
369  const labelListList& subMap,
370  const bool subHasFlip,
371  const labelListList& constructMap,
372  const bool constructHasFlip,
373  List<T>&,
374  const negateOp& negOp,
375  const int tag = UPstream::msgType()
376  );
377 
378  //- Distribute data. If multiple processors writing to same
379  // position adds contributions using cop.
380  template<class T, class CombineOp, class negateOp>
381  static void distribute
382  (
383  const Pstream::commsTypes commsType,
384  const List<labelPair>& schedule,
385  const label constructSize,
386  const labelListList& subMap,
387  const bool subHasFlip,
388  const labelListList& constructMap,
389  const bool constructHasFlip,
390  List<T>&,
391  const CombineOp& cop,
392  const negateOp& negOp,
393  const T& nullValue,
394  const int tag = UPstream::msgType()
395  );
396 
397  //- Distribute data using default commsType.
398  template<class T>
399  void distribute
400  (
401  List<T>& fld,
402  const int tag = UPstream::msgType()
403  ) const;
404 
405  //- Distribute data using default commsType.
406  template<class T, class negateOp>
407  void distribute
408  (
409  List<T>& fld,
410  const negateOp& negOp,
411  const int tag = UPstream::msgType()
412  ) const;
413 
414  //- Distribute data using default commsType.
415  template<class T>
416  void distribute
417  (
418  DynamicList<T>& fld,
419  const int tag = UPstream::msgType()
420  ) const;
421 
422  //- Reverse distribute data using default commsType.
423  template<class T>
424  void reverseDistribute
425  (
426  const label constructSize,
427  List<T>&,
428  const int tag = UPstream::msgType()
429  ) const;
430 
431  //- Reverse distribute data using default commsType.
432  // Since constructSize might be larger than supplied size supply
433  // a nullValue
434  template<class T>
435  void reverseDistribute
436  (
437  const label constructSize,
438  const T& nullValue,
439  List<T>& fld,
440  const int tag = UPstream::msgType()
441  ) const;
442 
443  //- Do all sends using PstreamBuffers
444  template<class T>
445  void send(PstreamBuffers&, const List<T>&) const;
446  //- Do all receives using PstreamBuffers
447  template<class T>
448  void receive(PstreamBuffers&, List<T>&) const;
449 
450  //- Debug: print layout. Can only be used on maps with sorted
451  // storage (local data first, then non-local data)
452  void printLayout(Ostream& os) const;
453 
454 
455  // Member Operators
456 
457  void operator=(const distributionMapBase&);
459 
460 
461  // IOstream Operators
462 
463  //- Read dictionary from Istream
465 
466  //- Write dictionary to Ostream
467  friend Ostream& operator<<(Ostream&, const distributionMapBase&);
468 };
469 
470 
471 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
472 
473 } // End namespace Foam
474 
475 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
476 
477 #ifdef NoRepository
479 #endif
480 
481 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
482 
483 #endif
484 
485 // ************************************************************************* //
static void distribute(const Pstream::commsTypes commsType, const List< labelPair > &schedule, const label constructSize, const labelListList &subMap, const bool subHasFlip, const labelListList &constructMap, const bool constructHasFlip, List< T > &, const negateOp &negOp, const int tag=UPstream::msgType())
Distribute data. Note:schedule only used for.
friend Ostream & operator<<(Ostream &, const distributionMapBase &)
Write dictionary to Ostream.
static label renumber(const globalIndex &, const List< Map< label >> &compactMap, const label globalElement)
Helper for construct from globalIndex. Renumbers element.
void receive(PstreamBuffers &, List< T > &) const
Do all receives using PstreamBuffers.
Class containing processor-to-processor mapping information.
void operator=(const distributionMapBase &)
void reverseDistribute(const label constructSize, List< T > &, const int tag=UPstream::msgType()) const
Reverse distribute data using default commsType.
commsTypes
Types of communications.
Definition: UPstream.H:64
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
const labelListList & constructMap() const
From subsetted data to new reconstructed data.
distributionMapBase()
Construct null.
static int & msgType()
Message tag of standard messages.
Definition: UPstream.H:476
const labelListList & subMap() const
From subsetted data back to original data.
bool subHasFlip_
Whether subMap includes flip or not.
static void checkReceivedSize(const label proci, const label expectedSize, const label receivedSize)
void compact(const boolList &elemIsUsed, const int tag=UPstream::msgType())
Compact maps. Gets per field a bool whether it is used (locally)
Calculates a unique integer (label so might not have enough room - 2G max) for processor + local inde...
Definition: globalIndex.H:63
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< ' ';}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< ' ';}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< ' ';}gmvFile<< nl;forAll(lagrangianScalarNames, i){ const word &name=lagrangianScalarNames[i];IOField< scalar > fld(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:56
Istream & operator>>(Istream &, directionInfo &)
void calcCompactAddressing(const globalIndex &globalNumbering, const labelList &elements, List< Map< label >> &compactMap) const
Construct per processor compact addressing of the global elements.
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
label constructSize() const
Constructed data size.
ClassName("distributionMapBase")
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
static void flipAndCombine(const UList< label > &map, const bool hasFlip, const UList< T > &rhs, const CombineOp &cop, const negateOp &negOp, List< T > &lhs)
bool subHasFlip() const
Does subMap include a sign.
friend Istream & operator>>(Istream &, distributionMapBase &)
Read dictionary from Istream.
void transfer(distributionMapBase &)
Transfer the contents of the argument and annul the argument.
Buffers for inter-processor communications streams (UOPstream, UIPstream).
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
bool constructHasFlip_
Whether constructMap includes flip or not.
static T accessAndFlip(const UList< T > &fld, const label index, const bool hasFlip, const negateOp &negOp)
Ostream & operator<<(Ostream &, const ensightPart &)
void exchangeAddressing(const int tag, const globalIndex &globalNumbering, labelList &elements, List< Map< label >> &compactMap, labelList &compactStart)
labelListList constructMap_
Maps from subsetted data to new reconstructed data.
void send(PstreamBuffers &, const List< T > &) const
Do all sends using PstreamBuffers.
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
label constructSize_
Size of reconstructed data.
void printLayout(Ostream &os) const
Debug: print layout. Can only be used on maps with sorted.
autoPtr< List< labelPair > > schedulePtr_
Schedule.
const List< labelPair > & schedule() const
Return a schedule. Demand driven. See above.
Namespace for OpenFOAM.
bool constructHasFlip() const
Does constructMap include a sign.
labelListList subMap_
Maps from subsetted data back to original data.