Loading...
Searching...
No Matches
globalOffset.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) 2025 Mark Olesen
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26Class
27 Foam::GlobalOffset
28
29Description
30 A Foam::OffsetRange (start, size, total) tuple with some additional
31 parallel functionality. A common use case for OffsetRange would be
32 the definition of non-overlapping global addressing.
33
34 OffsetRange is used for the addressing information,
35 and GlobalOffset for creating and managing that addressing.
36
37Note
38 In contrast to Foam::globalIndex, both GlobalOffset and OffsetRange
39 only have their own local addressing information (and total size)
40 without additional information about other ranks.
41
42SourceFiles
43 globalOffset.txx
44 globalOffsetI.H
45
46\*---------------------------------------------------------------------------*/
47
48#ifndef Foam_globalOffset_H
49#define Foam_globalOffset_H
50
51#include "OffsetRange.H"
52#include "OffsetRangeIO.H"
53#include "UPstream.H"
54
55// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
56
57namespace Foam
58{
59
60// Forward Declarations
61template<class IntType> class GlobalOffset;
63// Common Types
64//- A GlobalOffset with label for the addressing
66
67/*---------------------------------------------------------------------------*\
68 Class GlobalOffset Declaration
69\*---------------------------------------------------------------------------*/
70
71template<class IntType>
72class GlobalOffset
73:
74 public OffsetRange<IntType>
75{
76 // Private Member Functions
77
78 //- Return a list of globally-consistent start/size/total ranges
79 //- based on the given local input sizes.
80 template<class IntegralType>
81 static OffsetRange<IntegralType> calculate_impl
82 (
83 IntegralType localSize,
84 int communicator,
85 bool parallel
86 );
87
88 //- Return a list of globally-consistent start/size/total ranges
89 //- based on the given local input sizes.
90 template<class IntegralType>
91 static List<OffsetRange<IntegralType>> calculateList_impl
92 (
93 const UList<IntegralType>& localSizes,
94 int communicator,
95 bool parallel
96 );
97
98public:
99
100 // Generated Methods: copy/move construct, copy/move assignment
101
102 // Constructors
103
104 //- Default construct as (0,0,0)
105 inline constexpr GlobalOffset() noexcept;
106
107 //- Construct with specified length, starting at zero (0,len,len).
108 //- No communication.
109 inline constexpr GlobalOffset(IntType len) noexcept;
110
111 //- Construct from local size, with parallel coordination of
112 //- the offsets and total size
115 IntType len,
116 int communicator,
117 bool parallel = UPstream::parRun()
118 );
119
120 //- Copy construct from OffsetRange.
121 // Should be left as implicit to allow assignment from
122 // the results of the calculate() method
123 inline GlobalOffset(const OffsetRange<IntType>& range) noexcept;
124
125 //- Read construct from start/size/total tuple
126 explicit GlobalOffset(Istream& is);
127
128
129 // Member Functions
130
131 // Access
132
133 //- Same as start() - method name as per globalIndex
134 IntType localStart() const noexcept { return this->start(); }
135
136 //- Same as size() - method name as per globalIndex
137 IntType localSize() const noexcept { return this->size(); }
139 //- Same as total() - method name as per globalIndex
140 IntType totalSize() const noexcept { return this->total(); }
141
142
143 // Edit
144
145 //- Reset from local size with zero offset.
146 inline void reset(IntType len) noexcept;
147
148 //- Reset from local size, with parallel coordination of
149 //- the offsets and total size
150 inline void reset
151 (
152 IntType len,
153 int communicator,
154 bool parallel = UPstream::parRun()
155 );
156
157
158 // Parallel Functions
159
160 //- Based on the local input size,
161 //- determine the globally-consistent start offset and total size
162 void reduce(int communicator = UPstream::worldComm);
163
164 //- Calculate a globally-consistent set of start/size/total
165 //- based on the given local input size.
167 (
168 IntType localSize,
169 int communicator = UPstream::worldComm,
170 bool parallel = UPstream::parRun()
171 )
173 return calculate_impl<IntType>(localSize, communicator, parallel);
174 }
175
176 //- Calculate a globally-consistent set of start/size/total
177 //- based on the given local input size.
178 template
179 <
180 class Int,
181 class = std::enable_if_t<std::is_integral_v<Int>>
182 >
184 (
185 IntType localSize,
186 int communicator = UPstream::worldComm,
187 bool parallel = UPstream::parRun()
188 )
189 {
190 return calculate_impl<Int>(localSize, communicator, parallel);
191 }
193 //- Return a list of globally-consistent start/size/total ranges
194 //- based on the given local input sizes.
195 template
196 <
197 class Int,
198 class = std::enable_if_t<std::is_integral_v<Int>>
199 >
201 (
202 const UList<Int>& localSizes,
203 int communicator = UPstream::worldComm,
204 bool parallel = UPstream::parRun()
205 )
206 {
207 return calculateList_impl<Int>(localSizes, communicator, parallel);
208 }
209
210
211 // Some functionality as per globalIndex
212
213 // Local queries and renumbering
214
215 //- Test if index is within the range. Same as contains()
216 bool isLocal(IntType i) const noexcept
217 {
218 return this->contains(i);
219 }
220
221 //- From local to global index
222 template<class IntType2>
223 IntType2 toGlobal(IntType2 i) const noexcept
224 {
225 return (this->start() + i);
226 }
227
228 //- Convert a list from local to global index
229 template<class IntType2>
231
232 //- Convert a list (inplace) from local to global index
233 template<class IntType2>
234 void inplaceToGlobal(UList<IntType2>& labels) const;
235
236 //- From global to local
237 // FatalError for an out-of-range value
238 template<class IntType2>
239 IntType2 toLocal(IntType2 i) const;
240};
241
242} // End namespace Foam
243
244// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
245
246#include "globalOffsetI.H"
247
248// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
249
250#ifdef NoRepository
251 #include "globalOffset.txx"
252#endif
253
254// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
255
256#endif
258// ************************************************************************* //
Implementation of IO operators for Foam::OffsetRange.
A Foam::OffsetRange (start, size, total) tuple with some additional parallel functionality....
void reset(IntType len, int communicator, bool parallel=UPstream::parRun())
Reset from local size, with parallel coordination of the offsets and total size.
List< IntType2 > toGlobal(const UList< IntType2 > &labels) const
Convert a list from local to global index.
static OffsetRange< Int > calculate(IntType localSize, int communicator=UPstream::worldComm, bool parallel=UPstream::parRun())
Calculate a globally-consistent set of start/size/total based on the given local input size.
IntType localSize() const noexcept
Same as size() - method name as per globalIndex.
void inplaceToGlobal(UList< IntType2 > &labels) const
Convert a list (inplace) from local to global index.
static OffsetRange< IntType > calculate(IntType localSize, int communicator=UPstream::worldComm, bool parallel=UPstream::parRun())
Calculate a globally-consistent set of start/size/total based on the given local input size.
IntType2 toGlobal(IntType2 i) const noexcept
From local to global index.
label localStart() const noexcept
constexpr GlobalOffset() noexcept
Default construct as (0,0,0).
IntType2 toLocal(IntType2 i) const
From global to local.
void reduce(int communicator=UPstream::worldComm)
Based on the local input size, determine the globally-consistent start offset and total size.
IntType totalSize() const noexcept
Same as total() - method name as per globalIndex.
bool isLocal(IntType i) const noexcept
Test if index is within the range. Same as contains().
static List< OffsetRange< Int > > calculate(const UList< Int > &localSizes, int communicator=UPstream::worldComm, bool parallel=UPstream::parRun())
Return a list of globally-consistent start/size/total ranges based on the given local input sizes.
void reset(IntType len) noexcept
Reset from local size with zero offset.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
A tuple of integers comprising start, size, total.
Definition OffsetRange.H:82
IntRange< label > range() const noexcept
OffsetRange(const OffsetRange &) noexcept=default
Copy construct.
IntType start() const noexcept
The lower value of the range.
IntType size() const noexcept
The size.
IntType total() const noexcept
The total size.
bool contains(IntType value) const noexcept
True if the value is between the start and size range range.
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition UList.H:89
Inter-processor communications stream.
Definition UPstream.H:69
static bool parRun(const bool on) noexcept
Set as parallel run on/off.
Definition UPstream.H:1669
static label worldComm
Communicator for all ranks. May differ from commGlobal() if local worlds are in use.
Definition UPstream.H:1069
Namespace for OpenFOAM.
GlobalOffset< label > globalOffset
A GlobalOffset with label for the addressing.
const direction noexcept
Definition scalarImpl.H:265