Neko 1.99.9
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
conv1_kernel.h
Go to the documentation of this file.
1#ifndef __MATH_CONV1_KERNEL_H__
2#define __MATH_CONV1_KERNEL_H__
3
4#include "mfma_kernel.h"
5
6/*
7 Copyright (c) 2021-2026, The Neko Authors
8 All rights reserved.
9
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions
12 are met:
13
14 * Redistributions of source code must retain the above copyright
15 notice, this list of conditions and the following disclaimer.
16
17 * Redistributions in binary form must reproduce the above
18 copyright notice, this list of conditions and the following
19 disclaimer in the documentation and/or other materials provided
20 with the distribution.
21
22 * Neither the name of the authors nor the names of its
23 contributors may be used to endorse or promote products derived
24 from this software without specific prior written permission.
25
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
29 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
30 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
32 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 POSSIBILITY OF SUCH DAMAGE.
38*/
39
40#include "elem_block.h"
41
46template< typename T, const int LX, const int CHUNKS >
48 const T * __restrict__ u,
49 const T * __restrict__ vx,
50 const T * __restrict__ vy,
51 const T * __restrict__ vz,
52 const T * __restrict__ dx,
53 const T * __restrict__ dy,
54 const T * __restrict__ dz,
55 const T * __restrict__ drdx,
56 const T * __restrict__ dsdx,
57 const T * __restrict__ dtdx,
58 const T * __restrict__ drdy,
59 const T * __restrict__ dsdy,
60 const T * __restrict__ dtdy,
61 const T * __restrict__ drdz,
62 const T * __restrict__ dsdz,
63 const T * __restrict__ dtdz,
64 const T * __restrict__ jacinv) {
65
66 __shared__ T shu[LX * LX * LX];
67
68 __shared__ T shvx[LX * LX * LX];
69 __shared__ T shvy[LX * LX * LX];
70 __shared__ T shvz[LX * LX * LX];
71
75
77
78 const int e = blockIdx.x;
79 const int iii = threadIdx.x;
80 const int nchunks = (LX * LX * LX - 1) / CHUNKS + 1;
81 const int ele = e*LX*LX*LX;
82
83 if (iii < (LX * LX)) {
84 shdx[iii] = dx[iii];
85 shdy[iii] = dy[iii];
86 shdz[iii] = dz[iii];
87 }
88
89 int l = iii;
90 while(l < (LX * LX * LX)) {
91 shu[l] = u[l + ele];
92
93 shvx[l] = vx[l + ele];
94 shvy[l] = vy[l + ele];
95 shvz[l] = vz[l + ele];
96
97 shjacinv[l] = jacinv[l + ele];
98
99 l = l + CHUNKS;
100 }
101
103
104 for (int n = 0; n < nchunks; n++) {
105 const int ijk = iii + n * CHUNKS;
106 const int jk = ijk / LX;
107 const int i = ijk - jk * LX;
108 const int k = jk / LX;
109 const int j = jk - k * LX;
110 if ( i < LX && j < LX && k < LX) {
111 T rtmp = 0.0;
112 T stmp = 0.0;
113 T ttmp = 0.0;
114 for (int l = 0; l < LX; l++) {
115 rtmp += shdx[i + l * LX] * shu[l + j * LX + k * LX * LX];
116 stmp += shdy[j + l * LX] * shu[i + l * LX + k * LX * LX];
117 ttmp += shdz[k + l * LX] * shu[i + j * LX + l * LX * LX];
118 }
119
120 du[ijk + e * LX * LX * LX] = shjacinv[ijk] *
121 (shvx[ijk] * (drdx[ijk + ele] * rtmp
122 + dsdx[ijk + ele] * stmp
123 + dtdx[ijk + ele] * ttmp)
124 + shvy[ijk] * (drdy[ijk + ele] * rtmp
125 + dsdy[ijk + ele] * stmp
126 + dtdy[ijk + ele] * ttmp)
127 + shvz[ijk] * (drdz[ijk + ele] * rtmp
128 + dsdz[ijk + ele] * stmp
129 + dtdz[ijk + ele] * ttmp));
130 }
131 }
132}
133
134template< typename T, const int LX, const int EB >
137 const T * __restrict__ u,
154 const int nelv) {
155
156 __shared__ T shu[EB * LX * LX];
157
161
162 static_assert(sizeof(shu) +
163 sizeof(shdx) +
164 sizeof(shdy) +
165 sizeof(shdz)
167 "kstep block exceeds the LDS budget");
168
169 const int eb = (EB == 1) ? 0 : threadIdx.z;
170 const int e_blk = blockIdx.x * EB + eb;
171 /* Threads past the last element still have to reach the barriers in
172 the k loop, so clamp their reads and drop their stores rather than
173 returning early. At EB == 1 this all constant folds away */
174 const bool active = (EB == 1) ? true : (e_blk < nelv);
175 const int e = active ? e_blk : (nelv - 1);
176 const int sh = eb * LX * LX;
177 const int j = threadIdx.y;
178 const int i = threadIdx.x;
179 const int ij = i + j * LX;
180 const int ele = e*LX*LX*LX;
181
182 if (eb == 0) {
183 shdx[ij] = dx[ij];
184 shdy[ij] = dy[ij];
185 shdz[ij] = dz[ij];
186 }
187
193
194#pragma unroll LX
195 for (int k = 0; k < LX; ++k) {
196 ru[k] = u[ij + k*LX*LX + ele];
197 rvx[k] = vx[ij + k*LX*LX + ele];
198 rvy[k] = vy[ij + k*LX*LX + ele];
199 rvz[k] = vz[ij + k*LX*LX + ele];
200 rjacinv[k] = jacinv[ij + k*LX*LX + ele];
201 }
202
204
205#pragma unroll
206 for (int k = 0; k < LX; ++k) {
207 const int ijk = ij + k*LX*LX;
208 T ttmp = 0.0;
209 shu[sh + ij] = ru[k];
210#pragma unroll
211 for (int l = 0; l < LX; l++) {
212 ttmp += shdz[k+l*LX] * ru[l];
213 }
215
216 T rtmp = 0.0;
217 T stmp = 0.0;
218#pragma unroll
219 for (int l = 0; l < LX; l++) {
220 rtmp += shdx[i+l*LX] * shu[sh + l+j*LX];
221 stmp += shdy[j+l*LX] * shu[sh + i+l*LX];
222 }
223
224 if (active) {
225 du[ijk + ele] = rjacinv[k] *
226 (rvx[k] * (drdx[ijk + ele] * rtmp
227 + dsdx[ijk + ele] * stmp
228 + dtdx[ijk + ele] * ttmp)
229 + rvy[k] * (drdy[ijk + ele] * rtmp
230 + dsdy[ijk + ele] * stmp
231 + dtdy[ijk + ele] * ttmp)
232 + rvz[k] * (drdz[ijk + ele] * rtmp
233 + dsdz[ijk + ele] * stmp
234 + dtdz[ijk + ele] * ttmp));
235 }
237 }
238}
239
240
241
252#if defined(__gfx90a__) || defined(__gfx942__)
253
254template< typename T, const int LX, const int NWF >
256 const T * __restrict__ u,
257 const T * __restrict__ vx,
258 const T * __restrict__ vy,
259 const T * __restrict__ vz,
260 const T * __restrict__ dx,
261 const T * __restrict__ dy,
262 const T * __restrict__ dz,
263 const T * __restrict__ drdx,
264 const T * __restrict__ dsdx,
265 const T * __restrict__ dtdx,
266 const T * __restrict__ drdy,
267 const T * __restrict__ dsdy,
268 const T * __restrict__ dtdy,
269 const T * __restrict__ drdz,
270 const T * __restrict__ dsdz,
271 const T * __restrict__ dtdz,
272 const T * __restrict__ jacinv,
273 const int nelv) {
274 const int LX2 = LX * LX;
275 const int LX3 = LX * LX * LX;
276
277 /* NWF wavefronts per block, WPE of them cooperating on one element and the
278 block covering EB elements, see the note in mfma_kernel.h. At LX = 4 the
279 contraction offers one column group, so WPE is 1 and every wavefront gets
280 an element of its own rather than idling. */
281 enum { EB = NEKO_MFMA_EB_N(NWF, LX),
282 WPE = NWF / EB };
283 static_assert(WPE * EB == NWF,
284 "wavefronts per block must split evenly over the elements");
285
286 __shared__ T shdx[LX * LX];
287 __shared__ T shdy[LX * LX];
288 __shared__ T shdz[LX * LX];
289 __shared__ T shu[EB * LX * LX * LX]; // the staged field
290 __shared__ T shr[EB * LX * LX * LX]; // d/dr
291 __shared__ T shs[EB * LX * LX * LX]; // d/ds
292 __shared__ T sht[EB * LX * LX * LX]; // d/dt
293
294 static_assert(sizeof(shdx) + sizeof(shdy) + sizeof(shdz) +
295 sizeof(shu) + sizeof(shr) + sizeof(shs) + sizeof(sht)
297 "mfma block exceeds the shared memory budget");
298
299 const int lane = threadIdx.x; // 0..63 : lane within a wavefront
300 const int wf = threadIdx.y; // 0..NWF-1 : which wavefront
301 const int tid = wf * 64 + lane; // 0..NWF*64-1 : block-wide thread id
302 const int nthr = NWF * 64;
303
304 const int eb = wf / WPE; // which element this wavefront serves
305 const int sub = wf % WPE; // its rank among that element's waves
306 const int gtid = sub * 64 + lane; // thread id within the element group
307 const int gnthr = WPE * 64;
308
309 /* Threads past the last element still have to reach the block wide
310 barriers, so clamp their reads and drop their stores rather than
311 returning early. At EB == 1 the grid covers nelv exactly and this is
312 constant folded away */
313 const int e_blk = blockIdx.x * EB + eb;
314 const bool active = (EB == 1) ? true : (e_blk < nelv);
315 const int e = active ? e_blk : (nelv - 1);
316 const int ele = e * LX3;
317 const int sh = eb * LX3;
318
319 /* Reference derivative matrices, one copy shared by every element */
320 for (int p = tid; p < LX2; p += nthr) {
321 shdx[p] = dx[p];
322 shdy[p] = dy[p];
323 shdz[p] = dz[p];
324 }
325 /* Element-local field, staged by the wavefronts that own it */
326 for (int p = gtid; p < LX3; p += gnthr)
327 shu[sh + p] = u[p + ele];
328
330
331 /* Reference space derivatives ur, us, ut, striped across the cooperating
332 wavefronts by mfma_contract_sel */
333 mfma_contract_sel<T, LX, 0, false, false, WPE>::run(shr + sh, shdx,
334 shu + sh, lane, sub);
335 mfma_contract_sel<T, LX, 1, false, false, WPE>::run(shs + sh, shdy,
336 shu + sh, lane, sub);
337 mfma_contract_sel<T, LX, 2, false, false, WPE>::run(sht + sh, shdz,
338 shu + sh, lane, sub);
339
341
342 if (active) {
343 for (int p = gtid; p < LX3; p += gnthr) {
344 const int gp = p + ele;
345 const T rr = shr[sh + p], ss = shs[sh + p], tt = sht[sh + p];
346
347 du[gp] = jacinv[gp] *
348 (vx[gp] * (drdx[gp] * rr + dsdx[gp] * ss + dtdx[gp] * tt)
349 + vy[gp] * (drdy[gp] * rr + dsdy[gp] * ss + dtdy[gp] * tt)
350 + vz[gp] * (drdz[gp] * rr + dsdz[gp] * ss + dtdz[gp] * tt));
351 }
352 }
353}
354
355#endif // __gfx90a__ || __gfx942__
356
357/*
358 * Compile-time dispatch onto the MFMA element kernel. The launch macros in
359 * opr_conv1.hip are written for every LX the operator dispatches and for
360 * whatever `real` is, so every combination has to compile; the ones the
361 * strategy does not cover -- LX outside the supported range, a build without
362 * a matrix-core arch -- resolve to this no-op. The autotuner never selects
363 * the strategy for them, so the no-op is unreachable at runtime, see
364 * mfma_lx_supported() and hip_have_mfma() in mfma_kernel.h.
365 */
366template< typename T, const int LX, const int NWF >
368 __device__ static void run(T *, const T *, const T *, const T *, const T *,
369 const T *, const T *, const T *, const T *,
370 const T *, const T *, const T *, const T *,
371 const T *, const T *, const T *, const T *,
372 const T *, const int) {}
373};
374
375#if defined(__gfx90a__) || defined(__gfx942__)
376
377/* Keep in sync with mfma_lx_supported() in mfma_kernel.h */
378#define NEKO_CONV1_MFMA_DISPATCH(TYPE, LXV) \
379 template< const int NWF > \
380 struct conv1_mfma_dispatch< TYPE, LXV, NWF > { \
381 __device__ static void run(TYPE * du, \
382 const TYPE * u, \
383 const TYPE * vx, \
384 const TYPE * vy, \
385 const TYPE * vz, \
386 const TYPE * dx, \
387 const TYPE * dy, \
388 const TYPE * dz, \
389 const TYPE * drdx, \
390 const TYPE * dsdx, \
391 const TYPE * dtdx, \
392 const TYPE * drdy, \
393 const TYPE * dsdy, \
394 const TYPE * dtdy, \
395 const TYPE * drdz, \
396 const TYPE * dsdz, \
397 const TYPE * dtdz, \
398 const TYPE * jacinv, \
399 const int nelv) { \
400 conv1_mfma_elem< TYPE, LXV, NWF >(du, u, vx, vy, vz, dx, dy, dz, \
401 drdx, dsdx, dtdx, drdy, dsdy, dtdy, \
402 drdz, dsdz, dtdz, jacinv, nelv); \
403 } \
404 }
405
406NEKO_CONV1_MFMA_DISPATCH(double, 4);
407NEKO_CONV1_MFMA_DISPATCH(double, 5);
408NEKO_CONV1_MFMA_DISPATCH(double, 6);
409NEKO_CONV1_MFMA_DISPATCH(double, 7);
410NEKO_CONV1_MFMA_DISPATCH(double, 8);
411NEKO_CONV1_MFMA_DISPATCH(double, 9);
412NEKO_CONV1_MFMA_DISPATCH(double, 10);
413NEKO_CONV1_MFMA_DISPATCH(double, 11);
414NEKO_CONV1_MFMA_DISPATCH(double, 12);
415
422NEKO_CONV1_MFMA_DISPATCH(float, 10);
423NEKO_CONV1_MFMA_DISPATCH(float, 11);
424NEKO_CONV1_MFMA_DISPATCH(float, 12);
425
426#endif // __gfx90a__ || __gfx942__
427
428/*
429 * Note the bare __launch_bounds__ rather than NEKO_EB_BOUNDS, matching
430 * ax_helm_kernel_mfma: the kstep kernels ask for three waves per SIMD, and
431 * the matrix core kernels were validated without that constraint.
432 */
433template< typename T, const int LX, const int NWF >
436 const T * __restrict__ u,
437 const T * __restrict__ vx,
438 const T * __restrict__ vy,
439 const T * __restrict__ vz,
440 const T * __restrict__ dx,
441 const T * __restrict__ dy,
442 const T * __restrict__ dz,
443 const T * __restrict__ drdx,
444 const T * __restrict__ dsdx,
445 const T * __restrict__ dtdx,
446 const T * __restrict__ drdy,
447 const T * __restrict__ dsdy,
448 const T * __restrict__ dtdy,
449 const T * __restrict__ drdz,
450 const T * __restrict__ dsdz,
451 const T * __restrict__ dtdz,
452 const T * __restrict__ jacinv,
453 const int nelv) {
454
456 dsdx, dtdx, drdy, dsdy, dtdy, drdz,
457 dsdz, dtdz, jacinv, nelv);
458}
459
460#endif // __MATH_CONV1_KERNEL_H__
__global__ void ale_add_kinematics_kernel(const int n, T *__restrict__ wx, T *__restrict__ wy, T *__restrict__ wz, const T *__restrict__ x_ref, const T *__restrict__ y_ref, const T *__restrict__ z_ref, const T *__restrict__ phi, const T *__restrict__ x, const T *__restrict__ y, const T *__restrict__ z, const kinematics_params_t kin_params)
__shared__ T shu[LX *LX]
const bool active
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dz
T rvy[LX]
const int sh
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ jacinv
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dy
T ru[LX]
const int eb
__shared__ T shdy[LX *LX]
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ drdx
const int i
T rvx[LX]
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dx
const int ij
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const int nelv
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dtdx
T rvz[LX]
T rjacinv[LX]
__shared__ T shdx[LX *LX]
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dtdz
const int e
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dsdx
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dtdy
__shared__ T shdz[LX *LX]
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dsdz
const int e_blk
__global__ void conv1_kernel_1d(T *__restrict__ du, const T *__restrict__ u, const T *__restrict__ vx, const T *__restrict__ vy, const T *__restrict__ vz, const T *__restrict__ dx, const T *__restrict__ dy, const T *__restrict__ dz, const T *__restrict__ drdx, const T *__restrict__ dsdx, const T *__restrict__ dtdx, const T *__restrict__ drdy, const T *__restrict__ dsdy, const T *__restrict__ dtdy, const T *__restrict__ drdz, const T *__restrict__ dsdz, const T *__restrict__ dtdz, const T *__restrict__ jacinv)
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ vz
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dsdy
const int ele
__global__ void const T *__restrict__ const T *__restrict__ vx
__global__ void const T *__restrict__ u
const int j
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ drdy
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ vy
__syncthreads()
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ drdz
#define NEKO_EB_BOUNDS(NT)
Definition elem_block.h:95
__global__ void __launch_bounds__((LX *LX *EB), 3) conv1_kernel_kstep(T *__restrict__ du
#define NEKO_EB_MAX_LDS
Definition elem_block.h:79
#define NEKO_MFMA_EB_N(NWF, LX)
static __device__ void run(T *, const T *, const T *, const T *, const T *, const T *, const T *, const T *, const T *, const T *, const T *, const T *, const T *, const T *, const T *, const T *, const T *, const T *, const int)