globalIndexI.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-2016 OpenFOAM Foundation
9 Copyright (C) 2018-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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\*---------------------------------------------------------------------------*/
28
29#include "ListOps.H"
30#include "labelRange.H"
31
32// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33
35(
36 const labelUList& listOffsets
37)
38{
39 if (listOffsets.size() > 1)
40 {
41 offsets_ = listOffsets;
42 }
43}
44
45
47(
48 labelList&& listOffsets
49)
50{
51 if (listOffsets.size() > 1)
52 {
53 offsets_.transfer(listOffsets);
54 }
55 else
56 {
57 listOffsets.clear();
58 }
59}
60
61
63(
64 const labelUList& offsetsOrSizes,
65 enum globalIndex::accessType accType
66)
67{
68 if (accType == accessType::SIZES)
69 {
70 reset(offsetsOrSizes);
71 }
72 else if (offsetsOrSizes.size() > 1)
73 {
74 // accessType::OFFSETS
75 offsets_ = offsetsOrSizes;
76 }
77}
78
79
81(
82 const label localSize,
83 const label comm,
84 const bool parallel
85)
86{
87 reset(localSize, comm, parallel);
88}
89
90
92(
93 const label localSize,
95 const label comm
96)
97{
98 // Gather sizes (one-sided)
99 reset(UPstream::listGatherValues(localSize, comm));
100}
101
102
104(
105 const label localSize,
107 const label /* comm (ignored) */
108)
109:
110 offsets_(2)
111{
112 offsets_[0] = 0;
113 offsets_[1] = localSize;
114}
115
116
117// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
118
119inline bool Foam::globalIndex::empty() const
120{
121 return offsets_.empty() || offsets_.last() == 0;
122}
123
124
125inline Foam::label Foam::globalIndex::totalSize() const
126{
127 const label len = (offsets_.size() - 1);
128 return (len < 1) ? static_cast<label>(0) : offsets_[len];
129}
130
131
132inline Foam::label Foam::globalIndex::size() const
133{
134 return totalSize();
135}
136
137
139{
140 return localSizes();
141}
142
143
144inline Foam::label Foam::globalIndex::nProcs() const noexcept
145{
146 const label len = (offsets_.size() - 1);
147 return (len < 1) ? static_cast<label>(0) : len;
148}
149
150
152{
153 // Proc 0 -> nProcs
154 const label len = (offsets_.size() - 1);
155 return (len < 1) ? labelRange() : labelRange(0, len);
156}
157
158
160{
161 // Proc 1 -> nProcs
162 const label len = (offsets_.size() - 2);
163 return (len < 1) ? labelRange() : labelRange(1, len);
164}
165
166
168{
169 return offsets_;
170}
171
172
174{
175 return offsets_;
176}
177
178
180{
181 const label len = (offsets_.size() - 1);
182
183 if (len < 1) return labelUList::null();
184
185 return labelList::subList(offsets_, len);
186}
187
188
189inline Foam::label Foam::globalIndex::localStart(const label proci) const
190{
191 return offsets_[proci];
192}
193
194
195inline Foam::label Foam::globalIndex::localStart() const
196{
197 return localStart(Pstream::myProcNo());
198}
199
200
201inline Foam::label Foam::globalIndex::localSize(const label proci) const
202{
203 return offsets_[proci+1] - offsets_[proci];
204}
205
206
207inline Foam::label Foam::globalIndex::localSize() const
208{
209 return localSize(Pstream::myProcNo());
210}
211
212
213inline Foam::label Foam::globalIndex::maxSize() const
214{
215 // Use out-of-range proci to avoid excluding any processor
216 return maxNonLocalSize(-1);
217}
218
219
220inline Foam::label Foam::globalIndex::maxNonLocalSize() const
221{
222 return maxNonLocalSize(Pstream::myProcNo());
223}
224
225
226inline Foam::labelRange Foam::globalIndex::range(const label proci) const
227{
228 return labelRange(offsets_[proci], offsets_[proci+1] - offsets_[proci]);
229}
230
231
233{
234 return range(Pstream::myProcNo());
235}
236
237
238inline bool Foam::globalIndex::isLocal(const label proci, const label i) const
239{
240 return i >= offsets_[proci] && i < offsets_[proci+1];
241}
242
243
244inline bool Foam::globalIndex::isLocal(const label i) const
245{
246 return isLocal(Pstream::myProcNo(), i);
247}
248
249
251(
252 const label proci,
253 const label i
254) const
255{
256 return i + offsets_[proci];
257}
258
259
260inline Foam::label Foam::globalIndex::toGlobal(const label i) const
261{
262 return toGlobal(Pstream::myProcNo(), i);
263}
264
265
267(
268 const label proci,
269 const labelUList& labels
270) const
271{
272 labelList result(labels);
273 inplaceToGlobal(proci, result);
274
275 return result;
276}
277
278
280(
281 const labelUList& labels
282) const
283{
284 return toGlobal(Pstream::myProcNo(), labels);
285}
286
287
289(
290 const label proci,
291 labelUList& labels
292) const
293{
294 const label off = offsets_[proci];
295
296 for (label& val : labels)
297 {
298 val += off;
299 }
300}
301
302
304{
305 inplaceToGlobal(Pstream::myProcNo(), labels);
306}
307
308
309inline Foam::label
310Foam::globalIndex::toLocal(const label proci, const label i) const
311{
312 const label locali = i - offsets_[proci];
313
314 if (locali < 0 || i >= offsets_[proci+1])
315 {
317 << "Global " << i << " does not belong on processor "
318 << proci << nl << "Offsets:" << offsets_
319 << abort(FatalError);
320 }
321 return locali;
322}
323
324
325inline Foam::label Foam::globalIndex::toLocal(const label i) const
326{
327 return toLocal(Pstream::myProcNo(), i);
328}
329
330
331inline Foam::label Foam::globalIndex::whichProcID(const label i) const
332{
333 if (i < 0 || i >= totalSize())
334 {
336 << "Global " << i << " does not belong on any processor."
337 << " Offsets:" << offsets_
338 << abort(FatalError);
339 }
340
341 const label proci(Pstream::myProcNo());
342
343 return isLocal(proci, i) ? proci : findLower(offsets_, i+1);
344}
345
346
347// * * * * * * * * * * * * * * * * Iterators * * * * * * * * * * * * * * * * //
348
349inline Foam::globalIndex::const_iterator::
350const_iterator
351(
352 const globalIndex* globalIdx,
353 const label i
354) noexcept
355:
356 parent_(globalIdx),
357 proci_(i)
358{}
359
360
361inline Foam::label Foam::globalIndex::const_iterator::
362proci() const noexcept
363{
364 return proci_;
365}
366
367
368inline Foam::label Foam::globalIndex::const_iterator::
369start() const
370{
371 return (*parent_).localStart(proci_);
372}
373
374
375inline Foam::label Foam::globalIndex::const_iterator::
376size() const
377{
378 return (*parent_).localSize(proci_);
379}
380
381
382inline Foam::labelRange Foam::globalIndex::const_iterator::
383range() const
384{
385 return (*parent_).range(proci_);
386}
387
388
389inline Foam::labelRange Foam::globalIndex::const_iterator::
390operator*() const
391{
392 return this->range();
393}
394
395
397Foam::globalIndex::const_iterator::
398operator++()
399{
400 ++proci_;
401 return *this;
402}
403
404
406Foam::globalIndex::const_iterator::
407operator++(int)
408{
409 const_iterator old(*this);
410 ++proci_;
411 return old;
412}
413
414
416Foam::globalIndex::const_iterator::
417operator--()
418{
419 --proci_;
420 return *this;
421}
422
423
425Foam::globalIndex::const_iterator::
426operator--(int)
427{
428 const_iterator old(*this);
429 --proci_;
430 return old;
431}
432
433
434inline bool
435Foam::globalIndex::const_iterator::
436operator==
437(
438 const const_iterator& iter
439) const noexcept
440{
441 return (proci_ == iter.proci_);
442}
443
444
445inline bool
446Foam::globalIndex::const_iterator::
447operator!=
448(
449 const const_iterator& iter
450) const noexcept
451{
452 return (proci_ != iter.proci_);
453}
454
455
456// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
457
460{
461 return const_iterator(this);
462}
463
464
467{
468 return const_iterator(this, this->nProcs());
469}
470
471
474{
475 return const_iterator(this);
476}
477
478
480Foam::globalIndex::end() const noexcept
481{
482 return const_iterator(this, this->nProcs());
483}
484
485
486// ************************************************************************* //
scalar range
Various functions to operate on Lists.
SubList< label > subList
Declare type of subList.
Definition: List.H:111
static const UList< label > & null()
Return a UList reference to a nullObject.
Definition: UListI.H:53
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
static List< T > listGatherValues(const T &localValue, const label communicator=worldComm)
Gather individual values into list locations.
A const_iterator for iterating across on values.
Definition: bitSet.H:505
const edgeFaceCirculator cend() const
edgeFaceCirculator cbegin() const
edgeFaceCirculator begin() const
Iterator set to the beginning face. For internal edges this is.
label maxSize() const
Global max of localSizes.
Definition: globalIndexI.H:213
globalIndex()=default
Default construct.
label localSize() const
My local size.
Definition: globalIndexI.H:207
accessType
Disambiguation tag (list construction dispatch)
Definition: globalIndex.H:99
labelRange range() const
Return start/size range of local processor data.
Definition: globalIndexI.H:232
label size() const
Global sum of localSizes. Same as totalSize()
Definition: globalIndexI.H:132
label localStart() const
My local start.
Definition: globalIndexI.H:195
bool empty() const
Check for default constructed or global sum == 0.
Definition: globalIndexI.H:119
label nProcs() const noexcept
The number of processors covered by the offsets.
Definition: globalIndexI.H:144
label toGlobal(const label i) const
From local to global index.
Definition: globalIndexI.H:260
const labelUList localStarts() const
The local starts.
Definition: globalIndexI.H:179
void inplaceToGlobal(labelUList &labels) const
From local to global index (inplace)
Definition: globalIndexI.H:303
labelRange subProcs() const noexcept
Range of process indices for addressed sub-offsets (processes)
Definition: globalIndexI.H:159
label maxNonLocalSize() const
The max of localSizes, excluding current processor.
Definition: globalIndexI.H:220
label totalSize() const
Global sum of localSizes.
Definition: globalIndexI.H:125
labelList sizes() const
The local sizes. Same as localSizes()
Definition: globalIndexI.H:138
labelRange allProcs() const noexcept
Range of process indices for all addressed offsets (processes)
Definition: globalIndexI.H:151
label toLocal(const label i) const
From global to local on current processor.
Definition: globalIndexI.H:325
const labelList & offsets() const noexcept
Const-access to the offsets.
Definition: globalIndexI.H:167
bool isLocal(const label i) const
Is on local processor.
Definition: globalIndexI.H:244
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:58
int myProcNo() const noexcept
Return processor number.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
label findLower(const ListType &input, const T &val, const label start, const ComparePredicate &comp)
errorManip< error > abort(error &err)
Definition: errorManip.H:144
const direction noexcept
Definition: Scalar.H:223
error FatalError
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53