Nektar++
Loading...
Searching...
No Matches
HelmholtzBlockOpDeviceStdMatKernels.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: HelmholtzBlockOpDeviceStdMatKernels.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:
32//
33///////////////////////////////////////////////////////////////////////////////
34
35/**
36 * @file HelmholtzBlockOpDeviceStdMatKernels.hpp
37 * @brief Geometry and diffusion-tensor kernel of the device StdMat weak
38 * Helmholtz operator.
39 *
40 * @details
41 * The third step of the five-stage decomposition (see
42 * HelmholtzBlockOpDeviceStdMat.hpp), expressed as a grid-strided kernel over
43 * the quadrature points of the block, one variable and plane per index
44 * of the second grid dimension. It is compiled only into device
45 * translation units of a device-enabled build (NEKTAR_ENABLE_DEVICE and
46 * DEVICE_COMPILE_ONLY).
47 */
48
49#pragma once
50
53
54// Coefficients are passed as raw pointers. Variable tensor components are
55// separated by varDiffCoeffSize scalars. The kernels select the plane and
56// element from the base pointer; point offsets follow the same interleaving
57// as the input and derivative-factor arrays.
58
60{
61
62#if defined(NEKTAR_ENABLE_DEVICE) && defined(DEVICE_COMPILE_ONLY)
63/**
64 * @brief Apply the Jacobian, the derivative factors and the diffusion
65 * tensor to the block's reference derivatives, and scale its physical
66 * values by `scale * J`.
67 *
68 * @details
69 * One thread handles one quadrature point of one element, for one
70 * component-and-homogeneous-mode plane, striding over the plane by the
71 * grid size, and writes for its point
72 * \f[ \mathrm{out}_d = J \sum_e G_{de}
73 * \frac{\partial u}{\partial\xi_e}, \qquad
74 * G_{de} = \sum_{k,l} M_{kd} D_{kl} M_{le}, \f]
75 * as well as `bwd *= scale * J`, which is skipped when @p scale is zero;
76 * see HelmholtzBlockOpSerialAVXStdMatKernels.hpp for the meaning of the two
77 * terms and HelmholtzOp.hpp for the metric. The quadrature weights are carried
78 * by the standard-region inner-product matrices, not applied here.
79 *
80 * The variables and planes run together on the second grid dimension:
81 * block `c` there works on the `c`-th set of `nqTot * nelmt` values of
82 * @p in, @p out and @p bwd, so every plane reuses the same element
83 * geometry. On a regular block the metric is formed from the element's
84 * constant derivative factors; on a deformed block it is formed from the
85 * point's own, indexed with the transposed layout this path requests from
86 * DerivFactorKey (factor `k * dimension + d` of point `i` at
87 * `ndf * nqTot * e + (k * dimension + d) * nqTot + i`).
88 *
89 * @p out may alias @p in -- the block operator passes the same
90 * workspace for both, and a thread reads all of its point's inputs
91 * before writing its outputs.
92 *
93 * @tparam DEFORMED Geometry varies within an element.
94 * @tparam TthreadBlock Back-end thread-block handle supplying the
95 * global index and range.
96 * @tparam TData Floating-point type of the field data.
97 *
98 * @param nqTot Quadrature points per element.
99 * @param ncoord Coordinate dimension of the element.
100 * @param dimension Dimension of the reference element.
101 * @param nelmt Elements in the block, padding included.
102 * @param inoffset Stride between successive reference-derivative
103 * directions in @p in.
104 * @param outoffset Same, for @p out.
105 * @param diffCoeff Constant packed tensor, indexed through
106 * LibUtilities::GetDiffCoeffMap(), or the base
107 * pointer of the variable tensor (VARCOEFF).
108 * @param jacptr Jacobian: one value per element, or one per
109 * quadrature point when @p DEFORMED.
110 * @param dfptr Derivative factors in the transposed layout
111 * described above, with `dfptr[k * dimension + d]`
112 * = \f$M_{kd} = \partial\xi_d/\partial x_k\f$ per
113 * point.
114 * @param in Reference derivatives of the expansion,
115 * @p dimension directions @p inoffset apart, each
116 * holding `nqTot * nelmt` values per plane.
117 * @param out Receives \f$J G \nabla_\xi u\f$, same layout.
118 * @param bwd Physical values of the expansion, scaled in
119 * place by `scale * J` unless @p scale is zero.
120 * @param scale Mass-term weight, i.e. lambda.
121 * @param threadBlock Back-end thread-block handle.
122 */
123template <bool DEFORMED, bool VARCOEFF, typename TthreadBlock, typename TData>
125 const unsigned int nqTot, const unsigned int ncoord,
126 const unsigned int dimension, const size_t nelmt, const size_t inoffset,
127 const size_t outoffset, const TData *diffCoeff, const unsigned int nhomo,
128 const TData *jacptr, const TData *dfptr, const TData *in, TData *out,
129 TData *bwd, const TData scale, const TthreadBlock &threadBlock)
130{
131 const auto ndf = ncoord * dimension;
132 const size_t nsize = nqTot * nelmt;
133 const size_t varDiffCoeffSize = VARCOEFF ? nsize * nhomo : 1;
134
135 size_t idx = getGlobalIdx<0>(threadBlock);
136 const unsigned int c = getBlockIdx<1>(threadBlock);
137 const TData *inptr = in + nsize * c;
138 TData *outptr = out + nsize * c;
139 TData *bwdptr = bwd + nsize * c;
140 while (idx < nsize)
141 {
142 const TData *diffCoeffPtr =
143 VARCOEFF ? diffCoeff + (c % nhomo) * nsize + idx : diffCoeff;
144 if constexpr (DEFORMED)
145 {
146 size_t e = idx / nqTot;
147 size_t i = idx - e * nqTot;
148 TData tmp[3] = {0.0}, tmp0 = 0.0, metric[3];
149
150 auto jac = jacptr[idx];
151
152 for (unsigned int k = 0; k < ncoord; ++k)
153 {
154 tmp[k] = dfptr[ndf * nqTot * e + (k * dimension) * nqTot + i] *
155 inptr[idx];
156 for (unsigned int d = 1; d < dimension; d++)
157 {
158 tmp[k] += dfptr[ndf * nqTot * e +
159 (k * dimension + d) * nqTot + i] *
160 inptr[idx + d * inoffset];
161 }
162 }
163
164 // Write. bwdptr is left unset when scale is zero.
165 if (scale != 0.0)
166 {
167 bwdptr[idx] *= scale * jac;
168 }
169
170 // Compute metric.
171 for (unsigned int d = 0; d < dimension; d++)
172 {
173 for (unsigned int k = 0; k < ncoord; ++k)
174 {
175 metric[k] = dfptr[ndf * nqTot * e + d * nqTot + i] *
176 diffCoeffPtr[(LibUtilities::GetDiffCoeffMap(
177 ncoord, k)) *
178 varDiffCoeffSize];
179 for (unsigned int l = 1; l < ncoord; ++l)
180 {
181 metric[k] +=
182 dfptr[ndf * nqTot * e +
183 (l * dimension + d) * nqTot + i] *
184 diffCoeffPtr[(LibUtilities::GetDiffCoeffMap(
185 ncoord, l * ncoord + k)) *
186 varDiffCoeffSize];
187 }
188 }
189 tmp0 = metric[0] * tmp[0];
190 for (unsigned int k = 1; k < ncoord; k++)
191 {
192 tmp0 += metric[k] * tmp[k];
193 }
194
195 // Write.
196 outptr[d * outoffset + idx] = tmp0 * jac;
197 }
198 }
199 else
200 {
201 size_t e = idx / nqTot;
202 TData tmp[3] = {0.0}, metric[3];
203
204 // Compute metric.
205 for (unsigned int d = 0; d < dimension; d++)
206 {
207 for (unsigned int k = 0; k < ncoord; ++k)
208 {
209 metric[k] = dfptr[(ndf * e + d)] *
210 diffCoeffPtr[(LibUtilities::GetDiffCoeffMap(
211 ncoord, k)) *
212 varDiffCoeffSize];
213 for (unsigned int l = 1; l < ncoord; ++l)
214 {
215 metric[k] +=
216 dfptr[(ndf * e + l * dimension + d)] *
217 diffCoeffPtr[(LibUtilities::GetDiffCoeffMap(
218 ncoord, l * ncoord + k)) *
219 varDiffCoeffSize];
220 }
221 }
222 for (unsigned int k = 0; k < dimension; ++k)
223 {
224 TData sum = 0.0;
225 for (unsigned int l = 0; l < ncoord; ++l)
226 {
227 sum += metric[l] * dfptr[ndf * e + l * dimension + k];
228 }
229 tmp[d] += sum * inptr[idx + k * inoffset];
230 }
231 }
232
233 // Write.
234 auto jac = jacptr[e];
235 for (unsigned int d = 0; d < dimension; d++)
236 {
237 outptr[d * outoffset + idx] = tmp[d] * jac;
238 }
239 // bwdptr is left unset when scale is zero.
240 if (scale != 0.0)
241 {
242 bwdptr[idx] *= scale * jac;
243 }
244 }
245
246 idx += getGlobalRange<0>(threadBlock);
247 }
248}
249#endif
250} // namespace Nektar::MultiRegions::detail
#define NEK_DEVICE_KERNEL
Definition Backends.hpp:297
std::map< int64_t, double > sum
NEK_HOSTDEVICE_INLINE unsigned int GetDiffCoeffMap(const unsigned int ncoord, const unsigned int index)
NEK_DEVICE_KERNEL void const TData *NEK_RESTRICT const TData *NEK_RESTRICT const size_t const unsigned int const unsigned int const unsigned int const unsigned int const size_t const TData *NEK_RESTRICT const TData *NEK_RESTRICT const TUseSharedWsp TData *NEK_RESTRICT const TData *NEK_RESTRICT TData *NEK_RESTRICT const bool const bool unsigned char const TthreadBlock & threadBlock
NEK_DEVICE_KERNEL void const TData *NEK_RESTRICT const TData *NEK_RESTRICT const size_t const unsigned int const unsigned int const unsigned int const unsigned int const size_t const TData *NEK_RESTRICT const TData *NEK_RESTRICT const TUseSharedWsp TData *NEK_RESTRICT const TData *NEK_RESTRICT in
static NEK_FORCE_INLINE void ApplyMetricKernel(const unsigned int nqTot, const unsigned int ncoord, const unsigned int dimension, const size_t nelmt, const size_t insize, const size_t outsize, const TScalar *diffCoeff, const TData *varDiffCoeff, const size_t varDiffCoeffSize, const TData *jacptr, const TData *dfptr, const TData *inptr, TData *outptr, TData *bwdptr, const TScalar scale)
Apply the Jacobian, the derivative factors and the diffusion tensor to one group's reference derivati...
NEK_DEVICE_KERNEL void const TData *NEK_RESTRICT const TData *NEK_RESTRICT const size_t const unsigned int const unsigned int const unsigned int const unsigned int const size_t const TData *NEK_RESTRICT const TData *NEK_RESTRICT jac
NEK_DEVICE_KERNEL void const TData *NEK_RESTRICT const TData *NEK_RESTRICT const size_t nelmt
NEK_DEVICE_KERNEL void const TData *NEK_RESTRICT const TData *NEK_RESTRICT const size_t const unsigned int const unsigned int const unsigned int const unsigned int const size_t const TData *NEK_RESTRICT const TData *NEK_RESTRICT const TUseSharedWsp TData *NEK_RESTRICT const TData *NEK_RESTRICT TData *NEK_RESTRICT out
std::vector< double > d(NPUPPER *NPUPPER)