globalIndex.C
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 "globalIndex.H"
30#include "labelRange.H"
31
32// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
33
34void Foam::globalIndex::reportOverflowAndExit
35(
36 const label idx,
37 const labelUList& localLens
38)
39{
41 << "Overflow : sum of sizes exceeds labelMax ("
42 << labelMax << ") after index " << idx;
43
44 if (!localLens.empty())
45 {
46 FatalError << " of " << flatOutput(localLens);
47 }
48
50 << nl
51 << "Please recompile with larger datatype for label." << nl
52 << exit(FatalError);
53}
54
55
57Foam::globalIndex::calcOffsets
58(
59 const labelUList& localLens,
60 const bool checkOverflow
61)
62{
64
65 const label len = localLens.size();
66
67 if (len)
68 {
69 values.resize(len+1);
70
71 label start = 0;
72 for (label i = 0; i < len; ++i)
73 {
74 values[i] = start;
75 start += localLens[i];
76
77 if (checkOverflow && start < values[i])
78 {
79 reportOverflowAndExit(i, localLens);
80 }
81 }
82 values[len] = start;
83 }
84
85 return values;
86}
87
88
90Foam::globalIndex::calcRanges
91(
92 const labelUList& localLens,
93 const bool checkOverflow
94)
95{
96 List<labelRange> values;
97
98 const label len = localLens.size();
99
100 if (len)
101 {
102 values.resize(len);
103
104 label start = 0;
105 for (label i = 0; i < len; ++i)
106 {
107 values[i].reset(start, localLens[i]);
108 start += localLens[i];
109
110 if (checkOverflow && start < values[i].start())
111 {
112 reportOverflowAndExit(i, localLens);
113 }
114 }
115 }
116
117 return values;
118}
119
120
121// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
122
124{
125 is >> offsets_;
126}
127
128
129// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
130
132Foam::globalIndex::bin
133(
134 const labelUList& offsets,
135 const labelUList& globalIds,
136 labelList& order,
137 DynamicList<label>& validBins
138)
139{
140 sortedOrder(globalIds, order);
141 validBins.clear();
142
144
145 if (globalIds.size())
146 {
147 labelList& binOffsets = bins.offsets();
148 binOffsets.resize(offsets.size(), Zero);
149
150 labelList& binValues = bins.values();
151 binValues = UIndirectList<label>(globalIds, order);
152
153 const label id = binValues[0];
154 label proci = findLower(offsets, id+1);
155
156 validBins.append(proci);
157 label binSize = 1;
158
159 for (label i = 1; i < order.size(); i++)
160 {
161 const label id = binValues[i];
162
163 if (id < offsets[proci+1])
164 {
165 binSize++;
166 }
167 else
168 {
169 // Not local. Reset proci
170 label oldProci = proci;
171 proci = findLower(offsets, id+1);
172
173 // Set offsets
174 for (label j = oldProci+1; j < proci; ++j)
175 {
176 binOffsets[j] = binOffsets[oldProci]+binSize;
177 }
178 binOffsets[proci] = i;
179 validBins.append(proci);
180 binSize = 1;
181 }
182 }
183
184 for (label j = proci+1; j < binOffsets.size(); ++j)
185 {
186 binOffsets[j] = binOffsets[proci]+binSize;
187 }
188 }
189
190 return bins;
191}
192
193
195(
196 const label localSize,
198 const label comm
199)
200{
201 // Gather sizes (one-sided)
202 reset(UPstream::listGatherValues(localSize, comm));
203}
204
205
207(
208 const label localSize,
209 const label comm,
210 const bool parallel
211)
212{
213 labelList localLens;
214
215 const label len = Pstream::nProcs(comm);
216
217 if (len)
218 {
219 if (parallel && UPstream::parRun())
220 {
221 localLens = UPstream::listGatherValues(localSize, comm);
222 Pstream::broadcast(localLens, comm);
223 }
224 else
225 {
226 // Non-parallel branch: use localSize on-proc, zero elsewhere
227
228 localLens.resize(len, Zero);
229 localLens[Pstream::myProcNo(comm)] = localSize;
230 }
231
232 reset(localLens, true); // checkOverflow = true
233 }
234 else
235 {
236 // Nothing to do
237 offsets_.clear();
238 }
239}
240
241
243(
244 const labelUList& localLens,
245 const bool checkOverflow
246)
247{
248 const label len = localLens.size();
249
250 if (len)
251 {
252 offsets_.resize_nocopy(len+1);
253
254 label start = 0;
255 for (label i = 0; i < len; ++i)
256 {
257 offsets_[i] = start;
258 start += localLens[i];
259
260 if (checkOverflow && start < offsets_[i])
261 {
262 reportOverflowAndExit(i, localLens);
263 }
264 }
265 offsets_[len] = start;
266 }
267 else
268 {
269 offsets_.clear();
270 }
271}
272
273
274void Foam::globalIndex::setLocalSize(const label proci, const label len)
275{
276 if (proci >= 0 && proci+1 < offsets_.size() && len >= 0)
277 {
278 const label delta = (len - (offsets_[proci+1] - offsets_[proci]));
279
280 // TBD: additional overflow check
281 if (delta)
282 {
283 for (label i = proci+1; i < offsets_.size(); ++i)
284 {
285 offsets_[i] += delta;
286 }
287 }
288 }
289}
290
291
293{
294 labelList values;
295
296 const label len = (offsets_.size() - 1);
297
298 if (len < 1)
299 {
300 return values;
301 }
302
303 values.resize(len);
304
305 for (label proci=0; proci < len; ++proci)
306 {
307 values[proci] = offsets_[proci+1] - offsets_[proci];
308 }
309
310 return values;
311}
312
313
316{
317 List<labelRange> values;
318
319 const label len = (offsets_.size() - 1);
320
321 if (len < 1)
322 {
323 return values;
324 }
325
326 values.resize(len);
327
328 for (label proci=0; proci < len; ++proci)
329 {
330 values[proci].reset
331 (
332 offsets_[proci],
333 (offsets_[proci+1] - offsets_[proci])
334 );
335 }
336
337 return values;
338}
339
340
341Foam::label Foam::globalIndex::maxNonLocalSize(const label proci) const
342{
343 const label len = (offsets_.size() - 1);
344
345 if (len < 1)
346 {
347 return 0;
348 }
349
350 label maxLen = 0;
351
352 for (label i=0; i < len; ++i)
353 {
354 if (i != proci)
355 {
356 const label localLen = (offsets_[i+1] - offsets_[i]);
357 maxLen = max(maxLen, localLen);
358 }
359 }
360
361 return maxLen;
362}
363
364
365// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
366
368{
369 return is >> gi.offsets_;
370}
371
372
374{
375 return os << gi.offsets_;
376}
377
378
379// ************************************************************************* //
scalar delta
A packed storage unstructured matrix of objects of type <T> using an offset table for access.
const List< T > & values() const noexcept
Return the packed matrix of values.
const labelList & offsets() const noexcept
Return the offset table (= size()+1)
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:72
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:391
void append(const T &val)
Copy append an element to the end of this list.
Definition: DynamicListI.H:503
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
label nProcs() const noexcept
Number of ranks associated with PstreamBuffers.
static void broadcast(Type &value, const label comm=UPstream::worldComm)
A List with indirect addressing. Like IndirectList but does not store addressing.
Definition: IndirectList.H:79
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.
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:433
void reset()
Reset to defaults.
Calculates a unique integer (label so might not have enough room - 2G max) for processor + local inde...
Definition: globalIndex.H:68
globalIndex()=default
Default construct.
List< labelRange > ranges() const
Return start/size ranges for all data.
Definition: globalIndex.C:315
label maxNonLocalSize() const
The max of localSizes, excluding current processor.
Definition: globalIndexI.H:220
void setLocalSize(const label proci, const label len)
Alter local size for given processor.
Definition: globalIndex.C:274
labelList localSizes() const
The local sizes.
Definition: globalIndex.C:292
int myProcNo() const noexcept
Return processor number.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
OBJstream os(runTime.globalPath()/outputName)
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:149
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
List< label > labelList
A List of labels.
Definition: List.H:66
label findLower(const ListType &input, const T &val, const label start, const ComparePredicate &comp)
constexpr label labelMax
Definition: label.H:61
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Istream & operator>>(Istream &, directionInfo &)
labelList sortedOrder(const UList< T > &input)
Return the (stable) sort order for the list.
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:215
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
error FatalError
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
UList< label > labelUList
A UList of labels.
Definition: UList.H:85
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53