Nektar++
Loading...
Searching...
No Matches
CGBndCondSurfaceIntegral.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: CGBndCondSurfaceIntegral.hpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: Surface integral of a boundary field from its boundary
32// inner-product coefficients, summed in the memory space of the execution
33// space.
34//
35///////////////////////////////////////////////////////////////////////////////
36
37#pragma once
38
39#include <array>
40#include <vector>
41
45
47{
48
49/**
50 * @brief Adds the surface integral of boundary fields, per component and
51 * homogeneous plane, to a reduction in the memory space of @p ExecSpace, from
52 * the inner products of the fields with the basis of their boundary expansion
53 * lists.
54 *
55 * @details
56 * The surface integral of a field is the sum of its inner products with the
57 * basis, each weighted by the coefficient of the constant function one in
58 * that basis: one at every mode of a Lagrange basis, and, for a Modified_A
59 * basis, one at the vertex modes, the collapsed vertex of a triangle
60 * included, and zero at the others; the value of a point region is its
61 * integral. A component of a Lagrange or a point block is summed whole, its
62 * padding being zero. A component of a Modified_A block is summed over a
63 * mask laid out as one component of the block at its interleave width,
64 * holding one at the vertex modes of every element and zero elsewhere, the
65 * padding included. One storage index then addresses the same coefficient in
66 * both, whatever that width, and the sum is a masked reduction over the
67 * component as it is laid out, with no index arithmetic. The mask of a block
68 * is built at interleave width 1, the width of the boundary fields, when this
69 * is built, and at the interleave width of the execution space, the only
70 * other, the first time a field is read at it, and kept. The reduction is
71 * moved to the memory space of the execution space on one stream, which the
72 * block streams wait for; each block is then summed on its own stream, where
73 * it was written, into the reduction, which is not read back.
74 */
75template <typename ExecSpace, typename TData> class CGBndCondSurfaceIntegral
76{
77 using MemSpace = typename ExecSpace::memory_space;
78
79 // Interleave width of an interleaved field: 1 (Serial), the SIMD width
80 // (AVX) or the warp size (Device).
81 static constexpr unsigned int m_interleaveWidth =
83
84public:
86
87 /**
88 * @brief Build the sum of the surface integrals of the fields on the
89 * boundary expansion lists @p bndExpLists, of component nc of field n
90 * where isComp[n][nc] holds.
91 */
93 const std::vector<MultiRegions::ExpListSharedPtr> &bndExpLists,
94 const std::vector<std::vector<bool>> &isComp)
95 : m_isComp(isComp), m_vertexModes(bndExpLists.size()),
96 m_masks(bndExpLists.size())
97 {
98 for (unsigned int n = 0; n < bndExpLists.size(); ++n)
99 {
100 const bool isPoint = (bndExpLists[n]->GetShapeDimension() == 0);
101 auto collections = MultiRegions::GetCollections(bndExpLists[n]);
102 m_masks[n].resize(collections.size());
103 for (auto &collection : collections)
104 {
105 // Every coefficient of a point or a Lagrange block is summed.
106 m_vertexModes[n].emplace_back();
107 if (isPoint)
108 {
109 continue;
110 }
111
112 const auto exp = collection.GetExpVector()[0];
113 const auto basisType = exp->GetBasisType(0);
114 if (basisType == LibUtilities::eModified_A)
115 {
116 for (int k = 0; k < exp->GetNverts(); ++k)
117 {
118 m_vertexModes[n].back().push_back(exp->GetVertexMap(k));
119 }
120 }
121 else
122 {
125 "The surface integral of a boundary field needs "
126 "a Modified_A or a Lagrange basis.");
127 }
128 }
129
130 if (isPoint)
131 {
132 continue;
133 }
134
135 // The masks at interleave width 1, the width of the boundary
136 // fields.
137 const auto blockAttributes =
138 MultiRegions::GetBlockAttributes<TData, FieldState::Coeff>(
139 bndExpLists[n],
140 MultiRegions::UnitInterleaveWidths(bndExpLists[n]));
141 for (unsigned int blk = 0; blk < blockAttributes.size(); ++blk)
142 {
143 const unsigned int streamID = blk + 1;
144 if (!m_vertexModes[n][blk].empty())
145 {
146 BuildMask(n, blk, blockAttributes[blk], streamID);
147 }
148 }
149 }
150 }
151
152 /**
153 * @brief Add the surface integral of component nc of every field
154 * coeffs[n], the fields of the boundary expansion lists this was built
155 * for, in the same order, to data[nc], where isComp[n][nc] holds. With
156 * homogeneous planes, that of plane p of component nc is added to
157 * data[nc * nHomo + p].
158 */
161 {
162 // data is moved to the memory space of the execution space on
163 // m_opStreamID, which the block streams then wait for.
164 auto dataPtr = data.template GetPtr<MemSpace, ReadWrite>(m_opStreamID);
165 std::vector<unsigned int> blockStreams;
166 for (auto &field : coeffs)
167 {
168 for (unsigned int blk = blockStreams.size();
169 blk < field.GetBlocks().size(); ++blk)
170 {
171 blockStreams.push_back(blk + 1);
172 }
173 }
174 ReleaseStreams<ExecSpace>(m_opStreamID, blockStreams);
175
176 for (unsigned int n = 0; n < coeffs.size(); ++n)
177 {
178 const unsigned int nHomo = coeffs[n].GetNumHomoModes();
179 ASSERTL1(coeffs[n].GetNumComponents() * nHomo <= data.size(),
180 "The surface integral holds one value per component "
181 "and homogeneous plane.");
182
183 for (unsigned int blk = 0; blk < coeffs[n].GetBlocks().size();
184 ++blk)
185 {
186 const unsigned int streamID = blk + 1;
187 auto &block = coeffs[n].GetBlocks()[blk];
188 const uint8_t *maskPtr = GetMask(n, blk, block, streamID);
189 auto srcPtr =
190 block.template GetPtr<MemSpace, ReadOnly>(streamID);
191 const unsigned int nComp = coeffs[n].GetNumComponents();
192 for (unsigned int i = 0; i < nComp * nHomo; ++i)
193 {
194 if (!m_isComp[n][i / nHomo])
195 {
196 continue;
197 }
198
199 if (maskPtr != nullptr)
200 {
201 Math::reduceSumKernel<ExecSpace, false>(
202 block.CompSize(), maskPtr,
203 srcPtr + i * block.CompSize(), dataPtr + i,
204 streamID);
205 }
206 else
207 {
208 Math::reduceSumKernel<ExecSpace, false>(
209 block.CompSize(), srcPtr + i * block.CompSize(),
210 dataPtr + i, streamID);
211 }
212 }
213 }
214 }
215 }
216
217private:
218 // Stream on which data is moved before the block streams sum into it.
219 static constexpr unsigned int m_opStreamID = 1;
220 // Per field, whether each component is summed.
221 std::vector<std::vector<bool>> m_isComp;
222 // Per field and block, the positions within an element of the vertex
223 // modes of a Modified_A block; empty for a point or a Lagrange block.
224 std::vector<std::vector<std::vector<size_t>>> m_vertexModes;
225 // Per field and block, the masks of a Modified_A block at interleave
226 // width 1 and at that of the execution space, in that order: one at the
227 // vertex modes of every element, zero elsewhere, the padding included;
228 // empty until built.
229 std::vector<std::vector<std::array<LibUtilities::MemoryRegion<uint8_t>, 2>>>
231
232 /**
233 * @brief The index into #m_masks of interleave width @p interleaveWidth.
234 */
235 static unsigned int MaskIndex(const unsigned int interleaveWidth)
236 {
237 ASSERTL0(interleaveWidth == 1u || interleaveWidth == m_interleaveWidth,
238 "The surface integral takes fields at interleave width 1 or "
239 "at the interleave width of its execution space.");
240 return (interleaveWidth == 1u) ? 0u : 1u;
241 }
242
243 /**
244 * @brief The mask of block @p blk of field @p n at the interleave width of
245 * @p block, in the memory space of the execution space, built and
246 * uploaded on stream @p streamID the first time a block is read at a
247 * width other than 1; null for a block summed whole.
248 */
249 const uint8_t *GetMask(
250 const unsigned int n, const unsigned int blk,
252 const unsigned int streamID)
253 {
254 if (m_vertexModes[n][blk].empty())
255 {
256 return nullptr;
257 }
258
259 auto &mask = m_masks[n][blk][MaskIndex(block.GetInterleaveWidth())];
260 if (mask.size() == 0)
261 {
262 BuildMask(n, blk, block, streamID);
263 }
264
265 return mask.template GetPtr<MemSpace, ReadOnly>(streamID);
266 }
267
268 /**
269 * @brief Build the mask of the Modified_A block @p blk of field @p n at
270 * the interleave width of @p block, upload it on stream @p streamID and
271 * keep it.
272 */
274 const unsigned int n, const unsigned int blk,
276 const unsigned int streamID)
277 {
278 // Element e is lane ilane = e % interleaveWidth of group
279 // iwarp = e / interleaveWidth, its coefficient m at storage index
280 // (iwarp * nData + m) * interleaveWidth + ilane. The entries of the
281 // padded elements stay zero.
282 const unsigned int interleaveWidth = block.GetInterleaveWidth();
283 const size_t nData = block.GetNumData();
284 std::vector<uint8_t> hostMask(block.CompSize(), 0);
285 for (size_t e = 0; e < block.GetNumElements(); ++e)
286 {
287 const size_t ilane = e % interleaveWidth;
288 const size_t iwarp = e / interleaveWidth;
289 for (const size_t mode : m_vertexModes[n][blk])
290 {
291 hostMask[(iwarp * nData + mode) * interleaveWidth + ilane] = 1;
292 }
293 }
294
295 m_masks[n][blk][MaskIndex(interleaveWidth)] =
297 uint8_t>(
298 hostMask, streamID);
299 }
300};
301
302} // namespace Nektar::MultiRegions::detail
#define ASSERTL0(condition, msg)
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
A block means a group of elements of identical shape, basis and order. BlockAttributes stores the mos...
Definition Block.hpp:50
unsigned int GetInterleaveWidth(void) const
Definition Block.hpp:103
unsigned int GetNumData(void) const
Definition Block.hpp:93
size_t GetNumElements(void) const
Definition Block.hpp:83
A Field represents expansion data to be operated on.
A MemoryRegion represents a memory region.
size_t size() const
Get the storage size.
Adds the surface integral of boundary fields, per component and homogeneous plane,...
std::vector< std::vector< std::vector< size_t > > > m_vertexModes
void BuildMask(const unsigned int n, const unsigned int blk, const LibUtilities::BlockAttributes< FieldState::Coeff > &block, const unsigned int streamID)
Build the mask of the Modified_A block blk of field n at the interleave width of block,...
CGBndCondSurfaceIntegral(const std::vector< MultiRegions::ExpListSharedPtr > &bndExpLists, const std::vector< std::vector< bool > > &isComp)
Build the sum of the surface integrals of the fields on the boundary expansion lists bndExpLists,...
static unsigned int MaskIndex(const unsigned int interleaveWidth)
The index into m_masks of interleave width interleaveWidth.
const uint8_t * GetMask(const unsigned int n, const unsigned int blk, const LibUtilities::BlockAttributes< FieldState::Coeff > &block, const unsigned int streamID)
The mask of block blk of field n at the interleave width of block, in the memory space of the executi...
void Add(std::vector< LibUtilities::Field< TData, FieldState::Coeff > > &coeffs, LibUtilities::MemoryRegion< TData > &data)
Add the surface integral of component nc of every field coeffs[n], the fields of the boundary expansi...
std::vector< std::vector< std::array< LibUtilities::MemoryRegion< uint8_t >, 2 > > > m_masks
@ eGauss_Lagrange
Lagrange Polynomials using the Gauss points.
Definition BasisType.h:57
@ eGLL_Lagrange
Lagrange for SEM basis .
Definition BasisType.h:56
@ eModified_A
Principle Modified Functions .
Definition BasisType.h:48
Collections::CollectionVector GetCollections(MultiRegions::ExpListSharedPtr expansionList)