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-2021 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 \*---------------------------------------------------------------------------*/
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 
80 inline Foam::globalIndex::globalIndex(const label localSize)
81 {
83 }
84 
85 
87 (
88  const label localSize,
89  const int tag,
90  const label comm,
91  const bool parallel
92 )
93 {
94  reset(localSize, tag, comm, parallel);
95 }
96 
97 
98 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
99 
100 inline bool Foam::globalIndex::empty() const
101 {
102  return offsets_.empty() || offsets_.last() == 0;
103 }
104 
105 
106 inline Foam::label Foam::globalIndex::nProcs() const noexcept
107 {
108  const label len = (offsets_.size() - 1);
109  return (len < 1) ? static_cast<label>(0) : len;
110 }
111 
112 
114 {
115  // Proc 0 -> nProcs
116  const label len = (offsets_.size() - 1);
117  return (len < 1) ? labelRange() : labelRange(0, len);
118 }
119 
120 
122 {
123  // Proc 1 -> nProcs
124  const label len = (offsets_.size() - 2);
125  return (len < 1) ? labelRange() : labelRange(1, len);
126 }
127 
128 
129 inline const Foam::labelList& Foam::globalIndex::offsets() const noexcept
130 {
131  return offsets_;
132 }
133 
134 
136 {
137  return offsets_;
138 }
139 
140 
142 {
143  const label len = (offsets_.size() - 1);
144 
145  if (len < 1) return labelUList::null();
146 
147  return labelList::subList(offsets_, len);
148 }
149 
150 
151 inline Foam::label Foam::globalIndex::size() const
152 {
153  return offsets_.empty() ? static_cast<label>(0) : offsets_.last();
154 }
155 
156 
157 inline void Foam::globalIndex::reset(const label localSize)
158 {
159  reset(localSize, Pstream::msgType(), UPstream::worldComm, true);
160 }
161 
162 
163 inline Foam::label Foam::globalIndex::offset(const label proci) const
164 {
165  return offsets_[proci];
166 }
167 
168 
169 inline Foam::label Foam::globalIndex::localStart(const label proci) const
170 {
171  return offsets_[proci];
172 }
173 
174 
175 inline Foam::label Foam::globalIndex::localStart() const
176 {
177  return localStart(Pstream::myProcNo());
178 }
179 
180 
181 inline Foam::label Foam::globalIndex::localSize(const label proci) const
182 {
183  return offsets_[proci+1] - offsets_[proci];
184 }
185 
186 
187 inline Foam::label Foam::globalIndex::localSize() const
188 {
189  return localSize(Pstream::myProcNo());
190 }
191 
192 
193 inline Foam::label Foam::globalIndex::maxSize() const
194 {
195  // Use out-of-range proci to avoid excluding any processor
196  return maxNonLocalSize(-1);
197 }
198 
199 
200 inline Foam::label Foam::globalIndex::maxNonLocalSize() const
201 {
202  return maxNonLocalSize(Pstream::myProcNo());
203 }
204 
205 
206 inline Foam::labelRange Foam::globalIndex::range(const label proci) const
207 {
208  return labelRange(offsets_[proci], offsets_[proci+1] - offsets_[proci]);
209 }
210 
211 
213 {
214  return range(Pstream::myProcNo());
215 }
216 
217 
218 inline bool Foam::globalIndex::isLocal(const label proci, const label i) const
219 {
220  return i >= offsets_[proci] && i < offsets_[proci+1];
221 }
222 
223 
224 inline bool Foam::globalIndex::isLocal(const label i) const
225 {
226  return isLocal(Pstream::myProcNo(), i);
227 }
228 
229 
230 inline Foam::label Foam::globalIndex::toGlobal
231 (
232  const label proci,
233  const label i
234 ) const
235 {
236  return i + offsets_[proci];
237 }
238 
239 
240 inline Foam::label Foam::globalIndex::toGlobal(const label i) const
241 {
242  return toGlobal(Pstream::myProcNo(), i);
243 }
244 
245 
247 (
248  const label proci,
249  const labelUList& labels
250 ) const
251 {
252  labelList result(labels);
253  inplaceToGlobal(proci, result);
254 
255  return result;
256 }
257 
258 
260 (
261  const labelUList& labels
262 ) const
263 {
264  return toGlobal(Pstream::myProcNo(), labels);
265 }
266 
267 
269 (
270  const label proci,
271  labelList& labels
272 ) const
273 {
274  const label off = offsets_[proci];
275 
276  for (label& val : labels)
277  {
278  val += off;
279  }
280 }
281 
282 
284 {
285  inplaceToGlobal(Pstream::myProcNo(), labels);
286 }
287 
288 
289 inline Foam::label
290 Foam::globalIndex::toLocal(const label proci, const label i) const
291 {
292  const label locali = i - offsets_[proci];
293 
294  if (locali < 0 || i >= offsets_[proci+1])
295  {
297  << "Global " << i << " does not belong on processor "
298  << proci << nl << "Offsets:" << offsets_
299  << abort(FatalError);
300  }
301  return locali;
302 }
303 
304 
305 inline Foam::label Foam::globalIndex::toLocal(const label i) const
306 {
307  return toLocal(Pstream::myProcNo(), i);
308 }
309 
310 
311 inline Foam::label Foam::globalIndex::whichProcID(const label i) const
312 {
313  if (i < 0 || i >= size())
314  {
316  << "Global " << i << " does not belong on any processor."
317  << " Offsets:" << offsets_
318  << abort(FatalError);
319  }
320 
321  return findLower(offsets_, i+1);
322 }
323 
324 
325 // * * * * * * * * * * * * * * * * Iterators * * * * * * * * * * * * * * * * //
326 
329 (
330  const globalIndex* globalIdx,
331  const label i
332 ) noexcept
333 :
334  parent_(globalIdx),
335  proci_(i)
336 {}
337 
338 
339 inline Foam::label Foam::globalIndex::const_iterator::
340 proci() const noexcept
341 {
342  return proci_;
343 }
344 
345 
346 inline Foam::label Foam::globalIndex::const_iterator::
347 start() const
348 {
349  return (*parent_).localStart(proci_);
350 }
351 
352 
353 inline Foam::label Foam::globalIndex::const_iterator::
354 size() const
355 {
356  return (*parent_).localSize(proci_);
357 }
358 
359 
361 range() const
362 {
363  return (*parent_).range(proci_);
364 }
365 
366 
368 operator*() const
369 {
370  return this->range();
371 }
372 
373 
377 {
378  ++proci_;
379  return *this;
380 }
381 
382 
386 {
387  const_iterator old(*this);
388  ++proci_;
389  return old;
390 }
391 
392 
396 {
397  --proci_;
398  return *this;
399 }
400 
401 
405 {
406  const_iterator old(*this);
407  --proci_;
408  return old;
409 }
410 
411 
412 inline bool
413 Foam::globalIndex::const_iterator::
414 operator==
415 (
416  const const_iterator& iter
417 ) const noexcept
418 {
419  return (proci_ == iter.proci_);
420 }
421 
422 
423 inline bool
424 Foam::globalIndex::const_iterator::
425 operator!=
426 (
427  const const_iterator& iter
428 ) const noexcept
429 {
430  return (proci_ != iter.proci_);
431 }
432 
433 
434 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
435 
438 {
439  return const_iterator(this);
440 }
441 
442 
444 Foam::globalIndex::cend() const noexcept
445 {
446  return const_iterator(this, this->nProcs());
447 }
448 
449 
451 Foam::globalIndex::begin() const noexcept
452 {
453  return const_iterator(this);
454 }
455 
456 
458 Foam::globalIndex::end() const noexcept
459 {
460  return const_iterator(this, this->nProcs());
461 }
462 
463 
464 // ************************************************************************* //
Foam::globalIndex::const_iterator::start
label start() const
The local start.
Definition: globalIndexI.H:347
Foam::globalIndex::allProcs
labelRange allProcs() const noexcept
Range of process indices for all addressed offsets (processes)
Definition: globalIndexI.H:113
Foam::globalIndex::localStart
label localStart() const
My local start.
Definition: globalIndexI.H:175
Foam::globalIndex::range
labelRange range() const
Return start/size range of local processor data.
Definition: globalIndexI.H:212
Foam::globalIndex::const_iterator::operator*
labelRange operator*() const
The local range.
Definition: globalIndexI.H:368
Foam::globalIndex::localSize
label localSize() const
My local size.
Definition: globalIndexI.H:187
Foam::globalIndex::isLocal
bool isLocal(const label i) const
Is on local processor.
Definition: globalIndexI.H:224
Foam::globalIndex::inplaceToGlobal
void inplaceToGlobal(labelList &labels) const
From local to global index (inplace)
Definition: globalIndexI.H:283
Foam::globalIndex::empty
bool empty() const
Check for default constructed or global sum == 0.
Definition: globalIndexI.H:100
Foam::globalIndex::maxSize
label maxSize() const
Global max of localSizes.
Definition: globalIndexI.H:193
Foam::globalIndex::const_iterator
Forward input iterator with const access.
Definition: globalIndex.H:287
Foam::globalIndex::const_iterator::const_iterator
const_iterator(const globalIndex *globalIdx, const label proci=0) noexcept
Construct from globalIndex list at given index.
Definition: globalIndexI.H:329
Foam::findLower
label findLower(const ListType &input, const T &val, const label start, const ComparePredicate &comp)
Foam::globalIndex::accessType
accessType
Disambiguation tag (list construction dispatch)
Definition: globalIndex.H:93
Foam::globalIndex::end
const const_iterator end() const noexcept
A const_iterator set to beyond the end.
Definition: globalIndexI.H:458
Foam::labelRange
A range or interval of labels defined by a start and a size.
Definition: labelRange.H:55
Foam::List::subList
SubList< T > subList
Declare type of subList.
Definition: List.H:112
Foam::FatalError
error FatalError
Foam::globalIndex::const_iterator::size
label size() const
The local size.
Definition: globalIndexI.H:354
Foam::globalIndex::localStarts
const labelUList localStarts() const
The local starts.
Definition: globalIndexI.H:141
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
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:157
Foam::globalIndex::subProcs
labelRange subProcs() const noexcept
Range of process indices for addressed sub-offsets (processes)
Definition: globalIndexI.H:121
reset
meshPtr reset(new Foam::fvMesh(Foam::IOobject(regionName, runTime.timeName(), runTime, Foam::IOobject::MUST_READ), false))
Foam::globalIndex::cend
const const_iterator cend() const noexcept
A const_iterator set to beyond the end.
Definition: globalIndexI.H:444
Foam::globalIndex::const_iterator::operator--
const_iterator & operator--()
Definition: globalIndexI.H:395
Foam::globalIndex::toLocal
label toLocal(const label i) const
From global to local on current processor.
Definition: globalIndexI.H:305
Foam::globalIndex::const_iterator::range
labelRange range() const
The local range.
Definition: globalIndexI.H:361
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::UPstream::msgType
static int & msgType() noexcept
Message tag of standard messages.
Definition: UPstream.H:540
Foam::UPstream::myProcNo
static int myProcNo(const label communicator=worldComm)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:463
range
scalar range
Definition: LISASMDCalcMethod1.H:12
Foam::nl
constexpr char nl
Definition: Ostream.H:404
labelRange.H
Foam::globalIndex::size
label size() const
Global sum of localSizes.
Definition: globalIndexI.H:151
Foam::globalIndex::const_iterator::proci
label proci() const noexcept
The associated local proc.
Definition: globalIndexI.H:340
Foam::globalIndex::nProcs
label nProcs() const noexcept
The number of processors covered by the offsets.
Definition: globalIndexI.H:106
Foam::UPstream::worldComm
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:293
Foam::List< label >
Foam::globalIndex::offsets
const labelList & offsets() const noexcept
Const-access to the offsets.
Definition: globalIndexI.H:129
Foam::globalIndex::maxNonLocalSize
label maxNonLocalSize() const
The max of localSizes, excluding current processor.
Definition: globalIndexI.H:200
Foam::globalIndex::whichProcID
label whichProcID(const label i) const
Which processor does global come from? Binary search.
Definition: globalIndexI.H:311
Foam::UList< label >
Foam::globalIndex::cbegin
const_iterator cbegin() const noexcept
A const_iterator set to the beginning.
Definition: globalIndexI.H:437
Foam::globalIndex::globalIndex
globalIndex()=default
Default construct.
Foam::globalIndex::offset
label offset(const label proci) const
Start of proci data.
Definition: globalIndexI.H:163
Foam::globalIndex::begin
const_iterator begin() const noexcept
A const_iterator set to the beginning.
Definition: globalIndexI.H:451
ListOps.H
Various functions to operate on Lists.
Foam::UList::size
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Foam::UList::null
static const UList< T > & null()
Return a UList reference to a nullObject.
Definition: UListI.H:53
Foam::globalIndex::const_iterator::operator++
const_iterator & operator++()
Definition: globalIndexI.H:376
Foam::globalIndex::toGlobal
label toGlobal(const label i) const
From local to global index.
Definition: globalIndexI.H:240