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
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 
126  //- Schedule
128 
129 
130  // Private Member Functions
131 
132  static void checkReceivedSize
133  (
134  const label proci,
135  const label expectedSize,
136  const label receivedSize
137  );
138 
139  //- Construct per processor compact addressing of the global elements
140  // needed. The ones from the local processor are not included since
141  // these are always all needed.
143  (
144  const globalIndex& globalNumbering,
145  const labelUList& elements,
146  List<Map<label>>& compactMap
147  ) const;
148 
150  (
151  const globalIndex& globalNumbering,
152  const labelListList& elements,
153  List<Map<label>>& compactMap
154  ) const;
155 
156  void exchangeAddressing
157  (
158  const int tag,
159  const globalIndex& globalNumbering,
160  labelList& elements,
161  List<Map<label>>& compactMap,
162  labelList& compactStart
163  );
164  void exchangeAddressing
165  (
166  const int tag,
167  const globalIndex& globalNumbering,
168  labelListList& elements,
169  List<Map<label>>& compactMap,
170  labelList& compactStart
171  );
172 
173  template<class T, class CombineOp, class negateOp>
174  static void flipAndCombine
175  (
176  const labelUList& map,
177  const bool hasFlip,
178  const UList<T>& rhs,
179  const CombineOp& cop,
180  const negateOp& negOp,
181  List<T>& lhs
182  );
183 
184  template<class T, class negateOp>
185  static T accessAndFlip
186  (
187  const UList<T>& fld,
188  const label index,
189  const bool hasFlip,
190  const negateOp& negOp
191  );
192 
193 public:
194 
195  // Declare name of the class and its debug switch
196  ClassName("mapDistributeBase");
197 
198 
199  // Constructors
200 
201  //- Construct null
203 
204  //- Copy construct
206 
207  //- Move construct
209 
210  //- Construct from components
212  (
213  const label constructSize,
216  const bool subHasFlip = false,
217  const bool constructHasFlip = false
218  );
219 
220  //- Construct from reverse addressing: per data item the send
221  // processor and the receive processor. (note: data is not stored
222  // sorted per processor so cannot use printLayout).
224  (
225  const labelUList& sendProcs,
226  const labelUList& recvProcs
227  );
228 
229  //- Construct from list of (possibly) remote elements in globalIndex
230  // numbering (or -1). Determines compact numbering (see above) and
231  // distribute map to get data into this ordering and renumbers the
232  // elements to be in compact numbering.
234  (
235  const globalIndex&,
236  labelList& elements,
237  List<Map<label>>& compactMap,
238  const int tag = Pstream::msgType()
239  );
240 
241  //- Special variant that works with the info sorted into bins
242  // according to local indices. E.g. think cellCells where
243  // cellCells[localCellI] is a list of global cells
245  (
246  const globalIndex&,
247  labelListList& cellCells,
248  List<Map<label>>& compactMap,
249  const int tag = Pstream::msgType()
250  );
251 
252  //- Construct from my elements to send. Assumes layout is my elements
253  // first followed by elements from all other processors in consecutive
254  // order
255  explicit mapDistributeBase
256  (
258  const bool subHasFlip = false,
259  const bool constructHasFlip = false
260  );
261 
262  //- Construct from Istream
264 
265 
266  // Member Functions
267 
268  // Access
269 
270  //- Constructed data size
271  label constructSize() const
272  {
273  return constructSize_;
274  }
275 
276  //- Constructed data size
278  {
279  return constructSize_;
280  }
281 
282  //- From subsetted data back to original data
283  const labelListList& subMap() const
284  {
285  return subMap_;
286  }
287 
288  //- From subsetted data back to original data
290  {
291  return subMap_;
292  }
293 
294  //- From subsetted data to new reconstructed data
295  const labelListList& constructMap() const
296  {
297  return constructMap_;
298  }
299 
300  //- From subsetted data to new reconstructed data
302  {
303  return constructMap_;
304  }
305 
306  //- Does subMap include a sign
307  bool subHasFlip() const
308  {
309  return subHasFlip_;
310  }
311 
312  //- Does subMap include a sign
313  bool& subHasFlip()
314  {
315  return subHasFlip_;
316  }
317 
318  //- Does constructMap include a sign
319  bool constructHasFlip() const
320  {
321  return constructHasFlip_;
322  }
323 
324  //- Does constructMap include a sign
325  bool& constructHasFlip()
326  {
327  return constructHasFlip_;
328  }
329 
330  //- Calculate a schedule. See above.
332  (
333  const labelListList& subMap,
335  const int tag
336  );
337 
338  //- Return a schedule. Demand driven. See above.
339  const List<labelPair>& schedule() const;
340 
341 
342  // Other
343 
344  //- Transfer the contents of the argument and annul the argument.
345  void transfer(mapDistributeBase& rhs);
346 
347  //- Helper for construct from globalIndex. Renumbers element
348  // (in globalIndex numbering) into compact indices.
349  static label renumber
350  (
351  const globalIndex&,
352  const List<Map<label>>& compactMap,
353  const label globalElement
354  );
355 
356  //- Compact maps. Gets per field a bool whether it is used (locally)
357  // and works out itself what this side and sender side can remove
358  // from maps. Only compacts non-local elements (i.e. the stuff
359  // that gets sent over), does not change the local layout
360  void compact
361  (
362  const boolList& elemIsUsed,
363  const int tag = UPstream::msgType()
364  );
365 
366  //- Compact all maps and layout. Returns compaction maps for
367  // subMap and constructMap
368  void compact
369  (
370  const boolList& elemIsUsed,
371  const label localSize, // max index for subMap
372  labelList& oldToNewSub,
373  labelList& oldToNewConstruct,
374  const int tag = UPstream::msgType()
375  );
376 
377  //- Distribute data. Note:schedule only used for
378  // Pstream::commsTypes::scheduled for now, all others just use
379  // send-to-all, receive-from-all.
380  template<class T, 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,
389  const bool constructHasFlip,
390  List<T>&,
391  const negateOp& negOp,
392  const int tag = UPstream::msgType()
393  );
394 
395  //- Distribute data. If multiple processors writing to same
396  // position adds contributions using cop.
397  template<class T, class CombineOp, class negateOp>
398  static void distribute
399  (
400  const Pstream::commsTypes commsType,
401  const List<labelPair>& schedule,
402  const label constructSize,
403  const labelListList& subMap,
404  const bool subHasFlip,
406  const bool constructHasFlip,
407  List<T>&,
408  const CombineOp& cop,
409  const negateOp& negOp,
410  const T& nullValue,
411  const int tag = UPstream::msgType()
412  );
413 
414  //- Distribute data using default commsType.
415  template<class T>
416  void distribute
417  (
418  List<T>& fld,
419  const int tag = UPstream::msgType()
420  ) const;
421 
422  //- Distribute data using default commsType.
423  template<class T, class negateOp>
424  void distribute
425  (
426  List<T>& fld,
427  const negateOp& negOp,
428  const int tag = UPstream::msgType()
429  ) const;
430 
431  //- Distribute data using default commsType.
432  template<class T>
433  void distribute
434  (
436  const int tag = UPstream::msgType()
437  ) const;
438 
439  //- Reverse distribute data using default commsType.
440  template<class T>
441  void reverseDistribute
442  (
443  const label constructSize,
444  List<T>&,
445  const int tag = UPstream::msgType()
446  ) const;
447 
448  //- Reverse distribute data using default commsType.
449  // Since constructSize might be larger than supplied size supply
450  // a nullValue
451  template<class T>
452  void reverseDistribute
453  (
454  const label constructSize,
455  const T& nullValue,
456  List<T>& fld,
457  const int tag = UPstream::msgType()
458  ) const;
459 
460  //- Do all sends using PstreamBuffers
461  template<class T>
462  void send(PstreamBuffers&, const List<T>&) const;
463  //- Do all receives using PstreamBuffers
464  template<class T>
465  void receive(PstreamBuffers&, List<T>&) const;
466 
467  //- Debug: print layout. Can only be used on maps with sorted
468  // storage (local data first, then non-local data)
469  void printLayout(Ostream& os) const;
470 
471  //- Correct for topo change.
472  void updateMesh(const mapPolyMesh&)
473  {
475  }
476 
477 
478  // Member Operators
479 
480  //- Copy assignment
481  void operator=(const mapDistributeBase& rhs);
482 
483  //- Move assignment
484  void operator=(mapDistributeBase&& rhs);
485 
486 
487  // IOstream operators
488 
489  //- Read dictionary from Istream
491 
492  //- Write dictionary to Ostream
493  friend Ostream& operator<<(Ostream&, const mapDistributeBase&);
494 
495 };
496 
497 
498 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
499 
500 } // End namespace Foam
501 
502 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
503 
504 #ifdef NoRepository
506 #endif
507 
508 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
509 
510 #endif
511 
512 // ************************************************************************* //
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:1293
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:282
Foam::mapDistributeBase::constructHasFlip
bool constructHasFlip() const
Does constructMap include a sign.
Definition: mapDistributeBase.H:318
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:300
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:862
Foam::mapDistributeBase::operator=
void operator=(const mapDistributeBase &rhs)
Copy assignment.
Definition: mapDistributeBase.C:1269
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:57
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.
Definition: mapDistributeBaseTemplates.C:122
Foam::mapDistributeBase::send
void send(PstreamBuffers &, const List< T > &) const
Do all sends using PstreamBuffers.
Definition: mapDistributeBaseTemplates.C:1128
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:1162
Foam::Map< label >
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:228
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:840
Foam::mapDistributeBase::subHasFlip
bool & subHasFlip()
Does subMap include a sign.
Definition: mapDistributeBase.H:312
Foam::mapDistributeBase::schedulePtr_
autoPtr< List< labelPair > > schedulePtr_
Schedule.
Definition: mapDistributeBase.H:126
mapDistributeBaseTemplates.C
Map.H
NotImplemented
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:419
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:62
labelList.H
Foam::mapDistributeBase::schedule
const List< labelPair > & schedule() const
Return a schedule. Demand driven. See above.
Definition: mapDistributeBase.C:177
Foam::mapDistributeBase::mapDistributeBase
mapDistributeBase()
Construct null.
Definition: mapDistributeBase.C:541
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
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:286
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:194
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::subHasFlip_
bool subHasFlip_
Whether subMap includes flip or not.
Definition: mapDistributeBase.H:119
Foam::mapDistributeBase::subHasFlip
bool subHasFlip() const
Does subMap include a sign.
Definition: mapDistributeBase.H:306
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:885
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:211
Foam::mapDistributeBase::exchangeAddressing
void exchangeAddressing(const int tag, const globalIndex &globalNumbering, labelList &elements, List< Map< label >> &compactMap, labelList &compactStart)
Definition: mapDistributeBase.C:382
Foam::mapDistributeBase::constructSize
label constructSize() const
Constructed data size.
Definition: mapDistributeBase.H:270
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::UPstream::msgType
static int & msgType()
Message tag of standard messages.
Definition: UPstream.H:491
Foam::UPstream::commsTypes
commsTypes
Types of communications.
Definition: UPstream.H:66
Foam::mapDistributeBase::constructMap_
labelListList constructMap_
Maps from subsetted data to new reconstructed data.
Definition: mapDistributeBase.H:116
Foam::List< labelList >
Foam::mapDistributeBase::subMap
labelListList & subMap()
From subsetted data back to original data.
Definition: mapDistributeBase.H:288
Foam::mapDistributeBase::constructSize
label & constructSize()
Constructed data size.
Definition: mapDistributeBase.H:276
Foam::UList< label >
Foam::mapDistributeBase::constructHasFlip
bool & constructHasFlip()
Does constructMap include a sign.
Definition: mapDistributeBase.H:324
Foam::mapPolyMesh
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:160
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:294
Foam::mapDistributeBase::updateMesh
void updateMesh(const mapPolyMesh &)
Correct for topo change.
Definition: mapDistributeBase.H:471
Foam::mapDistributeBase::ClassName
ClassName("mapDistributeBase")
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &)
Definition: boundaryPatch.C:102
labelPair.H