Nektar++
Loading...
Searching...
No Matches
AdvectionDealiasBlockOpDeviceSumFacKernels.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: AdvectionDealiasBlockOpDeviceSumFacKernels.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: Fused per-shape launchers and fine-grid product for 3/2-rule
32// dealiased advection, SumFac (warp-per-element) convention.
33//
34///////////////////////////////////////////////////////////////////////////////
35
36#pragma once
37
41
44
46{
47
48#if defined(NEKTAR_ENABLE_DEVICE) && defined(DEVICE_COMPILE_ONLY)
49
50// Per-warp-group workspace, in TData elements: the reference derivatives, the
51// fine-grid fields, the products, then one tensor-contraction intermediate per
52// contracted direction. The launchers below lay the regions out in that order,
53// and the host sizes its static allocation with the same call: zero with
54// useSharedMemoryWsp, where AdvectionDealiasSharedMemorySize holds one warp
55// group's.
56template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
57 typename TPhysSizeParameter1D,
58 std::enable_if_t<std::is_same_v<Implementation, SumFac> &&
59 IsPhysSizeParameter1D_v<TPhysSizeParameter1D>,
60 bool>
61 Enable = true>
62NEK_HOSTDEVICE_INLINE constexpr size_t AdvectionDealiasWorkSpaceSize(
63 const size_t nelmt, const unsigned int ncoord, const unsigned int ncomp,
64 const TPhysSizeParameter1D sizeParam1D,
65 [[maybe_unused]] const bool localPipeline)
66{
67 const unsigned int nq0 = sizeParam1D.nq0();
68 const unsigned int nq0Fine = 3u * nq0 / 2u;
69
70 return (ncomp * ncoord * nq0 +
71 (ncoord + ncomp * ncoord + ncomp) * nq0Fine) *
72 nelmt;
73}
74
75template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
76 typename TPhysSizeParameter2D,
77 std::enable_if_t<std::is_same_v<Implementation, SumFac> &&
78 IsPhysSizeParameter2D_v<TPhysSizeParameter2D>,
79 bool>
80 Enable = true>
81NEK_HOSTDEVICE_INLINE constexpr size_t AdvectionDealiasWorkSpaceSize(
82 const size_t nelmt, const unsigned int ncoord, const unsigned int ncomp,
83 const TPhysSizeParameter2D sizeParam2D,
84 [[maybe_unused]] const bool localPipeline)
85{
86 const unsigned int nq0 = sizeParam2D.nq0();
87 const unsigned int nq0Fine = 3u * nq0 / 2u;
88
89 const unsigned int nq1 = sizeParam2D.nq1();
90 const unsigned int nq1Fine =
91 (nq0 - nq1 == 1u) ? nq0Fine - 1u : 3u * nq1 / 2u;
92
93 return (ncomp * ncoord * sizeParam2D.nqTot() +
94 (ncoord + ncomp * ncoord + ncomp) * nq0Fine * nq1Fine +
95 (nq1 + nq1Fine)) *
96 nelmt;
97}
98
99template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
100 typename TPhysSizeParameter3D,
101 std::enable_if_t<std::is_same_v<Implementation, SumFac> &&
102 IsPhysSizeParameter3D_v<TPhysSizeParameter3D>,
103 bool>
104 Enable = true>
105NEK_HOSTDEVICE_INLINE constexpr size_t AdvectionDealiasWorkSpaceSize(
106 const size_t nelmt, const unsigned int ncoord, const unsigned int ncomp,
107 const TPhysSizeParameter3D sizeParam3D,
108 [[maybe_unused]] const bool localPipeline)
109{
110 const unsigned int nq0 = sizeParam3D.nq0();
111 const unsigned int nq0Fine = 3u * nq0 / 2u;
112
113 const unsigned int nq1 = sizeParam3D.nq1();
114 const unsigned int nq2 = sizeParam3D.nq2();
115 const unsigned int nq1Fine =
116 (nq0 - nq1 == 1u) ? nq0Fine - 1u : 3u * nq1 / 2u;
117 const unsigned int nq2Fine =
118 (nq0 - nq2 == 1u) ? nq0Fine - 1u : 3u * nq2 / 2u;
119
120 return (ncomp * ncoord * sizeParam3D.nqTot() +
121 (ncoord + ncomp * ncoord + ncomp) * nq0Fine * nq1Fine * nq2Fine +
122 (nq1 * nq2 + nq1Fine * nq2Fine) + (nq2 + nq2Fine)) *
123 nelmt;
124}
125
126// SumFac has no per-element shared tier: the workspace lives either wholly
127// in the static allocation or, with useSharedMemoryWsp, wholly in shared
128// memory.
129template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
130 typename TPhysSizeParameter,
131 std::enable_if_t<std::is_same_v<Implementation, SumFac>, bool>
132 Enable = true>
133NEK_HOSTDEVICE_INLINE constexpr unsigned int AdvectionDealiasSharedWorkSpaceSize(
134 [[maybe_unused]] const TPhysSizeParameter sizeParam,
135 [[maybe_unused]] const bool localPipeline,
136 [[maybe_unused]] const bool sharedScratch)
137{
138 return 0u;
139}
140
141// Shared memory holds the collapsed-coordinate broadcast tables the reused
142// PhysDeriv kernels read, PhysDeriv's own requirement, followed with
143// useSharedMemoryWsp by the workspace of the thread block's elmtsPerBlock
144// elements.
145template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
146 typename TPhysSizeParameter,
147 std::enable_if_t<std::is_same_v<Implementation, SumFac>, bool>
148 Enable = true>
149inline constexpr unsigned int AdvectionDealiasSharedMemorySize(
150 const unsigned int ncoord, const unsigned int ncomp,
151 const TPhysSizeParameter sizeParam, const unsigned int elmtsPerBlock,
152 const bool localPipeline, [[maybe_unused]] const bool sharedScratch,
153 const bool useSharedMemoryWsp)
154{
155 const unsigned int wspSize =
157 ? static_cast<unsigned int>(
158 AdvectionDealiasWorkSpaceSize<SHAPE_TYPE, Implementation>(
159 elmtsPerBlock, ncoord, ncomp, sizeParam, localPipeline))
160 : 0u;
161
162 return PhysDerivSharedMemorySize<SHAPE_TYPE, Implementation>(
163 sizeParam, elmtsPerBlock, false) +
164 wspSize;
165}
166
167// Stage C: advVel/grad/out are already offset to this warp-group's data;
168// ilane selects the lane (element) within the warp.
169template <bool APPEND, typename TData>
170NEK_DEVICE_INLINE static void AdvectionDealiasCombineSumFacKernel(
171 const unsigned int ilane, const unsigned int nqTot,
172 const unsigned int coordDim, const TData *NEK_RESTRICT advVel,
173 const size_t advVelOffset, const TData *NEK_RESTRICT grad,
174 const size_t gradOffset, TData *NEK_RESTRICT out, const TData scale)
175{
176 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
177
178 for (unsigned int j = 0u; j < nqTot; ++j)
179 {
180 TData tmp = advVel[warpsize * j + ilane] * grad[warpsize * j + ilane];
181#pragma unroll
182 for (unsigned int d = 1u; d < coordDim; ++d)
183 {
184 // advVelOffset/gradOffset are already warpsize-scaled strides
185 // (matching the caller's nqTotFine*warpsize), so only the `j`
186 // term needs the extra warpsize factor here - not the whole
187 // (d*offset+j) sum.
188 tmp += advVel[d * advVelOffset + warpsize * j + ilane] *
189 grad[d * gradOffset + warpsize * j + ilane];
190 }
191
192 if constexpr (APPEND)
193 {
194 out[warpsize * j + ilane] += scale * tmp;
195 }
196 else
197 {
198 out[warpsize * j + ilane] = scale * tmp;
199 }
200 }
201}
202
203template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
204 bool APPEND, bool DEFORMED, bool LOCAL_PIPELINE, bool SHARED_SCRATCH,
205 typename TPhysSizeParameter1D, typename TthreadBlock, typename TData,
206 typename TUseSharedWsp,
207 std::enable_if_t<std::is_same_v<Implementation, SumFac>, bool>
208 Enable = true>
209NEK_DEVICE_KERNEL void AdvectionDealiasKernelLauncher(
210 const TPhysSizeParameter1D sizeParam1D, const size_t nelmt,
211 [[maybe_unused]] const unsigned int elmtsPerBlock, const unsigned int ncomp,
212 const unsigned int nhomo, const TUseSharedWsp useSharedMemoryWsp,
213 const TData *NEK_RESTRICT D0, const TData *NEK_RESTRICT df,
214 const TData *NEK_RESTRICT Binterp0, const TData *NEK_RESTRICT Bproject0,
215 const TData scale, const TData *NEK_RESTRICT in, const size_t inCompStride,
216 const TData *NEK_RESTRICT advVel, const size_t advVelPlaneStride,
217 TData *NEK_RESTRICT out, const size_t outCompStride,
218 TData *NEK_RESTRICT elmtWsp, unsigned char *shmemptr,
219 const TthreadBlock &threadBlock)
220{
221 static_assert(
222 IsPhysSizeParameter1D_v<TPhysSizeParameter1D>,
223 "Template argument must be either of type "
224 "NonTemplatedPhysSizeParameter1D or TemplatedPhysSizeParameter1D.");
225
227
228 const unsigned int coordDim = sizeParam1D.ncoord();
229 const unsigned int nq0 = sizeParam1D.nq0();
230 const unsigned int nqTot = sizeParam1D.nqTot();
231 const unsigned int nq0Fine = 3u * nq0 / 2u;
232
233 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
234 const unsigned int nqTotFine = nq0Fine;
235 const unsigned int dfsize = DEFORMED ? nqTot : 1u;
236
237 const size_t advVelCompStride = advVelPlaneStride * nhomo;
238
239 // Per-warp-group stride of the workspace laid out below.
240 const unsigned int wspSize = static_cast<unsigned int>(
241 AdvectionDealiasWorkSpaceSize<SHAPE_TYPE, Implementation>(
242 1u, coordDim, ncomp, sizeParam1D, LOCAL_PIPELINE));
243
244 DispatchWorkSpace(useSharedMemoryWsp, [&](const auto isShared) {
245 // Workspace extent: the whole launch, or the thread block's warp group.
246 TData *wspbase = isShared ? (TData *)shmemptr : elmtWsp;
247
248 size_t e = getGlobalIdx<0>(threadBlock);
249 const unsigned int p = getBlockIdx<1>(threadBlock);
250 while (e < nelmt)
251 {
252 const size_t ilane = e % warpsize;
253 const size_t iwarp = e / warpsize;
254
255 const TData *dfptr = df + coordDim * dfsize * warpsize * iwarp;
256 const TData *inGroup = in + nqTot * warpsize * iwarp;
257 const TData *advVelGroup =
258 advVel + p * advVelPlaneStride + nqTot * warpsize * iwarp;
259 TData *outGroup = out + nqTot * warpsize * iwarp;
260 // Per-warp-group workspace, laid out in this order by both the host
261 // and the kernel: the reference derivatives, the fine-grid fields,
262 // the products, then one tensor-contraction intermediate per
263 // contracted direction.
264 const size_t wspOffset =
265 isShared ? 0 : nelmt * p + warpsize * iwarp;
266 TData *wsp = wspbase + wspSize * wspOffset;
267 TData *derivGroup = wsp;
268 TData *fineGroup = derivGroup + ncomp * coordDim * nqTot * warpsize;
269 TData *combinedGroup = fineGroup + (coordDim + ncomp * coordDim) *
270 nqTotFine * warpsize;
271
272 for (unsigned int d = 0u; d < coordDim; ++d)
273 {
274 BwdTransSegSumFacKernel<false>(
275 ilane, nq0, nq0Fine, Binterp0,
276 advVelGroup + d * advVelCompStride,
277 fineGroup + d * nqTotFine * warpsize);
278 }
279
280 // One pass per component: reference derivatives on the native grid,
281 // interpolation of those derivatives to the fine grid, the
282 // advection product there, and the Galerkin projection back.
283 for (unsigned int c = 0u; c < ncomp; ++c)
284 {
285 TData *derivOut = derivGroup + c * coordDim * nqTot * warpsize;
286 TData *gradFine = fineGroup + (coordDim + c * coordDim) *
287 nqTotFine * warpsize;
288 TData *combinedOut = combinedGroup + c * nqTotFine * warpsize;
289
290 PhysDeriv1DSumFacKernel<DEFORMED>(
291 ilane, coordDim, nq0, nqTot * warpsize, D0, dfptr,
292 inGroup + (c * nhomo + p) * inCompStride, derivOut);
293
294 for (unsigned int d = 0u; d < coordDim; ++d)
295 {
296 BwdTransSegSumFacKernel<false>(
297 ilane, nq0, nq0Fine, Binterp0,
298 derivOut + d * nqTot * warpsize,
299 gradFine + d * nqTotFine * warpsize);
300 }
301
302 AdvectionDealiasCombineSumFacKernel<false>(
303 ilane, nqTotFine, coordDim, fineGroup, nqTotFine * warpsize,
304 gradFine, nqTotFine * warpsize, combinedOut, scale);
305
306 TData *outptr = outGroup + (c * nhomo + p) * outCompStride;
307 if constexpr (APPEND)
308 {
309 BwdTransSegSumFacKernel<true>(
310 ilane, nq0Fine, nq0, Bproject0, combinedOut, outptr);
311 }
312 else
313 {
314 BwdTransSegSumFacKernel<false>(
315 ilane, nq0Fine, nq0, Bproject0, combinedOut, outptr);
316 }
317 }
318
319 e += getGlobalRange<0>(threadBlock);
320 }
321 });
322}
323
324template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
325 bool APPEND, bool DEFORMED, bool LOCAL_PIPELINE, bool SHARED_SCRATCH,
326 typename TPhysSizeParameter2D, typename TthreadBlock, typename TData,
327 typename TUseSharedWsp,
328 std::enable_if_t<std::is_same_v<Implementation, SumFac>, bool>
329 Enable = true>
330NEK_DEVICE_KERNEL void AdvectionDealiasKernelLauncher(
331 const TPhysSizeParameter2D sizeParam2D, const size_t nelmt,
332 [[maybe_unused]] const unsigned int elmtsPerBlock, const unsigned int ncomp,
333 const unsigned int nhomo, const TUseSharedWsp useSharedMemoryWsp,
334 const TData *NEK_RESTRICT D0, const TData *NEK_RESTRICT D1,
335 const TData *NEK_RESTRICT f0, const TData *NEK_RESTRICT f1,
336 const TData *NEK_RESTRICT df, const TData *NEK_RESTRICT Binterp0,
337 const TData *NEK_RESTRICT Binterp1, const TData *NEK_RESTRICT Bproject0,
338 const TData *NEK_RESTRICT Bproject1, const TData scale,
339 const TData *NEK_RESTRICT in, const size_t inCompStride,
340 const TData *NEK_RESTRICT advVel, const size_t advVelPlaneStride,
341 TData *NEK_RESTRICT out, const size_t outCompStride,
342 TData *NEK_RESTRICT elmtWsp, unsigned char *shmemptr,
343 const TthreadBlock &threadBlock)
344{
345 static_assert(
346 IsPhysSizeParameter2D_v<TPhysSizeParameter2D>,
347 "Template argument must be either of type "
348 "NonTemplatedPhysSizeParameter2D or TemplatedPhysSizeParameter2D.");
349
351
352 const unsigned int coordDim = sizeParam2D.ncoord();
353 const unsigned int nq0 = sizeParam2D.nq0();
354 const unsigned int nq1 = sizeParam2D.nq1();
355 const unsigned int nqTot = sizeParam2D.nqTot();
356 const unsigned int nq0Fine = 3u * nq0 / 2u;
357 const unsigned int nq1Fine =
358 (nq0 - nq1 == 1u) ? nq0Fine - 1u : 3u * nq1 / 2u;
359 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
360 const unsigned int nqTotFine = nq0Fine * nq1Fine;
361 const unsigned int ndf = 2u * coordDim;
362 const unsigned int dfsize = DEFORMED ? nqTot : 1u;
363
364 TData *s_f0 = nullptr;
365 TData *s_f1 = nullptr;
366 TData *s_wsp = (TData *)shmemptr;
367
368 // Precompute geometric factors.
369 if constexpr (SHAPE_TYPE == LibUtilities::Tri ||
370 SHAPE_TYPE == LibUtilities::NodalTri)
371 {
372 s_f0 = (TData *)shmemptr;
373 s_f1 = s_f0 + nq0;
374
375 // Copy to shared memory.
376 LoadToSharedMemory(nq0, f0, s_f0, threadBlock);
377 LoadToSharedMemory(nq1, f1, s_f1, threadBlock);
378 s_wsp = s_f1 + nq1;
379
380 localBarrier(threadBlock);
381 }
382
383 const size_t advVelCompStride = advVelPlaneStride * nhomo;
384
385 // Per-warp-group stride of the workspace laid out below.
386 const unsigned int wspSize = static_cast<unsigned int>(
387 AdvectionDealiasWorkSpaceSize<SHAPE_TYPE, Implementation>(
388 1u, coordDim, ncomp, sizeParam2D, LOCAL_PIPELINE));
389
390 DispatchWorkSpace(useSharedMemoryWsp, [&](const auto isShared) {
391 // Workspace extent: the whole launch, or the thread block's warp group.
392 TData *wspbase = isShared ? s_wsp : elmtWsp;
393
394 size_t e = getGlobalIdx<0>(threadBlock);
395 const unsigned int p = getBlockIdx<1>(threadBlock);
396 while (e < nelmt)
397 {
398 const size_t ilane = e % warpsize;
399 const size_t iwarp = e / warpsize;
400
401 const TData *dfptr = df + ndf * dfsize * warpsize * iwarp;
402 const TData *inGroup = in + nqTot * warpsize * iwarp;
403 const TData *advVelGroup =
404 advVel + p * advVelPlaneStride + nqTot * warpsize * iwarp;
405 TData *outGroup = out + nqTot * warpsize * iwarp;
406 // Per-warp-group workspace, laid out in this order by both the host
407 // and the kernel: the reference derivatives, the fine-grid fields,
408 // the products, then one tensor-contraction intermediate per
409 // contracted direction.
410 const size_t wspOffset =
411 isShared ? 0 : nelmt * p + warpsize * iwarp;
412 TData *wsp = wspbase + wspSize * wspOffset;
413 TData *derivGroup = wsp;
414 TData *fineGroup = derivGroup + ncomp * coordDim * nqTot * warpsize;
415 TData *combinedGroup = fineGroup + (coordDim + ncomp * coordDim) *
416 nqTotFine * warpsize;
417 TData *wsp0 = combinedGroup + ncomp * nqTotFine * warpsize;
418
419 for (unsigned int d = 0u; d < coordDim; ++d)
420 {
421 BwdTransQuadSumFacKernel<false>(
422 ilane, nq0, nq1, nq0Fine, nq1Fine, Binterp0, Binterp1,
423 advVelGroup + d * advVelCompStride,
424 fineGroup + d * nqTotFine * warpsize, wsp0);
425 }
426
427 // One pass per component: reference derivatives on the native grid,
428 // interpolation of those derivatives to the fine grid, the
429 // advection product there, and the Galerkin projection back.
430 for (unsigned int c = 0u; c < ncomp; ++c)
431 {
432 TData *derivOut = derivGroup + c * coordDim * nqTot * warpsize;
433 TData *gradFine = fineGroup + (coordDim + c * coordDim) *
434 nqTotFine * warpsize;
435 TData *combinedOut = combinedGroup + c * nqTotFine * warpsize;
436
437 PhysDeriv2DSumFacKernel<SHAPE_TYPE, DEFORMED>(
438 ilane, coordDim, nq0, nq1, nqTot * warpsize, D0, D1, s_f0,
439 s_f1, dfptr, inGroup + (c * nhomo + p) * inCompStride,
440 derivOut);
441
442 for (unsigned int d = 0u; d < coordDim; ++d)
443 {
444 BwdTransQuadSumFacKernel<false>(
445 ilane, nq0, nq1, nq0Fine, nq1Fine, Binterp0, Binterp1,
446 derivOut + d * nqTot * warpsize,
447 gradFine + d * nqTotFine * warpsize, wsp0);
448 }
449
450 AdvectionDealiasCombineSumFacKernel<false>(
451 ilane, nqTotFine, coordDim, fineGroup, nqTotFine * warpsize,
452 gradFine, nqTotFine * warpsize, combinedOut, scale);
453
454 TData *outptr = outGroup + (c * nhomo + p) * outCompStride;
455 if constexpr (APPEND)
456 {
457 BwdTransQuadSumFacKernel<true>(ilane, nq0Fine, nq1Fine, nq0,
458 nq1, Bproject0, Bproject1,
459 combinedOut, outptr, wsp0);
460 }
461 else
462 {
463 BwdTransQuadSumFacKernel<false>(
464 ilane, nq0Fine, nq1Fine, nq0, nq1, Bproject0, Bproject1,
465 combinedOut, outptr, wsp0);
466 }
467 }
468
469 e += getGlobalRange<0>(threadBlock);
470 }
471 });
472}
473
474template <LibUtilities::ShapeType SHAPE_TYPE, typename Implementation,
475 bool APPEND, bool DEFORMED, bool LOCAL_PIPELINE, bool SHARED_SCRATCH,
476 typename TPhysSizeParameter3D, typename TthreadBlock, typename TData,
477 typename TUseSharedWsp,
478 std::enable_if_t<std::is_same_v<Implementation, SumFac>, bool>
479 Enable = true>
480NEK_DEVICE_KERNEL void AdvectionDealiasKernelLauncher(
481 const TPhysSizeParameter3D sizeParam3D, const size_t nelmt,
482 [[maybe_unused]] const unsigned int elmtsPerBlock, const unsigned int ncomp,
483 const unsigned int nhomo, const TUseSharedWsp useSharedMemoryWsp,
484 const TData *NEK_RESTRICT D0, const TData *NEK_RESTRICT D1,
485 const TData *NEK_RESTRICT D2, const TData *NEK_RESTRICT f0,
486 const TData *NEK_RESTRICT f1, const TData *NEK_RESTRICT f1m,
487 const TData *NEK_RESTRICT f2, const TData *NEK_RESTRICT df,
488 const TData *NEK_RESTRICT Binterp0, const TData *NEK_RESTRICT Binterp1,
489 const TData *NEK_RESTRICT Binterp2, const TData *NEK_RESTRICT Bproject0,
490 const TData *NEK_RESTRICT Bproject1, const TData *NEK_RESTRICT Bproject2,
491 const TData scale, const TData *NEK_RESTRICT in, const size_t inCompStride,
492 const TData *NEK_RESTRICT advVel, const size_t advVelPlaneStride,
493 TData *NEK_RESTRICT out, const size_t outCompStride,
494 TData *NEK_RESTRICT elmtWsp, unsigned char *shmemptr,
495 const TthreadBlock &threadBlock)
496{
497 static_assert(
498 IsPhysSizeParameter3D_v<TPhysSizeParameter3D>,
499 "Template argument must be either of type "
500 "NonTemplatedPhysSizeParameter3D or TemplatedPhysSizeParameter3D.");
501
503
504 const unsigned int nq0 = sizeParam3D.nq0();
505 const unsigned int nq1 = sizeParam3D.nq1();
506 const unsigned int nq2 = sizeParam3D.nq2();
507 const unsigned int nqTot = sizeParam3D.nqTot();
508 const unsigned int nq0Fine = 3u * nq0 / 2u;
509 const unsigned int nq1Fine =
510 (nq0 - nq1 == 1u) ? nq0Fine - 1u : 3u * nq1 / 2u;
511 const unsigned int nq2Fine =
512 (nq0 - nq2 == 1u) ? nq0Fine - 1u : 3u * nq2 / 2u;
513 constexpr unsigned int warpsize = NektarSpaces::Device::warpSize;
514 constexpr unsigned int coordDim3D = 3u;
515 const unsigned int nqTotFine = nq0Fine * nq1Fine * nq2Fine;
516 constexpr unsigned int ndf = 9u;
517 const unsigned int dfsize = DEFORMED ? nqTot : 1u;
518
519 TData *s_f0 = nullptr;
520 TData *s_f1 = nullptr;
521 TData *s_f1m = nullptr;
522 TData *s_f2 = nullptr;
523 TData *s_wsp = (TData *)shmemptr;
524
525 // Precompute geometric factors.
526 if constexpr (SHAPE_TYPE == LibUtilities::Tet ||
527 SHAPE_TYPE == LibUtilities::NodalTet)
528 {
529 s_f0 = (TData *)shmemptr;
530 s_f1 = s_f0 + nq0;
531 s_f1m = s_f1 + nq1;
532 s_f2 = s_f1m + nq1;
533
534 // Copy to shared memory.
535 LoadToSharedMemory(nq0, f0, s_f0, threadBlock);
536 LoadToSharedMemory(nq1, f1, s_f1, threadBlock);
537 LoadToSharedMemory(nq1, f1m, s_f1m, threadBlock);
538 LoadToSharedMemory(nq2, f2, s_f2, threadBlock);
539 s_wsp = s_f2 + nq2;
540
541 localBarrier(threadBlock);
542 }
543 else if constexpr (SHAPE_TYPE == LibUtilities::Prism ||
544 SHAPE_TYPE == LibUtilities::NodalPrism)
545 {
546 s_f0 = (TData *)shmemptr;
547 s_f2 = s_f0 + nq0;
548
549 // Copy to shared memory.
550 LoadToSharedMemory(nq0, f0, s_f0, threadBlock);
551 LoadToSharedMemory(nq2, f2, s_f2, threadBlock);
552 s_wsp = s_f2 + nq2;
553
554 localBarrier(threadBlock);
555 }
556 else if constexpr (SHAPE_TYPE == LibUtilities::Pyr ||
557 SHAPE_TYPE == LibUtilities::NodalPyr)
558 {
559 s_f0 = (TData *)shmemptr;
560 s_f1 = s_f0 + nq0;
561 s_f2 = s_f1 + nq1;
562
563 // Copy to shared memory.
564 LoadToSharedMemory(nq0, f0, s_f0, threadBlock);
565 LoadToSharedMemory(nq1, f1, s_f1, threadBlock);
566 LoadToSharedMemory(nq2, f2, s_f2, threadBlock);
567 s_wsp = s_f2 + nq2;
568
569 localBarrier(threadBlock);
570 }
571
572 const size_t advVelCompStride = advVelPlaneStride * nhomo;
573
574 // Per-warp-group stride of the workspace laid out below.
575 const unsigned int wspSize = static_cast<unsigned int>(
576 AdvectionDealiasWorkSpaceSize<SHAPE_TYPE, Implementation>(
577 1u, coordDim3D, ncomp, sizeParam3D, LOCAL_PIPELINE));
578
579 DispatchWorkSpace(useSharedMemoryWsp, [&](const auto isShared) {
580 // Workspace extent: the whole launch, or the thread block's warp group.
581 TData *wspbase = isShared ? s_wsp : elmtWsp;
582
583 size_t e = getGlobalIdx<0>(threadBlock);
584 const unsigned int p = getBlockIdx<1>(threadBlock);
585 while (e < nelmt)
586 {
587 const size_t ilane = e % warpsize;
588 const size_t iwarp = e / warpsize;
589
590 const TData *dfptr = df + ndf * dfsize * warpsize * iwarp;
591 const TData *inGroup = in + nqTot * warpsize * iwarp;
592 const TData *advVelGroup =
593 advVel + p * advVelPlaneStride + nqTot * warpsize * iwarp;
594 TData *outGroup = out + nqTot * warpsize * iwarp;
595 // Per-warp-group workspace, laid out in this order by both the host
596 // and the kernel: the reference derivatives, the fine-grid fields,
597 // the products, then one tensor-contraction intermediate per
598 // contracted direction.
599 const size_t wspOffset =
600 isShared ? 0 : nelmt * p + warpsize * iwarp;
601 TData *wsp = wspbase + wspSize * wspOffset;
602 TData *derivGroup = wsp;
603 TData *fineGroup =
604 derivGroup + ncomp * coordDim3D * nqTot * warpsize;
605 TData *combinedGroup =
606 fineGroup +
607 (coordDim3D + ncomp * coordDim3D) * nqTotFine * warpsize;
608 TData *wsp0 = combinedGroup + ncomp * nqTotFine * warpsize;
609 TData *wsp1 = wsp0 + (nq1 * nq2 + nq1Fine * nq2Fine) * warpsize;
610
611 // The direction count is a compile time constant, so this is
612 // unrolled rather than run.
613#pragma unroll
614 for (unsigned int d = 0u; d < coordDim3D; ++d)
615 {
616 BwdTransHexSumFacKernel<false>(
617 ilane, nq0, nq1, nq2, nq0Fine, nq1Fine, nq2Fine, Binterp0,
618 Binterp1, Binterp2, advVelGroup + d * advVelCompStride,
619 fineGroup + d * nqTotFine * warpsize, wsp0, wsp1);
620 }
621
622 // One pass per component: reference derivatives on the native grid,
623 // interpolation of those derivatives to the fine grid, the
624 // advection product there, and the Galerkin projection back.
625 for (unsigned int c = 0u; c < ncomp; ++c)
626 {
627 TData *derivOut =
628 derivGroup + c * coordDim3D * nqTot * warpsize;
629 TData *gradFine = fineGroup + (coordDim3D + c * coordDim3D) *
630 nqTotFine * warpsize;
631 TData *combinedOut = combinedGroup + c * nqTotFine * warpsize;
632
633 PhysDeriv3DSumFacKernel<SHAPE_TYPE, DEFORMED>(
634 ilane, nq0, nq1, nq2, nqTot * warpsize, D0, D1, D2, s_f0,
635 s_f1, s_f1m, s_f2, dfptr,
636 inGroup + (c * nhomo + p) * inCompStride, derivOut);
637
638#pragma unroll
639 for (unsigned int d = 0u; d < coordDim3D; ++d)
640 {
641 BwdTransHexSumFacKernel<false>(
642 ilane, nq0, nq1, nq2, nq0Fine, nq1Fine, nq2Fine,
643 Binterp0, Binterp1, Binterp2,
644 derivOut + d * nqTot * warpsize,
645 gradFine + d * nqTotFine * warpsize, wsp0, wsp1);
646 }
647
648 AdvectionDealiasCombineSumFacKernel<false>(
649 ilane, nqTotFine, coordDim3D, fineGroup,
650 nqTotFine * warpsize, gradFine, nqTotFine * warpsize,
651 combinedOut, scale);
652
653 TData *outptr = outGroup + (c * nhomo + p) * outCompStride;
654 if constexpr (APPEND)
655 {
656 BwdTransHexSumFacKernel<true>(
657 ilane, nq0Fine, nq1Fine, nq2Fine, nq0, nq1, nq2,
658 Bproject0, Bproject1, Bproject2, combinedOut, outptr,
659 wsp0, wsp1);
660 }
661 else
662 {
663 BwdTransHexSumFacKernel<false>(
664 ilane, nq0Fine, nq1Fine, nq2Fine, nq0, nq1, nq2,
665 Bproject0, Bproject1, Bproject2, combinedOut, outptr,
666 wsp0, wsp1);
667 }
668 }
669
670 e += getGlobalRange<0>(threadBlock);
671 }
672 });
673}
674
675#endif
676
677} // namespace Nektar::MultiRegions::detail
#define NEK_RESTRICT
Definition Backends.hpp:254
#define NEK_DEVICE_INLINE
Definition Backends.hpp:283
#define NEK_HOSTDEVICE_INLINE
Definition Backends.hpp:268
#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 physical derivative: one element per warp lane.
std::vector< NekDouble > grad
NEK_DEVICE_KERNEL void const TData *NEK_RESTRICT const TData *NEK_RESTRICT const size_t const unsigned int const unsigned int const unsigned int const unsigned int const size_t const TData *NEK_RESTRICT const TData *NEK_RESTRICT const TUseSharedWsp TData *NEK_RESTRICT const TData *NEK_RESTRICT TData *NEK_RESTRICT const bool const bool unsigned char const TthreadBlock & threadBlock
NEK_DEVICE_KERNEL void const TData *NEK_RESTRICT const TData *NEK_RESTRICT const size_t const unsigned int const unsigned int const unsigned int const unsigned int const size_t const TData *NEK_RESTRICT const TData *NEK_RESTRICT const TUseSharedWsp useSharedMemoryWsp
NEK_DEVICE_KERNEL void const TData *NEK_RESTRICT const TData *NEK_RESTRICT const size_t const unsigned int const unsigned int const unsigned int const unsigned int const size_t const TData *NEK_RESTRICT const TData *NEK_RESTRICT const TUseSharedWsp TData *NEK_RESTRICT const TData *NEK_RESTRICT in
NEK_DEVICE_KERNEL void const TData *NEK_RESTRICT const TData *NEK_RESTRICT const size_t const unsigned int const unsigned int const unsigned int const unsigned int const size_t const TData *NEK_RESTRICT const TData *NEK_RESTRICT const TUseSharedWsp TData *NEK_RESTRICT const TData *NEK_RESTRICT TData *NEK_RESTRICT const bool const bool unsigned char * shmemptr
DispatchWorkSpace(useSharedMemoryWsp, [&](const auto isShared) { TData *wspbase=isShared ?(TData *) shmemptr+tableSize :wsp;while(e< nelmt) { const size_t ilane=e % warpsize;const size_t iwarp=e/warpsize;const TData *inptr=in+numDataIn *(nelmt *c+warpsize *iwarp);TData *outptr=out+numDataOut *(nelmt *c+warpsize *iwarp);const size_t wspOffset=isShared ? 0 :nelmt *c+warpsize *iwarp;TData *wspptr=wspbase+(edgeSize+numDataOut) *wspOffset;TData *volptr=wspptr+edgeSize *warpsize;const unsigned int off1=warpsize *2 *tnq00;const unsigned int joff1=(DEFORMED) ? off1 :warpsize *2;for(unsigned int d=0;d< 2;++d) { const bool useD1=CompNeedsCollapsedFac(SHAPE_TYPE, d, 1);const TData *jacptr=jac+d *jacCompStride+numDataJac *warpsize *iwarp;if(!(append||d > 0)) { for(unsigned int p=0;p< numDataOut;++p) { outptr[warpsize *p+ilane]=0.0;} } { TData *dst=useD1 ? volptr :outptr;if(useD1) { for(unsigned int p=0;p< numDataOut;++p) { dst[warpsize *p+ilane]=0.0;} } const TData *nb=(0==d) ? dnbasis0 :nbasis0;const TData *tb=(traceDir0==d) ? dtbasis0 :tbasis0;const bool coll=(traceDir0==d) ? false :isColl0;const bool endp=(0==d) ? false :endPtsColl0;if(endp) { IPWRTPhysTraceEdgeN0Kernel< true, DEFORMED >(ilane, 0, 2, 2, nm0, nm1, nb, tnq00, tb, tw0, jacptr, wspptr, inptr, dst, coll);} else { IPWRTPhysTraceEdgeN0Kernel< false, DEFORMED >(ilane, 0, 2, 2, nm0, nm1, nb, tnq00, tb, tw0, jacptr, wspptr, inptr, dst, coll);} if(useD1) { ScaleAddCollapsedFacKernel(ilane, nm0, nm1, 1u, true, false, s_twoOver1, s_twoOver1, volptr, outptr);} } { const TData *nb=(1==d) ? dnbasis1 :nbasis1;const TData *tb=(traceDir1==d) ? dtbasis1 :tbasis1;const bool coll=(traceDir1==d) ? false :isColl1;const bool endp=(1==d) ? false :endPtsColl1;if(endp) { IPWRTPhysTraceEdgeN1Kernel< true, DEFORMED >(ilane, 0, nedgeN1, nedgeN1, nm0, nm1, nb, tnq10, tb, tw1, jacptr+joff1, wspptr, inptr+off1, outptr, coll);} else { IPWRTPhysTraceEdgeN1Kernel< false, DEFORMED >(ilane, 0, nedgeN1, nedgeN1, nm0, nm1, nb, tnq10, tb, tw1, jacptr+joff1, wspptr, inptr+off1, outptr, coll);} } } e+=getGlobalRange< 0 >(threadBlock);} })
NEK_DEVICE_KERNEL void const TData *NEK_RESTRICT const TData *NEK_RESTRICT const size_t const unsigned int elmtsPerBlock
NEK_DEVICE_KERNEL void const TData *NEK_RESTRICT const TData *NEK_RESTRICT const size_t const unsigned int const unsigned int const unsigned int const unsigned int const size_t const TData *NEK_RESTRICT const TData *NEK_RESTRICT const TUseSharedWsp TData *NEK_RESTRICT wsp
NEK_DEVICE_KERNEL void const TData *NEK_RESTRICT const TData *NEK_RESTRICT const size_t nelmt
NEK_DEVICE_KERNEL void const TData *NEK_RESTRICT const TData *NEK_RESTRICT const size_t const unsigned int const unsigned int const unsigned int const unsigned int const size_t const TData *NEK_RESTRICT const TData *NEK_RESTRICT const TUseSharedWsp TData *NEK_RESTRICT const TData *NEK_RESTRICT TData *NEK_RESTRICT out
std::vector< double > p(NPUPPER)
std::vector< double > d(NPUPPER *NPUPPER)
static constexpr unsigned int warpSize
Definition Backends.hpp:143