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