Nektar++
Loading...
Searching...
No Matches
MassBlockOpDeviceSumFacKernels.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: MassBlockOpDeviceSumFacKernels.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 mass operator.
32///////////////////////////////////////////////////////////////////////////////
33
34/**
35 * @file MassBlockOpDeviceSumFacKernels.hpp
36 * @brief Device SumFac kernels of the mass operator: one element per
37 * thread, the two composed families' shape kernels run back to back, with
38 * the intermediates in a global-memory workspace or, when it fits, in
39 * shared memory.
40 *
41 * @details
42 * These are the kernels the Device/SumFac block implementation launches
43 * (see MassBlockOpDeviceSumFac.hpp); the SumFacTOP counterparts, where a whole
44 * thread block cooperates on one element, are in
45 * MassBlockOpDeviceSumFacTOPKernels.hpp. Everything below is compiled only into
46 * device translation units of a device-enabled build (NEKTAR_ENABLE_DEVICE
47 * and DEVICE_COMPILE_ONLY).
48 *
49 * The MassKernelLauncher overloads own no mathematics of their own. Each
50 * grid-strides over the block's elements, and for its element calls the
51 * BwdTrans shape kernel of BwdTransBlockOpDeviceSumFacKernels.hpp followed by
52 * the IProductWRTBase shape kernel of
53 * IProductWRTBaseBlockOpDeviceSumFacKernels.hpp, the transform instantiated
54 * with APPEND false and the inner product with the launcher's SCALE and
55 * APPEND and given its scale; the transform kernels have no SCALE parameter
56 * and take no scale argument. The quadrature weights and the Jacobian therefore
57 * enter exactly where they do in a standalone inner product: inside the
58 * IProductWRTBase shape kernel, which receives the per-direction weight tables
59 * and the warp's Jacobian slice and whose DEFORMED parameter decides whether
60 * that slice holds one value per element or one per quadrature point. For the
61 * nodal shapes the element's coefficients are mapped to the modal basis by
62 * MatVecKernel before the transform and mapped back by the same kernel with
63 * TRANSPOSE set afterwards; there the inner product overwrites the modal slice
64 * and APPEND governs the mapping back.
65 *
66 * Element data is interleaved at the warp size: lane e % warpsize of warp
67 * e / warpsize owns element e, and every workspace slice is addressed by
68 * warp, so a shape kernel writes value k of its element at offset
69 * warpsize * k + ilane. One launch covers every component of the block:
70 * the grid's second dimension is the component count, a thread's own
71 * component c comes from getBlockIdx<1>, and in, out and every workspace
72 * region span all components back to back, a component's share starting at
73 * element index nelmt * c, ahead of the warp interleaving above. With
74 * useSharedMemoryWsp the workspace regions are instead those of the thread
75 * block's single warp in shared memory, laid out as MassWorkSpaceSize gives for
76 * warpSize elements.
77 *
78 * This file also provides the SumFac overloads of the two launch-size
79 * queries the block implementation calls, MassWorkSpaceSize and
80 * MassSharedMemorySize; the SumFacTOP overloads of the same names live in
81 * MassBlockOpDeviceSumFacTOPKernels.hpp.
82 */
83
84#pragma once
85
89
92
94{
95
96#if defined(NEKTAR_ENABLE_DEVICE) && defined(DEVICE_COMPILE_ONLY)
97/**
98 * @brief Workspace a 1D SumFac mass operator needs, in TData
99 * values.
100 *
101 * Per element the nq0 physical values handed from the transform to the
102 * inner product; the segment kernels need no further scratch.
103 *
104 * @tparam SHAPE_TYPE Shape of the block's elements (Seg).
105 * @tparam Implementation MultiRegions::SumFac; the SumFacTOP overload lives
106 * in MassBlockOpDeviceSumFacTOPKernels.hpp.
107 * @tparam TSizeParameter1D 1D size parameter (see ElmtHelper.hpp).
108 *
109 * @param nelmt Elements in the block, padding included.
110 * @param sizeParam1D Modal and quadrature sizes of the expansion.
111 *
112 * @return Workspace size in TData values per component, 0 for unhandled
113 * shapes.
114 */
115template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
116 typename TSizeParameter1D,
117 std::enable_if_t<std::is_same_v<Implementation, SumFac> &&
118 IsSizeParameter1D_v<TSizeParameter1D>,
119 bool>
120 Enable = true>
121inline constexpr size_t MassWorkSpaceSize(const size_t nelmt,
122 const TSizeParameter1D sizeParam1D)
123{
124 size_t wspsize = 0;
125
126 const unsigned int nq0 = sizeParam1D.nq0();
127
128 if constexpr (SHAPE_TYPE == LibUtilities::Seg)
129 {
130 wspsize = nq0 * nelmt;
131 }
132
133 return wspsize;
134}
135
136/**
137 * @brief Workspace a 2D SumFac mass operator needs, in TData
138 * values.
139 *
140 * Per element the nq0 * nq1 physical values passed between the two stages,
141 * then one scratch area the two stages share, sized for whichever needs
142 * more: nq1 values for the quadrilateral (nq1 for the inner product, nm1
143 * for the transform, and nq1 >= nm1 for a standard quadrature),
144 * max(nq1, nm0) for the triangles. The nodal triangle adds its modal
145 * coefficient count nmTot for the converted coefficients. Each region is
146 * laid out warp-interleaved like the field data and scaled by the
147 * component count by the caller (see the file notes).
148 *
149 * @tparam SHAPE_TYPE Shape of the block's elements.
150 * @tparam Implementation MultiRegions::SumFac.
151 * @tparam TSizeParameter2D 2D size parameter (see ElmtHelper.hpp).
152 *
153 * @param nelmt Elements in the block, padding included.
154 * @param sizeParam2D Modal and quadrature sizes per direction.
155 *
156 * @return Workspace size in TData values per component, 0 for unhandled
157 * shapes.
158 */
159template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
160 typename TSizeParameter2D,
161 std::enable_if_t<std::is_same_v<Implementation, SumFac> &&
162 IsSizeParameter2D_v<TSizeParameter2D>,
163 bool>
164 Enable = true>
165inline constexpr size_t MassWorkSpaceSize(const size_t nelmt,
166 const TSizeParameter2D sizeParam2D)
167{
168 size_t wspsize = 0;
169
170 const unsigned int nm0 = sizeParam2D.nm0();
171 const unsigned int nq1 = sizeParam2D.nq1();
172
173 if constexpr (SHAPE_TYPE == LibUtilities::Quad)
174 {
175 wspsize = (sizeParam2D.nqTot() + nq1) * nelmt;
176 }
177 else if constexpr (SHAPE_TYPE == LibUtilities::Tri)
178 {
179 wspsize = (sizeParam2D.nqTot() + std::max(nq1, nm0)) * nelmt;
180 }
181 else if constexpr (SHAPE_TYPE == LibUtilities::NodalTri)
182 {
183 const unsigned int nmTot = sizeParam2D.nmTot();
184
185 wspsize = (sizeParam2D.nqTot() + std::max(nq1, nm0) + nmTot) * nelmt;
186 }
187
188 return wspsize;
189}
190
191/**
192 * @brief Workspace a 3D SumFac mass operator needs, in TData
193 * values.
194 *
195 * Per element the nq0 * nq1 * nq2 physical values passed between the two
196 * stages, then the two scratch areas the two stages share. For the
197 * collapsed shapes each is sized to the larger of the two stages' demands
198 * -- the inner product's nq1 * nq2 and nq2, the transform's
199 * (2 nm1 - nm0 + 1) nm0 / 2 mode pairs and nm0 for the tetrahedra,
200 * nm0 * nm1 and nm0 for the prisms and the pyramid -- while the
201 * hexahedron's entry takes the inner product's nq1 * nq2 and nq2 directly,
202 * which covers the transform's nm1 * nm2 and nm2 whenever each direction
203 * has at least as many quadrature points as modes. The nodal shapes add
204 * their modal coefficient count for the converted coefficients.
205 *
206 * @tparam SHAPE_TYPE Shape of the block's elements.
207 * @tparam Implementation MultiRegions::SumFac.
208 * @tparam TSizeParameter3D 3D size parameter (see ElmtHelper.hpp).
209 *
210 * @param nelmt Elements in the block, padding included.
211 * @param sizeParam3D Modal and quadrature sizes per direction.
212 *
213 * @return Workspace size in TData values per component, 0 for unhandled
214 * shapes.
215 */
216template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
217 typename TSizeParameter3D,
218 std::enable_if_t<std::is_same_v<Implementation, SumFac> &&
219 IsSizeParameter3D_v<TSizeParameter3D>,
220 bool>
221 Enable = true>
222inline constexpr size_t MassWorkSpaceSize(const size_t nelmt,
223 const TSizeParameter3D sizeParam3D)
224{
225 size_t wspsize = 0;
226
227 const unsigned int nm0 = sizeParam3D.nm0();
228 const unsigned int nm1 = sizeParam3D.nm1();
229 const unsigned int nq1 = sizeParam3D.nq1();
230 const unsigned int nq2 = sizeParam3D.nq2();
231
232 if constexpr (SHAPE_TYPE == LibUtilities::Hex)
233 {
234 wspsize = (sizeParam3D.nqTot() + nq1 * nq2 + nq2) * nelmt;
235 }
236 else if constexpr (SHAPE_TYPE == LibUtilities::Tet)
237 {
238 const unsigned int nm01 = (2u * nm1 - nm0 + 1u) * nm0 / 2u;
239
240 wspsize = (sizeParam3D.nqTot() + std::max(nq1 * nq2, nm01) +
241 std::max(nq2, nm0)) *
242 nelmt;
243 }
244 else if constexpr (SHAPE_TYPE == LibUtilities::NodalTet)
245 {
246 const unsigned int nmTot = sizeParam3D.nmTot();
247 const unsigned int nm01 = (2u * nm1 - nm0 + 1u) * nm0 / 2u;
248
249 wspsize = (sizeParam3D.nqTot() + std::max(nq1 * nq2, nm01) +
250 std::max(nq2, nm0) + nmTot) *
251 nelmt;
252 }
253 else if constexpr (SHAPE_TYPE == LibUtilities::Prism)
254 {
255 wspsize = (sizeParam3D.nqTot() + std::max(nq1 * nq2, nm0 * nm1) +
256 std::max(nq2, nm0)) *
257 nelmt;
258 }
259 else if constexpr (SHAPE_TYPE == LibUtilities::NodalPrism)
260 {
261 const unsigned int nmTot = sizeParam3D.nmTot();
262
263 wspsize = (sizeParam3D.nqTot() + std::max(nq1 * nq2, nm0 * nm1) +
264 std::max(nq2, nm0) + nmTot) *
265 nelmt;
266 }
267 else if constexpr (SHAPE_TYPE == LibUtilities::Pyr)
268 {
269 wspsize = (sizeParam3D.nqTot() + std::max(nq1 * nq2, nm0 * nm1) +
270 std::max(nq2, nm0)) *
271 nelmt;
272 }
273 else if constexpr (SHAPE_TYPE == LibUtilities::NodalPyr)
274 {
275 const unsigned int nmTot = sizeParam3D.nmTot();
276
277 wspsize = (sizeParam3D.nqTot() + std::max(nq1 * nq2, nm0 * nm1) +
278 std::max(nq2, nm0) + nmTot) *
279 nelmt;
280 }
281
282 return wspsize;
283}
284
285/**
286 * @brief Dynamic shared memory a 1D SumFac launch needs, in TData values.
287 *
288 * The SumFac kernels read the basis and weight tables straight from global
289 * memory, so the only thing held per thread block is, with
290 * @p useSharedMemoryWsp, the workspace of its @p elmtsPerBlock elements, laid
291 * out as MassWorkSpaceSize gives for that many elements. The SumFacTOP
292 * overloads of the same name (MassBlockOpDeviceSumFacTOPKernels.hpp) return the
293 * size of the staging area that strategy uses.
294 *
295 * @tparam SHAPE_TYPE Shape of the block's elements (Seg).
296 * @tparam Implementation MultiRegions::SumFac.
297 * @tparam TData Floating-point type of the field data; unused.
298 * @tparam TSizeParameter1D 1D size parameter (see ElmtHelper.hpp).
299 *
300 * @param sizeParam1D Modal and quadrature sizes of the expansion.
301 * @param elmtsPerBlock Elements per thread block, from
302 * GetElmtsPerBlock().
303 * @param useSharedMemoryWsp Stage intermediates in shared memory.
304 *
305 * @return Number of TData values of shared memory per thread block.
306 */
307template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
308 typename TData, typename TSizeParameter1D,
309 std::enable_if_t<std::is_same_v<Implementation, SumFac> &&
310 IsSizeParameter1D_v<TSizeParameter1D>,
311 bool>
312 Enable = true>
313inline constexpr unsigned int MassSharedMemorySize(
314 const TSizeParameter1D sizeParam1D, const unsigned int elmtsPerBlock,
315 const bool useSharedMemoryWsp)
316{
317 return useSharedMemoryWsp
318 ? static_cast<unsigned int>(
319 MassWorkSpaceSize<SHAPE_TYPE, Implementation>(
320 elmtsPerBlock, sizeParam1D))
321 : 0u;
322}
323
324/// @brief Dynamic shared memory a 2D SumFac launch needs, as the 1D
325/// overload.
326template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
327 typename TData, typename TSizeParameter2D,
328 std::enable_if_t<std::is_same_v<Implementation, SumFac> &&
329 IsSizeParameter2D_v<TSizeParameter2D>,
330 bool>
331 Enable = true>
332inline constexpr unsigned int MassSharedMemorySize(
333 const TSizeParameter2D sizeParam2D, const unsigned int elmtsPerBlock,
334 const bool useSharedMemoryWsp)
335{
336 return useSharedMemoryWsp
337 ? static_cast<unsigned int>(
338 MassWorkSpaceSize<SHAPE_TYPE, Implementation>(
339 elmtsPerBlock, sizeParam2D))
340 : 0u;
341}
342
343/// @brief Dynamic shared memory a 3D SumFac launch needs, as the 1D
344/// overload.
345template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
346 typename TData, typename TSizeParameter3D,
347 std::enable_if_t<std::is_same_v<Implementation, SumFac> &&
348 IsSizeParameter3D_v<TSizeParameter3D>,
349 bool>
350 Enable = true>
351inline constexpr unsigned int MassSharedMemorySize(
352 const TSizeParameter3D sizeParam3D, const unsigned int elmtsPerBlock,
353 const bool useSharedMemoryWsp)
354{
355 return useSharedMemoryWsp
356 ? static_cast<unsigned int>(
357 MassWorkSpaceSize<SHAPE_TYPE, Implementation>(
358 elmtsPerBlock, sizeParam3D))
359 : 0u;
360}
361
362/**
363 * @brief Device entry point of the 1D SumFac mass operator: unpacks the
364 * size parameter and, grid-striding over the block's elements, transforms
365 * each thread's segment into its warp's workspace slice and integrates it
366 * straight back out, the weights and the Jacobian being applied inside the
367 * inner-product shape kernel.
368 *
369 * Enabled only for the SumFac tag, so the SumFacTOP overload of the same
370 * name (MassBlockOpDeviceSumFacTOPKernels.hpp) never competes for the call; a
371 * size parameter that is not 1D is rejected by the static_assert rather than by
372 * overload resolution. Both the runtime and the compile-time
373 * size-parameter forms are accepted; for this tag the launch bounds are
374 * the warp size either way (see GetMaxThreadPerBlock).
375 *
376 * @tparam SHAPE_TYPE Seg.
377 * @tparam Implementation MultiRegions::SumFac.
378 * @tparam SCALE Multiply the result by @p scale.
379 * @tparam APPEND Accumulate onto @p out instead of overwriting.
380 * @tparam DEFORMED @p jac holds one interleaved value per
381 * quadrature point rather than a single
382 * per-element value.
383 * @tparam TSizeParameter1D 1D size parameter, runtime or compile-time
384 * form.
385 * @tparam TthreadBlock Back-end thread-block handle type.
386 * @tparam TData Floating-point type of the field data.
387 * @tparam TUseSharedWsp Type of @p useSharedMemoryWsp: `bool`, or a
388 * `std::bool_constant` SelectSharedWorkSpace() fixed.
389 *
390 * @param sizeParam1D Mode and quadrature-point counts of an element.
391 * @param nelmt Elements in the block, padding included.
392 * @param elmtsPerBlock Elements per thread block, one per thread;
393 * unused.
394 * @param isModified First-direction basis is eModified_A; unused in 1D.
395 * @param useSharedMemoryWsp Stage intermediates in @p shmemptr rather than
396 * in
397 * @p wsp.
398 * @param basis0 Basis table (nm0 rows of nq0).
399 * @param w0 Quadrature weights.
400 * @param nodToMod Nodal-to-modal matrix; unused in 1D.
401 * @param jac Block's Jacobians, warp-interleaved.
402 * @param in Block's coefficients, every component back to
403 * back; this thread's own component is c.
404 * @param out Block's coefficients, every component back to
405 * back; this thread's own component is c,
406 * overwritten, or accumulated into when APPEND.
407 * @param wsp Global workspace, MassWorkSpaceSize values per
408 * component, back to back; unused if @p
409 * useSharedMemoryWsp.
410 * @param scale Factor applied when @p SCALE is set.
411 * @param shmemptr Dynamic shared-memory base, sized by
412 * MassSharedMemorySize; used only if @p
413 * useSharedMemoryWsp.
414 * @param threadBlock Thread-block handle the index helpers read.
415 */
416template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
417 bool SCALE, bool APPEND, bool DEFORMED, typename TSizeParameter1D,
418 typename TthreadBlock, typename TData, typename TUseSharedWsp,
419 std::enable_if_t<std::is_same_v<Implementation, SumFac>, bool>
420 Enable = true>
422 (GetMaxThreadPerBlock<Implementation, TSizeParameter1D>()))
423 MassKernelLauncher(const TSizeParameter1D sizeParam1D, const size_t nelmt,
424 [[maybe_unused]] const unsigned int elmtsPerBlock,
425 [[maybe_unused]] const bool isModified,
426 const TUseSharedWsp useSharedMemoryWsp,
427 const TData *NEK_RESTRICT basis0,
428 const TData *NEK_RESTRICT w0,
429 [[maybe_unused]] const TData *NEK_RESTRICT nodToMod,
430 const TData *NEK_RESTRICT jac,
431 const TData *NEK_RESTRICT in, TData *NEK_RESTRICT out,
432 TData *NEK_RESTRICT wsp, const TData scale,
433 unsigned char *shmemptr, const TthreadBlock &threadBlock)
434{
435 static_assert(IsSizeParameter1D_v<TSizeParameter1D>,
436 "Template argument must be either of type "
437 "NonTemplatedSizeParameter1D or TemplatedSizeParameter1D.");
438
440
441 const unsigned int nm0 = sizeParam1D.nm0();
442 const unsigned int nq0 = sizeParam1D.nq0();
443
444 const unsigned int jacsize = DEFORMED ? nq0 : 1u;
445
446 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
447
448 size_t e = getGlobalIdx<0>(threadBlock);
449 const unsigned int c = getBlockIdx<1>(threadBlock);
450
451 DispatchWorkSpace(useSharedMemoryWsp, [&](const auto isShared) {
452 // Workspace extent: the whole launch, or the thread block's warp.
453 TData *wspbase = isShared ? (TData *)shmemptr : wsp;
454 while (e < nelmt)
455 {
456 const size_t ilane = e % warpsize;
457 const size_t iwarp = e / warpsize;
458 const TData *jacptr =
459 DEFORMED ? jac + jacsize * warpsize * iwarp : jac + e;
460 const TData *inptr = in + nm0 * (nelmt * c + warpsize * iwarp);
461 TData *outptr = out + nm0 * (nelmt * c + warpsize * iwarp);
462
463 const size_t wspOffset =
464 isShared ? 0 : nelmt * c + warpsize * iwarp;
465 TData *wspptr = wspbase + nq0 * wspOffset;
466 BwdTransSegSumFacKernel<false>(ilane, nm0, nq0, basis0, inptr,
467 wspptr);
468 IProductWRTBaseSegSumFacKernel<SCALE, APPEND, DEFORMED>(
469 ilane, nm0, nq0, basis0, w0, jacptr, wspptr, outptr, scale);
470
471 e += getGlobalRange<0>(threadBlock);
472 }
473 });
474}
475
476/**
477 * @brief Device entry point of the 2D SumFac mass operator: unpacks the
478 * size parameter and, grid-striding over the block's elements, transforms
479 * and integrates one quadrilateral or triangle per thread.
480 *
481 * The workspace is sliced as MassWorkSpaceSize describes it, each region
482 * spanning nelmt * ncomp elements (see the file notes), or with
483 * @p useSharedMemoryWsp only the thread block's warp: bwd takes the leading
484 * nqTot values per element of that extent and holds the physical values between
485 * the stages, and the scratch area behind it is passed to both shape kernels in
486 * turn. For a nodal triangle the coefficients are first mapped to the modal
487 * basis into a further slice, and the inner product writes back into that slice
488 * so that the transposed mapping can produce the nodal output.
489 *
490 * @tparam SHAPE_TYPE Quad, Tri or NodalTri.
491 * @tparam Implementation MultiRegions::SumFac.
492 * @tparam SCALE Multiply the result by @p scale.
493 * @tparam APPEND Accumulate onto @p out instead of overwriting.
494 * @tparam DEFORMED @p jac holds one interleaved value per
495 * quadrature point rather than a single
496 * per-element value.
497 * @tparam TSizeParameter2D 2D size parameter, runtime or compile-time
498 * form.
499 * @tparam TthreadBlock Back-end thread-block handle type.
500 * @tparam TData Floating-point type of the field data.
501 * @tparam TUseSharedWsp Type of @p useSharedMemoryWsp: `bool`, or a
502 * `std::bool_constant` SelectSharedWorkSpace() fixed.
503 *
504 * @param sizeParam2D Mode and quadrature-point counts of an element.
505 * @param nelmt Elements in the block, padding included.
506 * @param elmtsPerBlock Elements per thread block, one per thread;
507 * unused.
508 * @param isModified First-direction basis is eModified_A.
509 * @param useSharedMemoryWsp Stage intermediates in @p shmemptr rather than
510 * in
511 * @p wsp.
512 * @param index0 Mode-index table; unused by this strategy and
513 * passed null, the parameter being present so that
514 * both strategies' launchers take the same argument
515 * list.
516 * @param basis0 Direction-0 basis table.
517 * @param basis1 Direction-1 basis table; for the triangles indexed
518 * by the combined (p,q) mode.
519 * @param w0,w1 Per-direction quadrature weights.
520 * @param nodToMod Nodal-to-modal matrix (nodal shapes only).
521 * @param jac Block's Jacobians, warp-interleaved.
522 * @param in Block's coefficients, every component back to
523 * back; this thread's own component is c.
524 * @param out Block's coefficients, every component back to
525 * back; this thread's own component is c,
526 * overwritten, or accumulated into when APPEND.
527 * @param wsp Global workspace: MassWorkSpaceSize's regions, each
528 * scaled by the component count (see above); unused
529 * if @p useSharedMemoryWsp.
530 * @param scale Factor applied when @p SCALE is set.
531 * @param shmemptr Dynamic shared-memory base, sized by
532 * MassSharedMemorySize; used only if @p
533 * useSharedMemoryWsp.
534 * @param threadBlock Thread-block handle the index helpers read.
535 */
536template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
537 bool SCALE, bool APPEND, bool DEFORMED, typename TSizeParameter2D,
538 typename TthreadBlock, typename TData, typename TUseSharedWsp,
539 std::enable_if_t<std::is_same_v<Implementation, SumFac>, bool>
540 Enable = true>
542 (GetMaxThreadPerBlock<Implementation, TSizeParameter2D>()))
544 const TSizeParameter2D sizeParam2D, const size_t nelmt,
545 [[maybe_unused]] const unsigned int elmtsPerBlock,
546 const bool isModified, const TUseSharedWsp useSharedMemoryWsp,
547 [[maybe_unused]] const unsigned int *NEK_RESTRICT index0,
548 const TData *NEK_RESTRICT basis0, const TData *NEK_RESTRICT basis1,
549 const TData *NEK_RESTRICT w0, const TData *NEK_RESTRICT w1,
550 const TData *NEK_RESTRICT nodToMod, const TData *NEK_RESTRICT jac,
551 const TData *NEK_RESTRICT in, TData *NEK_RESTRICT out,
552 TData *NEK_RESTRICT wsp, const TData scale, unsigned char *shmemptr,
553 const TthreadBlock &threadBlock)
554{
555 static_assert(IsSizeParameter2D_v<TSizeParameter2D>,
556 "Template argument must be either of type "
557 "NonTemplatedSizeParameter2D or TemplatedSizeParameter2D.");
558
560
561 constexpr bool isNodal = SHAPE_TYPE == LibUtilities::NodalTri;
562
563 const unsigned int nm0 = sizeParam2D.nm0();
564 const unsigned int nm1 = sizeParam2D.nm1();
565 const unsigned int nmTot = sizeParam2D.nmTot();
566 const unsigned int nq0 = sizeParam2D.nq0();
567 const unsigned int nq1 = sizeParam2D.nq1();
568 const unsigned int nqTot = sizeParam2D.nqTot();
569
570 const unsigned int jacsize = DEFORMED ? nqTot : 1u;
571
572 unsigned int wsp0Size = 0;
573 if constexpr (SHAPE_TYPE == LibUtilities::Quad)
574 {
575 wsp0Size = nq1;
576 }
577 else if constexpr (SHAPE_TYPE == LibUtilities::Tri ||
578 SHAPE_TYPE == LibUtilities::NodalTri)
579 {
580 wsp0Size = std::max(nq1, nm0);
581 }
582
583 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
584
585 size_t e = getGlobalIdx<0>(threadBlock);
586 const unsigned int c = getBlockIdx<1>(threadBlock);
587 const unsigned int ncomp = getBlockRange<1>(threadBlock);
588
589 DispatchWorkSpace(useSharedMemoryWsp, [&](const auto isShared) {
590 // Workspace extent: the whole launch, or the thread block's warp.
591 TData *wspbase = isShared ? (TData *)shmemptr : wsp;
592 const size_t wspNelmt = isShared ? warpsize : nelmt * ncomp;
593 while (e < nelmt)
594 {
595 const size_t ilane = e % warpsize;
596 const size_t iwarp = e / warpsize;
597 const TData *jacptr =
598 DEFORMED ? jac + jacsize * warpsize * iwarp : jac + e;
599 const TData *inptr = in + nmTot * (nelmt * c + warpsize * iwarp);
600 TData *outptr = out + nmTot * (nelmt * c + warpsize * iwarp);
601
602 const size_t wspOffset =
603 isShared ? 0 : nelmt * c + warpsize * iwarp;
604 TData *bwd = wspbase + nqTot * wspOffset;
605 TData *modal = wspbase + nqTot * wspNelmt + nmTot * wspOffset;
606 TData *wsp0 = wspbase +
607 (nqTot + (isNodal ? nmTot : 0u)) * wspNelmt +
608 wsp0Size * wspOffset;
609
610 if constexpr (isNodal)
611 {
612 MatVecKernel(ilane, nmTot, nodToMod, inptr, modal);
613 }
614
615 BwdTrans2DSumFacKernel<SHAPE_TYPE, false>(
616 ilane, nm0, nm1, nq0, nq1, isModified, basis0, basis1,
617 isNodal ? modal : inptr, bwd, wsp0);
618
619 IProductWRTBase2DSumFacKernel<SHAPE_TYPE, SCALE, APPEND && !isNodal,
620 DEFORMED>(
621 ilane, nm0, nm1, nq0, nq1, isModified, basis0, basis1, w0, w1,
622 jacptr, bwd, isNodal ? modal : outptr, wsp0, scale);
623
624 if constexpr (isNodal)
625 {
626 // Multiply by transpose nodToMod to transform coeffs.
627 MatVecKernel<APPEND, true>(ilane, nmTot, nodToMod, modal,
628 outptr);
629 }
630
631 e += getGlobalRange<0>(threadBlock);
632 }
633 });
634}
635
636/**
637 * @brief Device entry point of the 3D SumFac mass operator: unpacks the
638 * size parameter and, grid-striding over the block's elements, transforms
639 * and integrates one element per thread.
640 *
641 * As in 2D, with two shared scratch areas instead of one, each region
642 * scaled by the component count (see the file notes) or, with
643 * @p useSharedMemoryWsp, covering only the thread block's warp.
644 *
645 * @note For the two tetrahedral shapes the layout computed here does not
646 * match MassWorkSpaceSize in two places: the first scratch area is given
647 * the inner product's stride nq1 * nq2 where the reservation is
648 * max(nq1 * nq2, nm01), nm01 = (2 nm1 - nm0 + 1) nm0 / 2 being the number
649 * of mode pairs the transform stages there, and the second area's base
650 * offset is placed behind the first at that same nq1 * nq2 per element
651 * (times the region extent) instead of at the reserved maximum. Layout and
652 * reservation therefore agree only while nq1 * nq2 >= nm01. The
653 * hexahedron's reservation takes no such maximum to begin with, so its
654 * matching plain nq1 * nq2 stride never disagrees with it; the second
655 * area's stride, and every offset of the other shapes, are the ones
656 * MassWorkSpaceSize reserves.
657 *
658 * @tparam SHAPE_TYPE Hex, Tet, NodalTet, Prism, NodalPrism, Pyr or
659 * NodalPyr.
660 * @tparam Implementation MultiRegions::SumFac.
661 * @tparam SCALE Multiply the result by @p scale.
662 * @tparam APPEND Accumulate onto @p out instead of overwriting.
663 * @tparam DEFORMED @p jac holds one interleaved value per
664 * quadrature point rather than a single
665 * per-element value.
666 * @tparam TSizeParameter3D 3D size parameter, runtime or compile-time
667 * form.
668 * @tparam TthreadBlock Back-end thread-block handle type.
669 * @tparam TData Floating-point type of the field data.
670 * @tparam TUseSharedWsp Type of @p useSharedMemoryWsp: `bool`, or a
671 * `std::bool_constant` SelectSharedWorkSpace() fixed.
672 *
673 * @param sizeParam3D Mode and quadrature-point counts of an element.
674 * @param nelmt Elements in the block, padding included.
675 * @param elmtsPerBlock Elements per thread block, one per thread;
676 * unused.
677 * @param isModified First-direction basis is eModified_A.
678 * @param useSharedMemoryWsp Stage intermediates in @p shmemptr rather than
679 * in
680 * @p wsp.
681 * @param index0,index1,index2,index3
682 * Mode-index tables; unused by this strategy and
683 * passed null (see the 2D launcher).
684 * @param basis0 Direction-0 basis table.
685 * @param basis1 Direction-1 basis table; combined-mode indexed for
686 * the tetrahedra.
687 * @param basis2 Direction-2 basis table; combined-mode indexed for
688 * the collapsed shapes.
689 * @param w0,w1,w2 Per-direction quadrature weights.
690 * @param nodToMod Nodal-to-modal matrix (nodal shapes only).
691 * @param jac Block's Jacobians, warp-interleaved.
692 * @param in Block's coefficients, every component back to
693 * back; this thread's own component is c.
694 * @param out Block's coefficients, every component back to
695 * back; this thread's own component is c,
696 * overwritten, or accumulated into when APPEND.
697 * @param wsp Global workspace: MassWorkSpaceSize's regions, each
698 * scaled by the component count (see above); unused
699 * if @p useSharedMemoryWsp.
700 * @param scale Factor applied when @p SCALE is set.
701 * @param shmemptr Dynamic shared-memory base, sized by
702 * MassSharedMemorySize; used only if @p
703 * useSharedMemoryWsp.
704 * @param threadBlock Thread-block handle the index helpers read.
705 */
706template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
707 bool SCALE, bool APPEND, bool DEFORMED, typename TSizeParameter3D,
708 typename TthreadBlock, typename TData, typename TUseSharedWsp,
709 std::enable_if_t<std::is_same_v<Implementation, SumFac>, bool>
710 Enable = true>
712 (GetMaxThreadPerBlock<Implementation, TSizeParameter3D>()))
714 const TSizeParameter3D sizeParam3D, const size_t nelmt,
715 [[maybe_unused]] const unsigned int elmtsPerBlock,
716 const bool isModified, const TUseSharedWsp useSharedMemoryWsp,
717 [[maybe_unused]] const unsigned int *NEK_RESTRICT index0,
718 [[maybe_unused]] const unsigned int *NEK_RESTRICT index1,
719 [[maybe_unused]] const unsigned int *NEK_RESTRICT index2,
720 [[maybe_unused]] const unsigned int *NEK_RESTRICT index3,
721 const TData *NEK_RESTRICT basis0, const TData *NEK_RESTRICT basis1,
722 const TData *NEK_RESTRICT basis2, const TData *NEK_RESTRICT w0,
723 const TData *NEK_RESTRICT w1, const TData *NEK_RESTRICT w2,
724 const TData *NEK_RESTRICT nodToMod, const TData *NEK_RESTRICT jac,
725 const TData *NEK_RESTRICT in, TData *NEK_RESTRICT out,
726 TData *NEK_RESTRICT wsp, const TData scale, unsigned char *shmemptr,
727 const TthreadBlock &threadBlock)
728{
729 static_assert(IsSizeParameter3D_v<TSizeParameter3D>,
730 "Template argument must be either of type "
731 "NonTemplatedSizeParameter3D or TemplatedSizeParameter3D.");
732
734
735 constexpr bool isNodal = SHAPE_TYPE == LibUtilities::NodalTet ||
736 SHAPE_TYPE == LibUtilities::NodalPrism ||
737 SHAPE_TYPE == LibUtilities::NodalPyr;
738
739 const unsigned int nm0 = sizeParam3D.nm0();
740 const unsigned int nm1 = sizeParam3D.nm1();
741 const unsigned int nm2 = sizeParam3D.nm2();
742 const unsigned int nmTot = sizeParam3D.nmTot();
743 const unsigned int nq0 = sizeParam3D.nq0();
744 const unsigned int nq1 = sizeParam3D.nq1();
745 const unsigned int nq2 = sizeParam3D.nq2();
746 const unsigned int nqTot = sizeParam3D.nqTot();
747
748 const unsigned int jacsize = DEFORMED ? nqTot : 1u;
749
750 unsigned int wsp0Size = 0, wsp1Size = 0;
751 if constexpr (SHAPE_TYPE == LibUtilities::Hex)
752 {
753 wsp0Size = nq1 * nq2;
754 wsp1Size = nq2;
755 }
756 else if constexpr (SHAPE_TYPE == LibUtilities::Tet ||
757 SHAPE_TYPE == LibUtilities::NodalTet)
758 {
759 wsp0Size = nq1 * nq2;
760 wsp1Size = std::max(nq2, nm0);
761 }
762 else if constexpr (SHAPE_TYPE == LibUtilities::Prism ||
763 SHAPE_TYPE == LibUtilities::NodalPrism ||
764 SHAPE_TYPE == LibUtilities::Pyr ||
765 SHAPE_TYPE == LibUtilities::NodalPyr)
766 {
767 wsp0Size = std::max(nq1 * nq2, nm0 * nm1);
768 wsp1Size = std::max(nq2, nm0);
769 }
770
771 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
772
773 size_t e = getGlobalIdx<0>(threadBlock); // use size_t to prevent overflow
774 const unsigned int c = getBlockIdx<1>(threadBlock);
775 const unsigned int ncomp = getBlockRange<1>(threadBlock);
776
777 DispatchWorkSpace(useSharedMemoryWsp, [&](const auto isShared) {
778 // Workspace extent: the whole launch, or the thread block's warp.
779 TData *wspbase = isShared ? (TData *)shmemptr : wsp;
780 const size_t wspNelmt = isShared ? warpsize : nelmt * ncomp;
781 while (e < nelmt)
782 {
783 const size_t ilane = e % warpsize;
784 const size_t iwarp = e / warpsize;
785 const TData *jacptr =
786 DEFORMED ? jac + jacsize * warpsize * iwarp : jac + e;
787 const TData *inptr = in + nmTot * (nelmt * c + warpsize * iwarp);
788 TData *outptr = out + nmTot * (nelmt * c + warpsize * iwarp);
789
790 const size_t wspOffset =
791 isShared ? 0 : nelmt * c + warpsize * iwarp;
792 TData *bwd = wspbase + nqTot * wspOffset;
793 TData *modal = wspbase + nqTot * wspNelmt + nmTot * wspOffset;
794 TData *wsp0 = wspbase +
795 (nqTot + (isNodal ? nmTot : 0u)) * wspNelmt +
796 wsp0Size * wspOffset;
797 TData *wsp1 =
798 wspbase +
799 (nqTot + (isNodal ? nmTot : 0u) + wsp0Size) * wspNelmt +
800 wsp1Size * wspOffset;
801
802 if constexpr (isNodal)
803 {
804 MatVecKernel(ilane, nmTot, nodToMod, inptr, modal);
805 }
806
807 BwdTrans3DSumFacKernel<SHAPE_TYPE, false>(
808 ilane, nm0, nm1, nm2, nq0, nq1, nq2, isModified, basis0, basis1,
809 basis2, isNodal ? modal : inptr, bwd, wsp0, wsp1);
810
811 IProductWRTBase3DSumFacKernel<SHAPE_TYPE, SCALE, APPEND && !isNodal,
812 DEFORMED>(
813 ilane, nm0, nm1, nm2, nq0, nq1, nq2, isModified, basis0, basis1,
814 basis2, w0, w1, w2, jacptr, bwd, isNodal ? modal : outptr, wsp0,
815 wsp1, scale);
816
817 if constexpr (isNodal)
818 {
819 // Multiply by transpose nodToMod to transform coeffs.
820 MatVecKernel<APPEND, true>(ilane, nmTot, nodToMod, modal,
821 outptr);
822 }
823
824 e += getGlobalRange<0>(threadBlock);
825 }
826 });
827}
828
829#endif
830
831} // namespace Nektar::MultiRegions::detail
#define NEK_RESTRICT
Definition Backends.hpp:254
#define NEK_DEVICE_KERNEL
Definition Backends.hpp:297
Device SumFac kernels of the backward transform: one element per thread, with the sum-factorisation i...
Device SumFac kernels of the inner product with the basis: one element per thread,...
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.
static NEK_FORCE_INLINE void MassKernelLauncher(const TSizeParameter1D sizeParam1D, const bool isModified, const typename simd_type::scalarType *basis0, const typename simd_type::scalarType *w0, const simd_type *NtoM, const simd_type *NtoMTrans, const simd_type *jac, simd_type *bwd, const simd_type *in, simd_type *out, typename simd_type::scalarType scale=1.0)
1D launcher: backward transform then inner product with the basis for one element group of segments.
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
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 const unsigned int const unsigned int const unsigned int const unsigned int const size_t const TData *NEK_RESTRICT const TData *NEK_RESTRICT jac
NEK_DEVICE_KERNEL void const TData *NEK_RESTRICT const TData *NEK_RESTRICT const size_t nelmt
NEK_DEVICE_KERNEL void const TData *NEK_RESTRICT const TData *NEK_RESTRICT const size_t const unsigned int const unsigned int const unsigned int const unsigned int const size_t const TData *NEK_RESTRICT const TData *NEK_RESTRICT const TUseSharedWsp TData *NEK_RESTRICT const TData *NEK_RESTRICT TData *NEK_RESTRICT out
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