Nektar++
Loading...
Searching...
No Matches
DivergenceBlockOpDeviceSumFacKernels.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: DivergenceBlockOpDeviceSumFacKernels.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 DivergenceBlockOpDeviceSumFacKernels.hpp
37 * @brief SumFac device kernel launchers of the block divergence -- one
38 * element per thread -- together with their shared-memory sizing
39 * helpers.
40 *
41 * @details
42 * The DivergenceKernelLauncher overloads of this file, one per
43 * dimension, write \f$\sum_c \partial u_c/\partial x_c\f$ to the
44 * output. In 1D that is PhysDerivDir1DSumFacKernel
45 * (PhysDeriv/PhysDerivBlockOpDeviceSumFacKernels.hpp, included here).
46 * In 2D and 3D, Divergence{2,3}DSumFacKernel differentiate every
47 * component in every reference direction at a point, apply the
48 * collapsed-coordinate correction where the shape requires it, contract
49 * each component with its own row of the derivative factors and store
50 * the sum once, so the output is written once per point.
51 * DivergenceSharedMemorySize reports how much dynamic shared memory a
52 * launch must request.
53 *
54 * Each thread owns one element: element e is handled by lane
55 * `e % warpSize` of warp `e / warpSize`, matching the warp-interleaved
56 * block storage the host side reshapes to, and the kernels grid-stride
57 * over the elements left after the first pass (padding elements
58 * included -- their results are ignored). The homogeneous planes ride
59 * the second grid dimension: each thread block offsets the input and
60 * output to the plane given by its second block index, all planes
61 * sharing the derivative factors. The launchers are
62 * constrained to the SumFac implementation tag, so the identically
63 * named SumFacTOP launchers in DivergenceBlockOpDeviceSumFacTOPKernels.hpp
64 * are the ones that resolve when that tag is used instead. The whole
65 * file is compiled only for the device back-ends (NEKTAR_ENABLE_DEVICE
66 * together with DEVICE_COMPILE_ONLY).
67 *
68 * @see DivergenceBlockOpDeviceSumFac.hpp for the host side configuring and
69 * launching these kernels.
70 */
71
72#pragma once
73
77
79
81{
82
83#if defined(NEKTAR_ENABLE_DEVICE) && defined(DEVICE_COMPILE_ONLY)
84/**
85 * @brief Dynamic shared memory, counted in TData entries, that one
86 * thread block of the 1D SumFac divergence kernel requires: none, since
87 * a segment has no collapsed-coordinate tables to stage.
88 *
89 * @tparam SHAPE_TYPE Element shape; unused.
90 * @tparam Implementation Constrained to MultiRegions::SumFac.
91 * @tparam TPhysSizeParameter1D 1D physical-space size parameter (see
92 * ElmtHelper.hpp).
93 *
94 * @param sizeParam1D Element sizes; unused.
95 * @param elmtsPerBlock Elements per thread block; unused.
96 * @param useSharedMemoryWsp Unused; SumFac reads the input in place.
97 *
98 * @return Zero.
99 */
100template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
101 typename TPhysSizeParameter1D,
102 std::enable_if_t<std::is_same_v<Implementation, SumFac> &&
103 IsPhysSizeParameter1D_v<TPhysSizeParameter1D>,
104 bool>
105 Enable = true>
106inline constexpr unsigned int DivergenceSharedMemorySize(
107 [[maybe_unused]] const TPhysSizeParameter1D sizeParam1D,
108 [[maybe_unused]] const unsigned int elmtsPerBlock,
109 [[maybe_unused]] const bool useSharedMemoryWsp)
110{
111 return 0;
112}
113
114/**
115 * @brief Dynamic shared memory, counted in TData entries, that one
116 * thread block of the 2D SumFac divergence kernel requires.
117 *
118 * Quadrilaterals need none. The shapes carrying a collapsed
119 * coordinate stage their two factor tables in shared memory before
120 * the element loop, one entry per quadrature point of the direction
121 * each table belongs to.
122 *
123 * @tparam SHAPE_TYPE Element shape.
124 * @tparam Implementation Constrained to MultiRegions::SumFac.
125 * @tparam TPhysSizeParameter2D 2D physical-space size parameter (see
126 * ElmtHelper.hpp).
127 *
128 * @param sizeParam2D Quadrature-point counts per direction.
129 * @param elmtsPerBlock Elements per thread block; unused.
130 * @param useSharedMemoryWsp Unused; SumFac reads the input in place.
131 *
132 * @return Entry count; the caller multiplies by sizeof(TData).
133 */
134template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
135 typename TPhysSizeParameter2D,
136 std::enable_if_t<std::is_same_v<Implementation, SumFac> &&
137 IsPhysSizeParameter2D_v<TPhysSizeParameter2D>,
138 bool>
139 Enable = true>
140inline constexpr unsigned int DivergenceSharedMemorySize(
141 [[maybe_unused]] const TPhysSizeParameter2D sizeParam2D,
142 [[maybe_unused]] const unsigned int elmtsPerBlock,
143 [[maybe_unused]] const bool useSharedMemoryWsp)
144{
145 if constexpr (SHAPE_TYPE == LibUtilities::Quad)
146 {
147 return 0;
148 }
149 else if constexpr (SHAPE_TYPE == LibUtilities::Tri ||
150 SHAPE_TYPE == LibUtilities::NodalTri)
151 {
152 const unsigned int nq0 = sizeParam2D.nq0();
153 const unsigned int nq1 = sizeParam2D.nq1();
154
155 return nq0 + nq1;
156 }
157}
158
159/**
160 * @brief Dynamic shared memory, counted in TData entries, that one
161 * thread block of the 3D SumFac divergence kernel requires.
162 *
163 * Hexahedra need none. The remaining shapes stage the factor tables
164 * their collapsed coordinates need: tetrahedra four tables (two of
165 * them on direction 1), prisms two and pyramids three, each holding
166 * one entry per quadrature point of its direction.
167 *
168 * @tparam SHAPE_TYPE Element shape.
169 * @tparam Implementation Constrained to MultiRegions::SumFac.
170 * @tparam TPhysSizeParameter3D 3D physical-space size parameter (see
171 * ElmtHelper.hpp).
172 *
173 * @param sizeParam3D Quadrature-point counts per direction.
174 * @param elmtsPerBlock Elements per thread block; unused.
175 * @param useSharedMemoryWsp Unused; SumFac reads the input in place.
176 *
177 * @return Entry count; the caller multiplies by sizeof(TData).
178 */
179template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
180 typename TPhysSizeParameter3D,
181 std::enable_if_t<std::is_same_v<Implementation, SumFac> &&
182 IsPhysSizeParameter3D_v<TPhysSizeParameter3D>,
183 bool>
184 Enable = true>
185inline constexpr unsigned int DivergenceSharedMemorySize(
186 const TPhysSizeParameter3D sizeParam3D,
187 [[maybe_unused]] const unsigned int elmtsPerBlock,
188 [[maybe_unused]] const bool useSharedMemoryWsp)
189{
190 const unsigned int nq0 = sizeParam3D.nq0();
191 const unsigned int nq1 = sizeParam3D.nq1();
192 const unsigned int nq2 = sizeParam3D.nq2();
193
194 if constexpr (SHAPE_TYPE == LibUtilities::Hex)
195 {
196 return 0;
197 }
198 else if constexpr (SHAPE_TYPE == LibUtilities::Tet ||
199 SHAPE_TYPE == LibUtilities::NodalTet)
200 {
201 return nq0 + 2u * nq1 + nq2;
202 }
203 else if constexpr (SHAPE_TYPE == LibUtilities::Prism ||
204 SHAPE_TYPE == LibUtilities::NodalPrism)
205 {
206 return nq0 + nq2;
207 }
208 else if constexpr (SHAPE_TYPE == LibUtilities::Pyr ||
209 SHAPE_TYPE == LibUtilities::NodalPyr)
210 {
211 return nq0 + nq1 + nq2;
212 }
213}
214
215/**
216 * @brief One element, two dimensions: the divergence
217 * \f$\partial u/\partial x + \partial v/\partial y\f$ of the two
218 * components at @p in and @p in + @p inoffset, written once per point.
219 *
220 * Per point each component is differentiated in both reference directions,
221 * the triangle correction is applied where the shape requires it, and its
222 * derivatives are contracted with its own row of the derivative factors; the
223 * sum is kept in a register and stored once.
224 *
225 * @tparam SHAPE_TYPE Quad, Tri or NodalTri.
226 * @tparam DEFORMED Derivative factors vary point by point.
227 * @tparam TData Floating-point type of the field data.
228 *
229 * @param ilane Lane index within the warp; selects the element.
230 * @param ncoord Cartesian components the factor array holds;
231 * sets the per-point factor stride
232 * `ndf = 2 * ncoord`.
233 * @param nq0,nq1 Quadrature points per direction.
234 * @param inoffset Stride from one input component to the next.
235 * @param D0,D1 1D differentiation matrices, column-major.
236 * @param f0,f1 Collapsed-coordinate factor tables (triangles only).
237 * @param df Derivative factors of this lane's warp.
238 * @param in First input component, warp interleaved.
239 * @param out Divergence, warp interleaved.
240 */
241template <LibUtilities::ShapeType SHAPE_TYPE, bool DEFORMED, typename TData>
242NEK_DEVICE_INLINE static void Divergence2DSumFacKernel(
243 const unsigned int ilane, const unsigned int ncoord, const unsigned int nq0,
244 const unsigned int nq1, const size_t inoffset, const TData *NEK_RESTRICT D0,
245 const TData *NEK_RESTRICT D1, [[maybe_unused]] const TData *NEK_RESTRICT f0,
246 [[maybe_unused]] const TData *NEK_RESTRICT f1, const TData *NEK_RESTRICT df,
247 const TData *NEK_RESTRICT in, TData *NEK_RESTRICT out)
248{
249 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
250
251 const unsigned int ndf = 2 * ncoord;
252
253 for (unsigned int j = 0u, cnt_ji = 0u; j < nq1; ++j)
254 {
255 for (unsigned int i = 0u; i < nq0; ++i, ++cnt_ji)
256 {
257 const unsigned int index = warpsize * cnt_ji + ilane;
258 const unsigned int dfindex =
259 DEFORMED ? ndf * warpsize * cnt_ji + ilane : ilane;
260
261 TData div = 0.0;
262#pragma unroll
263 for (unsigned int c = 0u; c < 2u; ++c)
264 {
265 const TData *inc = in + c * inoffset;
266
267 // Compute tensorial derivative.
268 // Direction 0
269 TData d0 = 0.0;
270#pragma unroll
271 for (unsigned int q = 0u; q < nq0; ++q)
272 {
273 d0 +=
274 D0[q * nq0 + i] * inc[warpsize * (nq0 * j + q) + ilane];
275 }
276
277 // Direction 1
278 TData d1 = 0.0;
279#pragma unroll
280 for (unsigned int q = 0u; q < nq1; ++q)
281 {
282 d1 +=
283 D1[q * nq1 + j] * inc[warpsize * (nq0 * q + i) + ilane];
284 }
285
286 // Moving from collapsed to standard coordinates.
287 if constexpr (SHAPE_TYPE == LibUtilities::Tri ||
288 SHAPE_TYPE == LibUtilities::NodalTri)
289 {
290 d0 *= f1[j];
291 d1 += d0 * f0[i];
292 }
293
294 // Multiply by derivative factors.
295 div += d0 * df[2u * c * warpsize + dfindex] +
296 d1 * df[(2u * c + 1u) * warpsize + dfindex];
297 }
298
299 out[index] = div;
300 }
301 }
302}
303
304/**
305 * @brief One element, three dimensions: the divergence
306 * \f$\partial u/\partial x + \partial v/\partial y
307 * + \partial w/\partial z\f$ of the three components at @p in,
308 * @p in + @p inoffset and @p in + 2 @p inoffset, written once per point.
309 *
310 * As Divergence2DSumFacKernel, with the collapsed-coordinate corrections of
311 * the three-dimensional shapes and nine derivative factors per point.
312 *
313 * @tparam SHAPE_TYPE Hex, Tet, NodalTet, Prism, NodalPrism, Pyr or NodalPyr.
314 * @tparam DEFORMED Derivative factors vary point by point.
315 * @tparam TData Floating-point type of the field data.
316 *
317 * @param ilane Lane index within the warp; selects the
318 * element.
319 * @param nq0,nq1,nq2 Quadrature points per direction.
320 * @param inoffset Stride from one input component to the next.
321 * @param D0,D1,D2 1D differentiation matrices, column-major.
322 * @param f0,f1,f1m,f2 Collapsed-coordinate factor tables, as in
323 * PhysDeriv3DSumFacKernel.
324 * @param df Derivative factors of this lane's warp.
325 * @param in First input component, warp interleaved.
326 * @param out Divergence, warp interleaved.
327 */
328template <LibUtilities::ShapeType SHAPE_TYPE, bool DEFORMED, typename TData>
329NEK_DEVICE_INLINE static void Divergence3DSumFacKernel(
330 const unsigned int ilane, const unsigned int nq0, const unsigned int nq1,
331 const unsigned int nq2, const size_t inoffset, const TData *NEK_RESTRICT D0,
332 const TData *NEK_RESTRICT D1, const TData *NEK_RESTRICT D2,
333 [[maybe_unused]] const TData *NEK_RESTRICT f0,
334 [[maybe_unused]] const TData *NEK_RESTRICT f1,
335 [[maybe_unused]] const TData *NEK_RESTRICT f1m,
336 [[maybe_unused]] const TData *NEK_RESTRICT f2, const TData *NEK_RESTRICT df,
337 const TData *NEK_RESTRICT in, TData *NEK_RESTRICT out)
338{
339 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
340
341 constexpr unsigned int ndf = 9u;
342
343 for (unsigned int k = 0u, cnt_kji = 0u; k < nq2; k++)
344 {
345 for (unsigned int j = 0u; j < nq1; j++)
346 {
347 for (unsigned int i = 0u; i < nq0; i++, cnt_kji++)
348 {
349 const unsigned int index = warpsize * cnt_kji + ilane;
350 const unsigned int dfindex =
351 DEFORMED ? ndf * warpsize * cnt_kji + ilane : ilane;
352
353 TData div = 0.0;
354#pragma unroll
355 for (unsigned int c = 0u; c < 3u; ++c)
356 {
357 const TData *inc = in + c * inoffset;
358
359 // Compute tensorial derivative.
360 // Direction 0
361 TData d0 = 0.0;
362#pragma unroll
363 for (unsigned int q = 0u; q < nq0; ++q)
364 {
365 d0 += D0[q * nq0 + i] *
366 inc[warpsize * (nq0 * nq1 * k + nq0 * j + q) +
367 ilane];
368 }
369
370 // Direction 1
371 TData d1 = 0.0;
372#pragma unroll
373 for (unsigned int q = 0u; q < nq1; ++q)
374 {
375 d1 += D1[q * nq1 + j] *
376 inc[warpsize * (nq0 * nq1 * k + nq0 * q + i) +
377 ilane];
378 }
379
380 // Direction 2
381 TData d2 = 0.0;
382#pragma unroll
383 for (unsigned int q = 0u; q < nq2; ++q)
384 {
385 d2 += D2[q * nq2 + k] *
386 inc[warpsize * (nq0 * nq1 * q + nq0 * j + i) +
387 ilane];
388 }
389
390 // Moving from collapsed to standard coordinates.
391 if constexpr (SHAPE_TYPE == LibUtilities::Tet ||
392 SHAPE_TYPE == LibUtilities::NodalTet)
393 {
394 TData tmp0 = f1m[j] * f2[k] * d0;
395 TData tmp1 = f0[i] * tmp0;
396 TData tmp2 = f2[k] * d1;
397 d0 = tmp0;
398 d1 = tmp1 + tmp2;
399 d2 += tmp1 + f1[j] * tmp2;
400 }
401 else if constexpr (SHAPE_TYPE == LibUtilities::Prism ||
402 SHAPE_TYPE == LibUtilities::NodalPrism)
403 {
404 d0 *= f2[k];
405 d2 += f0[i] * d0;
406 }
407 else if constexpr (SHAPE_TYPE == LibUtilities::Pyr ||
408 SHAPE_TYPE == LibUtilities::NodalPyr)
409 {
410 d0 *= f2[k];
411 d1 *= f2[k];
412 d2 += f0[i] * d0 + f1[j] * d1;
413 }
414
415 // Multiply by derivative factors.
416 div += d0 * df[(3u * c) * warpsize + dfindex] +
417 d1 * df[(3u * c + 1u) * warpsize + dfindex] +
418 d2 * df[(3u * c + 2u) * warpsize + dfindex];
419 }
420
421 out[index] = div;
422 }
423 }
424 }
425}
426
427/**
428 * @brief Kernel entry point of the 1D SumFac divergence: unpacks the
429 * size parameter, then differentiates one component of the block and
430 * scales it by the element's derivative factor for direction 0,
431 * overwriting the output.
432 *
433 * Resolves only when the Implementation tag is SumFac, so that the
434 * launch site in DivergenceBlockOpImpl::OperatorNDImpl picks the right
435 * kernel family without a further switch; a static_assert requires
436 * the size parameter to be one of the 1D physical-space forms --
437 * runtime or compile-time, see ElmtHelper.hpp. Launched through
438 * DEVICE_2DGRID_KERNEL_LAUNCHER, which appends the shared-memory
439 * pointer (unused here) and the back-end's thread-block handle.
440 *
441 * The thread takes element `e` = global thread index and grid-strides
442 * until the block's elements are exhausted; the warp's interleaved
443 * data is addressed through `iwarp` and lane `ilane`, and
444 * PhysDerivDir1DSumFacKernel differentiates the element's data.
445 *
446 * @tparam SHAPE_TYPE Element shape (segments only; unused).
447 * @tparam Implementation Implementation tag; SumFac here.
448 * @tparam DEFORMED Derivative factors vary per quadrature
449 * point rather than per element.
450 * @tparam TPhysSizeParameter1D 1D physical-space size parameter.
451 * @tparam TthreadBlock Back-end thread-block handle type.
452 *
453 * @param sizeParam1D Element sizes (coordinate dimension and point
454 * count).
455 * @param nelmt Elements of one plane of the block, padding
456 * included.
457 * @param elmtsPerBlock Elements per thread block, one per thread; unused.
458 * @param inoffset Component stride; unused in one dimension.
459 * @param useSharedMemoryWsp Unused; SumFac reads the input in place.
460 * @param D0 1D derivative matrix.
461 * @param df Derivative factors of the block,
462 * warp-interleaved.
463 * @param in Component data of the block.
464 * @param out Output data of the block.
465 * @param shmemptr Dynamic shared memory of the thread block;
466 * unused in one dimension.
467 * @param threadBlock Back-end thread-block handle.
468 */
469template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
470 bool DEFORMED, typename TPhysSizeParameter1D, typename TthreadBlock,
471 typename TData,
472 std::enable_if_t<std::is_same_v<Implementation, SumFac>, bool>
473 Enable = true>
475 const TPhysSizeParameter1D sizeParam1D, const size_t nelmt,
476 [[maybe_unused]] const unsigned int elmtsPerBlock,
477 [[maybe_unused]] const size_t inoffset,
478 [[maybe_unused]] const bool useSharedMemoryWsp,
479 const TData *NEK_RESTRICT D0, const TData *NEK_RESTRICT df,
480 const TData *NEK_RESTRICT in, TData *NEK_RESTRICT out,
481 [[maybe_unused]] unsigned char *shmemptr, const TthreadBlock &threadBlock)
482{
483 static_assert(
484 IsPhysSizeParameter1D_v<TPhysSizeParameter1D>,
485 "Template argument must be either of type "
486 "NonTemplatedPhysSizeParameter1D or TemplatedPhysSizeParameter1D.");
487
488 const unsigned int ncoord = sizeParam1D.ncoord();
489 const unsigned int nq0 = sizeParam1D.nq0();
490
491 const unsigned int ndf = ncoord;
492 const unsigned int dfsize = DEFORMED ? nq0 : 1u;
493
494 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
495
496 size_t e = getGlobalIdx<0>(threadBlock);
497 const unsigned int p = getBlockIdx<1>(threadBlock);
498 while (e < nelmt)
499 {
500 const size_t ilane = e % warpsize;
501 const size_t iwarp = e / warpsize;
502 const TData *dfptr = df + ndf * dfsize * warpsize * iwarp;
503 const TData *inptr = in + nq0 * (nelmt * p + warpsize * iwarp);
504 TData *outptr = out + nq0 * (nelmt * p + warpsize * iwarp);
505 PhysDerivDir1DSumFacKernel<false, DEFORMED, 0>(ilane, ncoord, nq0, D0,
506 dfptr, inptr, outptr);
507
508 e += getGlobalRange<0>(threadBlock);
509 }
510}
511
512/**
513 * @brief Kernel entry point of the 2D SumFac divergence: binds the
514 * dynamic shared memory and accumulates
515 * \f$\partial u/\partial x + \partial v/\partial y\f$, one element
516 * per thread.
517 *
518 * Constrained as the 1D launcher, on the SumFac tag alone; a
519 * static_assert requires one of the 2D physical-space size
520 * parameters. FETCH_SHARED_MEMORY points @p shmemptr at the thread
521 * block's dynamic shared memory on the back-ends that provide it
522 * that way; the launch site sizes that allocation with
523 * DivergenceSharedMemorySize.
524 *
525 * For the shapes with a collapsed coordinate the two factor tables
526 * are first copied into that shared memory, the quadrilateral
527 * instantiation leaving the pointers null since its kernels ignore
528 * them. Each thread then takes element `e` = global thread index and
529 * grid-strides over the block: with the derivative factors, input and
530 * output advanced to the thread's warp, Divergence2DSumFacKernel writes
531 * \f$\partial u/\partial x + \partial v/\partial y\f$, the second
532 * component at @p inoffset, to the output once per point.
533 *
534 * @tparam SHAPE_TYPE Element shape.
535 * @tparam Implementation Implementation tag; SumFac here.
536 * @tparam DEFORMED Derivative factors vary per quadrature
537 * point rather than per element.
538 * @tparam TPhysSizeParameter2D 2D physical-space size parameter.
539 * @tparam TthreadBlock Back-end thread-block handle type.
540 *
541 * @param sizeParam2D Element sizes (coordinate dimension and point
542 * counts).
543 * @param nelmt Elements of one plane of the block, padding
544 * included.
545 * @param elmtsPerBlock Elements per thread block, one per thread; unused.
546 * @param inoffset Stride from one input component to the next,
547 * a component spanning all the planes.
548 * @param useSharedMemoryWsp Unused; SumFac reads the input in place.
549 * @param D0 1D derivative matrix of direction 0.
550 * @param D1 1D derivative matrix of direction 1.
551 * @param f0 Collapsed-coordinate factor table of
552 * direction 0; unused for quadrilaterals.
553 * @param f1 Collapsed-coordinate factor table of
554 * direction 1; unused for quadrilaterals.
555 * @param df Derivative factors of the block,
556 * warp-interleaved.
557 * @param in First component of the block's vector field.
558 * @param out Output data of the block.
559 * @param shmemptr Dynamic shared memory of the thread block,
560 * sized by DivergenceSharedMemorySize.
561 * @param threadBlock Back-end thread-block handle.
562 */
563template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
564 bool DEFORMED, typename TPhysSizeParameter2D, typename TthreadBlock,
565 typename TData,
566 std::enable_if_t<std::is_same_v<Implementation, SumFac>, bool>
567 Enable = true>
569 const TPhysSizeParameter2D sizeParam2D, const size_t nelmt,
570 [[maybe_unused]] const unsigned int elmtsPerBlock, const size_t inoffset,
571 [[maybe_unused]] const bool useSharedMemoryWsp,
572 const TData *NEK_RESTRICT D0, const TData *NEK_RESTRICT D1,
573 const TData *NEK_RESTRICT f0, const TData *NEK_RESTRICT f1,
574 const TData *NEK_RESTRICT df, const TData *NEK_RESTRICT in,
575 TData *NEK_RESTRICT out, unsigned char *shmemptr,
576 const TthreadBlock &threadBlock)
577{
578 static_assert(
579 IsPhysSizeParameter2D_v<TPhysSizeParameter2D>,
580 "Template argument must be either of type "
581 "NonTemplatedPhysSizeParameter2D or TemplatedPhysSizeParameter2D.");
582
584
585 const unsigned int ncoord = sizeParam2D.ncoord();
586 const unsigned int nq0 = sizeParam2D.nq0();
587 const unsigned int nq1 = sizeParam2D.nq1();
588 const unsigned int nqTot = sizeParam2D.nqTot();
589
590 const unsigned int ndf = 2 * ncoord;
591 const unsigned int dfsize = DEFORMED ? nqTot : 1u;
592
593 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
594
595 TData *s_f0 = nullptr;
596 TData *s_f1 = nullptr;
597
598 // Precompute geometric factors.
599
600 if constexpr (SHAPE_TYPE == LibUtilities::Tri ||
601 SHAPE_TYPE == LibUtilities::NodalTri)
602 {
603 s_f0 = (TData *)shmemptr;
604 s_f1 = s_f0 + nq0;
605
606 // Copy to shared memory.
607 LoadToSharedMemory(nq0, f0, s_f0, threadBlock);
608 LoadToSharedMemory(nq1, f1, s_f1, threadBlock);
609
610 localBarrier(threadBlock);
611 }
612
613 size_t e = getGlobalIdx<0>(threadBlock);
614 const unsigned int p = getBlockIdx<1>(threadBlock);
615 while (e < nelmt)
616 {
617 const size_t ilane = e % warpsize;
618 const size_t iwarp = e / warpsize;
619 const TData *dfptr = df + ndf * dfsize * warpsize * iwarp;
620 const TData *inptr = in + nqTot * (nelmt * p + warpsize * iwarp);
621 TData *outptr = out + nqTot * (nelmt * p + warpsize * iwarp);
622
623 Divergence2DSumFacKernel<SHAPE_TYPE, DEFORMED>(
624 ilane, ncoord, nq0, nq1, inoffset, D0, D1, s_f0, s_f1, dfptr, inptr,
625 outptr);
626
627 e += getGlobalRange<0>(threadBlock);
628 }
629}
630
631/**
632 * @brief Kernel entry point of the 3D SumFac divergence: binds the
633 * dynamic shared memory and accumulates
634 * \f$\partial u/\partial x + \partial v/\partial y
635 * + \partial w/\partial z\f$, one element per thread.
636 *
637 * Constrained as the other launchers, on the SumFac tag alone; a
638 * static_assert requires one of the 3D physical-space size
639 * parameters.
640 *
641 * The factor tables the shape's collapsed coordinates need are staged
642 * in that shared memory (none for hexahedra), then each thread
643 * grid-strides over the elements and calls Divergence3DSumFacKernel,
644 * which reads the components at @p in, @p inoffset and twice
645 * @p inoffset and writes the output once per point. The element is assumed to
646 * have coordinate dimension three, so it carries nine derivative factors.
647 *
648 * @tparam SHAPE_TYPE Element shape.
649 * @tparam Implementation Implementation tag; SumFac here.
650 * @tparam DEFORMED Derivative factors vary per quadrature
651 * point rather than per element.
652 * @tparam TPhysSizeParameter3D 3D physical-space size parameter.
653 * @tparam TthreadBlock Back-end thread-block handle type.
654 *
655 * @param sizeParam3D Element sizes (point counts per direction).
656 * @param nelmt Elements of one plane of the block, padding
657 * included.
658 * @param elmtsPerBlock Elements per thread block, one per thread; unused.
659 * @param inoffset Stride from one input component to the next,
660 * a component spanning all the planes.
661 * @param useSharedMemoryWsp Unused; SumFac reads the input in place.
662 * @param D0 1D derivative matrix of direction 0.
663 * @param D1 1D derivative matrix of direction 1.
664 * @param D2 1D derivative matrix of direction 2.
665 * @param f0 \f$\tfrac{1}{2}(1+\eta_0)\f$ at the direction-0
666 * points.
667 * @param f1 \f$\tfrac{1}{2}(1+\eta_1)\f$ at the direction-1
668 * points.
669 * @param f1m \f$2/(1-\eta_1)\f$ at the direction-1 points.
670 * @param f2 \f$2/(1-\eta_2)\f$ at the direction-2 points.
671 * All four are unused for hexahedra.
672 * @param df Derivative factors of the block,
673 * warp-interleaved.
674 * @param in First component of the block's vector field.
675 * @param out Output data of the block.
676 * @param shmemptr Dynamic shared memory of the thread block,
677 * sized by DivergenceSharedMemorySize.
678 * @param threadBlock Back-end thread-block handle.
679 */
680template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
681 bool DEFORMED, typename TPhysSizeParameter3D, typename TthreadBlock,
682 typename TData,
683 std::enable_if_t<std::is_same_v<Implementation, SumFac>, bool>
684 Enable = true>
686 const TPhysSizeParameter3D sizeParam3D, const size_t nelmt,
687 [[maybe_unused]] const unsigned int elmtsPerBlock, const size_t inoffset,
688 [[maybe_unused]] const bool useSharedMemoryWsp,
689 const TData *NEK_RESTRICT D0, const TData *NEK_RESTRICT D1,
690 const TData *NEK_RESTRICT D2, const TData *NEK_RESTRICT f0,
691 const TData *NEK_RESTRICT f1, const TData *NEK_RESTRICT f1m,
692 const TData *NEK_RESTRICT f2, const TData *NEK_RESTRICT df,
693 const TData *NEK_RESTRICT in, TData *NEK_RESTRICT out,
694 unsigned char *shmemptr, const TthreadBlock &threadBlock)
695{
696 static_assert(
697 IsPhysSizeParameter3D_v<TPhysSizeParameter3D>,
698 "Template argument must be either of type "
699 "NonTemplatedPhysSizeParameter3D or TemplatedPhysSizeParameter3D.");
700
702
703 const unsigned int nq0 = sizeParam3D.nq0();
704 const unsigned int nq1 = sizeParam3D.nq1();
705 const unsigned int nq2 = sizeParam3D.nq2();
706 const unsigned int nqTot = sizeParam3D.nqTot();
707
708 constexpr unsigned int ndf = 9u;
709 const unsigned int dfsize = DEFORMED ? nqTot : 1u;
710
711 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
712
713 TData *s_f0 = nullptr;
714 TData *s_f1 = nullptr;
715 TData *s_f1m = nullptr;
716 TData *s_f2 = nullptr;
717
718 // Precompute geometric factors.
719
720 if constexpr (SHAPE_TYPE == LibUtilities::Tet ||
721 SHAPE_TYPE == LibUtilities::NodalTet)
722 {
723 s_f0 = (TData *)shmemptr;
724 s_f1 = s_f0 + nq0;
725 s_f1m = s_f1 + nq1;
726 s_f2 = s_f1m + nq1;
727
728 // Copy to shared memory.
729 LoadToSharedMemory(nq0, f0, s_f0, threadBlock);
730 LoadToSharedMemory(nq1, f1, s_f1, threadBlock);
731 LoadToSharedMemory(nq1, f1m, s_f1m, threadBlock);
732 LoadToSharedMemory(nq2, f2, s_f2, threadBlock);
733
734 localBarrier(threadBlock);
735 }
736 else if constexpr (SHAPE_TYPE == LibUtilities::Prism ||
737 SHAPE_TYPE == LibUtilities::NodalPrism)
738 {
739 s_f0 = (TData *)shmemptr;
740 s_f2 = s_f0 + nq0;
741
742 // Copy to shared memory.
743 LoadToSharedMemory(nq0, f0, s_f0, threadBlock);
744 LoadToSharedMemory(nq2, f2, s_f2, threadBlock);
745
746 localBarrier(threadBlock);
747 }
748 else if constexpr (SHAPE_TYPE == LibUtilities::Pyr ||
749 SHAPE_TYPE == LibUtilities::NodalPyr)
750 {
751 s_f0 = (TData *)shmemptr;
752 s_f1 = s_f0 + nq0;
753 s_f2 = s_f1 + nq1;
754
755 // Copy to shared memory.
756 LoadToSharedMemory(nq0, f0, s_f0, threadBlock);
757 LoadToSharedMemory(nq1, f1, s_f1, threadBlock);
758 LoadToSharedMemory(nq2, f2, s_f2, threadBlock);
759
760 localBarrier(threadBlock);
761 }
762
763 size_t e = getGlobalIdx<0>(threadBlock); // use size_t to prevent overflow
764 const unsigned int p = getBlockIdx<1>(threadBlock);
765 while (e < nelmt)
766 {
767 const size_t ilane = e % warpsize;
768 const size_t iwarp = e / warpsize;
769 const TData *dfptr = df + ndf * dfsize * warpsize * iwarp;
770 const TData *inptr = in + nqTot * (nelmt * p + warpsize * iwarp);
771 TData *outptr = out + nqTot * (nelmt * p + warpsize * iwarp);
772 Divergence3DSumFacKernel<SHAPE_TYPE, DEFORMED>(
773 ilane, nq0, nq1, nq2, inoffset, D0, D1, D2, s_f0, s_f1, s_f1m, s_f2,
774 dfptr, inptr, outptr);
775
776 e += getGlobalRange<0>(threadBlock);
777 }
778}
779
780#endif
781
782} // namespace Nektar::MultiRegions::detail
#define NEK_RESTRICT
Definition Backends.hpp:254
#define NEK_DEVICE_INLINE
Definition Backends.hpp:283
#define NEK_DEVICE_KERNEL
Definition Backends.hpp:297
Device SumFac kernels of the physical derivative: one element per warp lane.
void div(const TData alpha, LibUtilities::MemoryRegion< TData > &x, LibUtilities::MemoryRegion< TData > &y)
Definition Math.hpp:988
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 DivergenceKernelLauncher(const TPhysSizeParameter1D sizeParam1D, const typename simd_type::scalarType *D0, const simd_type *df_ptr, simd_type *wsp0, const simd_type *in0, simd_type *out)
Differentiate one element group of a 1D block: out = du/dx, with u the single component at in0.
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 useSharedMemoryWsp
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
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 * shmemptr
NEK_DEVICE_KERNEL void const TData *NEK_RESTRICT const TData *NEK_RESTRICT const size_t const unsigned int elmtsPerBlock
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)
static constexpr unsigned int warpSize
Definition Backends.hpp:143