Nektar++
Loading...
Searching...
No Matches
AdvectionBlockOpDeviceSumFacKernels.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: AdvectionBlockOpDeviceSumFacKernels.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: SumFac device kernels of the advection operator, one element
32// per warp lane.
33//
34///////////////////////////////////////////////////////////////////////////////
35
36/**
37 * @file AdvectionBlockOpDeviceSumFacKernels.hpp
38 * @brief SumFac device kernels of the advection operator: one element per
39 * thread, with the elements of a warp interleaved lane by lane.
40 *
41 * @details
42 * Lane ilane of warp iwarp owns one element, and the block storage is
43 * interleaved at the warp size so the lanes of a warp read consecutive
44 * addresses. Each kernel contracts the one-dimensional derivative matrices
45 * itself and then applies, in the same pass, the collapsed-to-standard
46 * coordinate correction for simplex-type shapes, the geometric derivative
47 * factors and the dot product with the advection velocity, writing (or
48 * accumulating) scale * (V . grad u) per quadrature point.
49 *
50 * The file provides, for each dimensionality: the per-element kernel
51 * (Advection*DSumFacKernel) and the device entry point the block operator
52 * launches (the AdvectionKernelLauncher overload for that dimension),
53 * which unpacks the size parameter, stages any collapsed-coordinate tables
54 * in shared memory, walks the block's elements with a grid stride -- one
55 * grid row per component or homogeneous mode -- and calls the per-element
56 * kernel. AdvectionSharedMemorySize reports how much dynamic shared memory
57 * a launch must request. The launchers are constrained to the SumFac
58 * implementation tag, so the identically named SumFacTOP launchers in
59 * AdvectionBlockOpDeviceSumFacTOPKernels.hpp are the ones that resolve when
60 * that tag is used instead.
61 *
62 * Everything in the namespace is guarded by NEKTAR_ENABLE_DEVICE and
63 * DEVICE_COMPILE_ONLY, so it exists only in translation units compiled for
64 * a device back-end.
65 */
66
67#pragma once
68
73
75
77{
78
79#if defined(NEKTAR_ENABLE_DEVICE) && defined(DEVICE_COMPILE_ONLY)
80/**
81 * @brief Dynamic shared memory a SumFac launch needs per thread block, in
82 * elements of TData, for a one-dimensional shape: none, since a segment
83 * has no collapsed-coordinate tables to stage.
84 *
85 * @tparam SHAPE_TYPE Element shape; unused.
86 * @tparam Implementation Implementation tag; this overload is
87 * constrained to SumFac.
88 * @tparam TPhysSizeParameter1D 1D physical-space size parameter (see
89 * ElmtHelper.hpp).
90 *
91 * @param sizeParam1D Element sizes; unused.
92 * @param elmtsPerBlock Elements per thread block; unused.
93 * @param useSharedMemoryWsp Unused; SumFac reads the input in place.
94 *
95 * @return Zero.
96 */
97template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
98 typename TPhysSizeParameter1D,
99 std::enable_if_t<std::is_same_v<Implementation, SumFac> &&
100 IsPhysSizeParameter1D_v<TPhysSizeParameter1D>,
101 bool>
102 Enable = true>
103inline constexpr unsigned int AdvectionSharedMemorySize(
104 [[maybe_unused]] const TPhysSizeParameter1D sizeParam1D,
105 [[maybe_unused]] const unsigned int elmtsPerBlock,
106 [[maybe_unused]] const bool useSharedMemoryWsp)
107{
108 return 0;
109}
110
111/**
112 * @brief Dynamic shared memory a SumFac launch needs per thread block, in
113 * elements of TData, for a two-dimensional shape.
114 *
115 * The only shared data are the collapsed-coordinate factor tables the
116 * launcher stages before the element loop, so quadrilaterals need none and
117 * triangles need one entry per quadrature point of each direction.
118 *
119 * @tparam SHAPE_TYPE Element shape.
120 * @tparam Implementation Implementation tag; this overload is
121 * constrained to SumFac.
122 * @tparam TPhysSizeParameter2D 2D physical-space size parameter (see
123 * ElmtHelper.hpp).
124 *
125 * @param sizeParam2D Quadrature-point counts per direction.
126 * @param elmtsPerBlock Elements per thread block; unused.
127 * @param useSharedMemoryWsp Unused; SumFac reads the input in place.
128 *
129 * @return Shared-memory requirement in elements of TData.
130 */
131template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
132 typename TPhysSizeParameter2D,
133 std::enable_if_t<std::is_same_v<Implementation, SumFac> &&
134 IsPhysSizeParameter2D_v<TPhysSizeParameter2D>,
135 bool>
136 Enable = true>
137inline constexpr unsigned int AdvectionSharedMemorySize(
138 [[maybe_unused]] const TPhysSizeParameter2D sizeParam2D,
139 [[maybe_unused]] const unsigned int elmtsPerBlock,
140 [[maybe_unused]] const bool useSharedMemoryWsp)
141{
142 if constexpr (SHAPE_TYPE == LibUtilities::Quad)
143 {
144 return 0;
145 }
146 else if constexpr (SHAPE_TYPE == LibUtilities::Tri ||
147 SHAPE_TYPE == LibUtilities::NodalTri)
148 {
149 const unsigned int nq0 = sizeParam2D.nq0();
150 const unsigned int nq1 = sizeParam2D.nq1();
151
152 return nq0 + nq1;
153 }
154}
155
156/**
157 * @brief Dynamic shared memory a SumFac launch needs per thread block, in
158 * elements of TData, for a three-dimensional shape.
159 *
160 * As in two dimensions the shared data are the collapsed-coordinate factor
161 * tables: none for hexahedra, one table per direction for pyramids, two
162 * (directions 0 and 2) for prisms, and four for tetrahedra, which stage
163 * two tables over direction 1.
164 *
165 * @tparam SHAPE_TYPE Element shape.
166 * @tparam Implementation Implementation tag; this overload is
167 * constrained to SumFac.
168 * @tparam TPhysSizeParameter3D 3D physical-space size parameter (see
169 * ElmtHelper.hpp).
170 *
171 * @param sizeParam3D Quadrature-point counts per direction.
172 * @param elmtsPerBlock Elements per thread block; unused.
173 * @param useSharedMemoryWsp Unused; SumFac reads the input in place.
174 *
175 * @return Shared-memory requirement in elements of TData.
176 */
177template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
178 typename TPhysSizeParameter3D,
179 std::enable_if_t<std::is_same_v<Implementation, SumFac> &&
180 IsPhysSizeParameter3D_v<TPhysSizeParameter3D>,
181 bool>
182 Enable = true>
183inline constexpr unsigned int AdvectionSharedMemorySize(
184 const TPhysSizeParameter3D sizeParam3D,
185 [[maybe_unused]] const unsigned int elmtsPerBlock,
186 [[maybe_unused]] const bool useSharedMemoryWsp)
187{
188 const unsigned int nq0 = sizeParam3D.nq0();
189 const unsigned int nq1 = sizeParam3D.nq1();
190 const unsigned int nq2 = sizeParam3D.nq2();
191
192 if constexpr (SHAPE_TYPE == LibUtilities::Hex)
193 {
194 return 0;
195 }
196 else if constexpr (SHAPE_TYPE == LibUtilities::Tet ||
197 SHAPE_TYPE == LibUtilities::NodalTet)
198 {
199 return nq0 + 2u * nq1 + nq2;
200 }
201 else if constexpr (SHAPE_TYPE == LibUtilities::Prism ||
202 SHAPE_TYPE == LibUtilities::NodalPrism)
203 {
204 return nq0 + nq2;
205 }
206 else if constexpr (SHAPE_TYPE == LibUtilities::Pyr ||
207 SHAPE_TYPE == LibUtilities::NodalPyr)
208 {
209 return nq0 + nq1 + nq2;
210 }
211}
212
213/**
214 * @brief One element of a 1D block, held by one warp lane:
215 * out = scale * du/dxi * sum_d df(d) V_d at every quadrature point.
216 *
217 * The derivative along the single reference direction is formed by
218 * contracting the 1D derivative matrix over the element's values, then
219 * multiplied by the derivative factor and velocity component of each of
220 * the up to three coordinate directions of a possibly embedded segment.
221 *
222 * All arrays are addressed lane by lane: entry i of this element sits at
223 * warpsize * i + ilane of a warp's slice. The derivative factors follow
224 * suit, with the ncoord factors of a point adjacent when the geometry is
225 * deformed and a single set per warp otherwise.
226 *
227 * @tparam APPEND Accumulate onto @p out instead of overwriting.
228 * @tparam DEFORMED Read per-point rather than per-element derivative
229 * factors.
230 *
231 * @param ilane Lane index within the warp, i.e. which element of
232 * the warp's slice this thread owns.
233 * @param ncoord Coordinate dimension, i.e. number of derivative
234 * factors and velocity components.
235 * @param nq0 Quadrature points per element.
236 * @param D0 1D derivative matrix of the direction.
237 * @param df Derivative factors of this warp's elements.
238 * @param advVel Velocity data of this warp's elements; component
239 * d starts at d * advVelOffset.
240 * @param advVelOffset Stride between velocity components.
241 * @param in Input values of this warp's elements.
242 * @param out Output values of this warp's elements.
243 * @param scale Scalar factor applied to the result.
244 */
245template <bool APPEND, bool DEFORMED, typename TData>
246NEK_DEVICE_INLINE static void Advection1DSumFacKernel(
247 const unsigned int ilane, const unsigned int ncoord, const unsigned int nq0,
248 const TData *NEK_RESTRICT D0, const TData *NEK_RESTRICT df,
249 const TData *NEK_RESTRICT advVel, const size_t advVelOffset,
250 const TData *NEK_RESTRICT in, TData *NEK_RESTRICT out, const TData scale)
251{
252 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
253
254 for (unsigned int i = 0u; i < nq0; ++i)
255 {
256 const unsigned int index = warpsize * i + ilane;
257 const unsigned int dfindex =
258 DEFORMED ? ncoord * warpsize * i + ilane : ilane;
259
260 // Compute tensorial derivative.
261 TData d0 = 0.0;
262#pragma unroll
263 for (unsigned int q = 0u; q < nq0; ++q)
264 {
265 d0 += D0[q * nq0 + i] * in[warpsize * q + ilane];
266 }
267
268 // Multiply by derivative factors.
269 TData tmp = 0.0;
270 for (unsigned int d = 0u; d < ncoord; d++)
271 {
272 tmp += d0 * df[d * warpsize + dfindex] *
273 advVel[d * advVelOffset + index];
274 }
275
276 if constexpr (APPEND)
277 {
278 out[index] += scale * tmp;
279 }
280 else
281 {
282 out[index] = scale * tmp;
283 }
284 }
285}
286
287/**
288 * @brief One element of a 2D block, held by one warp lane:
289 * out = scale * sum_k V_k sum_d df(k,d) du/dxi_d at every quadrature
290 * point.
291 *
292 * The two tensor-direction derivatives are formed by contracting D0 over
293 * direction 0 and D1 over direction 1. For triangles the tensor directions
294 * are the collapsed coordinates, so d0 is first multiplied by
295 * f1[j] = 2/(1 - eta_1) and d1 augmented by d0 * f0[i] with
296 * f0[i] = (1 + eta_0)/2, recovering the standard-coordinate derivatives
297 * before the 2 * ncoord derivative factors are applied. With
298 * @p ncoord == 3 the element is embedded in three-dimensional space and a
299 * third factor/velocity pair contributes.
300 *
301 * @tparam SHAPE_TYPE Element shape; selects the collapsed-coordinate
302 * correction.
303 * @tparam APPEND Accumulate onto @p out instead of overwriting.
304 * @tparam DEFORMED Read per-point rather than per-element derivative
305 * factors.
306 *
307 * @param ilane Lane index within the warp.
308 * @param ncoord Coordinate dimension, i.e. number of
309 * gradient/velocity components (2 or 3).
310 * @param nq0 Quadrature points in direction 0.
311 * @param nq1 Quadrature points in direction 1.
312 * @param D0 1D derivative matrix of direction 0.
313 * @param D1 1D derivative matrix of direction 1.
314 * @param f0 Collapsed-coordinate factor (1 + eta_0)/2 at the
315 * direction-0 points (triangles only).
316 * @param f1 Collapsed-coordinate factor 2/(1 - eta_1) at the
317 * direction-1 points (triangles only).
318 * @param df Derivative factors of this warp's elements,
319 * 2 * ncoord per element or per point.
320 * @param advVel Velocity data of this warp's elements; component
321 * k starts at k * advVelOffset.
322 * @param advVelOffset Stride between velocity components.
323 * @param in Input values of this warp's elements.
324 * @param out Output values of this warp's elements.
325 * @param scale Scalar factor applied to the result.
326 */
327template <LibUtilities::ShapeType SHAPE_TYPE, bool APPEND, bool DEFORMED,
328 typename TData>
329NEK_DEVICE_INLINE static void Advection2DSumFacKernel(
330 const unsigned int ilane, const unsigned int ncoord, const unsigned int nq0,
331 const unsigned int nq1, const TData *NEK_RESTRICT D0,
332 const TData *NEK_RESTRICT D1, [[maybe_unused]] const TData *NEK_RESTRICT f0,
333 [[maybe_unused]] const TData *NEK_RESTRICT f1, const TData *NEK_RESTRICT df,
334 const TData *NEK_RESTRICT advVel, const size_t advVelOffset,
335 const TData *NEK_RESTRICT in, TData *NEK_RESTRICT out, const TData scale)
336{
337 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
338
339 const unsigned int ndf = 2 * ncoord;
340
341 for (unsigned int j = 0u, cnt_ji = 0u; j < nq1; ++j)
342 {
343 for (unsigned int i = 0u; i < nq0; ++i, ++cnt_ji)
344 {
345 const unsigned int index = warpsize * cnt_ji + ilane;
346 const unsigned int dfindex =
347 DEFORMED ? ndf * warpsize * cnt_ji + ilane : ilane;
348
349 // Get advection velocity.
350 TData vx, vy;
351 vx = advVel[index];
352 vy = advVel[index + 1u * advVelOffset];
353
354 // Compute tensorial derivative.
355 // Direction 0
356 TData d0 = 0.0;
357#pragma unroll
358 for (unsigned int q = 0u; q < nq0; ++q)
359 {
360 d0 += D0[q * nq0 + i] * in[warpsize * (nq0 * j + q) + ilane];
361 }
362
363 // Direction 1
364 TData d1 = 0.0;
365#pragma unroll
366 for (unsigned int q = 0u; q < nq1; ++q)
367 {
368 d1 += D1[q * nq1 + j] * in[warpsize * (nq0 * q + i) + ilane];
369 }
370
371 // Moving from collapsed to standard coordinates.
372 if constexpr (SHAPE_TYPE == LibUtilities::Tri ||
373 SHAPE_TYPE == LibUtilities::NodalTri)
374 {
375 d0 *= f1[j];
376 d1 += d0 * f0[i];
377 }
378
379 // Multiply by derivative factors and Advection Vel.
380 TData tmp;
381 tmp = vx * (d0 * df[0u * warpsize + dfindex] +
382 d1 * df[1u * warpsize + dfindex]);
383 tmp += vy * (d0 * df[2u * warpsize + dfindex] +
384 d1 * df[3u * warpsize + dfindex]);
385
386 if (ncoord == 3u)
387 {
388 // Multiply by derivative factors and Advection Vel.
389 TData vz = advVel[index + 2u * advVelOffset];
390 tmp += vz * (d0 * df[4u * warpsize + dfindex] +
391 d1 * df[5u * warpsize + dfindex]);
392 }
393
394 if constexpr (APPEND)
395 {
396 out[index] += scale * tmp;
397 }
398 else
399 {
400 out[index] = scale * tmp;
401 }
402 }
403 }
404}
405
406/**
407 * @brief One element of a 3D block, held by one warp lane:
408 * out = scale * sum_k V_k sum_d df(k,d) du/dxi_d at every quadrature
409 * point.
410 *
411 * The three tensor-direction derivatives are formed by contracting D0, D1
412 * and D2 over their directions and then, for the shapes with collapsed
413 * coordinates, chain-ruled back to standard coordinates with the
414 * tabulated factors: hexahedra need no correction, prisms and pyramids
415 * fold the corrected direction-0 (and, for pyramids, direction-1)
416 * derivative into direction 2, and tetrahedra combine all four tables. All
417 * nine derivative factors are then applied and contracted with the
418 * velocity.
419 *
420 * @tparam SHAPE_TYPE Element shape; selects the collapsed-coordinate
421 * correction.
422 * @tparam APPEND Accumulate onto @p out instead of overwriting.
423 * @tparam DEFORMED Read per-point rather than per-element derivative
424 * factors.
425 *
426 * @param ilane Lane index within the warp.
427 * @param nq0 Quadrature points in direction 0.
428 * @param nq1 Quadrature points in direction 1.
429 * @param nq2 Quadrature points in direction 2.
430 * @param D0 1D derivative matrix of direction 0.
431 * @param D1 1D derivative matrix of direction 1.
432 * @param D2 1D derivative matrix of direction 2.
433 * @param f0 (1 + eta_0)/2 at the direction-0 points.
434 * @param f1 (1 + eta_1)/2 at the direction-1 points.
435 * @param f1m 2/(1 - eta_1) at the direction-1 points.
436 * @param f2 2/(1 - eta_2) at the direction-2 points.
437 * @param df Derivative factors of this warp's elements, nine
438 * per element or per point.
439 * @param advVel Velocity data of this warp's elements; component
440 * k starts at k * advVelOffset.
441 * @param advVelOffset Stride between velocity components.
442 * @param in Input values of this warp's elements.
443 * @param out Output values of this warp's elements.
444 * @param scale Scalar factor applied to the result.
445 */
446template <LibUtilities::ShapeType SHAPE_TYPE, bool APPEND, bool DEFORMED,
447 typename TData>
448NEK_DEVICE_INLINE static void Advection3DSumFacKernel(
449 const unsigned int ilane, const unsigned int nq0, const unsigned int nq1,
450 const unsigned int nq2, const TData *NEK_RESTRICT D0,
451 const TData *NEK_RESTRICT D1, const TData *NEK_RESTRICT D2,
452 [[maybe_unused]] const TData *NEK_RESTRICT f0,
453 [[maybe_unused]] const TData *NEK_RESTRICT f1,
454 [[maybe_unused]] const TData *NEK_RESTRICT f1m,
455 [[maybe_unused]] const TData *NEK_RESTRICT f2, const TData *NEK_RESTRICT df,
456 const TData *NEK_RESTRICT advVel, const size_t advVelOffset,
457 const TData *NEK_RESTRICT in, TData *NEK_RESTRICT out, const TData scale)
458{
459 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
460
461 constexpr unsigned int ndf = 9u;
462
463 for (unsigned int k = 0u, cnt_kji = 0u; k < nq2; k++)
464 {
465 for (unsigned int j = 0u; j < nq1; j++)
466 {
467 for (unsigned int i = 0u; i < nq0; i++, cnt_kji++)
468 {
469 const unsigned int index = warpsize * cnt_kji + ilane;
470 const unsigned int dfindex =
471 DEFORMED ? ndf * warpsize * cnt_kji + ilane : ilane;
472
473 // Get advection velocity.
474 TData vx, vy, vz;
475 vx = advVel[index];
476 vy = advVel[index + 1u * advVelOffset];
477 vz = advVel[index + 2u * advVelOffset];
478
479 // Compute tensorial derivative.
480 // Direction 0
481 TData d0 = 0.0;
482#pragma unroll
483 for (unsigned int q = 0u; q < nq0; ++q)
484 {
485 d0 += D0[q * nq0 + i] *
486 in[warpsize * (nq0 * nq1 * k + nq0 * j + q) + ilane];
487 }
488
489 // Direction 1
490 TData d1 = 0.0;
491#pragma unroll
492 for (unsigned int q = 0u; q < nq1; ++q)
493 {
494 d1 += D1[q * nq1 + j] *
495 in[warpsize * (nq0 * nq1 * k + nq0 * q + i) + ilane];
496 }
497
498 // Direction 2
499 TData d2 = 0.0;
500#pragma unroll
501 for (unsigned int q = 0u; q < nq2; ++q)
502 {
503 d2 += D2[q * nq2 + k] *
504 in[warpsize * (nq0 * nq1 * q + nq0 * j + i) + ilane];
505 }
506
507 // Moving from collapsed to standard coordinates.
508 if constexpr (SHAPE_TYPE == LibUtilities::Tet ||
509 SHAPE_TYPE == LibUtilities::NodalTet)
510 {
511 TData tmp0 = f1m[j] * f2[k] * d0;
512 TData tmp1 = f0[i] * tmp0;
513 TData tmp2 = f2[k] * d1;
514 d0 = tmp0;
515 d1 = tmp1 + tmp2;
516 d2 += tmp1 + f1[j] * tmp2;
517 }
518 else if constexpr (SHAPE_TYPE == LibUtilities::Prism ||
519 SHAPE_TYPE == LibUtilities::NodalPrism)
520 {
521 d0 *= f2[k];
522 d2 += f0[i] * d0;
523 }
524 else if constexpr (SHAPE_TYPE == LibUtilities::Pyr ||
525 SHAPE_TYPE == LibUtilities::NodalPyr)
526 {
527 d0 *= f2[k];
528 d1 *= f2[k];
529 d2 += f0[i] * d0 + f1[j] * d1;
530 }
531
532 // Multiply by derivative factors and Advection Velocity.
533 TData tmp = 0.0;
534 tmp = vx * (d0 * df[0u * warpsize + dfindex] +
535 d1 * df[1u * warpsize + dfindex] +
536 d2 * df[2u * warpsize + dfindex]);
537 tmp += vy * (d0 * df[3u * warpsize + dfindex] +
538 d1 * df[4u * warpsize + dfindex] +
539 d2 * df[5u * warpsize + dfindex]);
540 tmp += vz * (d0 * df[6u * warpsize + dfindex] +
541 d1 * df[7u * warpsize + dfindex] +
542 d2 * df[8u * warpsize + dfindex]);
543
544 if constexpr (APPEND)
545 {
546 out[index] += scale * tmp;
547 }
548 else
549 {
550 out[index] = scale * tmp;
551 }
552 }
553 }
554 }
555}
556
557/**
558 * @brief Device entry point of the 1D SumFac advection kernel.
559 *
560 * Launched by AdvectionBlockOpImpl::OperatorNDImpl through
561 * DEVICE_2DGRID_KERNEL_LAUNCHER, which appends the shared-memory pointer
562 * (unused here) and the thread-block handle. Unpacks the size parameter
563 * -- runtime or compile-time, both satisfying IsPhysSizeParameter1D_v as
564 * the static_assert enforces. Each thread then starts at its global index
565 * in the grid's first dimension and strides by that dimension's range, so
566 * the block's @p nelmt elements (padding included) are covered whatever
567 * grid size was launched. The grid's second dimension gives the index c
568 * of the component -- or homogeneous mode -- being advected, read once and
569 * fixed for the whole stride loop, since that dimension holds exactly one
570 * block row for each. The element index splits into the warp it belongs
571 * to and the lane within it: the warp's slice of the input and output
572 * arrays is selected by pointer arithmetic that also offsets by c, since
573 * @p in and @p out address every component, the velocity slice by the
574 * warp and the plane c % @p nhomo, and the derivative-factor slice by the
575 * warp alone -- the same velocity and factors serve every component. The
576 * lane is then passed to Advection1DSumFacKernel, which computes the
577 * element's output. The enable_if pins this overload to the SumFac tag,
578 * leaving the SumFacTOP tag to the identically named launcher in
579 * AdvectionBlockOpDeviceSumFacTOPKernels.hpp.
580 *
581 * @tparam SHAPE_TYPE Element shape (segments only; unused).
582 * @tparam Implementation Implementation tag; SumFac here.
583 * @tparam APPEND Accumulate onto @p out.
584 * @tparam DEFORMED Per-point derivative factors.
585 * @tparam TPhysSizeParameter1D 1D physical-space size parameter.
586 * @tparam TthreadBlock Back-end thread-block handle type.
587 *
588 * @param sizeParam1D Element sizes: coordinate dimension and
589 * quadrature points.
590 * @param nelmt Elements in the block, including padding.
591 * @param elmtsPerBlock Elements per thread block, one per thread;
592 * unused.
593 * @param useSharedMemoryWsp Unused; SumFac reads the input in place.
594 * @param D0 1D derivative matrix of the direction.
595 * @param df Derivative factors of the block.
596 * @param advVel Velocity data of the block.
597 * @param advVelOffset Stride between velocity components.
598 * @param nhomo Homogeneous planes per variable.
599 * @param in Input values of the block.
600 * @param out Output values of the block.
601 * @param scale Scalar factor applied to the result.
602 * @param shmemptr Dynamic shared memory of the thread block; unused
603 * in one dimension.
604 * @param threadBlock Thread-block handle supplied by the launcher
605 * macro.
606 */
607template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
608 bool APPEND, bool DEFORMED, typename TPhysSizeParameter1D,
609 typename TthreadBlock, typename TData,
610 std::enable_if_t<std::is_same_v<Implementation, SumFac>, bool>
611 Enable = true>
613 const TPhysSizeParameter1D sizeParam1D, const size_t nelmt,
614 [[maybe_unused]] const unsigned int elmtsPerBlock,
615 [[maybe_unused]] const bool useSharedMemoryWsp,
616 const TData *NEK_RESTRICT D0, const TData *NEK_RESTRICT df,
617 const TData *NEK_RESTRICT advVel, const size_t advVelOffset,
618 const unsigned int nhomo, const TData *NEK_RESTRICT in,
619 TData *NEK_RESTRICT out, const TData scale,
620 [[maybe_unused]] unsigned char *shmemptr, const TthreadBlock &threadBlock)
621{
622 static_assert(
623 IsPhysSizeParameter1D_v<TPhysSizeParameter1D>,
624 "Template argument must be either of type "
625 "NonTemplatedPhysSizeParameter1D or TemplatedPhysSizeParameter1D.");
626
627 const unsigned int ncoord = sizeParam1D.ncoord();
628 const unsigned int nq0 = sizeParam1D.nq0();
629
630 const unsigned int ndf = ncoord;
631 const unsigned int dfsize = DEFORMED ? nq0 : 1u;
632
633 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
634
635 size_t e = getGlobalIdx<0>(threadBlock);
636 const unsigned int c = getBlockIdx<1>(threadBlock);
637 while (e < nelmt)
638 {
639 const size_t ilane = e % warpsize;
640 const size_t iwarp = e / warpsize;
641 const TData *dfptr = df + ndf * dfsize * warpsize * iwarp;
642 const TData *inptr = in + nq0 * (nelmt * c + warpsize * iwarp);
643 TData *outptr = out + nq0 * (nelmt * c + warpsize * iwarp);
644 const TData *advVelPtr =
645 advVel + nq0 * (nelmt * (c % nhomo) + warpsize * iwarp);
646 Advection1DSumFacKernel<APPEND, DEFORMED>(ilane, ncoord, nq0, D0, dfptr,
647 advVelPtr, advVelOffset,
648 inptr, outptr, scale);
649
650 e += getGlobalRange<0>(threadBlock);
651 }
652}
653
654/**
655 * @brief Device entry point of the 2D SumFac advection kernel.
656 *
657 * As in 1D, but the shared memory is used: FETCH_SHARED_MEMORY() binds
658 * @p shmemptr to the block's dynamic shared memory (a no-op on the
659 * back-ends that pass it in). For triangles the collapsed-coordinate
660 * factor tables are then copied from global into the thread block's
661 * shared memory once, before the element loop, and a barrier makes them
662 * visible to the whole block; the per-element kernel then reads the shared
663 * copies. For quadrilaterals no shared memory is requested and the two
664 * table pointers stay null. The remaining unpacking and grid-stride loop
665 * are as in 1D, calling Advection2DSumFacKernel once per element.
666 *
667 * @tparam SHAPE_TYPE Element shape.
668 * @tparam Implementation Implementation tag; SumFac here.
669 * @tparam APPEND Accumulate onto @p out.
670 * @tparam DEFORMED Per-point derivative factors.
671 * @tparam TPhysSizeParameter2D 2D physical-space size parameter.
672 * @tparam TthreadBlock Back-end thread-block handle type.
673 *
674 * @param sizeParam2D Element sizes: coordinate dimension and
675 * per-direction quadrature points.
676 * @param nelmt Elements in the block, including padding.
677 * @param elmtsPerBlock Elements per thread block, one per thread;
678 * unused.
679 * @param useSharedMemoryWsp Unused; SumFac reads the input in place.
680 * @param D0 1D derivative matrix of direction 0.
681 * @param D1 1D derivative matrix of direction 1.
682 * @param f0 Collapsed-coordinate factor of direction 0.
683 * @param f1 Collapsed-coordinate factor of direction 1.
684 * @param df Derivative factors of the block.
685 * @param advVel Velocity data of the block.
686 * @param advVelOffset Stride between velocity components.
687 * @param nhomo Homogeneous planes per variable.
688 * @param in Input values of the block.
689 * @param out Output values of the block.
690 * @param scale Scalar factor applied to the result.
691 * @param shmemptr Dynamic shared memory of the thread block, at
692 * least AdvectionSharedMemorySize elements of TData.
693 * @param threadBlock Thread-block handle supplied by the launcher
694 * macro.
695 */
696template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
697 bool APPEND, bool DEFORMED, typename TPhysSizeParameter2D,
698 typename TthreadBlock, typename TData,
699 std::enable_if_t<std::is_same_v<Implementation, SumFac>, bool>
700 Enable = true>
702 const TPhysSizeParameter2D sizeParam2D, const size_t nelmt,
703 [[maybe_unused]] const unsigned int elmtsPerBlock,
704 [[maybe_unused]] const bool useSharedMemoryWsp,
705 const TData *NEK_RESTRICT D0, const TData *NEK_RESTRICT D1,
706 const TData *NEK_RESTRICT f0, const TData *NEK_RESTRICT f1,
707 const TData *NEK_RESTRICT df, const TData *NEK_RESTRICT advVel,
708 const size_t advVelOffset, const unsigned int nhomo,
709 const TData *NEK_RESTRICT in, TData *NEK_RESTRICT out, const TData scale,
710 unsigned char *shmemptr, const TthreadBlock &threadBlock)
711{
712 static_assert(
713 IsPhysSizeParameter2D_v<TPhysSizeParameter2D>,
714 "Template argument must be either of type "
715 "NonTemplatedPhysSizeParameter2D or TemplatedPhysSizeParameter2D.");
716
718
719 const unsigned int ncoord = sizeParam2D.ncoord();
720 const unsigned int nq0 = sizeParam2D.nq0();
721 const unsigned int nq1 = sizeParam2D.nq1();
722 const unsigned int nqTot = sizeParam2D.nqTot();
723
724 const unsigned int ndf = 2 * ncoord;
725 const unsigned int dfsize = DEFORMED ? nqTot : 1u;
726
727 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
728
729 TData *s_f0 = nullptr;
730 TData *s_f1 = nullptr;
731
732 // Precompute geometric factors.
733 if constexpr (SHAPE_TYPE == LibUtilities::Tri ||
734 SHAPE_TYPE == LibUtilities::NodalTri)
735 {
736 s_f0 = (TData *)shmemptr;
737 s_f1 = s_f0 + nq0;
738
739 // Copy to shared memory.
740 LoadToSharedMemory(nq0, f0, s_f0, threadBlock);
741 LoadToSharedMemory(nq1, f1, s_f1, threadBlock);
742
743 localBarrier(threadBlock);
744 }
745
746 size_t e = getGlobalIdx<0>(threadBlock);
747 const unsigned int c = getBlockIdx<1>(threadBlock);
748 while (e < nelmt)
749 {
750 const size_t ilane = e % warpsize;
751 const size_t iwarp = e / warpsize;
752 const TData *dfptr = df + ndf * dfsize * warpsize * iwarp;
753 const TData *inptr = in + nqTot * (nelmt * c + warpsize * iwarp);
754 TData *outptr = out + nqTot * (nelmt * c + warpsize * iwarp);
755 const TData *advVelPtr =
756 advVel + nqTot * (nelmt * (c % nhomo) + warpsize * iwarp);
757 Advection2DSumFacKernel<SHAPE_TYPE, APPEND, DEFORMED>(
758 ilane, ncoord, nq0, nq1, D0, D1, s_f0, s_f1, dfptr, advVelPtr,
759 advVelOffset, inptr, outptr, scale);
760
761 e += getGlobalRange<0>(threadBlock);
762 }
763}
764
765/**
766 * @brief Device entry point of the 3D SumFac advection kernel.
767 *
768 * Counterpart of the 2D overload for three-dimensional shapes; the size
769 * parameter carries no coordinate dimension, since a 3D element always
770 * has three. The shape's collapsed-coordinate tables -- four for
771 * tetrahedra, two for prisms, three for pyramids, none for hexahedra --
772 * are staged in shared memory before the element loop, laid out back to
773 * back in the order the shape needs them, and a barrier publishes them to
774 * the block before Advection3DSumFacKernel is called once per element.
775 *
776 * @tparam SHAPE_TYPE Element shape.
777 * @tparam Implementation Implementation tag; SumFac here.
778 * @tparam APPEND Accumulate onto @p out.
779 * @tparam DEFORMED Per-point derivative factors.
780 * @tparam TPhysSizeParameter3D 3D physical-space size parameter.
781 * @tparam TthreadBlock Back-end thread-block handle type.
782 *
783 * @param sizeParam3D Element sizes: per-direction quadrature points.
784 * @param nelmt Elements in the block, including padding.
785 * @param elmtsPerBlock Elements per thread block, one per thread;
786 * unused.
787 * @param useSharedMemoryWsp Unused; SumFac reads the input in place.
788 * @param D0 1D derivative matrix of direction 0.
789 * @param D1 1D derivative matrix of direction 1.
790 * @param D2 1D derivative matrix of direction 2.
791 * @param f0 (1 + eta_0)/2 at the direction-0 points.
792 * @param f1 (1 + eta_1)/2 at the direction-1 points.
793 * @param f1m 2/(1 - eta_1) at the direction-1 points.
794 * @param f2 2/(1 - eta_2) at the direction-2 points.
795 * @param df Derivative factors of the block.
796 * @param advVel Velocity data of the block.
797 * @param advVelOffset Stride between velocity components.
798 * @param nhomo Homogeneous planes per variable; unused in 3D.
799 * @param in Input values of the block.
800 * @param out Output values of the block.
801 * @param scale Scalar factor applied to the result.
802 * @param shmemptr Dynamic shared memory of the thread block, at
803 * least AdvectionSharedMemorySize elements of TData.
804 * @param threadBlock Thread-block handle supplied by the launcher
805 * macro.
806 */
807template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
808 bool APPEND, bool DEFORMED, typename TPhysSizeParameter3D,
809 typename TthreadBlock, typename TData,
810 std::enable_if_t<std::is_same_v<Implementation, SumFac>, bool>
811 Enable = true>
813 const TPhysSizeParameter3D sizeParam3D, const size_t nelmt,
814 [[maybe_unused]] const unsigned int elmtsPerBlock,
815 [[maybe_unused]] const bool useSharedMemoryWsp,
816 const TData *NEK_RESTRICT D0, const TData *NEK_RESTRICT D1,
817 const TData *NEK_RESTRICT D2, const TData *NEK_RESTRICT f0,
818 const TData *NEK_RESTRICT f1, const TData *NEK_RESTRICT f1m,
819 const TData *NEK_RESTRICT f2, const TData *NEK_RESTRICT df,
820 const TData *NEK_RESTRICT advVel, const size_t advVelOffset,
821 [[maybe_unused]] const unsigned int nhomo, const TData *NEK_RESTRICT in,
822 TData *NEK_RESTRICT out, const TData scale, unsigned char *shmemptr,
823 const TthreadBlock &threadBlock)
824{
825 static_assert(
826 IsPhysSizeParameter3D_v<TPhysSizeParameter3D>,
827 "Template argument must be either of type "
828 "NonTemplatedPhysSizeParameter3D or TemplatedPhysSizeParameter3D.");
829
831
832 const unsigned int nq0 = sizeParam3D.nq0();
833 const unsigned int nq1 = sizeParam3D.nq1();
834 const unsigned int nq2 = sizeParam3D.nq2();
835 const unsigned int nqTot = sizeParam3D.nqTot();
836
837 constexpr unsigned int ndf = 9u;
838 const unsigned int dfsize = DEFORMED ? nqTot : 1u;
839
840 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
841
842 TData *s_f0 = nullptr;
843 TData *s_f1 = nullptr;
844 TData *s_f1m = nullptr;
845 TData *s_f2 = nullptr;
846
847 // Precompute geometric factors.
848 if constexpr (SHAPE_TYPE == LibUtilities::Tet ||
849 SHAPE_TYPE == LibUtilities::NodalTet)
850 {
851 s_f0 = (TData *)shmemptr;
852 s_f1 = s_f0 + nq0;
853 s_f1m = s_f1 + nq1;
854 s_f2 = s_f1m + nq1;
855
856 // Copy to shared memory.
857 LoadToSharedMemory(nq0, f0, s_f0, threadBlock);
858 LoadToSharedMemory(nq1, f1, s_f1, threadBlock);
859 LoadToSharedMemory(nq1, f1m, s_f1m, threadBlock);
860 LoadToSharedMemory(nq2, f2, s_f2, threadBlock);
861
862 localBarrier(threadBlock);
863 }
864 else if constexpr (SHAPE_TYPE == LibUtilities::Prism ||
865 SHAPE_TYPE == LibUtilities::NodalPrism)
866 {
867 s_f0 = (TData *)shmemptr;
868 s_f2 = s_f0 + nq0;
869
870 // Copy to shared memory.
871 LoadToSharedMemory(nq0, f0, s_f0, threadBlock);
872 LoadToSharedMemory(nq2, f2, s_f2, threadBlock);
873
874 localBarrier(threadBlock);
875 }
876 else if constexpr (SHAPE_TYPE == LibUtilities::Pyr ||
877 SHAPE_TYPE == LibUtilities::NodalPyr)
878 {
879 s_f0 = (TData *)shmemptr;
880 s_f1 = s_f0 + nq0;
881 s_f2 = s_f1 + nq1;
882
883 // Copy to shared memory.
884 LoadToSharedMemory(nq0, f0, s_f0, threadBlock);
885 LoadToSharedMemory(nq1, f1, s_f1, threadBlock);
886 LoadToSharedMemory(nq2, f2, s_f2, threadBlock);
887
888 localBarrier(threadBlock);
889 }
890
891 size_t e = getGlobalIdx<0>(threadBlock); // use size_t to prevent overflow
892 const unsigned int c = getBlockIdx<1>(threadBlock);
893 while (e < nelmt)
894 {
895 const size_t ilane = e % warpsize;
896 const size_t iwarp = e / warpsize;
897 const TData *dfptr = df + ndf * dfsize * warpsize * iwarp;
898 const TData *inptr = in + nqTot * (nelmt * c + warpsize * iwarp);
899 TData *outptr = out + nqTot * (nelmt * c + warpsize * iwarp);
900 const TData *advVelPtr = advVel + nqTot * warpsize * iwarp;
901 Advection3DSumFacKernel<SHAPE_TYPE, APPEND, DEFORMED>(
902 ilane, nq0, nq1, nq2, D0, D1, D2, s_f0, s_f1, s_f1m, s_f2, dfptr,
903 advVelPtr, advVelOffset, inptr, outptr, scale);
904
905 e += getGlobalRange<0>(threadBlock);
906 }
907}
908
909#endif
910
911} // 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
Kernel size-parameter types shared by every element-operator family, the traits that recognise them a...
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 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
static NEK_FORCE_INLINE void AdvectionKernelLauncher(const TPhysSizeParameter1D sizeParam1D, const typename simd_type::scalarType *D0, const simd_type *df_ptr, const simd_type *advVel0_ptr, const simd_type *advVel1_ptr, const simd_type *advVel2_ptr, simd_type *deriv0, const simd_type *in, simd_type *out, const typename simd_type::scalarType scale)
Advect one element group of a 1D block: tensor-product differentiation followed by Advection1DKernel.
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)
static constexpr unsigned int warpSize
Definition Backends.hpp:143