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 OpenFOAM Foundation
6  \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
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  Class mapDistributeBase Declaration
90 \*---------------------------------------------------------------------------*/
91 
93 {
94 protected:
95 
96  // Protected data
97 
98  //- Size of reconstructed data
100 
101  //- Maps from subsetted data back to original data
103 
104  //- Maps from subsetted data to new reconstructed data
106 
107  //- Whether subMap includes flip or not
108  bool subHasFlip_;
109 
110  //- Whether constructMap includes flip or not
111  bool constructHasFlip_;
112 
113 
114  //- Schedule
116 
117 
118  // Private Member Functions
119 
120  static void checkReceivedSize
121  (
122  const label procI,
123  const label expectedSize,
124  const label receivedSize
125  );
126 
128  (
129  const globalIndex& globalNumbering,
130  const labelList& elements,
131  List<Map<label> >& compactMap
132  ) const;
134  (
135  const globalIndex& globalNumbering,
136  const labelListList& elements,
137  List<Map<label> >& compactMap
138  ) const;
139 
140  void exchangeAddressing
141  (
142  const int tag,
143  const globalIndex& globalNumbering,
144  labelList& elements,
145  List<Map<label> >& compactMap,
146  labelList& compactStart
147  );
148  void exchangeAddressing
149  (
150  const int tag,
151  const globalIndex& globalNumbering,
152  labelListList& elements,
153  List<Map<label> >& compactMap,
154  labelList& compactStart
155  );
156 
157  template<class T, class CombineOp, class negateOp>
158  static void flipAndCombine
159  (
160  const UList<label>& map,
161  const bool hasFlip,
162  const UList<T>& rhs,
163  const CombineOp& cop,
164  const negateOp& negOp,
165  List<T>& lhs
166  );
167 
168  template<class T, class negateOp>
169  static T accessAndFlip
170  (
171  const UList<T>& fld,
172  const label index,
173  const bool hasFlip,
174  const negateOp& negOp
175  );
176 
177 public:
178 
179  // Declare name of the class and its debug switch
180  ClassName("mapDistributeBase");
181 
182 
183  // Constructors
184 
185  //- Construct null
187 
188  //- Construct from components
190  (
191  const label constructSize,
194  const bool subHasFlip = false,
195  const bool constructHasFlip = false
196  );
197 
198  //- Construct from reverse addressing: per data item the send
199  // processor and the receive processor. (note: data is not stored
200  // sorted per processor so cannot use printLayout).
202  (
203  const labelList& sendProcs,
204  const labelList& recvProcs
205  );
206 
207  //- Construct from list of (possibly) remote elements in globalIndex
208  // numbering (or -1). Determines compact numbering (see above) and
209  // distribute map to get data into this ordering and renumbers the
210  // elements to be in compact numbering.
212  (
213  const globalIndex&,
214  labelList& elements,
215  List<Map<label> >& compactMap,
216  const int tag = Pstream::msgType()
217  );
218 
219  //- Special variant that works with the info sorted into bins
220  // according to local indices. E.g. think cellCells where
221  // cellCells[localCellI] is a list of global cells
223  (
224  const globalIndex&,
225  labelListList& cellCells,
226  List<Map<label> >& compactMap,
227  const int tag = Pstream::msgType()
228  );
229 
230  //- Construct by transferring parameter content
232 
233  //- Construct copy
235 
236  //- Construct from Istream
238 
239 
240  // Member Functions
241 
242  // Access
243 
244  //- Constructed data size
245  label constructSize() const
246  {
247  return constructSize_;
248  }
249 
250  //- Constructed data size
252  {
253  return constructSize_;
254  }
255 
256  //- From subsetted data back to original data
257  const labelListList& subMap() const
258  {
259  return subMap_;
260  }
261 
262  //- From subsetted data back to original data
264  {
265  return subMap_;
266  }
267 
268  //- From subsetted data to new reconstructed data
269  const labelListList& constructMap() const
270  {
271  return constructMap_;
272  }
273 
274  //- From subsetted data to new reconstructed data
276  {
277  return constructMap_;
278  }
279 
280  //- Does subMap include a sign
281  bool subHasFlip() const
282  {
283  return subHasFlip_;
284  }
285 
286  //- Does subMap include a sign
287  bool& subHasFlip()
288  {
289  return subHasFlip_;
290  }
291 
292  //- Does constructMap include a sign
293  bool constructHasFlip() const
294  {
295  return constructHasFlip_;
296  }
297 
298  //- Does constructMap include a sign
299  bool& constructHasFlip()
300  {
301  return constructHasFlip_;
302  }
303 
304  //- Calculate a schedule. See above.
306  (
307  const labelListList& subMap,
309  const int tag
310  );
311 
312  //- Return a schedule. Demand driven. See above.
313  const List<labelPair>& schedule() const;
314 
315 
316  // Other
317 
318  //- Transfer the contents of the argument and annul the argument.
320 
321  //- Transfer contents to the Xfer container
323 
324  //- Helper for construct from globalIndex. Renumbers element
325  // (in globalIndex numbering) into compact indices.
326  static label renumber
327  (
328  const globalIndex&,
329  const List<Map<label> >& compactMap,
330  const label globalElement
331  );
332 
333  //- Compact maps. Gets per field a bool whether it is used (locally)
334  // and works out itself what this side and sender side can remove
335  // from maps. Only compacts non-local elements (i.e. the stuff
336  // that gets sent over), does not change the local layout
337  void compact
338  (
339  const boolList& elemIsUsed,
340  const int tag = UPstream::msgType()
341  );
342 
343  //- Compact all maps and layout. Returns compaction maps for
344  // subMap and constructMap
345  void compact
346  (
347  const boolList& elemIsUsed,
348  const label localSize, // max index for subMap
349  labelList& oldToNewSub,
350  labelList& oldToNewConstruct,
351  const int tag = UPstream::msgType()
352  );
353 
354  //- Distribute data. Note:schedule only used for Pstream::scheduled
355  // for now, all others just use send-to-all, receive-from-all.
356  template<class T, class negateOp>
357  static void distribute
358  (
359  const Pstream::commsTypes commsType,
360  const List<labelPair>& schedule,
361  const label constructSize,
362  const labelListList& subMap,
363  const bool subHasFlip,
365  const bool constructHasFlip,
366  List<T>&,
367  const negateOp& negOp,
368  const int tag = UPstream::msgType()
369  );
370 
371  //- Distribute data. If multiple processors writing to same
372  // position adds contributions using cop.
373  template<class T, class CombineOp, class negateOp>
374  static void distribute
375  (
376  const Pstream::commsTypes commsType,
377  const List<labelPair>& schedule,
378  const label constructSize,
379  const labelListList& subMap,
380  const bool subHasFlip,
382  const bool constructHasFlip,
383  List<T>&,
384  const CombineOp& cop,
385  const negateOp& negOp,
386  const T& nullValue,
387  const int tag = UPstream::msgType()
388  );
389 
390  //- Distribute data using default commsType.
391  template<class T>
392  void distribute
393  (
394  List<T>& fld,
395  const int tag = UPstream::msgType()
396  ) const;
397 
398  //- Distribute data using default commsType.
399  template<class T, class negateOp>
400  void distribute
401  (
402  List<T>& fld,
403  const negateOp& negOp,
404  const int tag = UPstream::msgType()
405  ) const;
406 
407  //- Distribute data using default commsType.
408  template<class T>
409  void distribute
410  (
412  const int tag = UPstream::msgType()
413  ) const;
414 
415  //- Reverse distribute data using default commsType.
416  template<class T>
417  void reverseDistribute
418  (
419  const label constructSize,
420  List<T>&,
421  const int tag = UPstream::msgType()
422  ) const;
423 
424  //- Reverse distribute data using default commsType.
425  // Since constructSize might be larger than supplied size supply
426  // a nullValue
427  template<class T>
428  void reverseDistribute
429  (
430  const label constructSize,
431  const T& nullValue,
432  List<T>& fld,
433  const int tag = UPstream::msgType()
434  ) const;
435 
436  //- Do all sends using PstreamBuffers
437  template<class T>
438  void send(PstreamBuffers&, const List<T>&) const;
439  //- Do all receives using PstreamBuffers
440  template<class T>
441  void receive(PstreamBuffers&, List<T>&) const;
442 
443  //- Debug: print layout. Can only be used on maps with sorted
444  // storage (local data first, then non-local data)
445  void printLayout(Ostream& os) const;
446 
447  //- Correct for topo change.
448  void updateMesh(const mapPolyMesh&)
449  {
451  }
452 
453  // Member Operators
454 
455  void operator=(const mapDistributeBase&);
456 
457  // IOstream operators
458 
459  //- Read dictionary from Istream
461 
462  //- Write dictionary to Ostream
463  friend Ostream& operator<<(Ostream&, const mapDistributeBase&);
464 
465 };
466 
467 
468 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
469 
470 } // End namespace Foam
471 
472 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
473 
474 #ifdef NoRepository
476 #endif
477 
478 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
479 
480 #endif
481 
482 // ************************************************************************* //
Foam::mapDistributeBase::reverseDistribute
void reverseDistribute(const label constructSize, List< T > &, const int tag=UPstream::msgType()) const
Reverse distribute data using default commsType.
Definition: mapDistributeBaseTemplates.C:1242
Foam::mapDistributeBase::exchangeAddressing
void exchangeAddressing(const int tag, const globalIndex &globalNumbering, labelList &elements, List< Map< label > > &compactMap, labelList &compactStart)
Definition: mapDistributeBase.C:388
Foam::mapDistributeBase::accessAndFlip
static T accessAndFlip(const UList< T > &fld, const label index, const bool hasFlip, const negateOp &negOp)
Definition: mapDistributeBaseTemplates.C:80
Foam::mapDistributeBase::subMap
const labelListList & subMap() const
From subsetted data back to original data.
Definition: mapDistributeBase.H:256
Foam::mapDistributeBase::constructHasFlip
bool constructHasFlip() const
Does constructMap include a sign.
Definition: mapDistributeBase.H:292
Foam::mapDistributeBase::subMap_
labelListList subMap_
Maps from subsetted data back to original data.
Definition: mapDistributeBase.H:101
Foam::mapDistributeBase::operator>>
friend Istream & operator>>(Istream &, mapDistributeBase &)
Read dictionary from Istream.
Foam::mapDistributeBase::operator<<
friend Ostream & operator<<(Ostream &, const mapDistributeBase &)
Write dictionary to Ostream.
Foam::mapDistributeBase::constructMap
labelListList & constructMap()
From subsetted data to new reconstructed data.
Definition: mapDistributeBase.H:274
boolList.H
Foam::mapDistributeBase::operator=
void operator=(const mapDistributeBase &)
Definition: mapDistributeBase.C:1232
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:56
Foam::mapDistributeBase::distribute
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 Pstream::scheduled.
Definition: mapDistributeBaseTemplates.C:119
Foam::mapDistributeBase::send
void send(PstreamBuffers &, const List< T > &) const
Do all sends using PstreamBuffers.
Definition: mapDistributeBaseTemplates.C:1077
Foam::PstreamBuffers
Buffers for inter-processor communications streams (UOPstream, UIPstream).
Definition: PstreamBuffers.H:85
Foam::mapDistributeBase::receive
void receive(PstreamBuffers &, List< T > &) const
Do all receives using PstreamBuffers.
Definition: mapDistributeBaseTemplates.C:1111
Foam::Map< label >
Foam::mapDistributeBase::renumber
static label renumber(const globalIndex &, const List< Map< label > > &compactMap, const label globalElement)
Helper for construct from globalIndex. Renumbers element.
Definition: mapDistributeBase.C:825
Foam::mapDistributeBase::xfer
Xfer< mapDistributeBase > xfer()
Transfer contents to the Xfer container.
Definition: mapDistributeBase.C:818
Foam::mapDistributeBase::subHasFlip
bool & subHasFlip()
Does subMap include a sign.
Definition: mapDistributeBase.H:286
Foam::mapDistributeBase::schedulePtr_
autoPtr< List< labelPair > > schedulePtr_
Schedule.
Definition: mapDistributeBase.H:114
Foam::Xfer
A simple container for copying or transferring objects of type <T>.
Definition: Xfer.H:85
mapDistributeBaseTemplates.C
Map.H
NotImplemented
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:365
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
labelList.H
Foam::mapDistributeBase::schedule
const List< labelPair > & schedule() const
Return a schedule. Demand driven. See above.
Definition: mapDistributeBase.C:168
Foam::mapDistributeBase::mapDistributeBase
mapDistributeBase()
Construct null.
Definition: mapDistributeBase.C:555
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::mapDistributeBase::calcCompactAddressing
void calcCompactAddressing(const globalIndex &globalNumbering, const labelList &elements, List< Map< label > > &compactMap) const
Definition: mapDistributeBase.C:280
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:55
Foam::mapDistributeBase::constructSize_
label constructSize_
Size of reconstructed data.
Definition: mapDistributeBase.H:98
Foam::mapDistributeBase::subHasFlip_
bool subHasFlip_
Whether subMap includes flip or not.
Definition: mapDistributeBase.H:107
Foam::mapDistributeBase::transfer
void transfer(mapDistributeBase &)
Transfer the contents of the argument and annul the argument.
Definition: mapDistributeBase.C:807
fld
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))
Foam::mapDistributeBase::subHasFlip
bool subHasFlip() const
Does subMap include a sign.
Definition: mapDistributeBase.H:280
Foam::mapDistributeBase::constructHasFlip_
bool constructHasFlip_
Whether constructMap includes flip or not.
Definition: mapDistributeBase.H:110
Pstream.H
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::mapDistributeBase::compact
void compact(const boolList &elemIsUsed, const int tag=UPstream::msgType())
Compact maps. Gets per field a bool whether it is used (locally)
Definition: mapDistributeBase.C:848
Foam::globalIndex
Calculates a unique integer (label so might not have enough room - 2G max) for processor + local inde...
Definition: globalIndex.H:63
Foam::mapDistributeBase::printLayout
void printLayout(Ostream &os) const
Debug: print layout. Can only be used on maps with sorted.
Definition: mapDistributeBase.C:202
Foam::mapDistributeBase::constructSize
label constructSize() const
Constructed data size.
Definition: mapDistributeBase.H:244
Foam::autoPtr
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:117
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::mapDistributeBase::constructMap_
labelListList constructMap_
Maps from subsetted data to new reconstructed data.
Definition: mapDistributeBase.H:104
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::mapDistributeBase::subMap
labelListList & subMap()
From subsetted data back to original data.
Definition: mapDistributeBase.H:262
Foam::mapDistributeBase::constructSize
label & constructSize()
Constructed data size.
Definition: mapDistributeBase.H:250
Foam::UList
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
Foam::mapDistributeBase::flipAndCombine
static void flipAndCombine(const UList< label > &map, const bool hasFlip, const UList< T > &rhs, const CombineOp &cop, const negateOp &negOp, List< T > &lhs)
Definition: mapDistributeBaseTemplates.C:35
Foam::mapDistributeBase::constructHasFlip
bool & constructHasFlip()
Does constructMap include a sign.
Definition: mapDistributeBase.H:298
Foam::mapPolyMesh
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:158
Foam::mapDistributeBase
Class containing processor-to-processor mapping information.
Definition: mapDistributeBase.H:91
Foam::mapDistributeBase::checkReceivedSize
static void checkReceivedSize(const label procI, const label expectedSize, const label receivedSize)
Definition: mapDistributeBase.C:185
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::mapDistributeBase::constructMap
const labelListList & constructMap() const
From subsetted data to new reconstructed data.
Definition: mapDistributeBase.H:268
Foam::mapDistributeBase::updateMesh
void updateMesh(const mapPolyMesh &)
Correct for topo change.
Definition: mapDistributeBase.H:447
Foam::mapDistributeBase::ClassName
ClassName("mapDistributeBase")
labelPair.H