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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2015-2017 OpenFOAM Foundation
9  Copyright (C) 2015-2016 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::mapDistributeBase
29 
30 Description
31  Class containing processor-to-processor mapping information.
32 
33  We store mapping from the bits-to-send to the complete starting list
34  (subXXXMap) and from the received bits to their location in the new
35  list (constructXXXMap).
36 
37 Note:
38  Schedule is a list of processor pairs (one send, one receive. One of
39  them will be myself) which forms a scheduled (i.e. non-buffered) exchange.
40  See distribute on how to use it.
41  Note2: number of items sent on one processor have to equal the number
42  of items received on the other processor.
43 
44  To aid constructing these maps there are the constructors from global
45  numbering, either with or without transforms.
46 
47  Constructors using compact numbering: layout is
48  - all my own elements first (whether used or not)
49  - followed by used-only remote elements sorted by remote processor.
50  So e.g 4 procs and on proc 1 the compact
51  table will first have all globalIndex.localSize() elements from proc1
52  followed by used-only elements of proc0, proc2, proc3.
53  The constructed mapDistributeBase sends the local elements from and
54  receives the remote elements into their compact position.
55  compactMap[proci] is the position of elements from proci in the compact
56  map. compactMap[myProcNo()] is empty since trivial addressing.
57 
58  It rewrites the input global indices into indices into the constructed
59  data.
60 
61  When constructing from components optionally a 'flip' on
62  the maps can be specified. This will interpret the map
63  values as index+flip, similar to e.g. faceProcAddressing. The flip
64  will only be applied to fieldTypes (scalar, vector, .. triad)
65 
66 
67 SourceFiles
68  mapDistributeBase.C
69  mapDistributeBaseTemplates.C
70 
71 \*---------------------------------------------------------------------------*/
72 
73 #ifndef mapDistributeBase_H
74 #define mapDistributeBase_H
75 
76 #include "labelList.H"
77 #include "labelPair.H"
78 #include "Pstream.H"
79 #include "boolList.H"
80 #include "Map.H"
81 
82 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
83 
84 namespace Foam
85 {
86 
87 class mapPolyMesh;
88 class globalIndex;
89 class PstreamBuffers;
90 
91 
92 // Forward declaration of friend functions and operators
93 
94 class mapDistributeBase;
95 
96 Istream& operator>>(Istream&, mapDistributeBase&);
97 Ostream& operator<<(Ostream&, const mapDistributeBase&);
98 
99 
100 /*---------------------------------------------------------------------------*\
101  Class mapDistributeBase Declaration
102 \*---------------------------------------------------------------------------*/
103 
104 class mapDistributeBase
105 {
106 protected:
107 
108  // Protected data
109 
110  //- Size of reconstructed data
111  label constructSize_;
112 
113  //- Maps from subsetted data back to original data
115 
116  //- Maps from subsetted data to new reconstructed data
118 
119  //- Whether subMap includes flip or not
120  bool subHasFlip_;
121 
122  //- Whether constructMap includes flip or not
123  bool constructHasFlip_;
124 
125  //- Communicator to use for parallel operations
126  label comm_;
127 
128  //- Schedule
130 
131 
132  // Private Member Functions
133 
134  static void checkReceivedSize
135  (
136  const label proci,
137  const label expectedSize,
138  const label receivedSize
139  );
140 
141  //- Construct per processor compact addressing of the global elements
142  // needed. The ones from the local processor are not included since
143  // these are always all needed.
145  (
146  const globalIndex& globalNumbering,
147  const labelUList& elements,
148  List<Map<label>>& compactMap
149  ) const;
150 
152  (
153  const globalIndex& globalNumbering,
154  const labelListList& elements,
155  List<Map<label>>& compactMap
156  ) const;
157 
158  void exchangeAddressing
159  (
160  const int tag,
161  const globalIndex& globalNumbering,
162  labelList& elements,
163  List<Map<label>>& compactMap,
164  labelList& compactStart
165  );
166  void exchangeAddressing
167  (
168  const int tag,
169  const globalIndex& globalNumbering,
170  labelListList& elements,
171  List<Map<label>>& compactMap,
172  labelList& compactStart
173  );
174 
175  template<class T, class CombineOp, class negateOp>
176  static void flipAndCombine
177  (
178  const labelUList& map,
179  const bool hasFlip,
180  const UList<T>& rhs,
181  const CombineOp& cop,
182  const negateOp& negOp,
183  List<T>& lhs
184  );
185 
186  template<class T, class negateOp>
187  static T accessAndFlip
188  (
189  const UList<T>& fld,
190  const label index,
191  const bool hasFlip,
192  const negateOp& negOp
193  );
194 
195 public:
196 
197  // Declare name of the class and its debug switch
198  ClassName("mapDistributeBase");
199 
200 
201  // Constructors
202 
203  //- Construct null
205 
206  //- Copy construct
208 
209  //- Move construct
211 
212  //- Construct from components
214  (
215  const label constructSize,
218  const bool subHasFlip = false,
219  const bool constructHasFlip = false,
220  const label comm = UPstream::worldComm
221  );
222 
223  //- Construct from reverse addressing: per data item the send
224  // processor and the receive processor. (note: data is not stored
225  // sorted per processor so cannot use printLayout).
227  (
228  const labelUList& sendProcs,
229  const labelUList& recvProcs,
230  const label comm = UPstream::worldComm
231  );
232 
233  //- Construct from list of (possibly) remote elements in globalIndex
234  // numbering (or -1). Determines compact numbering (see above) and
235  // distribute map to get data into this ordering and renumbers the
236  // elements to be in compact numbering.
238  (
239  const globalIndex&,
240  labelList& elements,
241  List<Map<label>>& compactMap,
242  const int tag = Pstream::msgType(),
243  const label comm = UPstream::worldComm
244  );
245 
246  //- Special variant that works with the info sorted into bins
247  // according to local indices. E.g. think cellCells where
248  // cellCells[localCellI] is a list of global cells
250  (
251  const globalIndex&,
252  labelListList& cellCells,
253  List<Map<label>>& compactMap,
254  const int tag = Pstream::msgType(),
255  const label comm = UPstream::worldComm
256  );
257 
258  //- Construct from my elements to send. Assumes layout is my elements
259  // first followed by elements from all other processors in consecutive
260  // order
261  explicit mapDistributeBase
262  (
264  const bool subHasFlip = false,
265  const bool constructHasFlip = false,
266  const label comm = UPstream::worldComm
267  );
268 
269  //- Construct from Istream
271 
272 
273  // Member Functions
274 
275  // Access
276 
277  //- Constructed data size
278  label constructSize() const
279  {
280  return constructSize_;
281  }
282 
283  //- Constructed data size
284  label& constructSize()
285  {
286  return constructSize_;
287  }
288 
289  //- From subsetted data back to original data
290  const labelListList& subMap() const
291  {
292  return subMap_;
293  }
294 
295  //- From subsetted data back to original data
297  {
298  return subMap_;
299  }
300 
301  //- From subsetted data to new reconstructed data
302  const labelListList& constructMap() const
303  {
304  return constructMap_;
305  }
306 
307  //- From subsetted data to new reconstructed data
309  {
310  return constructMap_;
311  }
312 
313  //- Does subMap include a sign
314  bool subHasFlip() const
315  {
316  return subHasFlip_;
317  }
318 
319  //- Does subMap include a sign
320  bool& subHasFlip()
321  {
322  return subHasFlip_;
323  }
324 
325  //- Does constructMap include a sign
326  bool constructHasFlip() const
327  {
328  return constructHasFlip_;
329  }
330 
331  //- Does constructMap include a sign
332  bool& constructHasFlip()
333  {
334  return constructHasFlip_;
335  }
336 
337  label comm() const
338  {
339  return comm_;
340  }
341 
342  //- Calculate a schedule. See above.
344  (
345  const labelListList& subMap,
347  const int tag,
348  const label comm = UPstream::worldComm
349  );
350 
351  //- Return a schedule. Demand driven. See above.
352  const List<labelPair>& schedule() const;
353 
354 
355  // Other
356 
357  //- Transfer the contents of the argument and annul the argument.
358  void transfer(mapDistributeBase& rhs);
359 
360  //- Helper for construct from globalIndex. Renumbers element
361  // (in globalIndex numbering) into compact indices.
362  static label renumber
363  (
364  const globalIndex&,
365  const List<Map<label>>& compactMap,
366  const label globalElement
367  );
368 
369  //- Compact maps. Gets per field a bool whether it is used (locally)
370  // and works out itself what this side and sender side can remove
371  // from maps. Only compacts non-local elements (i.e. the stuff
372  // that gets sent over), does not change the local layout
373  void compact
374  (
375  const boolList& elemIsUsed,
376  const int tag = UPstream::msgType()
377  );
378 
379  //- Compact all maps and layout. Returns compaction maps for
380  // subMap and constructMap
381  void compact
382  (
383  const boolList& elemIsUsed,
384  const label localSize, // max index for subMap
385  labelList& oldToNewSub,
386  labelList& oldToNewConstruct,
387  const int tag = UPstream::msgType()
388  );
389 
390  //- Distribute data. Note:schedule only used for
391  // Pstream::commsTypes::scheduled for now, all others just use
392  // send-to-all, receive-from-all.
393  template<class T, class negateOp>
394  static void distribute
395  (
396  const Pstream::commsTypes commsType,
397  const List<labelPair>& schedule,
398  const label constructSize,
399  const labelListList& subMap,
400  const bool subHasFlip,
402  const bool constructHasFlip,
403  List<T>&,
404  const negateOp& negOp,
405  const int tag = UPstream::msgType(),
406  const label comm = UPstream::worldComm
407  );
408 
409  //- Distribute data. If multiple processors writing to same
410  // position adds contributions using cop.
411  template<class T, class CombineOp, class negateOp>
412  static void distribute
413  (
414  const Pstream::commsTypes commsType,
415  const List<labelPair>& schedule,
416  const label constructSize,
417  const labelListList& subMap,
418  const bool subHasFlip,
420  const bool constructHasFlip,
421  List<T>&,
422  const T& nullValue,
423  const CombineOp& cop,
424  const negateOp& negOp,
425  const int tag = UPstream::msgType(),
426  const label comm = UPstream::worldComm
427  );
428 
429  //- Distribute data using default commsType.
430  template<class T>
431  void distribute
432  (
433  List<T>& fld,
434  const int tag = UPstream::msgType()
435  ) const;
436 
437  //- Distribute data using default commsType.
438  template<class T, class negateOp>
439  void distribute
440  (
441  List<T>& fld,
442  const negateOp& negOp,
443  const int tag = UPstream::msgType()
444  ) const;
445 
446  //- Distribute data using default commsType.
447  template<class T>
448  void distribute
449  (
451  const int tag = UPstream::msgType()
452  ) const;
453 
454  //- Reverse distribute data using default commsType.
455  template<class T>
456  void reverseDistribute
457  (
458  const label constructSize,
459  List<T>&,
460  const int tag = UPstream::msgType()
461  ) const;
462 
463  //- Reverse distribute data using default commsType.
464  // Since constructSize might be larger than supplied size supply
465  // a nullValue
466  template<class T>
467  void reverseDistribute
468  (
469  const label constructSize,
470  const T& nullValue,
471  List<T>& fld,
472  const int tag = UPstream::msgType()
473  ) const;
474 
475  //- Do all sends using PstreamBuffers
476  template<class T>
477  void send(PstreamBuffers&, const List<T>&) const;
478  //- Do all receives using PstreamBuffers
479  template<class T>
480  void receive(PstreamBuffers&, List<T>&) const;
481 
482  //- Debug: print layout. Can only be used on maps with sorted
483  // storage (local data first, then non-local data)
484  void printLayout(Ostream& os) const;
485 
486  //- Correct for topo change.
487  void updateMesh(const mapPolyMesh&)
488  {
490  }
491 
492 
493  // Member Operators
494 
495  //- Copy assignment
496  void operator=(const mapDistributeBase& rhs);
497 
498  //- Move assignment
499  void operator=(mapDistributeBase&& rhs);
500 
501 
502  // IOstream operators
503 
504  //- Read dictionary from Istream
506 
507  //- Write dictionary to Ostream
508  friend Ostream& operator<<(Ostream&, const mapDistributeBase&);
509 
510 };
511 
512 
513 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
514 
515 } // End namespace Foam
516 
517 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
518 
519 #ifdef NoRepository
521 #endif
522 
523 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
524 
525 #endif
526 
527 // ************************************************************************* //
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:1344
Foam::mapDistributeBase::accessAndFlip
static T accessAndFlip(const UList< T > &fld, const label index, const bool hasFlip, const negateOp &negOp)
Definition: mapDistributeBaseTemplates.C:83
Foam::mapDistributeBase::subMap
const labelListList & subMap() const
From subsetted data back to original data.
Definition: mapDistributeBase.H:289
Foam::mapDistributeBase::constructHasFlip
bool constructHasFlip() const
Does constructMap include a sign.
Definition: mapDistributeBase.H:325
Foam::mapDistributeBase::subMap_
labelListList subMap_
Maps from subsetted data back to original data.
Definition: mapDistributeBase.H:113
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:307
boolList.H
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:906
Foam::mapDistributeBase::operator=
void operator=(const mapDistributeBase &rhs)
Copy assignment.
Definition: mapDistributeBase.C:1327
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:55
Foam::mapDistributeBase::send
void send(PstreamBuffers &, const List< T > &) const
Do all sends using PstreamBuffers.
Definition: mapDistributeBaseTemplates.C:1176
Foam::PstreamBuffers
Buffers for inter-processor communications streams (UOPstream, UIPstream).
Definition: PstreamBuffers.H:88
Foam::mapDistributeBase::receive
void receive(PstreamBuffers &, List< T > &) const
Do all receives using PstreamBuffers.
Definition: mapDistributeBaseTemplates.C:1210
Foam::Map< label >
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::mapDistributeBase::flipAndCombine
static void flipAndCombine(const labelUList &map, const bool hasFlip, const UList< T > &rhs, const CombineOp &cop, const negateOp &negOp, List< T > &lhs)
Definition: mapDistributeBaseTemplates.C:38
Foam::mapDistributeBase::transfer
void transfer(mapDistributeBase &rhs)
Transfer the contents of the argument and annul the argument.
Definition: mapDistributeBase.C:883
Foam::mapDistributeBase::subHasFlip
bool & subHasFlip()
Does subMap include a sign.
Definition: mapDistributeBase.H:319
Foam::mapDistributeBase::schedulePtr_
autoPtr< List< labelPair > > schedulePtr_
Schedule.
Definition: mapDistributeBase.H:128
mapDistributeBaseTemplates.C
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Map.H
NotImplemented
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:517
Foam::mapDistributeBase::mapDistributeBase
mapDistributeBase(const label comm=UPstream::worldComm)
Construct null.
Definition: mapDistributeBase.C:566
Foam::mapDistributeBase::comm_
label comm_
Communicator to use for parallel operations.
Definition: mapDistributeBase.H:125
labelList.H
Foam::mapDistributeBase::schedule
const List< labelPair > & schedule() const
Return a schedule. Demand driven. See above.
Definition: mapDistributeBase.C:187
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
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(), const label comm=UPstream::worldComm)
Distribute data. Note:schedule only used for.
Definition: mapDistributeBaseTemplates.C:122
Foam::mapDistributeBase::calcCompactAddressing
void calcCompactAddressing(const globalIndex &globalNumbering, const labelUList &elements, List< Map< label >> &compactMap) const
Construct per processor compact addressing of the global elements.
Definition: mapDistributeBase.C:299
Foam::T
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
Definition: FieldFieldFunctions.C:58
Foam::mapDistributeBase::constructSize_
label constructSize_
Size of reconstructed data.
Definition: mapDistributeBase.H:110
Foam::mapDistributeBase::checkReceivedSize
static void checkReceivedSize(const label proci, const label expectedSize, const label receivedSize)
Definition: mapDistributeBase.C:204
fld
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< ' ';}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< ' ';}gmvFile<< nl;for(const word &name :lagrangianScalarNames){ IOField< scalar > fld(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Definition: gmvOutputLagrangian.H:23
Foam::mapDistributeBase::comm
label comm() const
Definition: mapDistributeBase.H:336
Foam::mapDistributeBase::subHasFlip_
bool subHasFlip_
Whether subMap includes flip or not.
Definition: mapDistributeBase.H:119
os
OBJstream os(runTime.globalPath()/outputName)
Foam::mapDistributeBase::subHasFlip
bool subHasFlip() const
Does subMap include a sign.
Definition: mapDistributeBase.H:313
Foam::mapDistributeBase::constructHasFlip_
bool constructHasFlip_
Whether constructMap includes flip or not.
Definition: mapDistributeBase.H:122
Pstream.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
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:930
Foam::globalIndex
Calculates a unique integer (label so might not have enough room - 2G max) for processor + local inde...
Definition: globalIndex.H:68
Foam::mapDistributeBase::printLayout
void printLayout(Ostream &os) const
Debug: print layout. Can only be used on maps with sorted.
Definition: mapDistributeBase.C:221
Foam::mapDistributeBase::exchangeAddressing
void exchangeAddressing(const int tag, const globalIndex &globalNumbering, labelList &elements, List< Map< label >> &compactMap, labelList &compactStart)
Definition: mapDistributeBase.C:401
Foam::mapDistributeBase::constructSize
label constructSize() const
Constructed data size.
Definition: mapDistributeBase.H:277
Foam::UPstream::commsTypes
commsTypes
Types of communications.
Definition: UPstream.H:69
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::mapDistributeBase::constructMap_
labelListList constructMap_
Maps from subsetted data to new reconstructed data.
Definition: mapDistributeBase.H:116
Foam::UPstream::msgType
static int & msgType() noexcept
Message tag of standard messages.
Definition: UPstream.H:540
Foam::UPstream::worldComm
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:293
Foam::List< labelList >
Foam::mapDistributeBase::subMap
labelListList & subMap()
From subsetted data back to original data.
Definition: mapDistributeBase.H:295
Foam::mapDistributeBase::constructSize
label & constructSize()
Constructed data size.
Definition: mapDistributeBase.H:283
Foam::UList< label >
Foam::mapDistributeBase::constructHasFlip
bool & constructHasFlip()
Does constructMap include a sign.
Definition: mapDistributeBase.H:331
Foam::mapPolyMesh
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:161
Foam::mapDistributeBase
Class containing processor-to-processor mapping information.
Definition: mapDistributeBase.H:103
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::mapDistributeBase::constructMap
const labelListList & constructMap() const
From subsetted data to new reconstructed data.
Definition: mapDistributeBase.H:301
Foam::mapDistributeBase::updateMesh
void updateMesh(const mapPolyMesh &)
Correct for topo change.
Definition: mapDistributeBase.H:486
Foam::mapDistributeBase::ClassName
ClassName("mapDistributeBase")
labelPair.H