globalIndex.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) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2018-2019 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::globalIndex
29 
30 Description
31  Calculates a unique integer (label so might not have enough room - 2G max)
32  for processor + local index. E.g.
33 
34  globalIndex globalFaces(mesh.nFaces());
35  label globalFacei = globalFaces.toGlobal(facei);
36 
37 SourceFiles
38  globalIndexI.H
39  globalIndex.C
40  globalIndexTemplates.C
41 
42 \*---------------------------------------------------------------------------*/
43 
44 #ifndef globalIndex_H
45 #define globalIndex_H
46 
47 #include "Pstream.H"
48 #include "CompactListList.H"
49 #include "DynamicList.H"
50 
51 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
52 
53 namespace Foam
54 {
55 
56 // Forward declarations
57 
58 class globalIndex;
59 class labelRange;
60 
61 Istream& operator>>(Istream& is, globalIndex& gi);
62 Ostream& operator<<(Ostream& os, const globalIndex& gi);
63 
64 
65 /*---------------------------------------------------------------------------*\
66  Class globalIndex Declaration
67 \*---------------------------------------------------------------------------*/
68 
69 class globalIndex
70 {
71  // Private Member Functions
72 
73  //- Sort and bin. validBins contains bins with non-zero size.
74  static void bin
75  (
76  const labelUList& offsets,
77  const labelUList& globalIds,
78  labelList& order,
79  CompactListList<label>& sortedElems,
80  DynamicList<label>& validBins
81  );
82 
83 
84  // Private data
85 
86  //- Start of proci. Size is nProcs()+1. (so like CompactListList)
87  labelList offsets_;
88 
89 
90 public:
91 
92  // Constructors
93 
94  //- Construct null
95  globalIndex() = default;
96 
97  //- Construct from local max size.
98  // Does communication with default communicator and message tag.
99  inline explicit globalIndex(const label localSize);
100 
101  //- Construct from local max size.
102  // Does communication with given communicator and message tag
103  inline globalIndex
104  (
105  const label localSize,
106  const int tag,
107  const label comm,
108  const bool parallel
109  );
110 
111  //- Copy construct from list of labels
112  inline explicit globalIndex(const labelUList& offsets);
113 
114  //- Move construct from list of labels
115  inline explicit globalIndex(labelList&& offsets);
116 
117  //- Construct from Istream
118  explicit globalIndex(Istream& is);
119 
120 
121  // Member Functions
122 
123  //- Check for null constructed or global sum == 0
124  inline bool empty() const;
125 
126  //- Const-access to the offsets
127  inline const labelList& offsets() const;
128 
129  //- Global sum of localSizes
130  inline label size() const;
131 
132  //- The local sizes
133  labelList sizes() const;
134 
135 
136  // Edit
137 
138  //- Write-access to the offsets, for changing after construction
139  inline labelList& offsets();
140 
141  //- Reset from local size.
142  // Does communication with default communicator and message tag.
143  inline void reset(const label localSize);
144 
145  //- Reset from local size.
146  // Does communication with given communicator and message tag
147  void reset
148  (
149  const label localSize,
150  const int tag,
151  const label comm,
152  const bool parallel
153  );
154 
155 
156  // Queries relating to my processor (using world communicator)
157 
158  //- My local start
159  inline label localStart() const;
160 
161  //- My local size
162  inline label localSize() const;
163 
164  //- Return start/size range of local processor data
165  inline labelRange range() const;
166 
167  //- Is on local processor
168  inline bool isLocal(const label i) const;
169 
170  //- From local to global index
171  inline label toGlobal(const label i) const;
172 
173  //- From local to global index
174  inline labelList toGlobal(const labelUList& labels) const;
175 
176  //- From local to global index (inplace)
177  inline void inplaceToGlobal(labelList& labels) const;
178 
179  //- From global to local on current processor.
180  // FatalError if not on local processor.
181  inline label toLocal(const label i) const;
182 
183 
184  // Global queries
185 
186  //- Start of proci data
187  inline label offset(const label proci) const;
188 
189  //- Start of proci data
190  inline label localStart(const label proci) const;
191 
192  //- Size of proci data
193  inline label localSize(const label proci) const;
194 
195  //- Return start/size range of proci data
196  inline labelRange range(const label proci) const;
197 
198  //- Is on processor proci
199  inline bool isLocal(const label proci, const label i) const;
200 
201  //- From local to global on proci
202  inline label toGlobal(const label proci, const label i) const;
203 
204  //- From local to global on proci
205  inline labelList toGlobal
206  (
207  const label proci,
208  const labelUList& labels
209  ) const;
210 
211  //- From local to global index on proci (inplace)
212  inline void inplaceToGlobal
213  (
214  const label proci,
215  labelList& labels
216  ) const;
217 
218 
219  //- From global to local on proci
220  inline label toLocal(const label proci, const label i) const;
221 
222  //- Which processor does global come from? Binary search.
223  inline label whichProcID(const label i) const;
224 
225 
226  // Other
227 
228  //- Collect data in processor order on master (== procIDs[0]).
229  // Needs offsets only on master.
230  template<class Container, class Type>
231  static void gather
232  (
233  const labelUList& offsets,
234  const label comm,
235  const Container& procIDs,
236  const UList<Type>& fld,
237  List<Type>& allFld,
238  const int tag = UPstream::msgType(),
239  const Pstream::commsTypes commsType =
241  );
242 
243  //- Collect data in processor order on master (== procIDs[0]).
244  // Needs offsets only on master.
245  template<class Container, class Type>
246  void gather
247  (
248  const label comm,
249  const Container& procIDs,
250  const UList<Type>& fld,
251  List<Type>& allFld,
252  const int tag = UPstream::msgType(),
253  const Pstream::commsTypes commsType =
255  ) const
256  {
257  gather(offsets_, comm, procIDs, fld, allFld, tag, commsType);
258  }
259 
260  //- Collect data in processor order on master.
261  // Does communication with default communicator and message tag.
262  template<class Type>
263  void gather
264  (
265  const UList<Type>& fld,
266  List<Type>& allFld,
267  const int tag = UPstream::msgType(),
268  const Pstream::commsTypes commsType =
270  ) const;
271 
272  //- Collect data in processor order on master.
273  // Does communication with default communicator and message tag.
274  template<class Type>
275  static void gatherOp
276  (
277  const UList<Type>& fld,
278  List<Type>& allFld,
279  const int tag = UPstream::msgType(),
280  const Pstream::commsTypes commsType =
282  );
283 
284 
285  //- Inplace collect in processor order on master (== procIDs[0]).
286  //- Needs offsets only on master.
287  template<class Container, class Type>
288  static void gather
289  (
290  const labelUList& offsets,
291  const label comm,
292  const Container& procIDs,
293  List<Type>& fld,
294  const int tag = UPstream::msgType(),
295  const Pstream::commsTypes commsType =
297  );
298 
299  //- Inplace collect in processor order on master (== procIDs[0]).
300  //- Needs offsets only on master.
301  template<class Container, class Type>
302  void gather
303  (
304  const label comm,
305  const Container& procIDs,
306  List<Type>& fld,
307  const int tag = UPstream::msgType(),
308  const Pstream::commsTypes commsType =
310  ) const
311  {
312  gather(offsets_, comm, procIDs, fld, tag, commsType);
313  }
314 
315  //- Inplace collect data in processor order on master
316  // Does communication with default communicator and message tag.
317  // After the gather, the field is zero-sized on the slaves.
318  template<class Type>
319  void gather
320  (
321  List<Type>& fld,
322  const int tag = UPstream::msgType(),
323  const Pstream::commsTypes commsType =
325  ) const;
326 
327  //- Inplace collect data in processor order on master
328  // Does communication with default communicator and message tag.
329  // After the gather, the field is zero-sized on the slaves.
330  template<class Type>
331  static void gatherOp
332  (
333  List<Type>& fld,
334  const int tag = UPstream::msgType(),
335  const Pstream::commsTypes commsType =
337  );
338 
339 
340  //- Distribute data in processor order. Requires fld to be sized!
341  template<class Container, class Type>
342  static void scatter
343  (
344  const labelUList& offsets,
345  const label comm,
346  const Container& procIDs,
347  const UList<Type>& allFld,
348  UList<Type>& fld,
349  const int tag = UPstream::msgType(),
350  const Pstream::commsTypes commsType =
352  );
353 
354  //- Distribute data in processor order. Requires fld to be sized!
355  template<class Container, class Type>
356  void scatter
357  (
358  const label comm,
359  const Container& procIDs,
360  const UList<Type>& allFld,
361  UList<Type>& fld,
362  const int tag = UPstream::msgType(),
363  const Pstream::commsTypes commsType =
365  ) const
366  {
367  scatter(offsets_, comm, procIDs, allFld, fld, tag, commsType);
368  }
369 
370  //- Distribute data in processor order. Requires fld to be sized!
371  // Does communication with default communicator and message tag.
372  template<class Type>
373  void scatter
374  (
375  const UList<Type>& allFld,
376  UList<Type>& fld,
377  const int tag = UPstream::msgType(),
378  const Pstream::commsTypes commsType =
380  ) const;
381 
382  //- Get (potentially remote) data. Elements required given as
383  // global indices
384  template<class Type, class CombineOp>
385  void get
386  (
387  List<Type>& allFld,
388  const labelUList& globalIds,
389  const CombineOp& cop,
390  const label comm = Pstream::worldComm,
391  const int tag = UPstream::msgType()
392  ) const;
393 
394 
395  // IOstream Operators
396 
397  friend Istream& operator>>(Istream& is, globalIndex& gi);
398  friend Ostream& operator<<(Ostream& os, const globalIndex& gi);
399 };
400 
401 
402 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
403 
404 } // End namespace Foam
405 
406 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
407 
408 #include "globalIndexI.H"
409 
410 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
411 
412 #ifdef NoRepository
413  #include "globalIndexTemplates.C"
414 #endif
415 
416 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
417 
418 #endif
419 
420 // ************************************************************************* //
globalIndexTemplates.C
Foam::UPstream::commsTypes::nonBlocking
Foam::globalIndex::localStart
label localStart() const
My local start.
Definition: globalIndexI.H:112
Foam::DynamicList< label >
Foam::globalIndex::range
labelRange range() const
Return start/size range of local processor data.
Definition: globalIndexI.H:136
Foam::CompactListList
A packed storage unstructured matrix of objects of type <T> using an offset table for access.
Definition: polyTopoChange.H:93
Foam::globalIndex::sizes
labelList sizes() const
The local sizes.
Definition: globalIndex.C:140
Foam::globalIndex::localSize
label localSize() const
My local size.
Definition: globalIndexI.H:124
Foam::globalIndex::isLocal
bool isLocal(const label i) const
Is on local processor.
Definition: globalIndexI.H:148
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::globalIndex::inplaceToGlobal
void inplaceToGlobal(labelList &labels) const
From local to global index (inplace)
Definition: globalIndexI.H:207
Foam::globalIndex::empty
bool empty() const
Check for null constructed or global sum == 0.
Definition: globalIndexI.H:70
Foam::globalIndex::operator>>
friend Istream & operator>>(Istream &is, globalIndex &gi)
globalIndexI.H
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::globalIndex::offsets
const labelList & offsets() const
Const-access to the offsets.
Definition: globalIndexI.H:76
Foam::labelRange
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:58
Foam::globalIndex::gatherOp
static void gatherOp(const UList< Type > &fld, List< Type > &allFld, const int tag=UPstream::msgType(), const Pstream::commsTypes commsType=Pstream::commsTypes::nonBlocking)
Collect data in processor order on master.
Definition: globalIndexTemplates.C:208
Foam::globalIndex::operator<<
friend Ostream & operator<<(Ostream &os, const globalIndex &gi)
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::globalIndex::get
void get(List< Type > &allFld, const labelUList &globalIds, const CombineOp &cop, const label comm=Pstream::worldComm, const int tag=UPstream::msgType()) const
Get (potentially remote) data. Elements required given as.
Definition: globalIndexTemplates.C:467
Foam::globalIndex::scatter
static void scatter(const labelUList &offsets, const label comm, const Container &procIDs, const UList< Type > &allFld, UList< Type > &fld, const int tag=UPstream::msgType(), const Pstream::commsTypes commsType=Pstream::commsTypes::nonBlocking)
Distribute data in processor order. Requires fld to be sized!
Definition: globalIndexTemplates.C:286
Pstream.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::globalIndex
Calculates a unique integer (label so might not have enough room - 2G max) for processor + local inde...
Definition: globalIndex.H:68
Foam::globalIndex::reset
void reset(const label localSize)
Reset from local size.
Definition: globalIndexI.H:94
Foam::globalIndex::toLocal
label toLocal(const label i) const
From global to local on current processor.
Definition: globalIndexI.H:229
Foam::UPstream::msgType
static int & msgType()
Message tag of standard messages.
Definition: UPstream.H:492
Foam::UPstream::commsTypes
commsTypes
Types of communications.
Definition: UPstream.H:66
Foam::globalIndex::size
label size() const
Global sum of localSizes.
Definition: globalIndexI.H:88
Foam::UPstream::worldComm
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:285
Foam::List< label >
Foam::globalIndex::whichProcID
label whichProcID(const label i) const
Which processor does global come from? Binary search.
Definition: globalIndexI.H:235
Foam::UList< label >
Foam::globalIndex::globalIndex
globalIndex()=default
Construct null.
Foam::globalIndex::offset
label offset(const label proci) const
Start of proci data.
Definition: globalIndexI.H:100
DynamicList.H
CompactListList.H
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::globalIndex::toGlobal
label toGlobal(const label i) const
From local to global index.
Definition: globalIndexI.H:164
Foam::globalIndex::gather
static void gather(const labelUList &offsets, const label comm, const Container &procIDs, const UList< Type > &fld, List< Type > &allFld, const int tag=UPstream::msgType(), const Pstream::commsTypes commsType=Pstream::commsTypes::nonBlocking)
Collect data in processor order on master (== procIDs[0]).
Definition: globalIndexTemplates.C:35