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-2020 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 
31 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 
34 {
35  is >> offsets_;
36 }
37 
38 
39 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
40 
41 void Foam::globalIndex::bin
42 (
43  const labelUList& offsets,
44  const labelUList& globalIds,
45  labelList& order,
47  DynamicList<label>& validBins
48 )
49 {
50  sortedOrder(globalIds, order);
51 
52  bins.m() = UIndirectList<label>(globalIds, order);
53 
54  labelList& binOffsets = bins.offsets();
55  binOffsets.setSize(offsets.size());
56  binOffsets = 0;
57 
58  validBins.clear();
59 
60  if (globalIds.size())
61  {
62  const label id = bins.m()[0];
63  label proci = findLower(offsets, id+1);
64 
65  validBins.append(proci);
66  label binSize = 1;
67 
68  for (label i = 1; i < order.size(); i++)
69  {
70  const label id = bins.m()[i];
71 
72  if (id < offsets[proci+1])
73  {
74  binSize++;
75  }
76  else
77  {
78  // Not local. Reset proci
79  label oldProci = proci;
80  proci = findLower(offsets, id+1);
81 
82  // Set offsets
83  for (label j = oldProci+1; j < proci; ++j)
84  {
85  binOffsets[j] = binOffsets[oldProci]+binSize;
86  }
87  binOffsets[proci] = i;
88  validBins.append(proci);
89  binSize = 1;
90  }
91  }
92 
93  for (label j = proci+1; j < binOffsets.size(); ++j)
94  {
95  binOffsets[j] = binOffsets[proci]+binSize;
96  }
97  }
98 }
99 
100 
102 (
103  const label localSize,
104  const int tag,
105  const label comm,
106  const bool parallel
107 )
108 {
109  offsets_.resize(Pstream::nProcs(comm)+1);
110 
111  labelList localSizes(Pstream::nProcs(comm), Zero);
112  localSizes[Pstream::myProcNo(comm)] = localSize;
113 
114  if (parallel)
115  {
116  Pstream::gatherList(localSizes, tag, comm);
117  Pstream::scatterList(localSizes, tag, comm);
118  }
119 
120  label offset = 0;
121  offsets_[0] = 0;
122  for (const int proci : Pstream::allProcs(comm))
123  {
124  const label oldOffset = offset;
125  offset += localSizes[proci];
126 
127  if (offset < oldOffset)
128  {
130  << "Overflow : sum of sizes " << localSizes
131  << " exceeds capability of label (" << labelMax
132  << "). Please recompile with larger datatype for label."
133  << exit(FatalError);
134  }
135  offsets_[proci+1] = offset;
136  }
137 }
138 
139 
141 {
143 
144  const label len = (offsets_.size() - 1);
145 
146  if (len < 1)
147  {
148  return values;
149  }
150 
151  values.resize(len);
152 
153  for (label proci=0; proci < len; ++proci)
154  {
155  values[proci] = offsets_[proci+1] - offsets_[proci];
156  }
157 
158  return values;
159 }
160 
161 
162 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
163 
165 {
166  return is >> gi.offsets_;
167 }
168 
169 
171 {
172  return os << gi.offsets_;
173 }
174 
175 
176 // ************************************************************************* //
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: polyTopoChange.H:93
globalIndex.H
Foam::globalIndex::sizes
labelList sizes() const
The local sizes.
Definition: globalIndex.C:140
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
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:474
Foam::DynamicList::clear
void clear()
Clear the addressed list, i.e. set the size to zero.
Definition: DynamicListI.H:348
Foam::FatalError
error FatalError
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::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:381
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::allProcs
static rangeType allProcs(const label communicator=worldComm)
Range of process indices for all processes.
Definition: UPstream.H:509
Foam::UPstream::myProcNo
static int myProcNo(const label communicator=worldComm)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:464
Foam::List< label >
Foam::UList< label >
Foam::globalIndex::globalIndex
globalIndex()=default
Construct null.
Foam::UList::size
void size(const label n) noexcept
Override size to be inconsistent with allocated storage.
Definition: UListI.H:360
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::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::List::setSize
void setSize(const label newSize)
Alias for resize(const label)
Definition: ListI.H:146
Foam::UPstream::nProcs
static label nProcs(const label communicator=worldComm)
Number of processes in parallel run, and 1 for serial run.
Definition: UPstream.H:446