Nektar++
Loading...
Searching...
No Matches
HelmholtzBlockOpDeviceSumFacKernels.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: HelmholtzBlockOpDeviceSumFacKernels.hpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description:
32//
33///////////////////////////////////////////////////////////////////////////////
34
35/**
36 * @file HelmholtzBlockOpDeviceSumFacKernels.hpp
37 * @brief Device sum-factorised (SumFac) kernels and launchers for the
38 * weak Helmholtz operator.
39 *
40 * @details
41 * Element-parallel kernels for the weak Helmholtz operator: each thread
42 * handles one element, warp-interleaved (lane `e % warpSize` of warp
43 * `e / warpSize`). Strings together:
44 * 1. BwdTrans{Shape}SumFacKernel to evaluate physical values;
45 * 2. PhysDeriv{1,2,3}DSumFacKernel to evaluate reference derivatives;
46 * 3. ApplyMetric{1,2,3}DSumFacKernel to apply the diffusion tensor and
47 * geometry metric, and scale physical values by lambda * J;
48 * 4. SumDerivTensor{1,2,3}DKernel to sum transposed derivatives;
49 * 5. IProductWRTBase{Shape}SumFacKernel to take the inner product with
50 * the basis.
51 * The intermediates are held in a global-memory workspace or, when it
52 * fits, in shared memory.
53 */
54#pragma once
55
59
63
64// Coefficients are passed as raw pointers. Variable tensor components are
65// separated by varDiffCoeffSize scalars. The kernels select the plane and
66// element from the base pointer; point offsets follow the same interleaving
67// as the input and derivative-factor arrays.
68
70{
71
72#if defined(NEKTAR_ENABLE_DEVICE) && defined(DEVICE_COMPILE_ONLY)
73// Helper function
74/**
75 * @brief Workspace a 1D SumFac Helmholtz launch needs, in TData values
76 * per variable and plane.
77 */
78template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
79 typename TSizeParameter1D,
80 std::enable_if_t<std::is_same_v<Implementation, SumFac> &&
81 IsSizeParameter1D_v<TSizeParameter1D>,
82 bool>
83 Enable = true>
84inline constexpr size_t HelmholtzWorkSpaceSize(
85 const size_t nelmt, const unsigned int ncoord,
86 const TSizeParameter1D sizeParam1D)
87{
88 size_t wspsize = 0;
89
90 const unsigned int nq0 = sizeParam1D.nq0();
91
92 if constexpr (SHAPE_TYPE == LibUtilities::Seg)
93 {
94 wspsize = (1 + ncoord) * nq0 * nelmt;
95 }
96
97 return wspsize;
98}
99
100/**
101 * @brief Workspace a 2D SumFac Helmholtz launch needs, in TData values
102 * per variable and plane.
103 */
104template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
105 typename TSizeParameter2D,
106 std::enable_if_t<std::is_same_v<Implementation, SumFac> &&
107 IsSizeParameter2D_v<TSizeParameter2D>,
108 bool>
109 Enable = true>
110inline constexpr size_t HelmholtzWorkSpaceSize(
111 const size_t nelmt, const unsigned int ncoord,
112 const TSizeParameter2D sizeParam2D)
113{
114 size_t wspsize = 0;
115
116 const unsigned int nm0 = sizeParam2D.nm0();
117 const unsigned int nq1 = sizeParam2D.nq1();
118
119 if constexpr (SHAPE_TYPE == LibUtilities::Quad)
120 {
121 wspsize = ((1 + ncoord) * sizeParam2D.nqTot() + nq1) * nelmt;
122 }
123 else if constexpr (SHAPE_TYPE == LibUtilities::Tri)
124 {
125 wspsize =
126 ((1 + ncoord) * sizeParam2D.nqTot() + std::max(nq1, nm0)) * nelmt;
127 }
128 else if constexpr (SHAPE_TYPE == LibUtilities::NodalTri)
129 {
130 const unsigned int nmTot = sizeParam2D.nmTot();
131
132 wspsize =
133 ((1 + ncoord) * sizeParam2D.nqTot() + std::max(nq1, nm0) + nmTot) *
134 nelmt;
135 }
136
137 return wspsize;
138}
139
140/**
141 * @brief Workspace a 3D SumFac Helmholtz launch needs, in TData values
142 * per variable and plane.
143 */
144template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
145 typename TSizeParameter3D,
146 std::enable_if_t<std::is_same_v<Implementation, SumFac> &&
147 IsSizeParameter3D_v<TSizeParameter3D>,
148 bool>
149 Enable = true>
150inline constexpr size_t HelmholtzWorkSpaceSize(
151 const size_t nelmt, [[maybe_unused]] const unsigned int ncoord,
152 const TSizeParameter3D sizeParam3D)
153{
154 size_t wspsize = 0;
155
156 const unsigned int nm0 = sizeParam3D.nm0();
157 const unsigned int nm1 = sizeParam3D.nm1();
158 const unsigned int nq1 = sizeParam3D.nq1();
159 const unsigned int nq2 = sizeParam3D.nq2();
160
161 if constexpr (SHAPE_TYPE == LibUtilities::Hex)
162 {
163 wspsize = (4 * sizeParam3D.nqTot() + nq1 * nq2 + nq2) * nelmt;
164 }
165 else if constexpr (SHAPE_TYPE == LibUtilities::Tet)
166 {
167 wspsize =
168 (4 * sizeParam3D.nqTot() + nq1 * nq2 + std::max(nq2, nm0)) * nelmt;
169 }
170 else if constexpr (SHAPE_TYPE == LibUtilities::NodalTet)
171 {
172 const unsigned int nmTot = sizeParam3D.nmTot();
173
174 wspsize =
175 (4 * sizeParam3D.nqTot() + nq1 * nq2 + std::max(nq2, nm0) + nmTot) *
176 nelmt;
177 }
178 else if constexpr (SHAPE_TYPE == LibUtilities::Prism)
179 {
180 wspsize = (4 * sizeParam3D.nqTot() + std::max(nq1 * nq2, nm0 * nm1) +
181 std::max(nq2, nm0)) *
182 nelmt;
183 }
184 else if constexpr (SHAPE_TYPE == LibUtilities::NodalPrism)
185 {
186 const unsigned int nmTot = sizeParam3D.nmTot();
187
188 wspsize = (4 * sizeParam3D.nqTot() + std::max(nq1 * nq2, nm0 * nm1) +
189 std::max(nq2, nm0) + nmTot) *
190 nelmt;
191 }
192 else if constexpr (SHAPE_TYPE == LibUtilities::Pyr)
193 {
194 wspsize = (4 * sizeParam3D.nqTot() + std::max(nq1 * nq2, nm0 * nm1) +
195 std::max(nq2, nm0)) *
196 nelmt;
197 }
198 else if constexpr (SHAPE_TYPE == LibUtilities::NodalPyr)
199 {
200 const unsigned int nmTot = sizeParam3D.nmTot();
201
202 wspsize = (4 * sizeParam3D.nqTot() + std::max(nq1 * nq2, nm0 * nm1) +
203 std::max(nq2, nm0) + nmTot) *
204 nelmt;
205 }
206
207 return wspsize;
208}
209
210/**
211 * @brief Dynamic shared memory a 1D SumFac Helmholtz launch needs, in
212 * TData values.
213 *
214 * With @p useSharedMemoryWsp, the workspace of the thread block's
215 * @p elmtsPerBlock elements, laid out as HelmholtzWorkSpaceSize gives for that
216 * many elements; otherwise none.
217 */
218template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
219 typename TData, typename TSizeParameter1D,
220 std::enable_if_t<std::is_same_v<Implementation, SumFac> &&
221 IsSizeParameter1D_v<TSizeParameter1D>,
222 bool>
223 Enable = true>
224inline constexpr unsigned int HelmholtzSharedMemorySize(
225 const unsigned int ncoord, const TSizeParameter1D sizeParam1D,
226 const unsigned int elmtsPerBlock, const bool useSharedMemoryWsp)
227{
228 return useSharedMemoryWsp
229 ? static_cast<unsigned int>(
230 HelmholtzWorkSpaceSize<SHAPE_TYPE, Implementation>(
231 elmtsPerBlock, ncoord, sizeParam1D))
232 : 0u;
233}
234
235/**
236 * @brief Dynamic shared memory a 2D SumFac Helmholtz launch needs, in
237 * TData values: the collapsed-coordinate factors of the triangles, then
238 * the block's workspace as the 1D overload.
239 */
240template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
241 typename TData, typename TSizeParameter2D,
242 std::enable_if_t<std::is_same_v<Implementation, SumFac> &&
243 IsSizeParameter2D_v<TSizeParameter2D>,
244 bool>
245 Enable = true>
246inline constexpr unsigned int HelmholtzSharedMemorySize(
247 const unsigned int ncoord, const TSizeParameter2D sizeParam2D,
248 const unsigned int elmtsPerBlock, const bool useSharedMemoryWsp)
249{
250 const unsigned int wspSize =
252 ? static_cast<unsigned int>(
253 HelmholtzWorkSpaceSize<SHAPE_TYPE, Implementation>(
254 elmtsPerBlock, ncoord, sizeParam2D))
255 : 0u;
256
257 if constexpr (SHAPE_TYPE == LibUtilities::Quad)
258 {
259 return wspSize;
260 }
261 else if constexpr (SHAPE_TYPE == LibUtilities::Tri ||
262 SHAPE_TYPE == LibUtilities::NodalTri)
263 {
264 const unsigned int nq0 = sizeParam2D.nq0();
265 const unsigned int nq1 = sizeParam2D.nq1();
266
267 return nq0 + nq1 + wspSize;
268 }
269}
270
271/**
272 * @brief Dynamic shared memory a 3D SumFac Helmholtz launch needs, in
273 * TData values: the collapsed-coordinate factors of the collapsed
274 * shapes, then the block's workspace as the 1D overload.
275 */
276template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
277 typename TData, typename TSizeParameter3D,
278 std::enable_if_t<std::is_same_v<Implementation, SumFac> &&
279 IsSizeParameter3D_v<TSizeParameter3D>,
280 bool>
281 Enable = true>
282inline constexpr unsigned int HelmholtzSharedMemorySize(
283 const unsigned int ncoord, const TSizeParameter3D sizeParam3D,
284 const unsigned int elmtsPerBlock, const bool useSharedMemoryWsp)
285{
286 const unsigned int nq0 = sizeParam3D.nq0();
287 const unsigned int nq1 = sizeParam3D.nq1();
288 const unsigned int nq2 = sizeParam3D.nq2();
289
290 const unsigned int wspSize =
292 ? static_cast<unsigned int>(
293 HelmholtzWorkSpaceSize<SHAPE_TYPE, Implementation>(
294 elmtsPerBlock, ncoord, sizeParam3D))
295 : 0u;
296
297 if constexpr (SHAPE_TYPE == LibUtilities::Hex)
298 {
299 return wspSize;
300 }
301 else if constexpr (SHAPE_TYPE == LibUtilities::Tet ||
302 SHAPE_TYPE == LibUtilities::NodalTet)
303 {
304 return nq0 + 2 * nq1 + nq2 + wspSize;
305 }
306 else if constexpr (SHAPE_TYPE == LibUtilities::Prism ||
307 SHAPE_TYPE == LibUtilities::NodalPrism)
308 {
309 return nq0 + nq2 + wspSize;
310 }
311 else if constexpr (SHAPE_TYPE == LibUtilities::Pyr ||
312 SHAPE_TYPE == LibUtilities::NodalPyr)
313 {
314 return nq0 + nq1 + nq2 + wspSize;
315 }
316}
317
318/**
319 * @brief 1D metric kernel applying diffusion tensor, metric terms and
320 * lambda-weighted mass scaling.
321 */
322template <bool DEFORMED, bool VARCOEFF, typename TData>
323NEK_DEVICE_INLINE static void ApplyMetric1DSumFacKernel(
324 const unsigned int ilane, const unsigned int ncoord, const unsigned int nq0,
325 const size_t inoffset, const TData *NEK_RESTRICT w0,
326 const TData *NEK_RESTRICT df, const TData *NEK_RESTRICT jac,
327 const TData *NEK_RESTRICT diffCoeff, const size_t varDiffCoeffSize,
328 const TData *NEK_RESTRICT in, TData *out, TData *NEK_RESTRICT bwd,
329 const TData lambda)
330{
331 const size_t diffCoeffStride = VARCOEFF ? varDiffCoeffSize : 1;
332 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
333
334 TData metric[3] = {0.0};
335 if constexpr (!DEFORMED && !VARCOEFF)
336 {
337 if (diffCoeff)
338 {
339 if (ncoord == 1)
340 {
341 metric[0] = diffCoeff[0] * df[ilane];
342 }
343 else if (ncoord == 2)
344 {
345 metric[0] = diffCoeff[0] * df[ilane] +
346 diffCoeff[1] * df[warpsize + ilane];
347 metric[1] = diffCoeff[1] * df[ilane] +
348 diffCoeff[2] * df[warpsize + ilane];
349 }
350 else if (ncoord == 3)
351 {
352 metric[0] = diffCoeff[0] * df[ilane] +
353 diffCoeff[1] * df[warpsize + ilane] +
354 diffCoeff[3] * df[2 * warpsize + ilane];
355 metric[1] = diffCoeff[1] * df[ilane] +
356 diffCoeff[2] * df[warpsize + ilane] +
357 diffCoeff[4] * df[2 * warpsize + ilane];
358 metric[2] = diffCoeff[3] * df[ilane] +
359 diffCoeff[4] * df[warpsize + ilane] +
360 diffCoeff[5] * df[2 * warpsize + ilane];
361 }
362 }
363 }
364
365 for (unsigned int i = 0u; i < nq0; ++i)
366 {
367 const unsigned int index = warpsize * i + ilane;
368 const unsigned int dfindex =
369 DEFORMED ? ncoord * warpsize * i + ilane : ilane;
370
371 if constexpr (DEFORMED || VARCOEFF)
372 {
373 const size_t diffCoeffIndex = VARCOEFF ? index : 0;
374 if (diffCoeff)
375 {
376 if (ncoord == 1)
377 {
378 metric[0] = diffCoeff[diffCoeffIndex] * df[dfindex];
379 }
380 else if (ncoord == 2)
381 {
382 metric[0] = diffCoeff[diffCoeffIndex] * df[dfindex] +
383 diffCoeff[diffCoeffStride + diffCoeffIndex] *
384 df[warpsize + dfindex];
385 metric[1] =
386 diffCoeff[diffCoeffStride + diffCoeffIndex] *
387 df[dfindex] +
388 diffCoeff[2 * diffCoeffStride + diffCoeffIndex] *
389 df[warpsize + dfindex];
390 }
391 else if (ncoord == 3)
392 {
393 metric[0] =
394 diffCoeff[diffCoeffIndex] * df[dfindex] +
395 diffCoeff[diffCoeffStride + diffCoeffIndex] *
396 df[warpsize + dfindex] +
397 diffCoeff[3 * diffCoeffStride + diffCoeffIndex] *
398 df[2 * warpsize + dfindex];
399 metric[1] =
400 diffCoeff[diffCoeffStride + diffCoeffIndex] *
401 df[dfindex] +
402 diffCoeff[2 * diffCoeffStride + diffCoeffIndex] *
403 df[warpsize + dfindex] +
404 diffCoeff[4 * diffCoeffStride + diffCoeffIndex] *
405 df[2 * warpsize + dfindex];
406 metric[2] =
407 diffCoeff[3 * diffCoeffStride + diffCoeffIndex] *
408 df[dfindex] +
409 diffCoeff[4 * diffCoeffStride + diffCoeffIndex] *
410 df[warpsize + dfindex] +
411 diffCoeff[5 * diffCoeffStride + diffCoeffIndex] *
412 df[2 * warpsize + dfindex];
413 }
414 }
415 }
416
417 TData sum = 0.0;
418 for (unsigned int k = 0u; k < ncoord; ++k)
419 {
420 if (diffCoeff)
421 {
422 sum += metric[k] * in[k * inoffset + index];
423 }
424 else
425 {
426 sum += df[k * warpsize + dfindex] * in[k * inoffset + index];
427 }
428 }
429
430 if constexpr (DEFORMED)
431 {
432 bwd[index] *= lambda * jac[index] * w0[i];
433 out[index] = sum * jac[index] * w0[i];
434 }
435 else
436 {
437 bwd[index] *= lambda * jac[0] * w0[i];
438 out[index] = sum * jac[0] * w0[i];
439 }
440 }
441}
442
443/**
444 * @brief 2D metric kernel applying diffusion tensor, metric terms and
445 * lambda-weighted mass scaling.
446 */
447template <LibUtilities::ShapeType SHAPE_TYPE, bool DEFORMED, bool VARCOEFF,
448 typename TData>
449NEK_DEVICE_INLINE static void ApplyMetric2DSumFacKernel(
450 const unsigned int ilane, const unsigned int ncoord, const unsigned int nq0,
451 const unsigned int nq1, const size_t inoffset, const TData *NEK_RESTRICT w0,
452 const TData *NEK_RESTRICT w1, [[maybe_unused]] const TData *NEK_RESTRICT f0,
453 [[maybe_unused]] const TData *NEK_RESTRICT f1, const TData *NEK_RESTRICT df,
454 const TData *NEK_RESTRICT jac, const TData *NEK_RESTRICT diffCoeff,
455 const size_t varDiffCoeffSize, const TData *NEK_RESTRICT in,
456 TData *NEK_RESTRICT out0, TData *NEK_RESTRICT out1, TData *NEK_RESTRICT bwd,
457 const TData lambda)
458{
459 const size_t diffCoeffStride = VARCOEFF ? varDiffCoeffSize : 1;
460 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
461
462 const unsigned int ndf = 2 * ncoord;
463
464 TData metric[6] = {0.0};
465 if constexpr (!DEFORMED && !VARCOEFF)
466 {
467 if (diffCoeff)
468 {
469 if (ncoord == 2)
470 {
471 metric[0] = diffCoeff[0] * df[ilane] +
472 diffCoeff[1] * df[2 * warpsize + ilane];
473 metric[1] = diffCoeff[0] * df[1 * warpsize + ilane] +
474 diffCoeff[1] * df[3 * warpsize + ilane];
475 metric[2] = diffCoeff[1] * df[ilane] +
476 diffCoeff[2] * df[2 * warpsize + ilane];
477 metric[3] = diffCoeff[1] * df[1 * warpsize + ilane] +
478 diffCoeff[2] * df[3 * warpsize + ilane];
479 }
480 else if (ncoord == 3)
481 {
482 metric[0] = diffCoeff[0] * df[ilane] +
483 diffCoeff[1] * df[2 * warpsize + ilane] +
484 diffCoeff[3] * df[4 * warpsize + ilane];
485 metric[1] = diffCoeff[0] * df[1 * warpsize + ilane] +
486 diffCoeff[1] * df[3 * warpsize + ilane] +
487 diffCoeff[3] * df[5 * warpsize + ilane];
488 metric[2] = diffCoeff[1] * df[ilane] +
489 diffCoeff[2] * df[2 * warpsize + ilane] +
490 diffCoeff[4] * df[4 * warpsize + ilane];
491 metric[3] = diffCoeff[1] * df[1 * warpsize + ilane] +
492 diffCoeff[2] * df[3 * warpsize + ilane] +
493 diffCoeff[4] * df[5 * warpsize + ilane];
494 metric[4] = diffCoeff[3] * df[ilane] +
495 diffCoeff[4] * df[2 * warpsize + ilane] +
496 diffCoeff[5] * df[4 * warpsize + ilane];
497 metric[5] = diffCoeff[3] * df[1 * warpsize + ilane] +
498 diffCoeff[4] * df[3 * warpsize + ilane] +
499 diffCoeff[5] * df[5 * warpsize + ilane];
500 }
501 }
502 }
503
504 for (unsigned int j = 0u, cnt_ji = 0u; j < nq1; ++j)
505 {
506 for (unsigned int i = 0u; i < nq0; ++i, ++cnt_ji)
507 {
508 const unsigned int index = warpsize * cnt_ji + ilane;
509 const unsigned int dfindex =
510 DEFORMED ? ndf * warpsize * cnt_ji + ilane : ilane;
511
512 if constexpr (DEFORMED || VARCOEFF)
513 {
514 const size_t diffCoeffIndex = VARCOEFF ? index : 0;
515 if (diffCoeff)
516 {
517 if (ncoord == 2)
518 {
519 metric[0] =
520 diffCoeff[diffCoeffIndex] * df[dfindex] +
521 diffCoeff[diffCoeffStride + diffCoeffIndex] *
522 df[2 * warpsize + dfindex];
523 metric[1] =
524 diffCoeff[diffCoeffIndex] *
525 df[1 * warpsize + dfindex] +
526 diffCoeff[diffCoeffStride + diffCoeffIndex] *
527 df[3 * warpsize + dfindex];
528 metric[2] =
529 diffCoeff[diffCoeffStride + diffCoeffIndex] *
530 df[dfindex] +
531 diffCoeff[2 * diffCoeffStride + diffCoeffIndex] *
532 df[2 * warpsize + dfindex];
533 metric[3] =
534 diffCoeff[diffCoeffStride + diffCoeffIndex] *
535 df[1 * warpsize + dfindex] +
536 diffCoeff[2 * diffCoeffStride + diffCoeffIndex] *
537 df[3 * warpsize + dfindex];
538 }
539 else if (ncoord == 3)
540 {
541 metric[0] =
542 diffCoeff[diffCoeffIndex] * df[dfindex] +
543 diffCoeff[diffCoeffStride + diffCoeffIndex] *
544 df[2 * warpsize + dfindex] +
545 diffCoeff[3 * diffCoeffStride + diffCoeffIndex] *
546 df[4 * warpsize + dfindex];
547 metric[1] =
548 diffCoeff[diffCoeffIndex] *
549 df[1 * warpsize + dfindex] +
550 diffCoeff[diffCoeffStride + diffCoeffIndex] *
551 df[3 * warpsize + dfindex] +
552 diffCoeff[3 * diffCoeffStride + diffCoeffIndex] *
553 df[5 * warpsize + dfindex];
554 metric[2] =
555 diffCoeff[diffCoeffStride + diffCoeffIndex] *
556 df[dfindex] +
557 diffCoeff[2 * diffCoeffStride + diffCoeffIndex] *
558 df[2 * warpsize + dfindex] +
559 diffCoeff[4 * diffCoeffStride + diffCoeffIndex] *
560 df[4 * warpsize + dfindex];
561 metric[3] =
562 diffCoeff[diffCoeffStride + diffCoeffIndex] *
563 df[1 * warpsize + dfindex] +
564 diffCoeff[2 * diffCoeffStride + diffCoeffIndex] *
565 df[3 * warpsize + dfindex] +
566 diffCoeff[4 * diffCoeffStride + diffCoeffIndex] *
567 df[5 * warpsize + dfindex];
568 metric[4] =
569 diffCoeff[3 * diffCoeffStride + diffCoeffIndex] *
570 df[dfindex] +
571 diffCoeff[4 * diffCoeffStride + diffCoeffIndex] *
572 df[2 * warpsize + dfindex] +
573 diffCoeff[5 * diffCoeffStride + diffCoeffIndex] *
574 df[4 * warpsize + dfindex];
575 metric[5] =
576 diffCoeff[3 * diffCoeffStride + diffCoeffIndex] *
577 df[1 * warpsize + dfindex] +
578 diffCoeff[4 * diffCoeffStride + diffCoeffIndex] *
579 df[3 * warpsize + dfindex] +
580 diffCoeff[5 * diffCoeffStride + diffCoeffIndex] *
581 df[5 * warpsize + dfindex];
582 }
583 }
584 }
585
586 TData sum1 = 0.0, sum2 = 0.0;
587 if (diffCoeff)
588 {
589 TData tmp = in[index];
590 sum1 += metric[0u] * tmp;
591 sum2 += metric[1u] * tmp;
592 tmp = in[inoffset + index];
593 sum1 += metric[2u] * tmp;
594 sum2 += metric[3u] * tmp;
595 if (ncoord == 3u)
596 {
597 tmp = in[2u * inoffset + index];
598 sum1 += metric[4u] * tmp;
599 sum2 += metric[5u] * tmp;
600 }
601 }
602 else
603 {
604 TData tmp = in[index];
605 sum1 += df[0u * warpsize + dfindex] * tmp;
606 sum2 += df[1u * warpsize + dfindex] * tmp;
607 tmp = in[inoffset + index];
608 sum1 += df[2u * warpsize + dfindex] * tmp;
609 sum2 += df[3u * warpsize + dfindex] * tmp;
610 if (ncoord == 3u)
611 {
612 tmp = in[2u * inoffset + index];
613 sum1 += df[4u * warpsize + dfindex] * tmp;
614 sum2 += df[5u * warpsize + dfindex] * tmp;
615 }
616 }
617
618 TData tmpQ = w0[i] * w1[j];
619 if constexpr (DEFORMED)
620 {
621 tmpQ *= jac[index];
622 }
623 else
624 {
625 tmpQ *= jac[0];
626 }
627
628 bwd[index] *= lambda * tmpQ;
629
630 // Moving from standard to collapsed coordinates.
631 if constexpr (SHAPE_TYPE == LibUtilities::Quad)
632 {
633 out0[index] = sum1 * tmpQ;
634 out1[index] = sum2 * tmpQ;
635 }
636 else if constexpr (SHAPE_TYPE == LibUtilities::Tri ||
637 SHAPE_TYPE == LibUtilities::NodalTri)
638 {
639 out0[index] = (sum1 + sum2 * f0[i]) * f1[j] * tmpQ;
640 out1[index] = sum2 * tmpQ;
641 }
642 }
643 }
644}
645
646/**
647 * @brief 3D metric kernel applying diffusion tensor, metric terms and
648 * lambda-weighted mass scaling.
649 */
650template <LibUtilities::ShapeType SHAPE_TYPE, bool DEFORMED, bool VARCOEFF,
651 typename TData>
652NEK_DEVICE_INLINE static void ApplyMetric3DSumFacKernel(
653 const unsigned int ilane, const unsigned int nq0, const unsigned int nq1,
654 const unsigned int nq2, const size_t inoffset, const TData *NEK_RESTRICT w0,
655 const TData *NEK_RESTRICT w1, const TData *NEK_RESTRICT w2,
656 [[maybe_unused]] const TData *NEK_RESTRICT f0,
657 [[maybe_unused]] const TData *NEK_RESTRICT f1,
658 [[maybe_unused]] const TData *NEK_RESTRICT f1m,
659 [[maybe_unused]] const TData *NEK_RESTRICT f2, const TData *NEK_RESTRICT df,
660 const TData *NEK_RESTRICT jac, const TData *NEK_RESTRICT diffCoeff,
661 const size_t varDiffCoeffSize, const TData *NEK_RESTRICT in,
662 TData *NEK_RESTRICT out0, TData *NEK_RESTRICT out1,
663 TData *NEK_RESTRICT out2, TData *NEK_RESTRICT bwd, const TData lambda)
664{
665 const size_t diffCoeffStride = VARCOEFF ? varDiffCoeffSize : 1;
666 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
667
668 constexpr unsigned int ndf = 9u;
669
670 TData metric[9] = {0.0};
671 if constexpr (!DEFORMED && !VARCOEFF)
672 {
673 if (diffCoeff)
674 {
675 metric[0] = diffCoeff[0] * df[ilane] +
676 diffCoeff[1] * df[3 * warpsize + ilane] +
677 diffCoeff[3] * df[6 * warpsize + ilane];
678 metric[1] = diffCoeff[0] * df[1 * warpsize + ilane] +
679 diffCoeff[1] * df[4 * warpsize + ilane] +
680 diffCoeff[3] * df[7 * warpsize + ilane];
681 metric[2] = diffCoeff[0] * df[2 * warpsize + ilane] +
682 diffCoeff[1] * df[5 * warpsize + ilane] +
683 diffCoeff[3] * df[8 * warpsize + ilane];
684 metric[3] = diffCoeff[1] * df[ilane] +
685 diffCoeff[2] * df[3 * warpsize + ilane] +
686 diffCoeff[4] * df[6 * warpsize + ilane];
687 metric[4] = diffCoeff[1] * df[1 * warpsize + ilane] +
688 diffCoeff[2] * df[4 * warpsize + ilane] +
689 diffCoeff[4] * df[7 * warpsize + ilane];
690 metric[5] = diffCoeff[1] * df[2 * warpsize + ilane] +
691 diffCoeff[2] * df[5 * warpsize + ilane] +
692 diffCoeff[4] * df[8 * warpsize + ilane];
693 metric[6] = diffCoeff[3] * df[ilane] +
694 diffCoeff[4] * df[3 * warpsize + ilane] +
695 diffCoeff[5] * df[6 * warpsize + ilane];
696 metric[7] = diffCoeff[3] * df[1 * warpsize + ilane] +
697 diffCoeff[4] * df[4 * warpsize + ilane] +
698 diffCoeff[5] * df[7 * warpsize + ilane];
699 metric[8] = diffCoeff[3] * df[2 * warpsize + ilane] +
700 diffCoeff[4] * df[5 * warpsize + ilane] +
701 diffCoeff[5] * df[8 * warpsize + ilane];
702 }
703 }
704
705 for (unsigned int k = 0u, cnt_kji = 0u; k < nq2; ++k)
706 {
707 for (unsigned int j = 0u; j < nq1; ++j)
708 {
709 for (unsigned int i = 0u; i < nq0; ++i, ++cnt_kji)
710 {
711 const unsigned int index = warpsize * cnt_kji + ilane;
712 const unsigned int dfindex =
713 DEFORMED ? ndf * warpsize * cnt_kji + ilane : ilane;
714
715 if constexpr (DEFORMED || VARCOEFF)
716 {
717 const size_t diffCoeffIndex = VARCOEFF ? index : 0;
718 if (diffCoeff)
719 {
720 metric[0] =
721 diffCoeff[diffCoeffIndex] * df[dfindex] +
722 diffCoeff[diffCoeffStride + diffCoeffIndex] *
723 df[3 * warpsize + dfindex] +
724 diffCoeff[3 * diffCoeffStride + diffCoeffIndex] *
725 df[6 * warpsize + dfindex];
726 metric[1] =
727 diffCoeff[diffCoeffIndex] *
728 df[1 * warpsize + dfindex] +
729 diffCoeff[diffCoeffStride + diffCoeffIndex] *
730 df[4 * warpsize + dfindex] +
731 diffCoeff[3 * diffCoeffStride + diffCoeffIndex] *
732 df[7 * warpsize + dfindex];
733 metric[2] =
734 diffCoeff[diffCoeffIndex] *
735 df[2 * warpsize + dfindex] +
736 diffCoeff[diffCoeffStride + diffCoeffIndex] *
737 df[5 * warpsize + dfindex] +
738 diffCoeff[3 * diffCoeffStride + diffCoeffIndex] *
739 df[8 * warpsize + dfindex];
740 metric[3] =
741 diffCoeff[diffCoeffStride + diffCoeffIndex] *
742 df[dfindex] +
743 diffCoeff[2 * diffCoeffStride + diffCoeffIndex] *
744 df[3 * warpsize + dfindex] +
745 diffCoeff[4 * diffCoeffStride + diffCoeffIndex] *
746 df[6 * warpsize + dfindex];
747 metric[4] =
748 diffCoeff[diffCoeffStride + diffCoeffIndex] *
749 df[1 * warpsize + dfindex] +
750 diffCoeff[2 * diffCoeffStride + diffCoeffIndex] *
751 df[4 * warpsize + dfindex] +
752 diffCoeff[4 * diffCoeffStride + diffCoeffIndex] *
753 df[7 * warpsize + dfindex];
754 metric[5] =
755 diffCoeff[diffCoeffStride + diffCoeffIndex] *
756 df[2 * warpsize + dfindex] +
757 diffCoeff[2 * diffCoeffStride + diffCoeffIndex] *
758 df[5 * warpsize + dfindex] +
759 diffCoeff[4 * diffCoeffStride + diffCoeffIndex] *
760 df[8 * warpsize + dfindex];
761 metric[6] =
762 diffCoeff[3 * diffCoeffStride + diffCoeffIndex] *
763 df[dfindex] +
764 diffCoeff[4 * diffCoeffStride + diffCoeffIndex] *
765 df[3 * warpsize + dfindex] +
766 diffCoeff[5 * diffCoeffStride + diffCoeffIndex] *
767 df[6 * warpsize + dfindex];
768 metric[7] =
769 diffCoeff[3 * diffCoeffStride + diffCoeffIndex] *
770 df[1 * warpsize + dfindex] +
771 diffCoeff[4 * diffCoeffStride + diffCoeffIndex] *
772 df[4 * warpsize + dfindex] +
773 diffCoeff[5 * diffCoeffStride + diffCoeffIndex] *
774 df[7 * warpsize + dfindex];
775 metric[8] =
776 diffCoeff[3 * diffCoeffStride + diffCoeffIndex] *
777 df[2 * warpsize + dfindex] +
778 diffCoeff[4 * diffCoeffStride + diffCoeffIndex] *
779 df[5 * warpsize + dfindex] +
780 diffCoeff[5 * diffCoeffStride + diffCoeffIndex] *
781 df[8 * warpsize + dfindex];
782 }
783 }
784
785 TData sum1 = 0.0, sum2 = 0.0, sum3 = 0.0;
786 if (diffCoeff)
787 {
788 TData tmp = in[index];
789 sum1 += metric[0u] * tmp;
790 sum2 += metric[1u] * tmp;
791 sum3 += metric[2u] * tmp;
792 tmp = in[inoffset + index];
793 sum1 += metric[3u] * tmp;
794 sum2 += metric[4u] * tmp;
795 sum3 += metric[5u] * tmp;
796 tmp = in[2u * inoffset + index];
797 sum1 += metric[6u] * tmp;
798 sum2 += metric[7u] * tmp;
799 sum3 += metric[8u] * tmp;
800 }
801 else
802 {
803 TData tmp = in[index];
804 sum1 += df[0u * warpsize + dfindex] * tmp;
805 sum2 += df[1u * warpsize + dfindex] * tmp;
806 sum3 += df[2u * warpsize + dfindex] * tmp;
807 tmp = in[inoffset + index];
808 sum1 += df[3u * warpsize + dfindex] * tmp;
809 sum2 += df[4u * warpsize + dfindex] * tmp;
810 sum3 += df[5u * warpsize + dfindex] * tmp;
811 tmp = in[2u * inoffset + index];
812 sum1 += df[6u * warpsize + dfindex] * tmp;
813 sum2 += df[7u * warpsize + dfindex] * tmp;
814 sum3 += df[8u * warpsize + dfindex] * tmp;
815 }
816
817 TData tmpQ = w0[i] * w1[j] * w2[k];
818 if constexpr (DEFORMED)
819 {
820 tmpQ *= jac[index];
821 }
822 else
823 {
824 tmpQ *= jac[0];
825 }
826
827 bwd[index] *= lambda * tmpQ;
828
829 if constexpr (SHAPE_TYPE == LibUtilities::Hex)
830 {
831 out0[index] = sum1 * tmpQ;
832 out1[index] = sum2 * tmpQ;
833 out2[index] = sum3 * tmpQ;
834 }
835 else if constexpr (SHAPE_TYPE == LibUtilities::Tet ||
836 SHAPE_TYPE == LibUtilities::NodalTet)
837 {
838 TData tmp = f2[k] * tmpQ;
839 out0[index] = (sum1 + (sum2 + sum3) * f0[i]) * f1m[j] * tmp;
840 out1[index] = (sum2 + sum3 * f1[j]) * tmp;
841 out2[index] = sum3 * tmpQ;
842 }
843 else if constexpr (SHAPE_TYPE == LibUtilities::Prism ||
844 SHAPE_TYPE == LibUtilities::NodalPrism)
845 {
846 out0[index] = (sum1 + sum3 * f0[i]) * f2[k] * tmpQ;
847 out1[index] = sum2 * tmpQ;
848 out2[index] = sum3 * tmpQ;
849 }
850 else if constexpr (SHAPE_TYPE == LibUtilities::Pyr ||
851 SHAPE_TYPE == LibUtilities::NodalPyr)
852 {
853 TData tmp = f2[k] * tmpQ;
854 out0[index] = (sum1 + sum3 * f0[i]) * tmp;
855 out1[index] = (sum2 + sum3 * f1[j]) * tmp;
856 out2[index] = sum3 * tmpQ;
857 }
858 }
859 }
860 }
861}
862
863/**
864 * @brief 1D launcher stringing together the five sum-factorised stages
865 * on device. The intermediates are in @p wsp or, with @p useSharedMemoryWsp, in
866 * the thread block's warp slice of @p shmemptr; each lane uses only its
867 * own column, so no barrier is needed.
868 */
869template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
870 bool DEFORMED, bool VARCOEFF, typename TSizeParameter1D,
871 typename TthreadBlock, typename TData, typename TUseSharedWsp,
872 std::enable_if_t<std::is_same_v<Implementation, SumFac>, bool>
873 Enable = true>
875 (GetMaxThreadPerBlock<Implementation, TSizeParameter1D>()))
877 const TSizeParameter1D sizeParam1D, const unsigned int ncoord,
878 const size_t nelmt, [[maybe_unused]] const unsigned int elmtsPerBlock,
879 [[maybe_unused]] const bool isModified,
880 const TUseSharedWsp useSharedMemoryWsp,
881 const TData *NEK_RESTRICT basis0, const TData *NEK_RESTRICT D0,
882 const TData *NEK_RESTRICT w0,
883 [[maybe_unused]] const TData *NEK_RESTRICT nodToMod,
884 const TData *NEK_RESTRICT df, const TData *NEK_RESTRICT jac,
885 const TData *NEK_RESTRICT diffCoeff, const size_t varDiffCoeffSize,
886 const unsigned int nhomo, const TData *NEK_RESTRICT in,
887 TData *NEK_RESTRICT out, TData *NEK_RESTRICT wsp, const TData lambda,
888 unsigned char *shmemptr, const TthreadBlock &threadBlock)
889{
890 static_assert(IsSizeParameter1D_v<TSizeParameter1D>,
891 "Template argument must be either of type "
892 "NonTemplatedSizeParameter1D or TemplatedSizeParameter1D.");
893
895
896 const unsigned int nm0 = sizeParam1D.nm0();
897 const unsigned int nq0 = sizeParam1D.nq0();
898
899 const unsigned int ndf = ncoord;
900 const unsigned int dfsize = DEFORMED ? nq0 : 1u;
901 const unsigned int jacsize = DEFORMED ? nq0 : 1u;
902
903 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
904
905 size_t e = getGlobalIdx<0>(threadBlock);
906 const unsigned int c = getBlockIdx<1>(threadBlock);
907 const unsigned int ncomp = getBlockRange<1>(threadBlock);
908
909 DispatchWorkSpace(useSharedMemoryWsp, [&](const auto isShared) {
910 // Workspace extent: the whole launch, or the thread block's warp.
911 TData *wspbase = isShared ? (TData *)shmemptr : wsp;
912 const size_t wspNelmt = isShared ? warpsize : nelmt * ncomp;
913 const size_t derivNelmt = isShared ? warpsize : nelmt;
914 while (e < nelmt)
915 {
916 const size_t ilane = e % warpsize;
917 const size_t iwarp = e / warpsize;
918 const TData *diffCoeffPtr =
919 VARCOEFF
920 ? diffCoeff + nq0 * (nelmt * (c % nhomo) + warpsize * iwarp)
921 : diffCoeff;
922 const TData *dfptr = df + ndf * dfsize * warpsize * iwarp;
923 const TData *jacptr =
924 DEFORMED ? jac + jacsize * warpsize * iwarp : jac + e;
925 const TData *inptr = in + nm0 * (nelmt * c + warpsize * iwarp);
926 TData *outptr = out + nm0 * (nelmt * c + warpsize * iwarp);
927
928 const size_t wspOffset =
929 isShared ? 0 : nelmt * c + warpsize * iwarp;
930 const size_t derivOffset =
931 isShared ? 0 : ncoord * nelmt * c + warpsize * iwarp;
932 TData *bwd = wspbase + nq0 * wspOffset;
933 TData *deriv = wspbase + nq0 * wspNelmt + nq0 * derivOffset;
934 BwdTransSegSumFacKernel<false>(ilane, nm0, nq0, basis0, inptr, bwd);
935 PhysDeriv1DSumFacKernel<DEFORMED>(
936 ilane, ncoord, nq0, derivNelmt * nq0, D0, dfptr, bwd, deriv);
937 ApplyMetric1DSumFacKernel<DEFORMED, VARCOEFF>(
938 ilane, ncoord, nq0, derivNelmt * nq0, w0, dfptr, jacptr,
939 diffCoeffPtr, varDiffCoeffSize, deriv, deriv, bwd, lambda);
940 SumDerivTensor1DKernel<false, true>(ilane, nq0, D0, deriv, bwd);
941 IProductWRTBaseSegSumFacKernel<false, false>(
942 ilane, nm0, nq0, basis0, bwd, outptr, (TData)1.0);
943
944 e += getGlobalRange<0>(threadBlock);
945 }
946 });
947}
948
949/**
950 * @brief 2D launcher stringing together the five sum-factorised stages
951 * on device. The intermediates are in @p wsp or, with @p useSharedMemoryWsp, in
952 * the thread block's warp slice of @p shmemptr; each lane uses only its
953 * own column, so no barrier is needed.
954 */
955template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
956 bool DEFORMED, bool VARCOEFF, typename TSizeParameter2D,
957 typename TthreadBlock, typename TData, typename TUseSharedWsp,
958 std::enable_if_t<std::is_same_v<Implementation, SumFac>, bool>
959 Enable = true>
961 (GetMaxThreadPerBlock<Implementation, TSizeParameter2D>()))
963 const TSizeParameter2D sizeParam2D, const unsigned int ncoord,
964 const size_t nelmt, [[maybe_unused]] const unsigned int elmtsPerBlock,
965 const bool isModified, const TUseSharedWsp useSharedMemoryWsp,
966 [[maybe_unused]] const unsigned int *NEK_RESTRICT index0,
967 const TData *NEK_RESTRICT basis0, const TData *NEK_RESTRICT basis1,
968 const TData *NEK_RESTRICT D0, const TData *NEK_RESTRICT D1,
969 const TData *NEK_RESTRICT w0, const TData *NEK_RESTRICT w1,
970 const TData *NEK_RESTRICT f0, const TData *NEK_RESTRICT f1,
971 const TData *NEK_RESTRICT nodToMod, const TData *NEK_RESTRICT df,
972 const TData *NEK_RESTRICT jac, const TData *NEK_RESTRICT diffCoeff,
973 const size_t varDiffCoeffSize, const unsigned int nhomo,
974 const TData *NEK_RESTRICT in, TData *NEK_RESTRICT out,
975 TData *NEK_RESTRICT wsp, const TData lambda, unsigned char *shmemptr,
976 const TthreadBlock &threadBlock)
977{
978 static_assert(IsSizeParameter2D_v<TSizeParameter2D>,
979 "Template argument must be either of type "
980 "NonTemplatedSizeParameter2D or TemplatedSizeParameter2D.");
981
983
984 constexpr bool isNodal = SHAPE_TYPE == LibUtilities::NodalTri;
985
986 const unsigned int nm0 = sizeParam2D.nm0();
987 const unsigned int nm1 = sizeParam2D.nm1();
988 const unsigned int nmTot = sizeParam2D.nmTot();
989 const unsigned int nq0 = sizeParam2D.nq0();
990 const unsigned int nq1 = sizeParam2D.nq1();
991 const unsigned int nqTot = sizeParam2D.nqTot();
992
993 const unsigned int ndf = 2 * ncoord;
994 const unsigned int dfsize = DEFORMED ? nqTot : 1u;
995 const unsigned int jacsize = DEFORMED ? nqTot : 1u;
996
997 unsigned int wsp0Size = 0;
998 if constexpr (SHAPE_TYPE == LibUtilities::Quad)
999 {
1000 wsp0Size = nq1;
1001 }
1002 else if constexpr (SHAPE_TYPE == LibUtilities::Tri ||
1003 SHAPE_TYPE == LibUtilities::NodalTri)
1004 {
1005 wsp0Size = std::max(nq1, nm0);
1006 }
1007
1008 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
1009
1010 TData *s_f0 = nullptr;
1011 TData *s_f1 = nullptr;
1012
1013 // Shared workspace, after any factors.
1014 TData *s_wsp = (TData *)shmemptr;
1015
1016 // Pre-compute factor.
1017 if constexpr (SHAPE_TYPE == LibUtilities::Tri ||
1018 SHAPE_TYPE == LibUtilities::NodalTri)
1019 {
1020 s_f0 = (TData *)shmemptr;
1021 s_f1 = s_f0 + nq0;
1022 s_wsp = s_f1 + nq1;
1023
1024 // Copy to shared memory.
1025 LoadToSharedMemory(nq0, f0, s_f0, threadBlock);
1026 LoadToSharedMemory(nq1, f1, s_f1, threadBlock);
1027
1028 localBarrier(threadBlock);
1029 }
1030
1031 size_t e = getGlobalIdx<0>(threadBlock);
1032 const unsigned int c = getBlockIdx<1>(threadBlock);
1033 const unsigned int ncomp = getBlockRange<1>(threadBlock);
1034
1035 DispatchWorkSpace(useSharedMemoryWsp, [&](const auto isShared) {
1036 // Workspace extent: the whole launch, or the thread block's warp.
1037 TData *wspbase = isShared ? s_wsp : wsp;
1038 const size_t wspNelmt = isShared ? warpsize : nelmt * ncomp;
1039 const size_t derivNelmt = isShared ? warpsize : nelmt;
1040 while (e < nelmt)
1041 {
1042 const size_t ilane = e % warpsize;
1043 const size_t iwarp = e / warpsize;
1044 const TData *diffCoeffPtr =
1045 VARCOEFF ? diffCoeff +
1046 nqTot * (nelmt * (c % nhomo) + warpsize * iwarp)
1047 : diffCoeff;
1048 const TData *dfptr = df + ndf * dfsize * warpsize * iwarp;
1049 const TData *jacptr =
1050 DEFORMED ? jac + jacsize * warpsize * iwarp : jac + e;
1051 const TData *inptr = in + nmTot * (nelmt * c + warpsize * iwarp);
1052 TData *outptr = out + nmTot * (nelmt * c + warpsize * iwarp);
1053
1054 const size_t wspOffset =
1055 isShared ? 0 : nelmt * c + warpsize * iwarp;
1056 const size_t derivOffset =
1057 isShared ? 0 : ncoord * nelmt * c + warpsize * iwarp;
1058 TData *bwd = wspbase + nqTot * wspOffset;
1059 TData *deriv = wspbase + nqTot * wspNelmt + nqTot * derivOffset;
1060 TData *deriv0 = deriv;
1061 TData *deriv1 = deriv + nqTot * derivNelmt;
1062
1063 TData *modal =
1064 wspbase + (1 + ncoord) * nqTot * wspNelmt + nmTot * wspOffset;
1065 TData *wsp0 =
1066 wspbase +
1067 ((1 + ncoord) * nqTot + (isNodal ? nmTot : 0u)) * wspNelmt +
1068 wsp0Size * wspOffset;
1069
1070 if constexpr (isNodal)
1071 {
1072 MatVecKernel(ilane, nmTot, nodToMod, inptr, modal);
1073 }
1074
1075 BwdTrans2DSumFacKernel<SHAPE_TYPE, false>(
1076 ilane, nm0, nm1, nq0, nq1, isModified, basis0, basis1,
1077 isNodal ? modal : inptr, bwd, wsp0);
1078
1079 PhysDeriv2DSumFacKernel<SHAPE_TYPE, DEFORMED>(
1080 ilane, ncoord, nq0, nq1, derivNelmt * nqTot, D0, D1, s_f0, s_f1,
1081 dfptr, bwd, deriv);
1082 ApplyMetric2DSumFacKernel<SHAPE_TYPE, DEFORMED, VARCOEFF>(
1083 ilane, ncoord, nq0, nq1, derivNelmt * nqTot, w0, w1, s_f0, s_f1,
1084 dfptr, jacptr, diffCoeffPtr, varDiffCoeffSize, deriv, deriv0,
1085 deriv1, bwd, lambda);
1086 SumDerivTensor2DKernel<false, true>(ilane, nq0, nq1, D0, D1, deriv0,
1087 deriv1, bwd);
1088 IProductWRTBase2DSumFacKernel<SHAPE_TYPE, false, false>(
1089 ilane, nm0, nm1, nq0, nq1, isModified, basis0, basis1, bwd,
1090 isNodal ? modal : outptr, wsp0, (TData)1.0);
1091
1092 if constexpr (isNodal)
1093 {
1094 // Multiply by transpose nodToMod to transform coeffs.
1095 MatVecKernel<false, true>(ilane, nmTot, nodToMod, modal,
1096 outptr);
1097 }
1098
1099 e += getGlobalRange<0>(threadBlock);
1100 }
1101 });
1102}
1103
1104/**
1105 * @brief 3D launcher stringing together the five sum-factorised stages
1106 * on device. The intermediates are in @p wsp or, with @p useSharedMemoryWsp, in
1107 * the thread block's warp slice of @p shmemptr; each lane uses only its
1108 * own column, so no barrier is needed.
1109 */
1110template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
1111 bool DEFORMED, bool VARCOEFF, typename TSizeParameter3D,
1112 typename TthreadBlock, typename TData, typename TUseSharedWsp,
1113 std::enable_if_t<std::is_same_v<Implementation, SumFac>, bool>
1114 Enable = true>
1116 (GetMaxThreadPerBlock<Implementation, TSizeParameter3D>()))
1118 const TSizeParameter3D sizeParam3D,
1119 [[maybe_unused]] const unsigned int ncoord, const size_t nelmt,
1120 [[maybe_unused]] const unsigned int elmtsPerBlock,
1121 const bool isModified, const TUseSharedWsp useSharedMemoryWsp,
1122 [[maybe_unused]] const unsigned int *NEK_RESTRICT index0,
1123 [[maybe_unused]] const unsigned int *NEK_RESTRICT index1,
1124 [[maybe_unused]] const unsigned int *NEK_RESTRICT index2,
1125 [[maybe_unused]] const unsigned int *NEK_RESTRICT index3,
1126 const TData *NEK_RESTRICT basis0, const TData *NEK_RESTRICT basis1,
1127 const TData *NEK_RESTRICT basis2, const TData *NEK_RESTRICT D0,
1128 const TData *NEK_RESTRICT D1, const TData *NEK_RESTRICT D2,
1129 const TData *NEK_RESTRICT w0, const TData *NEK_RESTRICT w1,
1130 const TData *NEK_RESTRICT w2, const TData *NEK_RESTRICT f0,
1131 const TData *NEK_RESTRICT f1, const TData *NEK_RESTRICT f1m,
1132 const TData *NEK_RESTRICT f2, const TData *NEK_RESTRICT nodToMod,
1133 const TData *NEK_RESTRICT df, const TData *NEK_RESTRICT jac,
1134 const TData *NEK_RESTRICT diffCoeff, const size_t varDiffCoeffSize,
1135 const unsigned int nhomo, const TData *NEK_RESTRICT in,
1136 TData *NEK_RESTRICT out, TData *NEK_RESTRICT wsp, const TData lambda,
1137 unsigned char *shmemptr, const TthreadBlock &threadBlock)
1138{
1139 static_assert(IsSizeParameter3D_v<TSizeParameter3D>,
1140 "Template argument must be either of type "
1141 "NonTemplatedSizeParameter3D or TemplatedSizeParameter3D.");
1142
1144
1145 constexpr bool isNodal = SHAPE_TYPE == LibUtilities::NodalTet ||
1146 SHAPE_TYPE == LibUtilities::NodalPrism ||
1147 SHAPE_TYPE == LibUtilities::NodalPyr;
1148
1149 const unsigned int nm0 = sizeParam3D.nm0();
1150 const unsigned int nm1 = sizeParam3D.nm1();
1151 const unsigned int nm2 = sizeParam3D.nm2();
1152 const unsigned int nmTot = sizeParam3D.nmTot();
1153 const unsigned int nq0 = sizeParam3D.nq0();
1154 const unsigned int nq1 = sizeParam3D.nq1();
1155 const unsigned int nq2 = sizeParam3D.nq2();
1156 const unsigned int nqTot = sizeParam3D.nqTot();
1157
1158 constexpr unsigned int ndf = 9u;
1159 const unsigned int dfsize = DEFORMED ? nqTot : 1u;
1160 const unsigned int jacsize = DEFORMED ? nqTot : 1u;
1161
1162 unsigned int wsp0Size = 0, wsp1Size = 0;
1163 if constexpr (SHAPE_TYPE == LibUtilities::Hex)
1164 {
1165 wsp0Size = nq1 * nq2;
1166 wsp1Size = nq2;
1167 }
1168 else if constexpr (SHAPE_TYPE == LibUtilities::Tet ||
1169 SHAPE_TYPE == LibUtilities::NodalTet)
1170 {
1171 wsp0Size = nq1 * nq2;
1172 wsp1Size = std::max(nq2, nm0);
1173 }
1174 else if constexpr (SHAPE_TYPE == LibUtilities::Prism ||
1175 SHAPE_TYPE == LibUtilities::NodalPrism ||
1176 SHAPE_TYPE == LibUtilities::Pyr ||
1177 SHAPE_TYPE == LibUtilities::NodalPyr)
1178 {
1179 wsp0Size = std::max(nq1 * nq2, nm0 * nm1);
1180 wsp1Size = std::max(nq2, nm0);
1181 }
1182
1183 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
1184
1185 TData *s_f0 = nullptr;
1186 TData *s_f1 = nullptr;
1187 TData *s_f1m = nullptr;
1188 TData *s_f2 = nullptr;
1189
1190 // Shared workspace, after any factors.
1191 TData *s_wsp = (TData *)shmemptr;
1192
1193 // Pre-compute factor.
1194 if constexpr (SHAPE_TYPE == LibUtilities::Tet ||
1195 SHAPE_TYPE == LibUtilities::NodalTet)
1196 {
1197 s_f0 = (TData *)shmemptr;
1198 s_f1 = s_f0 + nq0;
1199 s_f1m = s_f1 + nq1;
1200 s_f2 = s_f1m + nq1;
1201 s_wsp = s_f2 + nq2;
1202
1203 // Copy to shared memory.
1204 LoadToSharedMemory(nq0, f0, s_f0, threadBlock);
1205 LoadToSharedMemory(nq1, f1, s_f1, threadBlock);
1206 LoadToSharedMemory(nq1, f1m, s_f1m, threadBlock);
1207 LoadToSharedMemory(nq2, f2, s_f2, threadBlock);
1208
1209 localBarrier(threadBlock);
1210 }
1211 else if constexpr (SHAPE_TYPE == LibUtilities::Prism ||
1212 SHAPE_TYPE == LibUtilities::NodalPrism)
1213 {
1214 s_f0 = (TData *)shmemptr;
1215 s_f2 = s_f0 + nq0;
1216 s_wsp = s_f2 + nq2;
1217
1218 // Copy to shared memory.
1219 LoadToSharedMemory(nq0, f0, s_f0, threadBlock);
1220 LoadToSharedMemory(nq2, f2, s_f2, threadBlock);
1221
1222 localBarrier(threadBlock);
1223 }
1224 else if constexpr (SHAPE_TYPE == LibUtilities::Pyr ||
1225 SHAPE_TYPE == LibUtilities::NodalPyr)
1226 {
1227 s_f0 = (TData *)shmemptr;
1228 s_f1 = s_f0 + nq0;
1229 s_f2 = s_f1 + nq1;
1230 s_wsp = s_f2 + nq2;
1231
1232 // Copy to shared memory.
1233 LoadToSharedMemory(nq0, f0, s_f0, threadBlock);
1234 LoadToSharedMemory(nq1, f1, s_f1, threadBlock);
1235 LoadToSharedMemory(nq2, f2, s_f2, threadBlock);
1236
1237 localBarrier(threadBlock);
1238 }
1239
1240 size_t e = getGlobalIdx<0>(threadBlock); // use size_t to prevent overflow
1241 const unsigned int c = getBlockIdx<1>(threadBlock);
1242 const unsigned int ncomp = getBlockRange<1>(threadBlock);
1243
1244 DispatchWorkSpace(useSharedMemoryWsp, [&](const auto isShared) {
1245 // Workspace extent: the whole launch, or the thread block's warp.
1246 TData *wspbase = isShared ? s_wsp : wsp;
1247 const size_t wspNelmt = isShared ? warpsize : nelmt * ncomp;
1248 const size_t derivNelmt = isShared ? warpsize : nelmt;
1249 while (e < nelmt)
1250 {
1251 const size_t ilane = e % warpsize;
1252 const size_t iwarp = e / warpsize;
1253 const TData *diffCoeffPtr =
1254 VARCOEFF ? diffCoeff +
1255 nqTot * (nelmt * (c % nhomo) + warpsize * iwarp)
1256 : diffCoeff;
1257 const TData *dfptr = df + ndf * dfsize * warpsize * iwarp;
1258 const TData *jacptr =
1259 DEFORMED ? jac + jacsize * warpsize * iwarp : jac + e;
1260 const TData *inptr = in + nmTot * (nelmt * c + warpsize * iwarp);
1261 TData *outptr = out + nmTot * (nelmt * c + warpsize * iwarp);
1262
1263 const size_t wspOffset =
1264 isShared ? 0 : nelmt * c + warpsize * iwarp;
1265 const size_t derivOffset =
1266 isShared ? 0 : 3 * nelmt * c + warpsize * iwarp;
1267 TData *bwd = wspbase + nqTot * wspOffset;
1268 TData *deriv = wspbase + nqTot * wspNelmt + nqTot * derivOffset;
1269 TData *deriv0 = deriv;
1270 TData *deriv1 = deriv + nqTot * derivNelmt;
1271 TData *deriv2 = deriv + 2 * nqTot * derivNelmt;
1272
1273 TData *modal = wspbase + 4 * nqTot * wspNelmt + nmTot * wspOffset;
1274 TData *wsp0 = wspbase +
1275 (4 * nqTot + (isNodal ? nmTot : 0u)) * wspNelmt +
1276 wsp0Size * wspOffset;
1277 TData *wsp1 =
1278 wspbase +
1279 (4 * nqTot + (isNodal ? nmTot : 0u) + wsp0Size) * wspNelmt +
1280 wsp1Size * wspOffset;
1281
1282 if constexpr (isNodal)
1283 {
1284 MatVecKernel(ilane, nmTot, nodToMod, inptr, modal);
1285 }
1286
1287 BwdTrans3DSumFacKernel<SHAPE_TYPE, false>(
1288 ilane, nm0, nm1, nm2, nq0, nq1, nq2, isModified, basis0, basis1,
1289 basis2, isNodal ? modal : inptr, bwd, wsp0, wsp1);
1290 PhysDeriv3DSumFacKernel<SHAPE_TYPE, DEFORMED>(
1291 ilane, nq0, nq1, nq2, derivNelmt * nqTot, D0, D1, D2, s_f0,
1292 s_f1, s_f1m, s_f2, dfptr, bwd, deriv);
1293 ApplyMetric3DSumFacKernel<SHAPE_TYPE, DEFORMED, VARCOEFF>(
1294 ilane, nq0, nq1, nq2, derivNelmt * nqTot, w0, w1, w2, s_f0,
1295 s_f1, s_f1m, s_f2, dfptr, jacptr, diffCoeffPtr,
1296 varDiffCoeffSize, deriv, deriv0, deriv1, deriv2, bwd, lambda);
1297 SumDerivTensor3DKernel<true, true>(ilane, nq0, nq1, nq2, D0, D1, D2,
1298 deriv0, deriv1, deriv2, bwd);
1299 IProductWRTBase3DSumFacKernel<SHAPE_TYPE, false, false>(
1300 ilane, nm0, nm1, nm2, nq0, nq1, nq2, isModified, basis0, basis1,
1301 basis2, bwd, isNodal ? modal : outptr, wsp0, wsp1, (TData)1.0);
1302
1303 if constexpr (isNodal)
1304 {
1305 // Multiply by transpose nodToMod to transform coeffs.
1306 MatVecKernel<false, true>(ilane, nmTot, nodToMod, modal,
1307 outptr);
1308 }
1309
1310 e += getGlobalRange<0>(threadBlock);
1311 }
1312 });
1313}
1314
1315#endif
1316
1317} // namespace Nektar::MultiRegions::detail
#define NEK_RESTRICT
Definition Backends.hpp:254
#define NEK_DEVICE_INLINE
Definition Backends.hpp:283
#define NEK_DEVICE_KERNEL
Definition Backends.hpp:297
Device SumFac kernels of the 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,...
Device SumFac kernels of the physical derivative: one element per warp lane.
std::map< int64_t, double > sum
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
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
static NEK_FORCE_INLINE void HelmholtzKernelLauncher(const TSizeParameter1D sizeParam1D, const bool isModified, const unsigned int ncoord, const bool useDiffCoeff, const typename simd_type::scalarType *diffCoeff, const simd_type *varDiffCoeff, const size_t varDiffCoeffSize, const typename simd_type::scalarType *basis0, const typename simd_type::scalarType *D0, const typename simd_type::scalarType *w0, const simd_type *NtoM, const simd_type *NtoMTrans, const simd_type *jac, const simd_type *df, simd_type *deriv0, simd_type *bwd, const simd_type *in, simd_type *out, const typename simd_type::scalarType lambda)
1D launcher stringing together the five sum-factorised stages for one element group.
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