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-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 "globalIndex.H"
30 #include "labelRange.H"
31 
32 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
33 
36 (
37  const labelUList& localSizes,
38  const bool checkOverflow
39 )
40 {
42 
43  const label len = localSizes.size();
44 
45  if (len)
46  {
47  values.resize(len+1);
48 
49  label start = 0;
50  for (label i = 0; i < len; ++i)
51  {
52  values[i] = start;
53  start += localSizes[i];
54 
55  if (checkOverflow && start < values[i])
56  {
58  << "Overflow : sum of sizes exceeds labelMax ("
59  << labelMax << ") after index " << i << " of "
60  << flatOutput(localSizes) << nl
61  << "Please recompile with larger datatype for label." << nl
62  << exit(FatalError);
63  }
64  }
65  values[len] = start;
66  }
67 
68  return values;
69 }
70 
71 
74 (
75  const labelUList& localSizes,
76  const bool checkOverflow
77 )
78 {
80 
81  const label len = localSizes.size();
82 
83  if (len)
84  {
85  values.resize(len);
86 
87  label start = 0;
88  for (label i = 0; i < len; ++i)
89  {
90  values[i].reset(start, localSizes[i]);
91  start += localSizes[i];
92 
93  if (checkOverflow && start < values[i].start())
94  {
96  << "Overflow : sum of sizes exceeds labelMax ("
97  << labelMax << ") after index " << i << " of "
98  << flatOutput(localSizes) << nl
99  << "Please recompile with larger datatype for label." << nl
100  << exit(FatalError);
101  }
102  }
103  }
104 
105  return values;
106 }
107 
108 
109 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
110 
112 {
113  is >> offsets_;
114 }
115 
116 
117 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
118 
119 void Foam::globalIndex::bin
120 (
121  const labelUList& offsets,
122  const labelUList& globalIds,
123  labelList& order,
125  DynamicList<label>& validBins
126 )
127 {
128  sortedOrder(globalIds, order);
129 
130  bins.m() = UIndirectList<label>(globalIds, order);
131 
132  labelList& binOffsets = bins.offsets();
133  binOffsets.resize_nocopy(offsets.size());
134  binOffsets = Zero;
135 
136  validBins.clear();
137 
138  if (globalIds.size())
139  {
140  const label id = bins.m()[0];
141  label proci = findLower(offsets, id+1);
142 
143  validBins.append(proci);
144  label binSize = 1;
145 
146  for (label i = 1; i < order.size(); i++)
147  {
148  const label id = bins.m()[i];
149 
150  if (id < offsets[proci+1])
151  {
152  binSize++;
153  }
154  else
155  {
156  // Not local. Reset proci
157  label oldProci = proci;
158  proci = findLower(offsets, id+1);
159 
160  // Set offsets
161  for (label j = oldProci+1; j < proci; ++j)
162  {
163  binOffsets[j] = binOffsets[oldProci]+binSize;
164  }
165  binOffsets[proci] = i;
166  validBins.append(proci);
167  binSize = 1;
168  }
169  }
170 
171  for (label j = proci+1; j < binOffsets.size(); ++j)
172  {
173  binOffsets[j] = binOffsets[proci]+binSize;
174  }
175  }
176 }
177 
178 
180 (
181  const label localSize,
182  const int tag,
183  const label comm,
184  const bool parallel
185 )
186 {
187  const label len = Pstream::nProcs(comm);
188 
189  if (len)
190  {
191  // Seed with localSize, zero elsewhere (for non-parallel branch)
192  labelList localSizes(len, Zero);
193  localSizes[Pstream::myProcNo(comm)] = localSize;
194 
195  if (parallel)
196  {
197  Pstream::gatherList(localSizes, tag, comm);
198  Pstream::scatterList(localSizes, tag, comm);
199  }
200 
201  reset(localSizes, true); // checkOverflow = true
202  }
203  else
204  {
205  offsets_.clear();
206  }
207 }
208 
209 
211 (
212  const labelUList& localSizes,
213  const bool checkOverflow
214 )
215 {
216  const label len = localSizes.size();
217 
218  if (len)
219  {
220  offsets_.resize_nocopy(len+1);
221 
222  label start = 0;
223  for (label i = 0; i < len; ++i)
224  {
225  offsets_[i] = start;
226  start += localSizes[i];
227 
228  if (checkOverflow && start < offsets_[i])
229  {
231  << "Overflow : sum of sizes exceeds labelMax ("
232  << labelMax << ") after index " << i << " of "
233  << flatOutput(localSizes) << nl
234  << "Please recompile with larger datatype for label." << nl
235  << exit(FatalError);
236  }
237  }
238  offsets_[len] = start;
239  }
240  else
241  {
242  offsets_.clear();
243  }
244 }
245 
246 
247 void Foam::globalIndex::setLocalSize(const label proci, const label len)
248 {
249  if (proci >= 0 && proci+1 < offsets_.size() && len >= 0)
250  {
251  const label delta = (len - (offsets_[proci+1] - offsets_[proci]));
252 
253  // TBD: additional overflow check
254  if (delta)
255  {
256  for (label i = proci+1; i < offsets_.size(); ++i)
257  {
258  offsets_[i] += delta;
259  }
260  }
261  }
262 }
263 
264 
266 {
268 
269  const label len = (offsets_.size() - 1);
270 
271  if (len < 1)
272  {
273  return values;
274  }
275 
276  values.resize(len);
277 
278  for (label proci=0; proci < len; ++proci)
279  {
280  values[proci] = offsets_[proci+1] - offsets_[proci];
281  }
282 
283  return values;
284 }
285 
286 
289 {
291 
292  const label len = (offsets_.size() - 1);
293 
294  if (len < 1)
295  {
296  return values;
297  }
298 
299  values.resize(len);
300 
301  for (label proci=0; proci < len; ++proci)
302  {
303  values[proci].reset
304  (
305  offsets_[proci],
306  (offsets_[proci+1] - offsets_[proci])
307  );
308  }
309 
310  return values;
311 }
312 
313 
314 Foam::label Foam::globalIndex::maxNonLocalSize(const label proci) const
315 {
316  const label len = (offsets_.size() - 1);
317 
318  if (len < 1)
319  {
320  return 0;
321  }
322 
323  label maxLen = 0;
324 
325  for (label i=0; i < len; ++i)
326  {
327  if (i != proci)
328  {
329  const label localLen = (offsets_[i+1] - offsets_[i]);
330  maxLen = max(maxLen, localLen);
331  }
332  }
333 
334  return maxLen;
335 }
336 
337 
338 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
339 
341 {
342  return is >> gi.offsets_;
343 }
344 
345 
347 {
348  return os << gi.offsets_;
349 }
350 
351 
352 // ************************************************************************* //
Foam::CompactListList::offsets
const List< label > & offsets() const
Return the offset table (= size()+1)
Definition: CompactListListI.H:142
Foam::CompactListList::m
const List< T > & m() const
Return the packed matrix of data.
Definition: CompactListListI.H:156
Foam::labelMax
constexpr label labelMax
Definition: label.H:61
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::DynamicList< label >
Foam::HashTableOps::values
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:149
Foam::Pstream::scatterList
static void scatterList(const List< commsStruct > &comms, List< T > &Values, const int tag, const label comm)
Scatter data. Reverse of gatherList.
Definition: gatherScatterList.C:215
Foam::CompactListList
A packed storage unstructured matrix of objects of type <T> using an offset table for access.
Definition: CompactListList.H:63
globalIndex.H
Foam::globalIndex::sizes
labelList sizes() const
The local sizes.
Definition: globalIndex.C:265
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::List::resize_nocopy
void resize_nocopy(const label len)
Adjust allocated size of list without necessarily.
Definition: ListI.H:146
Foam::DynamicList::clear
void clear() noexcept
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:391
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::findLower
label findLower(const ListType &input, const T &val, const label start, const ComparePredicate &comp)
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::DynamicList::append
DynamicList< T, SizeMin > & append(const T &val)
Append an element to the end of this list.
Definition: DynamicListI.H:511
delta
scalar delta
Definition: LISASMDCalcMethod2.H:8
Foam::globalIndex::calcRanges
static List< labelRange > calcRanges(const labelUList &localSizes, const bool checkOverflow=false)
Definition: globalIndex.C:74
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
Foam::FatalError
error FatalError
os
OBJstream os(runTime.globalPath()/outputName)
Foam::flatOutput
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:216
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::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
reset
meshPtr reset(new Foam::fvMesh(Foam::IOobject(regionName, runTime.timeName(), runTime, Foam::IOobject::MUST_READ), false))
Foam::globalIndex::ranges
List< labelRange > ranges() const
Return start/size ranges for all data.
Definition: globalIndex.C:288
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::Pstream::gatherList
static void gatherList(const List< commsStruct > &comms, List< T > &Values, const int tag, const label comm)
Gather data but keep individual values separate.
Definition: gatherScatterList.C:52
Foam::UPstream::myProcNo
static int myProcNo(const label communicator=worldComm)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:463
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::globalIndex::setLocalSize
void setLocalSize(const label proci, const label len)
Alter local size for given processor.
Definition: globalIndex.C:247
labelRange.H
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:63
Foam::globalIndex::maxNonLocalSize
label maxNonLocalSize() const
The max of localSizes, excluding current processor.
Definition: globalIndexI.H:200
Foam::UList< label >
Foam::globalIndex::globalIndex
globalIndex()=default
Default construct.
Foam::UIndirectList
A List with indirect addressing.
Definition: faMatrix.H:60
Foam::sortedOrder
labelList sortedOrder(const UList< T > &input)
Return the (stable) sort order for the list.
Foam::UList::size
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::globalIndex::calcOffsets
static labelList calcOffsets(const labelUList &localSizes, const bool checkOverflow=false)
Definition: globalIndex.C:36
Foam::UPstream::nProcs
static label nProcs(const label communicator=worldComm)
Number of processes in parallel run, and 1 for serial run.
Definition: UPstream.H:445