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