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-2023 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  mapDistributeBaseIO.C
70  mapDistributeBaseTemplates.C
71 
72 \*---------------------------------------------------------------------------*/
73 
74 #ifndef Foam_mapDistributeBase_H
75 #define Foam_mapDistributeBase_H
76 
77 #include "boolList.H"
78 #include "labelList.H"
79 #include "labelPair.H"
80 #include "Pstream.H"
81 #include "Map.H"
82 #include "InfoProxy.H"
83 
84 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
85 
86 namespace Foam
87 {
88 
89 // Forward Declarations
90 class bitSet;
91 class dictionary;
92 class globalIndex;
93 class PstreamBuffers;
94 class mapPolyMesh;
95 class mapDistributeBase;
96 
97 Istream& operator>>(Istream&, mapDistributeBase&);
98 Ostream& operator<<(Ostream&, const mapDistributeBase&);
99 
100 template<>
101 Ostream& operator<<(Ostream&, const InfoProxy<mapDistributeBase>&);
102 
104 /*---------------------------------------------------------------------------*\
105  Class mapDistributeBase Declaration
106 \*---------------------------------------------------------------------------*/
107 
108 class mapDistributeBase
109 {
110 public:
111 
112  //- The map layout (eg, of the constructMap)
113  enum class layoutTypes : char
114  {
115  linear,
116  localFirst
117  };
118 
119 
120 private:
121 
122  // Private Data
123 
124  //- Size of reconstructed data
125  label constructSize_;
126 
127  //- Maps from subsetted data back to original data
128  labelListList subMap_;
129 
130  //- Maps from subsetted data to new reconstructed data
131  labelListList constructMap_;
132 
133  //- Whether subMap includes flip or not
134  bool subHasFlip_;
135 
136  //- Whether constructMap includes flip or not
137  bool constructHasFlip_;
138 
139  //- Communicator to use for parallel operations
140  label comm_;
141 
142  //- Schedule
143  mutable autoPtr<List<labelPair>> schedulePtr_;
144 
145 
146 protected:
147 
148  // Protected Member Functions
149 
150  //- Fatal if expected != received size
151  static void checkReceivedSize
152  (
153  const label proci,
154  const label expectedSize,
155  const label receivedSize
156  );
157 
158  //- Scan the maps for the max addressed index.
159  //
160  // \param maps The maps to scan
161  // \param hasFlip True if maps has flip addressing
162  // \return max-size needed for addressing (eg, constructSize)
163  static label getMappedSize
164  (
165  const labelListList& maps,
166  const bool hasFlip
167  );
168 
169  //- Construct per processor compact addressing of the global elements
170  // needed. The ones from the local processor are not included since
171  // these are always all needed.
173  (
174  const globalIndex& globalNumbering,
175  const labelUList& elements,
176  List<Map<label>>& compactMap
177  ) const;
178 
180  (
181  const globalIndex& globalNumbering,
182  const labelListList& elements,
183  List<Map<label>>& compactMap
184  ) const;
185 
186  void exchangeAddressing
187  (
188  const int tag,
189  const globalIndex& globalNumbering,
190  labelList& elements,
191  List<Map<label>>& compactMap,
192  labelList& compactStart
193  );
194  void exchangeAddressing
195  (
196  const int tag,
197  const globalIndex& globalNumbering,
198  labelListList& elements,
199  List<Map<label>>& compactMap,
200  labelList& compactStart
201  );
202 
203  //- Synchronize send/recv mask buffers as a 'copy' operation.
204  // Somewhat similar to Pstream::exchangeContainer
205  //
206  // The masks must be properly sized by the caller, which avoids
207  // a needless all-to-all for the sizes and the sizing is already
208  // given by the maps.
209  static void exchangeMasks
210  (
211  const UList<bitSet>& sendMasks,
212  UList<bitSet>& recvMasks,
213  const int tag,
214  const label comm
215  );
216 
217  //- Bi-direction sync of send/recv buffers using bitwise '&='
218  //- combine operation.
219  //
220  // The masks must be properly sized by the caller, which avoids
221  // a needless all-to-all for the sizes and the sizing is already
222  // given by the maps.
223  static void unionCombineMasks
224  (
225  UList<bitSet>& sendMasks,
226  UList<bitSet>& recvMasks,
227  const int tag,
228  const label comm
229  );
230 
231 
232  //- Combine field values (after any flip negation operation)
233  //- into the specified mapped target locations
234  template<class T, class CombineOp, class NegateOp>
235  static void flipAndCombine
236  (
238  List<T>& lhs,
240  const UList<T>& rhs,
242  const labelUList& map,
244  const bool hasFlip,
246  const CombineOp& cop,
248  const NegateOp& negOp
249  );
250 
251  //- Lookup field values at specified map indices and save
252  //- after any flip negation operations
253  template<class T, class NegateOp>
254  static void accessAndFlip
255  (
257  List<T>& output,
259  const UList<T>& values,
261  const labelUList& map,
263  const bool hasFlip,
265  const NegateOp& negOp
266  );
267 
268  //- Lookup field values at specified indices and return
269  //- after any flip negation operations
270  template<class T, class NegateOp>
271  static List<T> accessAndFlip
272  (
274  const UList<T>& values,
276  const labelUList& map,
278  const bool hasFlip,
280  const NegateOp& negOp
281  );
282 
283 
284 private:
285 
286  // Private Member Functions
287 
288  //- Helper for compactData (private: filescope only!)
289  // Establishes the exact send/recv elements used after masking.
290  //
291  // \param allowedLocalElems Permissible local mapped elements
292  // (true/false). Can be longer/shorter than actual number
293  // of mapped elements.
294  // \param allowedRemoteElems Permissible remote mapped elements
295  // (true/false). Can be longer/shorter than actual number
296  // of mapped elements.
297  // \param[out] sendMasks Mask of local elements sent to procs.
298  // \param[out] recvMasks Mask of remote elements received
299  // from procs
300  // \param tag The message tag
301  void calcCompactDataRequirements
302  (
303  const bitSet& allowedLocalElems,
304  const bitSet& allowedRemoteElems,
305  List<bitSet>& sendMasks, // [out]
306  List<bitSet>& recvMasks, // [out]
307  const int tag
308  );
309 
310  //- Helper for compactLocalData (private: filescope only!)
311  // Establishes the exact send/recv elements used after masking.
312  //
313  // \param allowedLocalElems Permissible local mapped elements
314  // (true/false). Can be longer/shorter than actual number
315  // of mapped elements.
316  // \param[out] sendMasks Mask of local elements sent to procs.
317  // \param[out] recvMasks Mask of remote elements received by proc.
318  // from procs
319  // \param tag The message tag
320  void calcCompactLocalDataRequirements
321  (
322  const bitSet& allowedLocalElems,
323  List<bitSet>& sendMasks, // [out]
324  List<bitSet>& recvMasks, // [out]
325  const int tag
326  );
327 
328  //- Helper for compactRemoteData (private: filescope only!)
329  // Establishes the exact send/recv elements used after masking.
330  //
331  // \param allowedRemoteElems Permissible remote mapped elements
332  // (true/false). Can be longer/shorter than actual number
333  // of mapped elements.
334  // \param[out] sendMasks Mask of local elements sent to procs.
335  // \param[out] recvMasks Mask of remote elements received by proc.
336  // \param tag The message tag
337  void calcCompactRemoteDataRequirements
338  (
339  const bitSet& allowedRemoteElems,
340  List<bitSet>& sendMasks, // [out]
341  List<bitSet>& recvMasks, // [out]
342  const int tag
343  );
344 
345  //- Implementation for compact{Local,Remote}Data (private).
346  // Also renumbers the subMap/constructMap if oldToNew maps
347  // are notNull().
348  //
349  // No communication
350  void compactData
351  (
352  const UList<bitSet>& sendMasks,
353  const UList<bitSet>& recvMasks,
354  labelList& oldToNewSub,
355  labelList& oldToNewConstruct,
356  const label localSize = -1
357  );
358 
359  //- Wrapper for compactData (private) that supplies oldToNew
360  //- maps for renumbering if doRenumber is true.
361  // No communication
362  void compactDataImpl
363  (
364  const UList<bitSet>& sendMasks,
365  const UList<bitSet>& recvMasks,
366  const bool doRenumber
367  );
368 
369 
370  //- Helper for renumbering compacted map elements and updating the
371  //- supplied old-to-new mapping to account for the visit order of
372  //- the original elements
373  //
374  // \param origElements The original elements visited (eg, meshPoints)
375  // \param[in,out] oldToNew The old-to-new mapping
376  // \param[in,out] mapElements The map to be renumbered
377  // \param hasFlip True if map has flip addressing
378  static void renumberVisitOrder
379  (
380  const labelUList& origElements,
381  labelList& oldToNew,
382  labelListList& maps,
383  const bool hasFlip
384  );
385 
386 
387 public:
388 
389  // Declare name of the class and its debug switch
390  ClassName("mapDistributeBase");
391 
392 
393  // Constructors
394 
395  //- Default construct (uses worldComm)
397 
398  //- Default construct with specified communicator
399  explicit mapDistributeBase(const label comm) noexcept;
400 
401  //- Copy construct
403 
404  //- Move construct
406 
407  //- Read construct from dictionary
408  explicit mapDistributeBase
409  (
410  const dictionary& dict,
411  const label comm = UPstream::worldComm
412  );
413 
414  //- Move construct from components
416  (
417  const label constructSize,
420  const bool subHasFlip = false,
421  const bool constructHasFlip = false,
422  const label comm = UPstream::worldComm
423  );
424 
425  //- Construct from reverse addressing: per data item the send
426  //- processor and the receive processor.
427  //
428  // \note data is not sorted per processor - cannot use printLayout!
430  (
431  const labelUList& sendProcs,
432  const labelUList& recvProcs,
433  const label comm = UPstream::worldComm
434  );
435 
436  //- Construct from list of (possibly) remote elements in globalIndex
437  //- numbering (or -1).
438  //
439  // Determines compact numbering (see above) and distribute map
440  // to get data into this ordering and renumbers the elements to
441  // be in compact numbering.
443  (
444  const globalIndex&,
445  labelList& elements,
446  List<Map<label>>& compactMap,
447  const int tag = UPstream::msgType(),
448  const label comm = UPstream::worldComm
449  );
450 
451  //- Special variant that works with the info sorted into bins
452  //- according to local indices.
453  //
454  // E.g. think cellCells where
455  // cellCells[localCellI] is a list of global cells
457  (
458  const globalIndex&,
459  labelListList& cellCells,
460  List<Map<label>>& compactMap,
461  const int tag = UPstream::msgType(),
462  const label comm = UPstream::worldComm
463  );
464 
465  //- Construct from my elements to send, targetting the specified
466  //- constructMap layout
467  explicit mapDistributeBase
468  (
469  const layoutTypes constructLayout,
471  const bool subHasFlip = false,
472  const bool constructHasFlip = false,
473  const label comm = UPstream::worldComm
474  );
475 
476  //- Construct from my elements to send.
477  // Assumes layout is my elements first followed by elements
478  // from all other processors in consecutive order.
479  explicit mapDistributeBase
480  (
482  const bool subHasFlip = false,
483  const bool constructHasFlip = false,
484  const label comm = UPstream::worldComm
485  );
486 
487  //- Construct from multiple maps and processor collation
488  // Assumes all local data first. Sorts contributions of maps
489  // in processor order i.e. constructed map has all local data first.
490  // Returns
491  // - startOfLocal : per input map the start of the local data. Extends
492  // one beyond number of maps so overall local size
493  // is startOfLocal.last()
494  // - compactMaps : per input map from slot position in the input map
495  // to new slot position. (note there is no information
496  // returned about which processor it is from)
498  (
499  const UPtrList<const mapDistributeBase>& maps,
500  const labelList& localRanks,
501  const label newComm,
502  const labelListList& newToOldRanks, // from rank in newComm to
503  // ranks in (old)comm
504  labelList& startOfLocal, // per map start of local data
505  List<Map<label>>& compactMaps // per map old slot to new slot
506  );
507 
508  //- Construct from Istream
509  explicit mapDistributeBase(Istream& is);
510 
511 
512  // Static Functions
513 
514  //- Test for flip addressing, where flips are encoded as negative
515  //- indices and non-flips are encoded as positive non-zero indices.
516  //
517  // Exits early on the first detected zero or negative, which
518  // makes this more efficient than testing min(map) < 0.
519  //
520  // \note may return a false negative (ie, no flips detected)
521  // even when flip addressing is used, but the local map does not
522  // contain any flipped elements
523  static bool hasFlipAddressing(const labelUList& map);
524 
525  //- Test for flip addressing, where flips are encoded as negative
526  //- indices and non-flips are encoded as positive non-zero indices.
527  //
528  // See notes above.
529  static bool hasFlipAddressing(const labelListList& maps);
530 
531  //- Count the number of unmapped elements.
532  //
533  // \param elements The elements that are expected to be mapped
534  // \param maps The maps to scan
535  // \param hasFlip True if maps has flip addressing
536  // \return number of unmapped elements
537  static label countUnmapped
538  (
539  const labelUList& elements,
540  const labelListList& maps,
541  const bool hasFlip
542  );
543 
544 
545  // Member Functions
546 
547  // Access
548 
549  //- Constructed data size
550  label constructSize() const noexcept
551  {
552  return constructSize_;
553  }
554 
555  //- Constructed data size
556  label& constructSize() noexcept
557  {
558  return constructSize_;
559  }
560 
561  //- From subsetted data back to original data
562  const labelListList& subMap() const noexcept
563  {
564  return subMap_;
565  }
566 
567  //- From subsetted data back to original data
569  {
570  return subMap_;
571  }
572 
573  //- From subsetted data to new reconstructed data
574  const labelListList& constructMap() const noexcept
575  {
576  return constructMap_;
577  }
578 
579  //- From subsetted data to new reconstructed data
581  {
582  return constructMap_;
583  }
584 
585  //- Does subMap include a sign
586  bool subHasFlip() const noexcept
587  {
588  return subHasFlip_;
589  }
590 
591  //- Does subMap include a sign
592  bool& subHasFlip() noexcept
593  {
594  return subHasFlip_;
595  }
596 
597  //- Does constructMap include a sign
598  bool constructHasFlip() const noexcept
599  {
600  return constructHasFlip_;
601  }
602 
603  //- Does constructMap include a sign
604  bool& constructHasFlip() noexcept
605  {
606  return constructHasFlip_;
607  }
608 
609  //- The communicator used
610  label comm() const noexcept
611  {
612  return comm_;
613  }
614 
615  //- The number of sub-lists within the maps
616  label nMaps() const noexcept
617  {
618  return constructMap_.size();
619  }
620 
621  //- The sizes of the subMap lists
622  labelList subMapSizes() const;
623 
624  //- The sizes of the constructMap lists
626 
627  //- The sum of the subMap list sizes
628  label subMapTotalSize() const noexcept;
629 
630  //- The sum of the constructMap list sizes
631  label constructMapTotalSize() const noexcept;
632 
633 
634  // Schedule
635 
636  //- Calculate a communication schedule. See above.
637  static List<labelPair> schedule
638  (
639  const labelListList& subMap,
641  const int tag, // Message tag: msgType()
642  const label comm = UPstream::worldComm
643  );
644 
645  //- Return a schedule. Demand driven. See above.
646  const List<labelPair>& schedule() const;
647 
648  //- Return real or dummy schedule depending on the
649  //- communication type
651  (
652  const UPstream::commsTypes commsType
653  ) const;
654 
655 
656  // Other
658  //- Reset to zero size, only retaining communicator
659  void clear();
660 
661  //- Transfer the contents of the argument and annul the argument.
662  void transfer(mapDistributeBase& rhs);
663 
664  //- Helper for construct from globalIndex. Renumbers element
665  // (in globalIndex numbering) into compact indices.
666  static label renumber
667  (
668  const globalIndex&,
669  const label comm,
670  const List<Map<label>>& compactMap,
671  const label globalElement
672  );
674  //- Helper for renumbering the (compacted) map elements
675  //- using the supplied old-to-new mapping.
676  // Only compacts the maps, does not change the local layout.
677  //
678  // \param[in,out] mapElements The map to be renumbered
679  // \param oldToNew The old-to-new mapping
680  // \param hasFlip True if map has flip addressing
681  //
682  // \return max-size needed for new addressing (eg, constructSize)
683  static label renumberMap
684  (
685  labelListList& mapElements,
686  const labelUList& oldToNew,
687  const bool hasFlip
688  );
690  //- Helper for renumbering the map elements. Assumes local elements
691  //- are first, followed by any remote. Local elements get offset,
692  //- remote elements are mapped.
693  //
694  // \param[in,out] map The map to be renumbered
695  // \param localSize elements < localSize are offset
696  // \param offset offset
697  // \param cMap map for non-local elements
698  // \param hasFlip True if map has flip addressing
699  //
700  // \return max-size needed for new addressing (eg, constructSize)
701  static label renumberMap
702  (
703  labelList& map,
704  const label localSize,
705  const label offset,
706  const Map<label>& cMap,
707  const bool hasFlip
708  );
709 
710  //- Helper for a list of maps. Calls above renumberMap for all elements
711  static label renumberMap
712  (
713  labelListList& mapElements,
714  const label localSize,
715  const label offset,
716  const Map<label>& cMap,
717  const bool hasFlip
718  );
719 
720 
721  // Compaction
722 
723  //- Compact send/receive maps based on selection of
724  //- originating local (send) elements.
725  // Determines and removes the correspondingly unneeded elements
726  // in the send/receive maps.
727  // Only compacts the maps, does not change the local layout.
728  //
729  // \param allowedLocalElems Permissible local mapped elements
730  // (true/false). Can be longer/shorter than actual number
731  // of mapped elements.
732  // \param tag The message tag
733  // \param doRenumber Apply oldToNew internally to renumber
734  // entries (uses renumberMap) and adjust the constructSize
735  //
736  // \note generally preferable to compact based on remote data
737  // (ie, the actual receiver)
738  void compactLocalData
739  (
740  const bitSet& allowedLocalElems,
741  const int tag = UPstream::msgType(),
742  const bool doRenumber = false
743  );
744 
745  //- Compact send/receive maps based on selection of
746  //- remote (receive) elements.
747  // Determines and removes the correspondingly unneeded elements
748  // in the send/receive maps.
749  // Only compacts the maps, does not change the local layout.
750  //
751  // \param allowedRemoteElems Permissible remote mapped elements
752  // (true/false). Can be longer/shorter than actual number
753  // of mapped elements.
754  // \param tag The message tag
755  // \param doRenumber Apply oldToNew internally to renumber
756  // entries (uses renumberMap) and adjust the constructSize
757  void compactRemoteData
758  (
759  const bitSet& allowedRemoteElems,
760  const int tag = UPstream::msgType(),
761  const bool doRenumber = false
762  );
763 
764 
765  //- Compact send/receive maps based on selection of
766  //- originating local (send) elements.
767  //- Returns compaction mappings for subMap and constructMap.
768  //
769  // \param allowedLocalElems Permissible local mapped elements
770  // (true/false). Can be longer/shorter than actual number
771  // of mapped elements.
772  // \param[out] oldToNewSub Old-to-new mapping: subMap
773  // Disabled if labelList::null(),
774  // \param[out] oldToNewConstruct Old-to-new mapping: constructMap
775  // Disabled if labelList::null(),
776  // \param localSize The max index for subMap (-1: auto-detect)
777  // \param tag The message tag
778  //
779  // \note Applies oldToNew to renumber entries
780  // (uses renumberMap) and adjust constructSize
781  //
782  // \note generally preferable to compact based on remote data
783  // (ie, the actual receiver)
784  void compactLocalData
785  (
786  const bitSet& allowedLocalElems,
787  labelList& oldToNewSub,
788  labelList& oldToNewConstruct,
789  const label localSize = -1,
790  const int tag = UPstream::msgType()
791  );
792 
793  //- Compact send/receive maps based on selection of
794  //- remote (receive) elements.
795  //- Returns compaction mappings for subMap and constructMap.
796  //
797  // \param allowedRemoteElems Permissible remote mapped elements
798  // (true/false). Can be longer/shorter than actual number
799  // of mapped elements.
800  // \param[out] oldToNewSub Old-to-new mapping: subMap
801  // Disabled if labelList::null(),
802  // \param[out] oldToNewConstruct Old-to-new mapping: constructMap
803  // Disabled if labelList::null(),
804  // \param localSize The max index for subMap (-1: auto-detect)
805  // \param tag The message tag
806  //
807  // \note Applies oldToNew to renumber entries
808  // (uses renumberMap) and adjust constructSize
809  void compactRemoteData
810  (
811  const bitSet& allowedRemoteElems,
812  labelList& oldToNewSub,
813  labelList& oldToNewConstruct,
814  const label localSize = -1,
815  const int tag = UPstream::msgType()
816  );
817 
818 
819  //- Compact send/receive maps based on selection of
820  //- originating local (send) and remote (receive) elements.
821  //
822  // The resulting compact numbering:
823  // - \c subMap (and \c oldToNewSub) :
824  // will follow the original ordering of \c localElements.
825  // - \c constructMap (and \c oldToNewConstruct) :
826  // will follow the original ordering of \c remoteElements.
827  // .
828  // \warning ill-defined behaviour if \c localElements
829  // or \c remoteElements contains duplicates.
830  void compactData
831  (
832  const labelUList& localElements,
833  const labelUList& remoteElements,
834  labelList& oldToNewSub,
835  labelList& oldToNewConstruct,
836  const label localSize = -1,
837  const int tag = UPstream::msgType()
838  );
839 
840  //- Compact send/receive maps based on selection of
841  //- originating local (send) elements.
842  //
843  // The resulting compact numbering:
844  // - \c subMap (and \c oldToNewSub) :
845  // will follow the original ordering of \c localElements.
846  // - \c constructMap (and \c oldToNewConstruct) :
847  // numbered in simple ascending order.
848  // .
849  // \warning ill-defined behaviour if \c localElements
850  // contains duplicates.
851  //
852  // \note generally preferable to compact based on remote data
853  // (ie, the actual receiver)
854  void compactLocalData
855  (
856  const labelUList& localElements,
857  labelList& oldToNewSub,
858  labelList& oldToNewConstruct,
859  const label localSize = -1,
860  const int tag = UPstream::msgType()
861  );
862 
863  //- Compact send/receive maps based on selection of
864  //- remote (receive) elements.
865  //
866  // The resulting compact numbering:
867  // - \c subMap (and \c oldToNewSub) :
868  // numbered in simple ascending order.
869  // - \c constructMap (and \c oldToNewConstruct) :
870  // will follow the original ordering of \c remoteElements.
871  // .
872  // \warning ill-defined behaviour if \c remoteElements
873  // contains duplicates.
874  void compactRemoteData
875  (
876  const labelUList& remoteElements,
877  labelList& oldToNewSub,
878  labelList& oldToNewConstruct,
879  const label localSize = -1,
880  const int tag = UPstream::msgType()
881  );
882 
883 
884  // Distribute
885 
886  //- Distribute combine data with specified combine operation
887  //- and negate operator (for flips).
888  //
889  // If multiple processors write to same position,
890  // contributions are added using the combine cop.
891  //
892  // \note schedule only used for UPstream::commsTypes::scheduled,
893  // others just use send-to-all, receive-from-all.
894  template<class T, class CombineOp, class NegateOp>
895  static void distribute
896  (
897  const UPstream::commsTypes commsType,
898  const List<labelPair>& schedule,
899  const label constructSize,
900  const labelListList& subMap,
901  const bool subHasFlip,
903  const bool constructHasFlip,
904  List<T>& field,
905  const T& nullValue,
906  const CombineOp& cop,
907  const NegateOp& negOp,
908  const int tag = UPstream::msgType(),
909  const label comm = UPstream::worldComm
910  );
911 
912  //- Distribute assign data with specified negate operator (for flips).
913  //- Uses assignment for combine operation.
914  //
915  // \note schedule only used for UPstream::commsTypes::scheduled,
916  // others just use send-to-all, receive-from-all.
917  template<class T, class NegateOp>
918  static void distribute
919  (
920  const UPstream::commsTypes commsType,
921  const List<labelPair>& schedule,
922  const label constructSize,
923  const labelListList& subMap,
924  const bool subHasFlip,
926  const bool constructHasFlip,
927  List<T>& field,
928  const NegateOp& negOp,
929  const int tag = UPstream::msgType(),
930  const label comm = UPstream::worldComm
931  );
932 
933 
934  // Distribute (simpler interface)
935 
936  //- Distribute List data using default commsType
937  //- and the default flip/negate operator.
938  template<class T>
939  void distribute
940  (
941  List<T>& values,
942  const int tag = UPstream::msgType()
943  ) const;
944 
945  //- Distribute DynamicList data using default commsType
946  //- and the default flip/negate operator.
947  template<class T>
948  void distribute
949  (
950  DynamicList<T>& values,
951  const int tag = UPstream::msgType()
952  ) const;
953 
954  //- Distribute List data using specified commsType
955  //- and the default flip/negate operator.
956  template<class T>
957  void distribute
958  (
959  const UPstream::commsTypes commsType,
960  List<T>& values,
961  const int tag = UPstream::msgType()
962  ) const;
963 
964  //- Distribute DynamicList data using specified commsType
965  //- and the default flip/negate operator.
966  template<class T>
967  void distribute
968  (
969  const UPstream::commsTypes commsType,
970  DynamicList<T>& values,
971  const int tag = UPstream::msgType()
972  ) const;
973 
974  //- Distribute data using default commsType
975  //- and the specified negate operator (for flips).
976  template<class T, class NegateOp>
977  void distribute
978  (
979  List<T>& values,
980  const NegateOp& negOp,
981  const int tag = UPstream::msgType()
982  ) const;
983 
984  //- Distribute data using specified commsType
985  //- and the specified negate operator (for flips).
986  // Accepts a nullValue to initialize unmapped elements
987  // (ie, when the constructSize is larger than the number of
988  // mapped elements).
989  template<class T, class NegateOp>
990  void distribute
991  (
992  const UPstream::commsTypes commsType,
993  List<T>& values,
994  const NegateOp& negOp,
995  const int tag = UPstream::msgType()
996  ) const;
997 
998  //- Distribute data using specified commsType
999  //- and the specified negate operator (for flips).
1000  // Accepts a nullValue to initialize unmapped elements
1001  // (ie, when the constructSize is larger than the number of
1002  // mapped elements).
1003  template<class T, class NegateOp>
1004  void distribute
1005  (
1006  const UPstream::commsTypes commsType,
1007  const T& nullValue,
1008  List<T>& values,
1009  const NegateOp& negOp,
1010  const int tag = UPstream::msgType()
1011  ) const;
1012 
1013 
1014  // Reverse Distribute (simpler interface)
1015 
1016  //- Reverse distribute data using default commsType
1017  //- and the default flip/negate operator
1018  template<class T>
1019  void reverseDistribute
1020  (
1021  const label constructSize,
1022  List<T>& values,
1023  const int tag = UPstream::msgType()
1024  ) const;
1025 
1026  //- Reverse distribute data using default commsType
1027  //- and the default flip/negate operator.
1028  // Accepts a nullValue to initialize unmapped elements
1029  // (ie, when the constructSize is larger than the subMap).
1030  template<class T>
1031  void reverseDistribute
1032  (
1033  const label constructSize,
1034  const T& nullValue,
1035  List<T>& values,
1036  const int tag = UPstream::msgType()
1037  ) const;
1038 
1039  //- Reverse distribute data using specified commsType
1040  //- and the default flip/negate operator
1041  template<class T>
1042  void reverseDistribute
1043  (
1044  const UPstream::commsTypes commsType,
1045  const label constructSize,
1046  List<T>& values,
1047  const int tag = UPstream::msgType()
1048  ) const;
1049 
1050  //- Reverse distribute data using specified commsType
1051  //- and the specified flip/negate operator.
1052  template<class T, class NegateOp>
1053  void reverseDistribute
1054  (
1055  const UPstream::commsTypes commsType,
1056  const label constructSize,
1057  List<T>& values,
1058  const NegateOp& negOp,
1059  const int tag = UPstream::msgType()
1060  ) const;
1061 
1062  //- Reverse distribute data using specified commsType
1063  //- and the default flip/negate operator.
1064  // Accepts a nullValue to initialize unmapped elements
1065  // (ie, when the constructSize is larger than the subMap).
1066  template<class T>
1067  void reverseDistribute
1068  (
1069  const UPstream::commsTypes commsType,
1070  const label constructSize,
1071  const T& nullValue,
1072  List<T>& values,
1073  const int tag = UPstream::msgType()
1074  ) const;
1075 
1076 
1077  // Send/Receive
1078 
1079  //- Do all sends using PstreamBuffers
1080  template<class T>
1081  void send(PstreamBuffers& pBufs, const List<T>& field) const;
1082 
1083  //- Do all receives using PstreamBuffers
1084  template<class T>
1085  void receive(PstreamBuffers& pBufs, List<T>& field) const;
1086 
1087 
1088  // Low-level. Non-blocking. TBD: receive map is usually contiguous
1089  // - receive into slices
1090 
1091  //- Start all sends and receives (always non-blocking)
1092  template<class T, class negateOp>
1093  static void send
1094  (
1095  const labelListList& subMap,
1096  const bool subHasFlip,
1097  const labelListList& constructMap,
1098  const bool constructHasFlip,
1099  const UList<T>& field,
1100  labelRange& sendRequests,
1101  PtrList<List<T>>& sendFields,
1102  labelRange& recvRequests,
1103  PtrList<List<T>>& recvFields,
1104  const negateOp& negOp,
1105  const int tag,
1106  const label comm
1107  );
1108 
1109  //- Start all sends and receives (always non-blocking)
1110  template<class T>
1111  void send
1112  (
1113  const UList<T>& field,
1114  labelRange& sendRequests,
1115  PtrList<List<T>>& sendFields,
1116  labelRange& recvRequests,
1117  PtrList<List<T>>& recvFields,
1118  const int tag = UPstream::msgType()
1119  ) const;
1120 
1121  //- Wait for (receive) requests to finish and consume
1122  template<class T, class CombineOp, class negateOp>
1123  static void receive
1124  (
1125  const label constructSize,
1126  const labelListList& constructMap,
1127  const bool constructHasFlip,
1128  const labelRange& requests,
1129  const UPtrList<List<T>>& recvFields,
1130  List<T>& field,
1131  const CombineOp& cop,
1132  const negateOp& negOp,
1133  const int tag,
1134  const label comm
1135  );
1136 
1137  //- Wait for (receive) requests to finish and consume
1138  template<class T>
1139  void receive
1140  (
1141  const labelRange& requests,
1142  const UPtrList<List<T>>& recvFields,
1143  List<T>& field,
1144  const int tag = UPstream::msgType()
1145  ) const;
1146 
1147 
1148  // Other
1149 
1150  //- Debug: print layout. Can only be used on maps with sorted
1151  // storage (local data first, then non-local data)
1152  void printLayout(Ostream& os) const;
1153 
1154 
1155  // Member Operators
1156 
1157  //- Copy assignment
1158  void operator=(const mapDistributeBase& rhs);
1159 
1160  //- Move assignment
1161  void operator=(mapDistributeBase&& rhs);
1162 
1163 
1164  // IOstream Operators
1165 
1166  //- Return info proxy,
1167  //- used to print summary information to a stream
1169  {
1170  return *this;
1171  }
1172 
1173  //- Read entries from dictionary format
1174  void readDict(const dictionary& dict);
1175 
1176  //- Write entries in dictionary format
1177  void writeEntries(Ostream& os) const;
1178 
1179  //- Read plain content (not dictionary) from Istream
1181 
1182  //- Write plain content (not dictionary) to Ostream
1183  friend Ostream& operator<<(Ostream&, const mapDistributeBase&);
1184 
1185 
1186  // Housekeeping
1187 
1188  //- No correction for topo change
1189  void updateMesh(const mapPolyMesh&)
1190  {
1192  }
1193 
1194  //- OpenFOAM-v2112 and earlier naming for compactRemoteData()
1195  //- using boolList.
1196  void compact
1197  (
1198  const boolList& remoteElemUsed,
1199  const int tag = UPstream::msgType()
1200  );
1201 
1202  //- OpenFOAM-v2112 and earlier naming for compactRemoteData().
1203  //- using boolList.
1204  void compact
1205  (
1206  const boolList& remoteElemUsed,
1207  const label localSize,
1208  labelList& oldToNewSub,
1209  labelList& oldToNewConstruct,
1210  const int tag = UPstream::msgType()
1211  );
1212 };
1213 
1214 
1215 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
1216 
1217 } // End namespace Foam
1218 
1219 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
1220 
1221 #ifdef NoRepository
1222  #include "mapDistributeBaseTemplates.C"
1223 #endif
1224 
1225 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
1226 
1227 #endif
1228 
1229 // ************************************************************************* //
void calcCompactAddressing(const globalIndex &globalNumbering, const labelUList &elements, List< Map< label >> &compactMap) const
Construct per processor compact addressing of the global elements.
dictionary dict
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
static label countUnmapped(const labelUList &elements, const labelListList &maps, const bool hasFlip)
Count the number of unmapped elements.
void compactLocalData(const bitSet &allowedLocalElems, const int tag=UPstream::msgType(), const bool doRenumber=false)
Compact send/receive maps based on selection of originating local (send) elements.
rDeltaTY field()
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
const List< labelPair > & schedule() const
Return a schedule. Demand driven. See above.
static void accessAndFlip(List< T > &output, const UList< T > &values, const labelUList &map, const bool hasFlip, const NegateOp &negOp)
Lookup field values at specified map indices and save after any flip negation operations.
void send(PstreamBuffers &pBufs, const List< T > &field) const
Do all sends using PstreamBuffers.
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:52
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
friend Ostream & operator<<(Ostream &, const mapDistributeBase &)
Write plain content (not dictionary) to Ostream.
label constructSize() const noexcept
Constructed data size.
label comm() const noexcept
The communicator used.
void compactRemoteData(const bitSet &allowedRemoteElems, const int tag=UPstream::msgType(), const bool doRenumber=false)
Compact send/receive maps based on selection of remote (receive) elements.
label nMaps() const noexcept
The number of sub-lists within the maps.
static int & msgType() noexcept
Message tag of standard messages.
Definition: UPstream.H:1229
InfoProxy< mapDistributeBase > info() const noexcept
Return info proxy, used to print summary information to a stream.
List< labelList > labelListList
List of labelList.
Definition: labelList.H:38
Class containing mesh-to-mesh mapping information after a change in polyMesh topology.
Definition: mapPolyMesh.H:157
friend Istream & operator>>(Istream &, mapDistributeBase &)
Read plain content (not dictionary) from Istream.
bool subHasFlip() const noexcept
Does subMap include a sign.
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:164
Calculates a unique integer (label so might not have enough room - 2G max) for processor + local inde...
Definition: globalIndex.H:61
layoutTypes
The map layout (eg, of the constructMap)
const List< labelPair > & whichSchedule(const UPstream::commsTypes commsType) const
Return real or dummy schedule depending on the communication type.
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:51
static bool hasFlipAddressing(const labelUList &map)
Test for flip addressing, where flips are encoded as negative indices and non-flips are encoded as po...
void readDict(const dictionary &dict)
Read entries from dictionary format.
static void exchangeMasks(const UList< bitSet > &sendMasks, UList< bitSet > &recvMasks, const int tag, const label comm)
Synchronize send/recv mask buffers as a &#39;copy&#39; operation.
void compact(const boolList &remoteElemUsed, const int tag=UPstream::msgType())
OpenFOAM-v2112 and earlier naming for compactRemoteData() using boolList.
Istream & operator>>(Istream &, directionInfo &)
void printLayout(Ostream &os) const
Debug: print layout. Can only be used on maps with sorted.
void reverseDistribute(const label constructSize, List< T > &values, const int tag=UPstream::msgType()) const
Reverse distribute data using default commsType and the default flip/negate operator.
void transfer(mapDistributeBase &rhs)
Transfer the contents of the argument and annul the argument.
A list of pointers to objects of type <T>, without allocation/deallocation management of the pointers...
Definition: HashTable.H:106
void updateMesh(const mapPolyMesh &)
No correction for topo change.
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:56
const direction noexcept
Definition: Scalar.H:258
bool constructHasFlip() const noexcept
Does constructMap include a sign.
Pair< label > labelPair
A pair of labels.
Definition: Pair.H:51
OBJstream os(runTime.globalPath()/outputName)
Buffers for inter-processor communications streams (UOPstream, UIPstream).
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
label subMapTotalSize() const noexcept
The sum of the subMap list sizes.
void receive(PstreamBuffers &pBufs, List< T > &field) const
Do all receives using PstreamBuffers.
static void unionCombineMasks(UList< bitSet > &sendMasks, UList< bitSet > &recvMasks, const int tag, const label comm)
Bi-direction sync of send/recv buffers using bitwise &#39;&=&#39; combine operation.
const labelListList & constructMap() const noexcept
From subsetted data to new reconstructed data.
static label renumber(const globalIndex &, const label comm, const List< Map< label >> &compactMap, const label globalElement)
Helper for construct from globalIndex. Renumbers element.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
static void flipAndCombine(List< T > &lhs, const UList< T > &rhs, const labelUList &map, const bool hasFlip, const CombineOp &cop, const NegateOp &negOp)
Combine field values (after any flip negation operation) into the specified mapped target locations...
A helper class for outputting values to Ostream.
Definition: ensightCells.H:43
void exchangeAddressing(const int tag, const globalIndex &globalNumbering, labelList &elements, List< Map< label >> &compactMap, labelList &compactStart)
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:59
labelList subMapSizes() const
The sizes of the subMap lists.
void clear()
Reset to zero size, only retaining communicator.
A list of pointers to objects of type <T>, with allocation/deallocation management of the pointers...
Definition: List.H:55
ClassName("mapDistributeBase")
static void checkReceivedSize(const label proci, const label expectedSize, const label receivedSize)
Fatal if expected != received size.
static label getMappedSize(const labelListList &maps, const bool hasFlip)
Scan the maps for the max addressed index.
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:44
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
label constructMapTotalSize() const noexcept
The sum of the constructMap list sizes.
List< label > labelList
A List of labels.
Definition: List.H:62
mapDistributeBase() noexcept
Default construct (uses worldComm)
const labelListList & subMap() const noexcept
From subsetted data back to original data.
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:686
List< bool > boolList
A List of bools.
Definition: List.H:60
labelList constructMapSizes() const
The sizes of the constructMap lists.
Inter-processor communications stream.
Definition: UPstream.H:60
void writeEntries(Ostream &os) const
Write entries in dictionary format.
static void distribute(const UPstream::commsTypes commsType, const List< labelPair > &schedule, const label constructSize, const labelListList &subMap, const bool subHasFlip, const labelListList &constructMap, const bool constructHasFlip, List< T > &field, const T &nullValue, const CombineOp &cop, const NegateOp &negOp, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
Distribute combine data with specified combine operation and negate operator (for flips)...
Namespace for OpenFOAM.
static label renumberMap(labelListList &mapElements, const labelUList &oldToNew, const bool hasFlip)
Helper for renumbering the (compacted) map elements using the supplied old-to-new mapping...
Local processor first, others in linear order.