Nektar++
Loading...
Searching...
No Matches
CurlCurlBlockOpDeviceStdMatKernels.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: CurlCurlBlockOpDeviceStdMatKernels.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 CurlCurlBlockOpDeviceStdMatKernels.hpp
37 * @brief Pointwise curl kernels of the Device StdMat curl-curl block
38 * operator (see CurlCurlBlockOpDeviceStdMat.hpp).
39 *
40 * @details
41 * These kernels chain-rule reference-space derivatives into Cartesian ones
42 * through the derivative factors and combine them into a curl. They run as
43 * device kernels over all quadrature points of the block, launched by the
44 * block operator on its device stream, and they read the derivative planes
45 * the batched GEMM lays out direction-major. The 2D kernels take the
46 * homogeneous planes of a 3DH1 block on the second grid dimension of the
47 * same launch.
48 */
49
50#pragma once
51
53
55{
56
57#if defined(NEKTAR_ENABLE_DEVICE) && defined(DEVICE_COMPILE_ONLY)
58/**
59 * @brief Scalar curl of a two-dimensional vector field,
60 * omega = d(u1)/dx0 - d(u0)/dx1, at every quadrature point of every
61 * element of the block.
62 *
63 * The derivative-factor entry k * 2 + d maps the reference-direction-d
64 * derivative into the Cartesian-k gradient component. The factors are read
65 * in the layout produced by DerivFactorKey with the transpose flag set: for
66 * regular geometry one set of four values per element, for deformed
67 * geometry one such set per element in which each factor component is
68 * contiguous over the element's nqTot points. The chain rule and the curl
69 * are applied in a single sweep. The second grid dimension picks the
70 * homogeneous plane, nqTot * nelmt values apart in @p deriv and @p omega.
71 *
72 * @tparam DEFORMED Read per-point rather than per-element derivative
73 * factors.
74 * @tparam TthreadBlock Back-end thread-block handle type.
75 *
76 * @param nqTot Quadrature points per element.
77 * @param nelmt Elements in the block, including padding.
78 * @param derivoffset Stride between the planes of @p deriv,
79 * nqTot * nelmt times the number of homogeneous
80 * planes.
81 * @param dfptr Derivative factors of the block, laid out as
82 * described above.
83 * @param deriv Reference-space derivatives of the two components,
84 * ordered as deriv[(d * 2 + c) * derivoffset + idx]
85 * for direction d and component c.
86 * @param omega Output plane of nqTot * nelmt values.
87 * @param threadBlock Thread-block handle supplied by the launcher
88 * macro.
89 */
90template <bool DEFORMED, typename TthreadBlock, typename TData>
92 const unsigned int nqTot, const size_t nelmt, const size_t derivoffset,
93 const TData *dfptr, const TData *deriv, TData *omega,
94 const TthreadBlock &threadBlock)
95{
96 constexpr unsigned int ndf = 4u;
97 const size_t nsize = nqTot * nelmt;
98
99 size_t idx = getGlobalIdx<0>(threadBlock);
100 const unsigned int p = getBlockIdx<1>(threadBlock);
101 const TData *derivptr = deriv + nsize * p;
102 TData *omegaptr = omega + nsize * p;
103 while (idx < nsize)
104 {
105 const size_t e = idx / nqTot;
106
107 const size_t dfbase =
108 DEFORMED ? ndf * nqTot * e + (idx - e * nqTot) : ndf * e;
109 const unsigned int dfstride = DEFORMED ? nqTot : 1u;
110
111 const TData d0u = derivptr[0u * derivoffset + idx];
112 const TData d0v = derivptr[1u * derivoffset + idx];
113 const TData d1u = derivptr[2u * derivoffset + idx];
114 const TData d1v = derivptr[3u * derivoffset + idx];
115
116 const TData dvdx = d0v * dfptr[dfbase + 0u * dfstride] +
117 d1v * dfptr[dfbase + 1u * dfstride];
118 const TData dudy = d0u * dfptr[dfbase + 2u * dfstride] +
119 d1u * dfptr[dfbase + 3u * dfstride];
120
121 omegaptr[idx] = dvdx - dudy;
122
123 idx += getGlobalRange<0>(threadBlock);
124 }
125}
126
127/**
128 * @brief Vector curl of a two-dimensional scalar field,
129 * out = ( d(omega)/dx1, -d(omega)/dx0 ), at every quadrature point of
130 * every element of the block.
131 *
132 * The derivative factors are read as in Curl2DScalarStdMatKernel; both
133 * output components are written by the same work item. The second grid
134 * dimension picks the homogeneous plane, nqTot * nelmt values apart in
135 * @p deriv and @p out.
136 *
137 * @tparam DEFORMED Read per-point rather than per-element derivative
138 * factors.
139 * @tparam TthreadBlock Back-end thread-block handle type.
140 *
141 * @param nqTot Quadrature points per element.
142 * @param nelmt Elements in the block, including padding.
143 * @param derivoffset Stride between the planes of @p deriv.
144 * @param outoffset Stride between the components of @p out.
145 * @param dfptr Derivative factors of the block.
146 * @param deriv Reference-space derivatives of omega, ordered as
147 * deriv[d * derivoffset + idx] for direction d.
148 * @param out Output, two components of nqTot * nelmt values.
149 * @param threadBlock Thread-block handle supplied by the launcher
150 * macro.
151 */
152template <bool DEFORMED, typename TthreadBlock, typename TData>
154 const unsigned int nqTot, const size_t nelmt, const size_t derivoffset,
155 const size_t outoffset, const TData *dfptr, const TData *deriv, TData *out,
156 const TthreadBlock &threadBlock)
157{
158 constexpr unsigned int ndf = 4u;
159 const size_t nsize = nqTot * nelmt;
160
161 size_t idx = getGlobalIdx<0>(threadBlock);
162 const unsigned int p = getBlockIdx<1>(threadBlock);
163 const TData *derivptr = deriv + nsize * p;
164 TData *outptr = out + nsize * p;
165 while (idx < nsize)
166 {
167 const size_t e = idx / nqTot;
168
169 const size_t dfbase =
170 DEFORMED ? ndf * nqTot * e + (idx - e * nqTot) : ndf * e;
171 const unsigned int dfstride = DEFORMED ? nqTot : 1u;
172
173 const TData d0 = derivptr[0u * derivoffset + idx];
174 const TData d1 = derivptr[1u * derivoffset + idx];
175
176 outptr[idx] = d0 * dfptr[dfbase + 2u * dfstride] +
177 d1 * dfptr[dfbase + 3u * dfstride];
178 outptr[outoffset + idx] = -(d0 * dfptr[dfbase + 0u * dfstride] +
179 d1 * dfptr[dfbase + 1u * dfstride]);
180
181 idx += getGlobalRange<0>(threadBlock);
182 }
183}
184
185/**
186 * @brief Curl of a three-dimensional vector field at every quadrature
187 * point of every element of the block; used for both passes of the
188 * curl-curl operator, omega = curl u and out = curl omega.
189 *
190 * The derivative-factor entry k * 3 + d maps the reference-direction-d
191 * derivative into the Cartesian-k gradient component, the nine factors
192 * being read as in Curl2DScalarStdMatKernel. Only the six off-diagonal
193 * Cartesian derivatives are formed, the diagonal ones cancelling out of
194 * the curl:
195 * out0 = d(u2)/dx1 - d(u1)/dx2,
196 * out1 = d(u0)/dx2 - d(u2)/dx0,
197 * out2 = d(u1)/dx0 - d(u0)/dx1.
198 *
199 * @tparam DEFORMED Read per-point rather than per-element derivative
200 * factors.
201 * @tparam TthreadBlock Back-end thread-block handle type.
202 *
203 * @param nqTot Quadrature points per element.
204 * @param nelmt Elements in the block, including padding.
205 * @param derivoffset Stride between the planes of @p deriv.
206 * @param outoffset Stride between the components of @p out: the plane
207 * stride of the workspace for the omega pass, the
208 * component size of the output block for the final
209 * pass.
210 * @param dfptr Derivative factors of the block.
211 * @param deriv Reference-space derivatives of the three
212 * components, ordered as
213 * deriv[(d * 3 + c) * derivoffset + idx] for
214 * direction d and component c.
215 * @param out Output, three components of nqTot * nelmt values.
216 * @param threadBlock Thread-block handle supplied by the launcher
217 * macro.
218 */
219template <bool DEFORMED, typename TthreadBlock, typename TData>
221 const unsigned int nqTot, const size_t nelmt, const size_t derivoffset,
222 const size_t outoffset, const TData *dfptr, const TData *deriv, TData *out,
223 const TthreadBlock &threadBlock)
224{
225 constexpr unsigned int ndf = 9u;
226 const size_t nsize = nqTot * nelmt;
227
228 size_t idx = getGlobalIdx<0>(threadBlock);
229 while (idx < nsize)
230 {
231 const size_t e = idx / nqTot;
232
233 const size_t dfbase =
234 DEFORMED ? ndf * nqTot * e + (idx - e * nqTot) : ndf * e;
235 const unsigned int dfstride = DEFORMED ? nqTot : 1u;
236
237 TData d0[3], d1[3], d2[3];
238#pragma unroll
239 for (unsigned int c = 0u; c < 3u; ++c)
240 {
241 d0[c] = deriv[(0u * 3u + c) * derivoffset + idx];
242 d1[c] = deriv[(1u * 3u + c) * derivoffset + idx];
243 d2[c] = deriv[(2u * 3u + c) * derivoffset + idx];
244 }
245
246 TData df[9];
247#pragma unroll
248 for (unsigned int m = 0u; m < 9u; ++m)
249 {
250 df[m] = dfptr[dfbase + m * dfstride];
251 }
252
253 // dw/dy - dv/dz
254 out[idx] = (d0[2] * df[3] + d1[2] * df[4] + d2[2] * df[5]) -
255 (d0[1] * df[6] + d1[1] * df[7] + d2[1] * df[8]);
256 // du/dz - dw/dx
257 out[outoffset + idx] = (d0[0] * df[6] + d1[0] * df[7] + d2[0] * df[8]) -
258 (d0[2] * df[0] + d1[2] * df[1] + d2[2] * df[2]);
259 // dv/dx - du/dy
260 out[2u * outoffset + idx] =
261 (d0[1] * df[0] + d1[1] * df[1] + d2[1] * df[2]) -
262 (d0[0] * df[3] + d1[0] * df[4] + d2[0] * df[5]);
263
264 idx += getGlobalRange<0>(threadBlock);
265 }
266}
267#endif
268
269} // namespace Nektar::MultiRegions::detail
#define NEK_DEVICE_KERNEL
Definition Backends.hpp:297
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
static NEK_FORCE_INLINE void Curl2DScalarStdMatKernel(const unsigned int nqTot, const TData *df_ptr, const TData *deriv, TData *omega)
Scalar curl of a two-dimensional vector field, omega = d(u1)/dx0 - d(u0)/dx1, per quadrature point wi...
static NEK_FORCE_INLINE void Curl3DStdMatKernel(const unsigned int nqTot, const TData *df_ptr, const TData *deriv, TData *out0, TData *out1, TData *out2)
Curl of a three-dimensional vector field, per quadrature point with one element per SIMD lane; used f...
static NEK_FORCE_INLINE void Curl2DVectorStdMatKernel(const unsigned int nqTot, const TData *df_ptr, const TData *deriv, TData *out0, TData *out1)
Vector curl of a two-dimensional scalar field, out = ( d(omega)/dx1, -d(omega)/dx0 ),...
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 > p(NPUPPER)