Nektar++
Loading...
Searching...
No Matches
BwdTransBlockOpDeviceSumFacKernels.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: BwdTransBlockOpDeviceSumFacKernels.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: Device SumFac kernels of the backward transform, one
32// element per thread
33//
34///////////////////////////////////////////////////////////////////////////////
35
36/**
37 * @file BwdTransBlockOpDeviceSumFacKernels.hpp
38 * @brief Device SumFac kernels of the backward transform: one element
39 * per thread, with the sum-factorisation intermediates held in a
40 * global-memory workspace or, when it fits, in shared memory.
41 *
42 * These are the kernels the Device/SumFac block implementation launches
43 * (see BwdTransBlockOpDeviceSumFac.hpp); the SumFacTOP counterparts, where a
44 * whole thread block cooperates on one element, are in
45 * BwdTransBlockOpDeviceSumFacTOPKernels.hpp. The other sum-factorised operator
46 * families include this header for the same kernels, so their
47 * signatures are shared. Everything below is compiled only into
48 * device translation units of a device-enabled build
49 * (NEKTAR_ENABLE_DEVICE and DEVICE_COMPILE_ONLY).
50 *
51 * ### Work decomposition and data layout
52 * One thread evaluates one element. The block storage is reshaped to
53 * interleave width warpSize before the launch, so that the elements of a
54 * warp are interleaved: element `e` is handled by lane
55 * `ilane = e % warpSize` of warp `iwarp = e / warpSize`, and value `n`
56 * of that element sits at offset `warpSize * n + ilane` within its
57 * warp's group of `num_data * warpSize` values. Consecutive lanes
58 * therefore address consecutive words.
59 *
60 * The BwdTransKernelLauncher overloads stride over the elements of the
61 * block: the stride, `getGlobalRange<0>(threadBlock)`, is a whole number
62 * of warps -- the launch uses one warp per block, see
63 * GetDeviceKernelParameters -- so a thread keeps its lane index for every
64 * element it visits. Each thread's component index,
65 * `c = getBlockIdx<1>(threadBlock)`, is fixed for that whole stride
66 * loop: the grid's second dimension holds `ncomp = GetNumComponents() *
67 * GetNumHomoModes()` blocks, one per component, so a single launch
68 * covers every component of the block (see BwdTransBlockOpDeviceSumFac.hpp).
69 *
70 * ### Sum factorisation
71 * Each shape kernel contracts the coefficients against one direction's
72 * 1D basis table at a time, holding the intermediates in the workspace
73 * slices (`wsp` in two dimensions, a pair of slices in three) that
74 * BwdTransWorkSpaceSize accounts for, in global memory or, with
75 * useSharedMemoryWsp, in the shared memory BwdTransSharedMemorySize sizes. A
76 * basis table `basisN` holds one row of nqN values per mode, so that `basisN[m
77 * * nqN
78 * + i]` is mode `m` evaluated at point `i`; along the collapsed directions of
79 * the triangle, tetrahedron, prism and pyramid the row index `m` is the shape's
80 * combined mode index rather than a single-direction mode number. Coefficients
81 * are read in that same mode ordering: for the tensor-product shapes
82 * (quadrilateral, hexahedron) the direction-0 mode index runs fastest, whereas
83 * along a collapsed direction it is the innermost index of the combined
84 * ordering that does -- q for the triangle, r for the tetrahedron, prism and
85 * pyramid. Physical values are written in point order, with the direction-0
86 * point running fastest.
87 *
88 * ### Modified bases and nodal shapes
89 * @p isModified marks a first-direction eModified_A basis, for which the
90 * shape kernels add the extra terms the modified expansion carries at
91 * its collapsed vertex modes and, for the tetrahedron, at its singular
92 * edge; each term is identical to the corresponding one in
93 * StdRegions/Operators/BwdTransSumFacStdKernels.hpp. For the nodal
94 * shapes the coefficients are first converted to the modified modal
95 * basis by MatVecKernel and the modal kernel of the matching non-nodal
96 * shape is then run on the converted values.
97 */
98
99#pragma once
100
104
106
108{
109
110#if defined(NEKTAR_ENABLE_DEVICE) && defined(DEVICE_COMPILE_ONLY)
111/**
112 * @brief Workspace a 1D SumFac backward transform needs, in TData
113 * values: none.
114 *
115 * The segment kernel contracts its only direction straight into the
116 * output and so keeps no intermediates. The overload exists so that the
117 * block implementation can query a workspace size uniformly for every
118 * dimension (see BwdTransBlockOpDeviceSumFac.hpp).
119 *
120 * @tparam SHAPE_TYPE Shape of the block's elements (Seg).
121 * @tparam Implementation MultiRegions::SumFac; the SumFacTOP overload
122 * lives in BwdTransBlockOpDeviceSumFacTOPKernels.hpp.
123 * @tparam TSizeParameter1D 1D size parameter, runtime or templated.
124 *
125 * @param nelmt Elements in the block, padding included.
126 * @param sizeParam1D Modal and quadrature sizes of the expansion.
127 *
128 * @return 0.
129 */
130template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
131 typename TSizeParameter1D,
132 std::enable_if_t<std::is_same_v<Implementation, SumFac> &&
133 IsSizeParameter1D_v<TSizeParameter1D>,
134 bool>
135 Enable = true>
136inline constexpr size_t BwdTransWorkSpaceSize(
137 [[maybe_unused]] const size_t nelmt,
138 [[maybe_unused]] const TSizeParameter1D sizeParam1D)
139{
140 size_t wspsize = 0;
141
142 if constexpr (SHAPE_TYPE == LibUtilities::Seg)
143 {
144 wspsize = 0;
145 }
146
147 return wspsize;
148}
149
150/**
151 * @brief Workspace a 2D SumFac backward transform needs, in TData
152 * values.
153 *
154 * One flat region serves all @p nelmt elements: a stage needing `s` values per
155 * element occupies `s * nelmt` consecutive values, warp-interleaved exactly
156 * like the field data, and the stages follow one another
157 * (BwdTransKernelLauncher does the slicing). Per element that is
158 * - Quad: nm1 values -- the direction-0 sums of one direction-0 point;
159 * - Tri: nm0 values -- the direction-1 sums of one direction-1 point;
160 * - NodalTri: the nmTot values of the modal-converted coefficients,
161 * followed by the nm0 values of the triangle.
162 *
163 * Shapes with no 2D SumFac kernel get 0.
164 *
165 * @tparam SHAPE_TYPE Shape of the block's elements.
166 * @tparam Implementation MultiRegions::SumFac.
167 * @tparam TSizeParameter2D 2D size parameter, runtime or templated.
168 *
169 * @param nelmt Elements in the block, padding included.
170 * @param sizeParam2D Modal and quadrature sizes per direction.
171 *
172 * @return Number of TData values the launch's workspace must hold.
173 */
174template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
175 typename TSizeParameter2D,
176 std::enable_if_t<std::is_same_v<Implementation, SumFac> &&
177 IsSizeParameter2D_v<TSizeParameter2D>,
178 bool>
179 Enable = true>
180inline constexpr size_t BwdTransWorkSpaceSize(
181 const size_t nelmt, const TSizeParameter2D sizeParam2D)
182{
183 size_t wspsize = 0;
184
185 const unsigned int nm0 = sizeParam2D.nm0();
186 const unsigned int nm1 = sizeParam2D.nm1();
187
188 if constexpr (SHAPE_TYPE == LibUtilities::Quad)
189 {
190 wspsize = nm1 * nelmt;
191 }
192 else if constexpr (SHAPE_TYPE == LibUtilities::Tri)
193 {
194 wspsize = nm0 * nelmt;
195 }
196 else if constexpr (SHAPE_TYPE == LibUtilities::NodalTri)
197 {
198 const unsigned int nmTot = sizeParam2D.nmTot();
199
200 wspsize = (nm0 + nmTot) * nelmt;
201 }
202
203 return wspsize;
204}
205
206/**
207 * @brief Workspace a 3D SumFac backward transform needs, in TData
208 * values.
209 *
210 * Laid out as in the 2D overload, with the two stage intermediates of
211 * the shape kernels following one another. Per element that is
212 * - Hex: nm1 * nm2, then nm2;
213 * - Tet: (2 * nm1 - nm0 + 1) * nm0 / 2 -- the number of (p,q) mode pairs
214 * the collapsed ordering carries -- then nm0;
215 * - Prism and Pyr: nm0 * nm1, then nm0;
216 * - the nodal shapes prepend their total mode count nmTot for the
217 * modal-converted coefficients.
218 *
219 * Shapes with no 3D SumFac kernel get 0.
220 *
221 * @tparam SHAPE_TYPE Shape of the block's elements.
222 * @tparam Implementation MultiRegions::SumFac.
223 * @tparam TSizeParameter3D 3D size parameter, runtime or templated.
224 *
225 * @param nelmt Elements in the block, padding included.
226 * @param sizeParam3D Modal and quadrature sizes per direction.
227 *
228 * @return Number of TData values the launch's workspace must hold.
229 */
230template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
231 typename TSizeParameter3D,
232 std::enable_if_t<std::is_same_v<Implementation, SumFac> &&
233 IsSizeParameter3D_v<TSizeParameter3D>,
234 bool>
235 Enable = true>
236inline constexpr size_t BwdTransWorkSpaceSize(
237 const size_t nelmt, const TSizeParameter3D sizeParam3D)
238{
239 size_t wspsize = 0;
240
241 const unsigned int nm0 = sizeParam3D.nm0();
242 const unsigned int nm1 = sizeParam3D.nm1();
243 const unsigned int nm2 = sizeParam3D.nm2();
244
245 if constexpr (SHAPE_TYPE == LibUtilities::Hex)
246 {
247 wspsize = (nm1 * nm2 + nm2) * nelmt;
248 }
249 else if constexpr (SHAPE_TYPE == LibUtilities::Tet)
250 {
251 wspsize = ((2 * nm1 - nm0 + 1) * nm0 / 2 + nm0) * nelmt;
252 }
253 else if constexpr (SHAPE_TYPE == LibUtilities::NodalTet)
254 {
255 const unsigned int nmTot = sizeParam3D.nmTot();
256
257 wspsize = (((2 * nm1 - nm0 + 1) * nm0 / 2 + nm0) + nmTot) * nelmt;
258 }
259 else if constexpr (SHAPE_TYPE == LibUtilities::Prism)
260 {
261 wspsize = (nm0 * nm1 + nm0) * nelmt;
262 }
263 else if constexpr (SHAPE_TYPE == LibUtilities::NodalPrism)
264 {
265 const unsigned int nmTot = sizeParam3D.nmTot();
266
267 wspsize = ((nm0 * nm1 + nm0) + nmTot) * nelmt;
268 }
269 else if constexpr (SHAPE_TYPE == LibUtilities::Pyr)
270 {
271 wspsize = (nm0 * nm1 + nm0) * nelmt;
272 }
273 else if constexpr (SHAPE_TYPE == LibUtilities::NodalPyr)
274 {
275 const unsigned int nmTot = sizeParam3D.nmTot();
276
277 wspsize = ((nm0 * nm1 + nm0) + nmTot) * nelmt;
278 }
279
280 return wspsize;
281}
282
283/**
284 * @brief Dynamic shared memory a 1D SumFac backward transform needs, in
285 * TData values.
286 *
287 * The SumFac kernels read the basis tables straight from global memory, so the
288 * only thing held per thread block is, with @p useSharedMemoryWsp, the
289 * workspace of its @p elmtsPerBlock elements, laid out as
290 * BwdTransWorkSpaceSize gives for that many elements. The SumFacTOP overloads
291 * of the same name (BwdTransBlockOpDeviceSumFacTOPKernels.hpp) return the size
292 * of the staging area that strategy uses.
293 *
294 * @tparam SHAPE_TYPE Shape of the block's elements (Seg).
295 * @tparam Implementation MultiRegions::SumFac.
296 * @tparam TSizeParameter1D 1D size parameter, runtime or templated.
297 *
298 * @param sizeParam1D Modal and quadrature sizes of the expansion.
299 * @param elmtsPerBlock Elements per thread block, from
300 * GetElmtsPerBlock().
301 * @param useSharedMemoryWsp Stage intermediates in shared memory.
302 *
303 * @return Number of TData values of shared memory per thread block.
304 */
305template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
306 typename TSizeParameter1D,
307 std::enable_if_t<std::is_same_v<Implementation, SumFac> &&
308 IsSizeParameter1D_v<TSizeParameter1D>,
309 bool>
310 Enable = true>
311inline constexpr unsigned int BwdTransSharedMemorySize(
312 const TSizeParameter1D sizeParam1D, const unsigned int elmtsPerBlock,
313 const bool useSharedMemoryWsp)
314{
315 return useSharedMemoryWsp
316 ? static_cast<unsigned int>(
317 BwdTransWorkSpaceSize<SHAPE_TYPE, Implementation>(
318 elmtsPerBlock, sizeParam1D))
319 : 0u;
320}
321
322/// @brief Dynamic shared memory a 2D SumFac backward transform needs,
323/// as the 1D overload.
324template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
325 typename TSizeParameter2D,
326 std::enable_if_t<std::is_same_v<Implementation, SumFac> &&
327 IsSizeParameter2D_v<TSizeParameter2D>,
328 bool>
329 Enable = true>
330inline constexpr unsigned int BwdTransSharedMemorySize(
331 const TSizeParameter2D sizeParam2D, const unsigned int elmtsPerBlock,
332 const bool useSharedMemoryWsp)
333{
334 return useSharedMemoryWsp
335 ? static_cast<unsigned int>(
336 BwdTransWorkSpaceSize<SHAPE_TYPE, Implementation>(
337 elmtsPerBlock, sizeParam2D))
338 : 0u;
339}
340
341/// @brief Dynamic shared memory a 3D SumFac backward transform needs,
342/// as the 1D overload.
343template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
344 typename TSizeParameter3D,
345 std::enable_if_t<std::is_same_v<Implementation, SumFac> &&
346 IsSizeParameter3D_v<TSizeParameter3D>,
347 bool>
348 Enable = true>
349inline constexpr unsigned int BwdTransSharedMemorySize(
350 const TSizeParameter3D sizeParam3D, const unsigned int elmtsPerBlock,
351 const bool useSharedMemoryWsp)
352{
353 return useSharedMemoryWsp
354 ? static_cast<unsigned int>(
355 BwdTransWorkSpaceSize<SHAPE_TYPE, Implementation>(
356 elmtsPerBlock, sizeParam3D))
357 : 0u;
358}
359
360/**
361 * @brief Segment SumFac kernel: evaluate one element's expansion at its
362 * quadrature points.
363 *
364 * There is nothing to factorise in one dimension, so the single basis
365 * table is contracted straight into the output and no workspace is
366 * used. @p in and @p out address the whole warp's interleaved data,
367 * from which @p ilane selects this thread's element.
368 *
369 * @tparam APPEND Accumulate onto @p out instead of overwriting it.
370 *
371 * @param ilane Lane index of this thread's element within its warp.
372 * @param nm0 Modes of the expansion.
373 * @param nq0 Quadrature points of the expansion.
374 * @param basis0 Basis table: `basis0[p * nq0 + i]` is mode p at
375 * point i.
376 * @param in Warp's interleaved coefficients, nm0 * warpSize
377 * values.
378 * @param out Warp's interleaved physical values, nq0 * warpSize
379 * values.
380 */
381template <bool APPEND, typename TData>
382NEK_DEVICE_INLINE static void BwdTransSegSumFacKernel(
383 const unsigned int ilane, const unsigned int nm0, const unsigned int nq0,
384 const TData *NEK_RESTRICT basis0, const TData *NEK_RESTRICT in,
385 TData *NEK_RESTRICT out)
386{
387 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
388
389 for (unsigned int i = 0u; i < nq0; ++i)
390 {
391 TData tmp = 0.0;
392#pragma unroll
393 for (unsigned int p = 0u; p < nm0; ++p)
394 {
395 tmp += in[warpsize * p + ilane] * basis0[p * nq0 + i];
396 }
397
398 if constexpr (APPEND)
399 {
400 out[warpsize * i + ilane] += tmp;
401 }
402 else
403 {
404 out[warpsize * i + ilane] = tmp;
405 }
406 }
407}
408
409/**
410 * @brief Quadrilateral SumFac kernel: tensor-product evaluation of one
411 * element, one direction-0 point at a time.
412 *
413 * For each direction-0 point i the direction-0 contraction reduces the
414 * coefficients to the nm1 values of @p wsp, one per direction-1 mode;
415 * the direction-1 contraction then turns those into the physical values
416 * of the whole line i. Coefficients are read as `in[q * nm0 + p]`, so
417 * the direction-0 mode index runs fastest.
418 *
419 * @tparam APPEND Accumulate onto @p out instead of overwriting it.
420 *
421 * @param ilane Lane index of this thread's element within its warp.
422 * @param nm0,nm1 Modes per direction.
423 * @param nq0,nq1 Quadrature points per direction.
424 * @param basis0 Direction-0 basis table (nm0 rows of nq0).
425 * @param basis1 Direction-1 basis table (nm1 rows of nq1).
426 * @param in Warp's interleaved coefficients, nm0 * nm1 per
427 * element.
428 * @param out Warp's interleaved physical values, nq0 * nq1 per
429 * element, direction-0 point running fastest.
430 * @param wsp Warp's workspace slice, nm1 values per element.
431 */
432template <bool APPEND, typename TData>
433NEK_DEVICE_INLINE static void BwdTransQuadSumFacKernel(
434 const unsigned int ilane, const unsigned int nm0, const unsigned int nm1,
435 const unsigned int nq0, const unsigned int nq1,
436 const TData *NEK_RESTRICT basis0, const TData *NEK_RESTRICT basis1,
437 const TData *NEK_RESTRICT in, TData *NEK_RESTRICT out,
438 TData *NEK_RESTRICT wsp)
439{
440 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
441
442 for (unsigned int i = 0u; i < nq0; ++i)
443 {
444 // direction 0
445 for (unsigned int q = 0u, cnt_qp = 0u; q < nm1; ++q)
446 {
447 TData tmp = 0.0;
448#pragma unroll
449 for (unsigned int p = 0u; p < nm0; ++p, ++cnt_qp)
450 {
451 tmp += in[warpsize * cnt_qp + ilane] * basis0[p * nq0 + i];
452 }
453 wsp[warpsize * q + ilane] = tmp;
454 }
455
456 // direction 1
457 for (unsigned int j = 0u; j < nq1; ++j)
458 {
459 TData tmp = 0.0;
460#pragma unroll
461 for (unsigned int q = 0u; q < nm1; ++q)
462 {
463 tmp += wsp[warpsize * q + ilane] * basis1[q * nq1 + j];
464 }
465
466 if constexpr (APPEND)
467 {
468 out[warpsize * (nq0 * j + i) + ilane] += tmp;
469 }
470 else
471 {
472 out[warpsize * (nq0 * j + i) + ilane] = tmp;
473 }
474 }
475 }
476}
477
478/**
479 * @brief Triangular SumFac kernel: evaluation of one element in the
480 * collapsed coordinate system, one direction-1 point at a time.
481 *
482 * The collapsed direction goes first: for each direction-1 point j and
483 * each p, the nm1 - p modes (p,q) -- consecutive in the triangular mode
484 * ordering -- are contracted against @p basis1, whose rows are indexed
485 * by that same combined mode. The resulting nm0 values in @p wsp are
486 * then contracted against @p basis0 for every point of the line j.
487 *
488 * With @p isModified set, the term
489 * `in[warpsize + ilane] * basis0[nq0 + i] * basis1[nq1 + j]` is added,
490 * exactly as in the StdRegions BwdTransTriKernel.
491 *
492 * @tparam APPEND Accumulate onto @p out instead of overwriting it.
493 *
494 * @param ilane Lane index of this thread's element within its
495 * warp.
496 * @param nm0,nm1 Modes per direction.
497 * @param nq0,nq1 Quadrature points per direction.
498 * @param isModified First-direction basis is eModified_A.
499 * @param basis0 Direction-0 basis table (nm0 rows of nq0).
500 * @param basis1 Direction-1 basis table, one row of nq1 per
501 * combined (p,q) mode.
502 * @param in Warp's interleaved coefficients in the
503 * triangular mode ordering.
504 * @param out Warp's interleaved physical values, nq0 * nq1
505 * per element.
506 * @param wsp Warp's workspace slice, nm0 values per element.
507 */
508template <bool APPEND, typename TData>
509NEK_DEVICE_INLINE static void BwdTransTriSumFacKernel(
510 const unsigned int ilane, const unsigned int nm0, const unsigned int nm1,
511 const unsigned int nq0, const unsigned int nq1, const bool isModified,
512 const TData *NEK_RESTRICT basis0, const TData *NEK_RESTRICT basis1,
513 const TData *NEK_RESTRICT in, TData *NEK_RESTRICT out,
514 TData *NEK_RESTRICT wsp)
515{
516 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
517
518 for (unsigned int j = 0u, cnt_ji = 0u; j < nq1; ++j)
519 {
520 // direction 1
521 for (unsigned int p = 0u, mode_pq = 0u; p < nm0; ++p)
522 {
523 TData tmp = 0.0;
524#pragma unroll
525 for (unsigned int q = 0u; q < (nm1 - p); ++q, ++mode_pq)
526 {
527 tmp +=
528 in[warpsize * mode_pq + ilane] * basis1[mode_pq * nq1 + j];
529 }
530 wsp[warpsize * p + ilane] = tmp;
531 }
532
533 // direction 0
534 for (unsigned int i = 0u; i < nq0; ++i, ++cnt_ji)
535 {
536 TData tmp = 0.0;
537
538 if (isModified)
539 {
540 tmp += in[warpsize + ilane] * basis0[nq0 + i] * basis1[nq1 + j];
541 }
542
543#pragma unroll
544 for (unsigned int p = 0u; p < nm0; ++p)
545 {
546 tmp += wsp[warpsize * p + ilane] * basis0[p * nq0 + i];
547 }
548
549 if constexpr (APPEND)
550 {
551 out[warpsize * cnt_ji + ilane] += tmp;
552 }
553 else
554 {
555 out[warpsize * cnt_ji + ilane] = tmp;
556 }
557 }
558 }
559}
560
561template <LibUtilities::ShapeType SHAPE_TYPE, bool APPEND, typename TData>
562NEK_DEVICE_INLINE static void BwdTrans2DSumFacKernel(
563 const unsigned int ilane, const unsigned int nm0, const unsigned int nm1,
564 const unsigned int nq0, const unsigned int nq1, const bool isModified,
565 const TData *NEK_RESTRICT basis0, const TData *NEK_RESTRICT basis1,
566 const TData *NEK_RESTRICT in, TData *NEK_RESTRICT out,
567 TData *NEK_RESTRICT wsp)
568{
569 if constexpr (SHAPE_TYPE == LibUtilities::Quad)
570 {
571 BwdTransQuadSumFacKernel<APPEND>(ilane, nm0, nm1, nq0, nq1, basis0,
572 basis1, in, out, wsp);
573 }
574 else if constexpr (SHAPE_TYPE == LibUtilities::Tri ||
575 SHAPE_TYPE == LibUtilities::NodalTri)
576 {
577 BwdTransTriSumFacKernel<APPEND>(ilane, nm0, nm1, nq0, nq1, isModified,
578 basis0, basis1, in, out, wsp);
579 }
580}
581
582/**
583 * @brief Hexahedral SumFac kernel: three-direction tensor-product
584 * evaluation of one element, one direction-0 point at a time.
585 *
586 * For each direction-0 point i the direction-0 contraction fills the
587 * nm1 * nm2 values of @p wsp0; then, for each direction-1 point j, the
588 * direction-1 contraction fills the nm2 values of @p wsp1 and the
589 * direction-2 contraction writes the nq2 physical values of the line
590 * (i,j). Coefficients are read as `in[(r * nm1 + q) * nm0 + p]`, the
591 * direction-0 mode index running fastest.
592 *
593 * @tparam APPEND Accumulate onto @p out instead of overwriting it.
594 *
595 * @param ilane Lane index of this thread's element within its
596 * warp.
597 * @param nm0,nm1,nm2 Modes per direction.
598 * @param nq0,nq1,nq2 Quadrature points per direction.
599 * @param basis0,basis1,basis2 Per-direction basis tables.
600 * @param in Warp's interleaved coefficients,
601 * nm0 * nm1 * nm2 per element.
602 * @param out Warp's interleaved physical values,
603 * nq0 * nq1 * nq2 per element, direction-0 point
604 * running fastest.
605 * @param wsp0 Warp's first workspace slice, nm1 * nm2 values
606 * per element.
607 * @param wsp1 Warp's second workspace slice, nm2 values per
608 * element.
609 */
610template <bool APPEND, typename TData>
611NEK_DEVICE_INLINE static void BwdTransHexSumFacKernel(
612 const unsigned int ilane, const unsigned int nm0, const unsigned int nm1,
613 const unsigned int nm2, const unsigned int nq0, const unsigned int nq1,
614 const unsigned int nq2, const TData *NEK_RESTRICT basis0,
615 const TData *NEK_RESTRICT basis1, const TData *NEK_RESTRICT basis2,
616 const TData *NEK_RESTRICT in, TData *NEK_RESTRICT out,
617 TData *NEK_RESTRICT wsp0, TData *NEK_RESTRICT wsp1)
618{
619 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
620
621 for (unsigned int i = 0u; i < nq0; ++i)
622 {
623 // direction 0
624 for (unsigned int r = 0u, cnt_rqp = 0u, cnt_rq = 0u; r < nm2; ++r)
625 {
626 for (unsigned int q = 0u; q < nm1; ++q, ++cnt_rq)
627 {
628 TData tmp = 0.0;
629#pragma unroll
630 for (unsigned int p = 0u; p < nm0; ++p, ++cnt_rqp)
631 {
632 tmp += in[warpsize * cnt_rqp + ilane] * basis0[p * nq0 + i];
633 }
634 wsp0[warpsize * cnt_rq + ilane] = tmp;
635 }
636 }
637
638 // direction 1
639 for (unsigned int j = 0u; j < nq1; ++j)
640 {
641 for (unsigned int r = 0u, cnt_rq = 0u; r < nm2; ++r)
642 {
643 TData tmp = 0.0;
644#pragma unroll
645 for (unsigned int q = 0u; q < nm1; ++q, ++cnt_rq)
646 {
647 tmp +=
648 wsp0[warpsize * cnt_rq + ilane] * basis1[q * nq1 + j];
649 }
650 wsp1[warpsize * r + ilane] = tmp;
651 }
652
653 // direction 2
654 for (unsigned int k = 0u; k < nq2; ++k)
655 {
656 TData tmp = 0.0;
657#pragma unroll
658 for (unsigned int r = 0u; r < nm2; ++r)
659 {
660 tmp += wsp1[warpsize * r + ilane] * basis2[r * nq2 + k];
661 }
662
663 if constexpr (APPEND)
664 {
665 out[warpsize * (k * nq1 * nq0 + j * nq0 + i) + ilane] +=
666 tmp;
667 }
668 else
669 {
670 out[warpsize * (k * nq1 * nq0 + j * nq0 + i) + ilane] = tmp;
671 }
672 }
673 }
674 }
675}
676
677/**
678 * @brief Tetrahedral SumFac kernel: evaluation of one element in the
679 * collapsed coordinate system, one direction-2 point at a time.
680 *
681 * The directions are contracted in the order 2, 1, 0. For each
682 * direction-2 point k, the modes (p,q,r) -- r consecutive within a
683 * (p,q) pair -- are contracted against @p basis2 into the @p fpq value
684 * of their (p,q) pair; then, per direction-1 point j, the (p,q) pairs
685 * are contracted against @p basis1 into the nm0 values of @p fp, which
686 * the direction-0 contraction turns into the physical values of the
687 * line (j,k).
688 *
689 * Two mode counters run through the first stage: `mode_pqr` walks the
690 * coefficients, while `mode2` walks the rows of @p basis2, which
691 * enumerate every (p,q,r) with q < nm2 - p. When nm2 > nm1 the table
692 * therefore holds rows the coefficient array has no entries for, and
693 * `mode2` is advanced past them at the end of each p.
694 *
695 * With @p isModified set, the top-vertex, bottom-vertex and
696 * singular-edge terms marked in the code are added, exactly as in the
697 * StdRegions BwdTransTetKernel.
698 *
699 * @tparam APPEND Accumulate onto @p out instead of overwriting it.
700 *
701 * @param ilane Lane index of this thread's element within its
702 * warp.
703 * @param nm0,nm1,nm2 Modes per direction.
704 * @param nq0,nq1,nq2 Quadrature points per direction.
705 * @param isModified First-direction basis is eModified_A.
706 * @param basis0 Direction-0 basis table (nm0 rows of nq0).
707 * @param basis1 Direction-1 basis table, one row of nq1 per
708 * combined (p,q) mode.
709 * @param basis2 Direction-2 basis table, one row of nq2 per
710 * combined (p,q,r) mode.
711 * @param in Warp's interleaved coefficients in the
712 * tetrahedral mode ordering.
713 * @param out Warp's interleaved physical values,
714 * nq0 * nq1 * nq2 per element.
715 * @param fpq Warp's first workspace slice, one value per
716 * (p,q) mode pair and element.
717 * @param fp Warp's second workspace slice, nm0 values per
718 * element.
719 */
720template <bool APPEND, typename TData>
721NEK_DEVICE_INLINE static void BwdTransTetSumFacKernel(
722 const unsigned int ilane, const unsigned int nm0, const unsigned int nm1,
723 const unsigned int nm2, const unsigned int nq0, const unsigned int nq1,
724 const unsigned int nq2, const bool isModified,
725 const TData *NEK_RESTRICT basis0, const TData *NEK_RESTRICT basis1,
726 const TData *NEK_RESTRICT basis2, const TData *NEK_RESTRICT in,
727 TData *NEK_RESTRICT out, TData *NEK_RESTRICT fpq, TData *NEK_RESTRICT fp)
728{
729 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
730
731 for (unsigned int k = 0u, cnt_kji = 0u; k < nq2; ++k)
732 {
733 // direction 2
734 for (unsigned int p = 0u, mode_pq = 0u, mode2 = 0u, mode_pqr = 0u;
735 p < nm0; ++p)
736 {
737 for (unsigned int q = 0u; q < nm1 - p; ++q, ++mode_pq)
738 {
739 TData tmp = 0.0;
740#pragma unroll
741 for (unsigned int r = 0u; r < nm2 - p - q;
742 ++r, ++mode2, ++mode_pqr)
743 {
744 tmp += in[warpsize * mode_pqr + ilane] *
745 basis2[nq2 * mode2 + k];
746 }
747 fpq[warpsize * mode_pq + ilane] = tmp;
748 }
749
750 // increment mode in case nm2>nm1
751#pragma unroll
752 for (unsigned int q = nm1 - p; q < nm2 - p; ++q)
753 {
754 mode2 += nm2 - p - q;
755 }
756 }
757
758 // direction 1
759 for (unsigned int j = 0u; j < nq1; ++j)
760 {
761 for (unsigned int p = 0u, mode_pq = 0u; p < nm0; ++p)
762 {
763 TData tmp = 0.0;
764#pragma unroll
765 for (unsigned int q = 0u; q < nm1 - p; ++q, ++mode_pq)
766 {
767 tmp += fpq[warpsize * mode_pq + ilane] *
768 basis1[mode_pq * nq1 + j];
769 }
770 fp[warpsize * p + ilane] = tmp;
771 }
772
773 // direction 0
774 for (unsigned int i = 0u; i < nq0; ++i, ++cnt_kji)
775 {
776 TData tmp = 0.0;
777
778 if (isModified)
779 {
780 // top vertex
781 tmp += basis0[i] * basis1[nq1 + j];
782 tmp += basis0[nq0 + i] * basis1[j];
783 tmp += basis0[nq0 + i] * basis1[nq1 + j];
784 tmp *= basis2[nq2 + k] * in[warpsize + ilane];
785
786 // bottom vertex
787 TData tmp1 = basis2[k] * in[warpsize * nm2 + ilane];
788
789 // singular edge
790#pragma unroll
791 for (unsigned int r = 1u; r < nm2 - 1u; ++r)
792 {
793 tmp1 += basis2[(r + 1u) * nq2 + k] *
794 in[warpsize * (nm2 + r) + ilane];
795 }
796 tmp += basis1[nq1 + j] * basis0[nq0 + i] * tmp1;
797 }
798
799#pragma unroll
800 for (unsigned int p = 0u; p < nm0; ++p)
801 {
802 tmp += fp[warpsize * p + ilane] * basis0[p * nq0 + i];
803 }
804
805 if constexpr (APPEND)
806 {
807 out[warpsize * cnt_kji + ilane] += tmp;
808 }
809 else
810 {
811 out[warpsize * cnt_kji + ilane] = tmp;
812 }
813 }
814 }
815 }
816}
817
818/**
819 * @brief Prismatic SumFac kernel: evaluation of one element in the
820 * collapsed coordinate system, one direction-2 point at a time.
821 *
822 * As for the tetrahedron the directions are contracted in the order
823 * 2, 1, 0, but only direction 2 is collapsed: for each p the
824 * coefficients carry nm2 - p modes r for every one of the nm1 modes q,
825 * and @p basis2 holds one row per combined (p,r) mode, whose block for
826 * p starts at the running offset `mode_pr`. The direction-2 contraction
827 * fills the nm0 * nm1 values of @p fpq, the direction-1 contraction the
828 * nm0 values of @p fp, and the direction-0 contraction the physical
829 * values of the line (j,k).
830 *
831 * With @p isModified set, the collapsed-vertex term summed over q in
832 * the code is added, exactly as in the StdRegions BwdTransPrismKernel.
833 *
834 * @tparam APPEND Accumulate onto @p out instead of overwriting it.
835 *
836 * @param ilane Lane index of this thread's element within its
837 * warp.
838 * @param nm0,nm1,nm2 Modes per direction.
839 * @param nq0,nq1,nq2 Quadrature points per direction.
840 * @param isModified First-direction basis is eModified_A.
841 * @param basis0 Direction-0 basis table (nm0 rows of nq0).
842 * @param basis1 Direction-1 basis table (nm1 rows of nq1).
843 * @param basis2 Direction-2 basis table, one row of nq2 per
844 * combined (p,r) mode.
845 * @param in Warp's interleaved coefficients in the
846 * prismatic mode ordering.
847 * @param out Warp's interleaved physical values,
848 * nq0 * nq1 * nq2 per element.
849 * @param fpq Warp's first workspace slice, nm0 * nm1 values
850 * per element.
851 * @param fp Warp's second workspace slice, nm0 values per
852 * element.
853 */
854template <bool APPEND, typename TData>
855NEK_DEVICE_INLINE static void BwdTransPrismSumFacKernel(
856 const unsigned int ilane, const unsigned int nm0, const unsigned int nm1,
857 const unsigned int nm2, const unsigned int nq0, const unsigned int nq1,
858 const unsigned int nq2, const bool isModified,
859 const TData *NEK_RESTRICT basis0, const TData *NEK_RESTRICT basis1,
860 const TData *NEK_RESTRICT basis2, const TData *NEK_RESTRICT in,
861 TData *NEK_RESTRICT out, TData *NEK_RESTRICT fpq, TData *NEK_RESTRICT fp)
862{
863 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
864
865 for (unsigned int k = 0u, cnt_kji = 0u; k < nq2; ++k)
866 {
867 // direction 2
868 for (unsigned int p = 0u, mode_pr = 0u, mode_pq = 0u, mode_pqr = 0u;
869 p < nm0; ++p)
870 {
871 for (unsigned int q = 0u; q < nm1; ++q, ++mode_pq)
872 {
873 TData tmp = 0.0;
874#pragma unroll
875 for (unsigned int r = 0u; r < nm2 - p; ++r, ++mode_pqr)
876 {
877 tmp += in[warpsize * mode_pqr + ilane] *
878 basis2[(mode_pr + r) * nq2 + k];
879 }
880 fpq[warpsize * mode_pq + ilane] = tmp;
881 }
882 mode_pr += nm2 - p;
883 }
884
885 // direction 1
886 for (unsigned int j = 0u; j < nq1; ++j)
887 {
888 for (unsigned int p = 0u, mode_pq = 0u; p < nm0; ++p)
889 {
890 TData tmp = 0.0;
891#pragma unroll
892 for (unsigned int q = 0u; q < nm1; ++q, ++mode_pq)
893 {
894 tmp +=
895 fpq[warpsize * mode_pq + ilane] * basis1[q * nq1 + j];
896 }
897 fp[warpsize * p + ilane] = tmp;
898 }
899
900 // direction 0
901 for (unsigned int i = 0u; i < nq0; ++i, ++cnt_kji)
902 {
903 TData tmp = 0.0;
904
905 if (isModified)
906 {
907#pragma unroll
908 for (unsigned int q = 0u; q < nm1; ++q)
909 {
910 tmp += basis1[q * nq1 + j] *
911 in[warpsize * (nm2 * q + 1u) + ilane];
912 }
913 tmp *= basis2[nq2 + k] * basis0[nq0 + i];
914 }
915
916#pragma unroll
917 for (unsigned int p = 0u; p < nm0; ++p)
918 {
919 tmp += fp[warpsize * p + ilane] * basis0[p * nq0 + i];
920 }
921
922 if constexpr (APPEND)
923 {
924 out[warpsize * cnt_kji + ilane] += tmp;
925 }
926 else
927 {
928 out[warpsize * cnt_kji + ilane] = tmp;
929 }
930 }
931 }
932 }
933}
934
935/**
936 * @brief Pyramidal SumFac kernel: evaluation of one element in the
937 * collapsed coordinate system, one direction-2 point at a time.
938 *
939 * Structured like the prismatic kernel, except that the direction-2
940 * mode count of a (p,q) pair is nm2 - max(p,q) and @p basis2 holds one
941 * row per combined (p,q,r) mode. As in the tetrahedral kernel the basis
942 * table enumerates pairs the coefficient array has no entries for when
943 * nm2 > nm1, and its counter `mode2` is advanced past them at the end
944 * of each p.
945 *
946 * With @p isModified set, the top-vertex term marked in the code is
947 * added, exactly as in the StdRegions BwdTransPyrKernel.
948 *
949 * @tparam APPEND Accumulate onto @p out instead of overwriting it.
950 *
951 * @param ilane Lane index of this thread's element within its
952 * warp.
953 * @param nm0,nm1,nm2 Modes per direction.
954 * @param nq0,nq1,nq2 Quadrature points per direction.
955 * @param isModified First-direction basis is eModified_A.
956 * @param basis0 Direction-0 basis table (nm0 rows of nq0).
957 * @param basis1 Direction-1 basis table (nm1 rows of nq1).
958 * @param basis2 Direction-2 basis table, one row of nq2 per
959 * combined (p,q,r) mode.
960 * @param in Warp's interleaved coefficients in the
961 * pyramidal mode ordering.
962 * @param out Warp's interleaved physical values,
963 * nq0 * nq1 * nq2 per element.
964 * @param fpq Warp's first workspace slice, nm0 * nm1 values
965 * per element.
966 * @param fp Warp's second workspace slice, nm0 values per
967 * element.
968 */
969template <bool APPEND, typename TData>
970NEK_DEVICE_INLINE static void BwdTransPyrSumFacKernel(
971 const unsigned int ilane, const unsigned int nm0, const unsigned int nm1,
972 const unsigned int nm2, const unsigned int nq0, const unsigned int nq1,
973 const unsigned int nq2, const bool isModified,
974 const TData *NEK_RESTRICT basis0, const TData *NEK_RESTRICT basis1,
975 const TData *NEK_RESTRICT basis2, const TData *NEK_RESTRICT in,
976 TData *NEK_RESTRICT out, TData *NEK_RESTRICT fpq, TData *NEK_RESTRICT fp)
977{
978 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
979
980 for (unsigned int k = 0u, cnt_kji = 0u; k < nq2; ++k)
981 {
982 // direction 2
983 for (unsigned int p = 0u, mode_pq = 0u, mode2 = 0u, mode_pqr = 0u;
984 p < nm0; ++p)
985 {
986 for (unsigned int q = 0u; q < nm1; ++q, ++mode_pq)
987 {
988 TData tmp = 0.0;
989#pragma unroll
990 for (unsigned int r = 0u; r < nm2 - std::max(p, q);
991 ++r, ++mode2, ++mode_pqr)
992 {
993 tmp += in[warpsize * mode_pqr + ilane] *
994 basis2[mode2 * nq2 + k];
995 }
996 fpq[warpsize * mode_pq + ilane] = tmp;
997 }
998
999 // increment mode in case nm2>nm1
1000#pragma unroll
1001 for (unsigned int q = nm1; q < nm2; ++q)
1002 {
1003 mode2 += nm2 - q;
1004 }
1005 }
1006
1007 // direction 1
1008 for (unsigned int j = 0u; j < nq1; ++j)
1009 {
1010 for (unsigned int p = 0u, mode_pq = 0u; p < nm0; ++p)
1011 {
1012 TData tmp = 0.0;
1013#pragma unroll
1014 for (unsigned int q = 0u; q < nm1; ++q, ++mode_pq)
1015 {
1016 tmp +=
1017 fpq[warpsize * mode_pq + ilane] * basis1[q * nq1 + j];
1018 }
1019 fp[warpsize * p + ilane] = tmp;
1020 }
1021
1022 // direction 0
1023 for (unsigned int i = 0u; i < nq0; ++i, ++cnt_kji)
1024 {
1025 TData tmp = 0.0;
1026
1027 if (isModified)
1028 {
1029 // top vertex
1030 tmp += basis0[i] * basis1[nq1 + j];
1031 tmp += basis0[nq0 + i] * basis1[j];
1032 tmp += basis0[nq0 + i] * basis1[nq1 + j];
1033 tmp *= basis2[nq2 + k] * in[warpsize + ilane];
1034 }
1035
1036#pragma unroll
1037 for (unsigned int p = 0u; p < nm0; ++p)
1038 {
1039 tmp += fp[warpsize * p + ilane] * basis0[p * nq0 + i];
1040 }
1041
1042 if constexpr (APPEND)
1043 {
1044 out[warpsize * cnt_kji + ilane] += tmp;
1045 }
1046 else
1047 {
1048 out[warpsize * cnt_kji + ilane] = tmp;
1049 }
1050 }
1051 }
1052 }
1053}
1054
1055template <LibUtilities::ShapeType SHAPE_TYPE, bool APPEND, typename TData>
1056NEK_DEVICE_INLINE static void BwdTrans3DSumFacKernel(
1057 const unsigned int ilane, const unsigned int nm0, const unsigned int nm1,
1058 const unsigned int nm2, const unsigned int nq0, const unsigned int nq1,
1059 const unsigned int nq2, const bool isModified,
1060 const TData *NEK_RESTRICT basis0, const TData *NEK_RESTRICT basis1,
1061 const TData *NEK_RESTRICT basis2, const TData *NEK_RESTRICT in,
1062 TData *NEK_RESTRICT out, TData *NEK_RESTRICT wsp0, TData *NEK_RESTRICT wsp1)
1063{
1064 if constexpr (SHAPE_TYPE == LibUtilities::Hex)
1065 {
1066 BwdTransHexSumFacKernel<APPEND>(ilane, nm0, nm1, nm2, nq0, nq1, nq2,
1067 basis0, basis1, basis2, in, out, wsp0,
1068 wsp1);
1069 }
1070 else if constexpr (SHAPE_TYPE == LibUtilities::Tet ||
1071 SHAPE_TYPE == LibUtilities::NodalTet)
1072 {
1073 BwdTransTetSumFacKernel<APPEND>(ilane, nm0, nm1, nm2, nq0, nq1, nq2,
1074 isModified, basis0, basis1, basis2, in,
1075 out, wsp0, wsp1);
1076 }
1077 else if constexpr (SHAPE_TYPE == LibUtilities::Prism ||
1078 SHAPE_TYPE == LibUtilities::NodalPrism)
1079 {
1080 BwdTransPrismSumFacKernel<APPEND>(ilane, nm0, nm1, nm2, nq0, nq1, nq2,
1081 isModified, basis0, basis1, basis2,
1082 in, out, wsp0, wsp1);
1083 }
1084 else if constexpr (SHAPE_TYPE == LibUtilities::Pyr ||
1085 SHAPE_TYPE == LibUtilities::NodalPyr)
1086 {
1087 BwdTransPyrSumFacKernel<APPEND>(ilane, nm0, nm1, nm2, nq0, nq1, nq2,
1088 isModified, basis0, basis1, basis2, in,
1089 out, wsp0, wsp1);
1090 }
1091}
1092
1093/**
1094 * @brief Device entry point of the 1D backward transform under SumFac:
1095 * unpack the size parameter, then walk the block's elements, one per
1096 * thread, applying the segment kernel to each.
1097 *
1098 * Launched through DEVICE_2DGRID_KERNEL_LAUNCHER, which appends the last
1099 * two arguments and supplies grid, block, shared-memory size and stream
1100 * (see BwdTransBlockOpDeviceSumFac.hpp). The defaulted Enable template
1101 * parameter restricts this definition to the SumFac tag, so that the
1102 * SumFacTOP file can define an overload of the same name for its own
1103 * tag; the requirement of a 1D size parameter is enforced by the
1104 * static_assert in the body instead. The launch bound comes from
1105 * GetMaxThreadPerBlock, which for SumFac is the warp size the launcher
1106 * uses as block size. FETCH_SHARED_MEMORY binds @p shmemptr to the
1107 * block's dynamic shared memory on the CUDA/HIP back-ends, and is a
1108 * no-op where the launcher macro passes the pointer in; SumFac requests
1109 * none either way, so @p shmemptr goes unused once unpacked.
1110 *
1111 * The thread then starts at its index in the grid's first dimension and
1112 * advances by that dimension's thread count, so the block's elements --
1113 * padding included, their results being discarded -- are covered however
1114 * many threads were launched. Each visit derives the element's warp and
1115 * lane and offsets @p in and @p out to that warp's interleaved group.
1116 *
1117 * @tparam SHAPE_TYPE Seg.
1118 * @tparam Implementation MultiRegions::SumFac.
1119 * @tparam APPEND Accumulate onto @p out instead of
1120 * overwriting.
1121 * @tparam TSizeParameter1D 1D size parameter, in the runtime or the
1122 * compile-time form; with the latter the sizes
1123 * below are compile-time constants.
1124 * @tparam TthreadBlock Back-end thread-block handle type.
1125 * @tparam TUseSharedWsp Type of @p useSharedMemoryWsp: `bool`, or a
1126 * `std::bool_constant` SelectSharedWorkSpace() fixed.
1127 *
1128 * @param sizeParam1D Mode and quadrature-point counts of an element.
1129 * @param nelmt Elements in the block, padding included.
1130 * @param elmtsPerBlock Elements per thread block, one per thread;
1131 * unused.
1132 * @param isModified First-direction basis is eModified_A; the
1133 * segment kernel needs no correction, so unused.
1134 * @param useSharedMemoryWsp Stage intermediates in @p shmemptr; the segment
1135 * kernel keeps none, so unused.
1136 * @param basis0 Basis table in device memory.
1137 * @param nodToMod Nodal-to-modal matrix; no 1D nodal shape, so
1138 * unused.
1139 * @param in Block's coefficients for every component.
1140 * @param out Block's physical values for every component.
1141 * @param wsp Block's workspace; the segment kernel keeps no
1142 * intermediates, so unused.
1143 * @param shmemptr Shared-memory base, appended by the launcher;
1144 * unused by SumFac.
1145 * @param threadBlock Thread-block handle, appended by the launcher.
1146 */
1147template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
1148 bool APPEND, typename TSizeParameter1D, typename TthreadBlock,
1149 typename TData, typename TUseSharedWsp,
1150 std::enable_if_t<std::is_same_v<Implementation, SumFac>, bool>
1151 Enable = true>
1153 (GetMaxThreadPerBlock<Implementation, TSizeParameter1D>()))
1155 const TSizeParameter1D sizeParam1D, const size_t nelmt,
1156 [[maybe_unused]] const unsigned int elmtsPerBlock,
1157 [[maybe_unused]] const bool isModified,
1158 [[maybe_unused]] const TUseSharedWsp useSharedMemoryWsp,
1159 const TData *NEK_RESTRICT basis0,
1160 [[maybe_unused]] const TData *NEK_RESTRICT nodToMod,
1161 const TData *NEK_RESTRICT in, TData *NEK_RESTRICT out,
1162 [[maybe_unused]] TData *NEK_RESTRICT wsp,
1163 [[maybe_unused]] unsigned char *shmemptr,
1164 const TthreadBlock &threadBlock)
1165{
1166 static_assert(IsSizeParameter1D_v<TSizeParameter1D>,
1167 "Template argument must be either of type "
1168 "NonTemplatedSizeParameter1D or TemplatedSizeParameter1D.");
1169
1171
1172 const unsigned int nm0 = sizeParam1D.nm0();
1173 const unsigned int nq0 = sizeParam1D.nq0();
1174
1175 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
1176
1177 size_t e = getGlobalIdx<0>(threadBlock);
1178 const unsigned int c = getBlockIdx<1>(threadBlock);
1179 while (e < nelmt)
1180 {
1181 const size_t ilane = e % warpsize;
1182 const size_t iwarp = e / warpsize;
1183 const TData *inptr = in + nm0 * (nelmt * c + warpsize * iwarp);
1184 TData *outptr = out + nq0 * (nelmt * c + warpsize * iwarp);
1185 BwdTransSegSumFacKernel<APPEND>(ilane, nm0, nq0, basis0, inptr, outptr);
1186
1187 e += getGlobalRange<0>(threadBlock);
1188 }
1189}
1190
1191/**
1192 * @brief Device entry point of the 2D backward transform under SumFac:
1193 * unpack the size parameter, then walk the block's elements, one per
1194 * thread, applying the shape kernel selected by @p SHAPE_TYPE to each.
1195 *
1196 * Constrained, launched and bounded as the 1D entry point.
1197 *
1198 * Strides over the elements as the 1D entry point does, and
1199 * additionally slices @p wsp: within a stage's region, component `c`'s
1200 * values for warp `iwarp` start at
1201 * `stage size * (nelmt * c + warpSize * iwarp)`, and the regions of
1202 * successive stages are `stage size * nelmt * ncomp` apart, matching
1203 * BwdTransWorkSpaceSize times ncomp. With @p useSharedMemoryWsp the regions
1204 * are those of @p shmemptr for the thread block's single warp, so `c`
1205 * and `iwarp` drop out of the offsets; each lane uses only its own
1206 * column, so no barrier is needed. For NodalTri the element's
1207 * coefficients are converted to the modified modal basis by MatVecKernel
1208 * into the first region, and the triangular kernel then runs on the
1209 * converted values with the second region as its workspace.
1210 *
1211 * @tparam SHAPE_TYPE Quad, Tri or NodalTri.
1212 * @tparam Implementation MultiRegions::SumFac.
1213 * @tparam APPEND Accumulate onto @p out instead of
1214 * overwriting.
1215 * @tparam TSizeParameter2D 2D size parameter, runtime or compile-time
1216 * form.
1217 * @tparam TthreadBlock Back-end thread-block handle type.
1218 * @tparam TUseSharedWsp Type of @p useSharedMemoryWsp: `bool`, or a
1219 * `std::bool_constant` SelectSharedWorkSpace() fixed.
1220 *
1221 * @param sizeParam2D Mode and quadrature-point counts of an element.
1222 * @param nelmt Elements in the block, padding included.
1223 * @param elmtsPerBlock Elements per thread block, one per thread;
1224 * unused.
1225 * @param isModified First-direction basis is eModified_A.
1226 * @param useSharedMemoryWsp Stage intermediates in @p shmemptr rather than
1227 * in @p wsp.
1228 * @param basis0,basis1 Per-direction basis tables.
1229 * @param nodToMod Nodal-to-modal matrix; nodal shapes only.
1230 * @param in Block's coefficients for every component.
1231 * @param out Block's physical values for every component.
1232 * @param wsp Block's workspace, sized by
1233 * BwdTransWorkSpaceSize times ncomp; unused if
1234 * @p useSharedMemoryWsp.
1235 * @param shmemptr Shared-memory base, appended by the launcher;
1236 * sized by BwdTransSharedMemorySize; used only
1237 * if @p useSharedMemoryWsp.
1238 * @param threadBlock Thread-block handle, appended by the launcher.
1239 */
1240template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
1241 bool APPEND, typename TSizeParameter2D, typename TthreadBlock,
1242 typename TData, typename TUseSharedWsp,
1243 std::enable_if_t<std::is_same_v<Implementation, SumFac>, bool>
1244 Enable = true>
1246 (GetMaxThreadPerBlock<Implementation, TSizeParameter2D>()))
1248 const TSizeParameter2D sizeParam2D, const size_t nelmt,
1249 [[maybe_unused]] const unsigned int elmtsPerBlock,
1250 const bool isModified, const TUseSharedWsp useSharedMemoryWsp,
1251 const TData *NEK_RESTRICT basis0, const TData *NEK_RESTRICT basis1,
1252 const TData *NEK_RESTRICT nodToMod, const TData *NEK_RESTRICT in,
1253 TData *NEK_RESTRICT out, TData *NEK_RESTRICT wsp,
1254 [[maybe_unused]] unsigned char *shmemptr,
1255 const TthreadBlock &threadBlock)
1256{
1257 static_assert(IsSizeParameter2D_v<TSizeParameter2D>,
1258 "Template argument must be either of type "
1259 "NonTemplatedSizeParameter2D or TemplatedSizeParameter2D.");
1260
1262
1263 constexpr bool isNodal = SHAPE_TYPE == LibUtilities::NodalTri;
1264
1265 const unsigned int nm0 = sizeParam2D.nm0();
1266 const unsigned int nm1 = sizeParam2D.nm1();
1267 const unsigned int nmTot = sizeParam2D.nmTot();
1268 const unsigned int nq0 = sizeParam2D.nq0();
1269 const unsigned int nq1 = sizeParam2D.nq1();
1270 const unsigned int nqTot = sizeParam2D.nqTot();
1271
1272 unsigned int wsp0Size = 0;
1273 if constexpr (SHAPE_TYPE == LibUtilities::Quad)
1274 {
1275 wsp0Size = nm1;
1276 }
1277 else if constexpr (SHAPE_TYPE == LibUtilities::Tri ||
1278 SHAPE_TYPE == LibUtilities::NodalTri)
1279 {
1280 wsp0Size = nm0;
1281 }
1282
1283 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
1284
1285 size_t e = getGlobalIdx<0>(threadBlock);
1286 const unsigned int c = getBlockIdx<1>(threadBlock);
1287 const unsigned int ncomp = getBlockRange<1>(threadBlock);
1288
1289 DispatchWorkSpace(useSharedMemoryWsp, [&](const auto isShared) {
1290 // Workspace extent: the whole launch, or the thread block's warp.
1291 TData *wspbase = isShared ? (TData *)shmemptr : wsp;
1292 const size_t wspNelmt = isShared ? warpsize : nelmt * ncomp;
1293 while (e < nelmt)
1294 {
1295 const size_t ilane = e % warpsize;
1296 const size_t iwarp = e / warpsize;
1297 const TData *inptr = in + nmTot * (nelmt * c + warpsize * iwarp);
1298 TData *outptr = out + nqTot * (nelmt * c + warpsize * iwarp);
1299
1300 const size_t wspOffset =
1301 isShared ? 0 : nelmt * c + warpsize * iwarp;
1302 TData *modal = wspbase + nmTot * wspOffset;
1303 TData *wsp0 = wspbase + (isNodal ? nmTot : 0u) * wspNelmt +
1304 wsp0Size * wspOffset;
1305
1306 if constexpr (isNodal)
1307 {
1308 MatVecKernel(ilane, nmTot, nodToMod, inptr, modal);
1309 }
1310
1311 BwdTrans2DSumFacKernel<SHAPE_TYPE, APPEND>(
1312 ilane, nm0, nm1, nq0, nq1, isModified, basis0, basis1,
1313 isNodal ? modal : inptr, outptr, wsp0);
1314
1315 e += getGlobalRange<0>(threadBlock);
1316 }
1317 });
1318}
1319
1320/**
1321 * @brief Device entry point of the 3D backward transform under SumFac:
1322 * unpack the size parameter, then walk the block's elements, one per
1323 * thread, applying the shape kernel selected by @p SHAPE_TYPE to each.
1324 *
1325 * Constrained, launched and bounded as the 1D entry point. The two
1326 * mode-index tables are part of the shared signature but are used only
1327 * by the SumFacTOP tetrahedral kernel; the block implementation passes
1328 * null pointers here.
1329 *
1330 * As the 2D entry point, with two stage regions in @p wsp instead of one
1331 * and, for NodalTet and NodalPrism, a leading region of nmTot values per
1332 * element holding the coefficients MatVecKernel has converted to the
1333 * modified modal basis.
1334 *
1335 * @tparam SHAPE_TYPE Hex, Tet, NodalTet, Prism, NodalPrism, Pyr,
1336 * or NodalPyr.
1337 * @tparam Implementation MultiRegions::SumFac.
1338 * @tparam APPEND Accumulate onto @p out instead of
1339 * overwriting.
1340 * @tparam TSizeParameter3D 3D size parameter, runtime or compile-time
1341 * form.
1342 * @tparam TthreadBlock Back-end thread-block handle type.
1343 * @tparam TUseSharedWsp Type of @p useSharedMemoryWsp: `bool`, or a
1344 * `std::bool_constant` SelectSharedWorkSpace() fixed.
1345 *
1346 * @param sizeParam3D Mode and quadrature-point counts of an element.
1347 * @param nelmt Elements in the block, padding included.
1348 * @param elmtsPerBlock Elements per thread block, one per thread;
1349 * unused.
1350 * @param isModified First-direction basis is eModified_A.
1351 * @param useSharedMemoryWsp Stage intermediates in @p shmemptr rather than
1352 * in @p wsp.
1353 * @param index0,index1 Mode-index tables; unused here.
1354 * @param basis0,basis1,basis2 Per-direction basis tables.
1355 * @param nodToMod Nodal-to-modal matrix; nodal shapes only.
1356 * @param in Block's coefficients for every component.
1357 * @param out Block's physical values for every component.
1358 * @param wsp Block's workspace, sized by
1359 * BwdTransWorkSpaceSize times ncomp; unused if
1360 * @p useSharedMemoryWsp.
1361 * @param shmemptr Shared-memory base, appended by the launcher;
1362 * sized by BwdTransSharedMemorySize; used only
1363 * if @p useSharedMemoryWsp.
1364 * @param threadBlock Thread-block handle, appended by the launcher.
1365 */
1366template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
1367 bool APPEND, typename TSizeParameter3D, typename TthreadBlock,
1368 typename TData, typename TUseSharedWsp,
1369 std::enable_if_t<std::is_same_v<Implementation, SumFac>, bool>
1370 Enable = true>
1372 (GetMaxThreadPerBlock<Implementation, TSizeParameter3D>()))
1374 const TSizeParameter3D sizeParam3D, const size_t nelmt,
1375 [[maybe_unused]] const unsigned int elmtsPerBlock,
1376 const bool isModified, const TUseSharedWsp useSharedMemoryWsp,
1377 [[maybe_unused]] const unsigned int *index0,
1378 [[maybe_unused]] const unsigned int *index1,
1379 const TData *NEK_RESTRICT basis0, const TData *NEK_RESTRICT basis1,
1380 const TData *NEK_RESTRICT basis2, const TData *NEK_RESTRICT nodToMod,
1381 const TData *NEK_RESTRICT in, TData *NEK_RESTRICT out,
1382 TData *NEK_RESTRICT wsp, [[maybe_unused]] unsigned char *shmemptr,
1383 const TthreadBlock &threadBlock)
1384{
1385 static_assert(IsSizeParameter3D_v<TSizeParameter3D>,
1386 "Template argument must be either of type "
1387 "NonTemplatedSizeParameter3D or TemplatedSizeParameter3D.");
1388
1390
1391 constexpr bool isNodal = SHAPE_TYPE == LibUtilities::NodalTet ||
1392 SHAPE_TYPE == LibUtilities::NodalPrism ||
1393 SHAPE_TYPE == LibUtilities::NodalPyr;
1394
1395 const unsigned int nm0 = sizeParam3D.nm0();
1396 const unsigned int nm1 = sizeParam3D.nm1();
1397 const unsigned int nm2 = sizeParam3D.nm2();
1398 const unsigned int nmTot = sizeParam3D.nmTot();
1399 const unsigned int nq0 = sizeParam3D.nq0();
1400 const unsigned int nq1 = sizeParam3D.nq1();
1401 const unsigned int nq2 = sizeParam3D.nq2();
1402 const unsigned int nqTot = sizeParam3D.nqTot();
1403
1404 unsigned int wsp0Size = 0, wsp1Size = 0;
1405 if constexpr (SHAPE_TYPE == LibUtilities::Hex)
1406 {
1407 wsp0Size = nm1 * nm2;
1408 wsp1Size = nm2;
1409 }
1410 else if constexpr (SHAPE_TYPE == LibUtilities::Tet ||
1411 SHAPE_TYPE == LibUtilities::NodalTet)
1412 {
1413 wsp0Size = (2u * nm1 - nm0 + 1u) * nm0 / 2u;
1414 wsp1Size = nm0;
1415 }
1416 else if constexpr (SHAPE_TYPE == LibUtilities::Prism ||
1417 SHAPE_TYPE == LibUtilities::NodalPrism ||
1418 SHAPE_TYPE == LibUtilities::Pyr ||
1419 SHAPE_TYPE == LibUtilities::NodalPyr)
1420 {
1421 wsp0Size = nm0 * nm1;
1422 wsp1Size = nm0;
1423 }
1424
1425 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
1426
1427 size_t e = getGlobalIdx<0>(threadBlock); // use size_t to prevent overflow
1428 const unsigned int c = getBlockIdx<1>(threadBlock);
1429 const unsigned int ncomp = getBlockRange<1>(threadBlock);
1430
1431 DispatchWorkSpace(useSharedMemoryWsp, [&](const auto isShared) {
1432 // Workspace extent: the whole launch, or the thread block's warp.
1433 TData *wspbase = isShared ? (TData *)shmemptr : wsp;
1434 const size_t wspNelmt = isShared ? warpsize : nelmt * ncomp;
1435 while (e < nelmt)
1436 {
1437 const size_t ilane = e % warpsize;
1438 const size_t iwarp = e / warpsize;
1439 const TData *inptr = in + nmTot * (nelmt * c + warpsize * iwarp);
1440 TData *outptr = out + nqTot * (nelmt * c + warpsize * iwarp);
1441
1442 const size_t wspOffset =
1443 isShared ? 0 : nelmt * c + warpsize * iwarp;
1444 TData *modal = wspbase + nmTot * wspOffset;
1445 TData *wsp0 = wspbase + (isNodal ? nmTot : 0u) * wspNelmt +
1446 wsp0Size * wspOffset;
1447 TData *wsp1 = wspbase +
1448 ((isNodal ? nmTot : 0u) + wsp0Size) * wspNelmt +
1449 wsp1Size * wspOffset;
1450
1451 if constexpr (isNodal)
1452 {
1453 MatVecKernel(ilane, nmTot, nodToMod, inptr, modal);
1454 }
1455
1456 BwdTrans3DSumFacKernel<SHAPE_TYPE, APPEND>(
1457 ilane, nm0, nm1, nm2, nq0, nq1, nq2, isModified, basis0, basis1,
1458 basis2, isNodal ? modal : inptr, outptr, wsp0, wsp1);
1459
1460 e += getGlobalRange<0>(threadBlock);
1461 }
1462 });
1463}
1464
1465#endif
1466
1467} // 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 __LAUNCH_BOUNDS__((GetMaxThreadPerBlock< Implementation, TTraceSizeParameter1D >())) IProductWRTPhysNormalDerivTraceKernelLauncher(const TTraceSizeParameter1D sizeParam1D
The one-dimensional lift: over a segment's two vertices, one lane per element.
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
static NEK_FORCE_INLINE void BwdTransKernelLauncher(const TSizeParameter1D sizeParam1D, const bool isModified, const typename simd_type::scalarType *basis0, const simd_type *NtoM, const simd_type *in, simd_type *out)
1D dispatch: forward one element group's transform to the segment kernel.
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
DispatchWorkSpace(useSharedMemoryWsp, [&](const auto isShared) { TData *wspbase=isShared ?(TData *) shmemptr+tableSize :wsp;while(e< nelmt) { const size_t ilane=e % warpsize;const size_t iwarp=e/warpsize;const TData *inptr=in+numDataIn *(nelmt *c+warpsize *iwarp);TData *outptr=out+numDataOut *(nelmt *c+warpsize *iwarp);const size_t wspOffset=isShared ? 0 :nelmt *c+warpsize *iwarp;TData *wspptr=wspbase+(edgeSize+numDataOut) *wspOffset;TData *volptr=wspptr+edgeSize *warpsize;const unsigned int off1=warpsize *2 *tnq00;const unsigned int joff1=(DEFORMED) ? off1 :warpsize *2;for(unsigned int d=0;d< 2;++d) { const bool useD1=CompNeedsCollapsedFac(SHAPE_TYPE, d, 1);const TData *jacptr=jac+d *jacCompStride+numDataJac *warpsize *iwarp;if(!(append||d > 0)) { for(unsigned int p=0;p< numDataOut;++p) { outptr[warpsize *p+ilane]=0.0;} } { TData *dst=useD1 ? volptr :outptr;if(useD1) { for(unsigned int p=0;p< numDataOut;++p) { dst[warpsize *p+ilane]=0.0;} } const TData *nb=(0==d) ? dnbasis0 :nbasis0;const TData *tb=(traceDir0==d) ? dtbasis0 :tbasis0;const bool coll=(traceDir0==d) ? false :isColl0;const bool endp=(0==d) ? false :endPtsColl0;if(endp) { IPWRTPhysTraceEdgeN0Kernel< true, DEFORMED >(ilane, 0, 2, 2, nm0, nm1, nb, tnq00, tb, tw0, jacptr, wspptr, inptr, dst, coll);} else { IPWRTPhysTraceEdgeN0Kernel< false, DEFORMED >(ilane, 0, 2, 2, nm0, nm1, nb, tnq00, tb, tw0, jacptr, wspptr, inptr, dst, coll);} if(useD1) { ScaleAddCollapsedFacKernel(ilane, nm0, nm1, 1u, true, false, s_twoOver1, s_twoOver1, volptr, outptr);} } { const TData *nb=(1==d) ? dnbasis1 :nbasis1;const TData *tb=(traceDir1==d) ? dtbasis1 :tbasis1;const bool coll=(traceDir1==d) ? false :isColl1;const bool endp=(1==d) ? false :endPtsColl1;if(endp) { IPWRTPhysTraceEdgeN1Kernel< true, DEFORMED >(ilane, 0, nedgeN1, nedgeN1, nm0, nm1, nb, tnq10, tb, tw1, jacptr+joff1, wspptr, inptr+off1, outptr, coll);} else { IPWRTPhysTraceEdgeN1Kernel< false, DEFORMED >(ilane, 0, nedgeN1, nedgeN1, nm0, nm1, nb, tnq10, tb, tw1, jacptr+joff1, wspptr, inptr+off1, outptr, coll);} } } e+=getGlobalRange< 0 >(threadBlock);} })
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 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 wsp
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 NEK_FORCE_INLINE void MatVecKernel(const unsigned int n, const simd_type *Mat, const simd_type *in, simd_type *out)
static constexpr unsigned int warpSize
Definition Backends.hpp:143