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 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 
34 inline Foam::globalIndex::globalIndex(const label localSize)
35 :
36  globalIndex()
37 {
39 }
40 
41 
43 (
44  const label localSize,
45  const int tag,
46  const label comm,
47  const bool parallel
48 )
49 :
50  globalIndex()
51 {
52  reset(localSize, tag, comm, parallel);
53 }
54 
55 
57 :
58  offsets_(offsets)
59 {}
60 
61 
63 :
64  offsets_(std::move(offsets))
65 {}
66 
67 
68 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
69 
70 inline bool Foam::globalIndex::empty() const
71 {
72  return offsets_.empty() || offsets_.last() == 0;
73 }
74 
75 
77 {
78  return offsets_;
79 }
80 
81 
83 {
84  return offsets_;
85 }
86 
87 
89 {
90  return offsets_.empty() ? 0 : offsets_.last();
91 }
92 
93 
94 inline void Foam::globalIndex::reset(const label localSize)
95 {
96  reset(localSize, Pstream::msgType(), 0, true);
97 }
98 
99 
100 inline Foam::label Foam::globalIndex::offset(const label proci) const
101 {
102  return offsets_[proci];
103 }
104 
105 
107 {
108  return offsets_[proci];
109 }
110 
111 
113 {
114  return localStart(Pstream::myProcNo());
115 }
116 
117 
119 {
120  return offsets_[proci+1] - offsets_[proci];
121 }
122 
123 
125 {
126  return localSize(Pstream::myProcNo());
127 }
128 
129 
131 {
132  return labelRange(offsets_[proci], offsets_[proci+1] - offsets_[proci]);
133 }
134 
135 
137 {
138  return range(Pstream::myProcNo());
139 }
140 
141 
142 inline bool Foam::globalIndex::isLocal(const label proci, const label i) const
143 {
144  return i >= offsets_[proci] && i < offsets_[proci+1];
145 }
146 
147 
148 inline bool Foam::globalIndex::isLocal(const label i) const
149 {
150  return isLocal(Pstream::myProcNo(), i);
151 }
152 
153 
155 (
156  const label proci,
157  const label i
158 ) const
159 {
160  return i + offsets_[proci];
161 }
162 
163 
165 {
166  return toGlobal(Pstream::myProcNo(), i);
167 }
168 
169 
171 (
172  const label proci,
173  const labelUList& labels
174 ) const
175 {
176  labelList result(labels);
177  inplaceToGlobal(proci, result);
178 
179  return result;
180 }
181 
182 
184 (
185  const labelUList& labels
186 ) const
187 {
188  return toGlobal(Pstream::myProcNo(), labels);
189 }
190 
191 
193 (
194  const label proci,
195  labelList& labels
196 ) const
197 {
198  const label off = offsets_[proci];
199 
200  for (label& val : labels)
201  {
202  val += off;
203  }
204 }
205 
206 
208 {
209  inplaceToGlobal(Pstream::myProcNo(), labels);
210 }
211 
212 
213 inline Foam::label
214 Foam::globalIndex::toLocal(const label proci, const label i) const
215 {
216  const label locali = i - offsets_[proci];
217 
218  if (locali < 0 || i >= offsets_[proci+1])
219  {
221  << "Global " << i << " does not belong on processor "
222  << proci << nl << "Offsets:" << offsets_
223  << abort(FatalError);
224  }
225  return locali;
226 }
227 
228 
230 {
231  return toLocal(Pstream::myProcNo(), i);
232 }
233 
234 
236 {
237  if (i < 0 || i >= size())
238  {
240  << "Global " << i << " does not belong on any processor."
241  << " Offsets:" << offsets_
242  << abort(FatalError);
243  }
244 
245  return findLower(offsets_, i+1);
246 }
247 
248 
249 // ************************************************************************* //
Foam::val
label ListType::const_reference val
Definition: ListOps.H:407
Foam::globalIndex::localStart
label localStart() const
My local start.
Definition: globalIndexI.H:112
Foam::globalIndex::range
labelRange range() const
Return start/size range of local processor data.
Definition: globalIndexI.H:136
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::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::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
Foam::findLower
label findLower(const ListType &input, const T &val, const label start, const ComparePredicate &comp)
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::FatalError
error FatalError
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
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::UPstream::myProcNo
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:444
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:491
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
range
scalar range
Definition: LISASMDCalcMethod1.H:12
Foam::nl
constexpr char nl
Definition: Ostream.H:372
labelRange.H
Foam::globalIndex::size
label size() const
Global sum of localSizes.
Definition: globalIndexI.H:88
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
ListOps.H
Various functions to operate on Lists.
Foam::globalIndex::toGlobal
label toGlobal(const label i) const
From local to global index.
Definition: globalIndexI.H:164