Neko 1.99.9
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
dudxyz_kernel.h
Go to the documentation of this file.
1#ifndef __MATH_DUDXYZ_KERNEL_H__
2#define __MATH_DUDXYZ_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__ dr,
50 const T * __restrict__ ds,
51 const T * __restrict__ dt,
52 const T * __restrict__ dx,
53 const T * __restrict__ dy,
54 const T * __restrict__ dz,
55 const T * __restrict__ jacinv) {
56
57 __shared__ T shu[LX * LX * LX];
58 __shared__ T shdr[LX * LX * LX];
59 __shared__ T shds[LX * LX * LX];
60 __shared__ T shdt[LX * LX * LX];
61
65
67
68 const int e = blockIdx.x;
69 const int iii = threadIdx.x;
70 const int nchunks = (LX * LX * LX - 1) / CHUNKS + 1;
71
72 if (iii < (LX * LX)) {
73 shdx[iii] = dx[iii];
74 shdy[iii] = dy[iii];
75 shdz[iii] = dz[iii];
76 }
77
78 int l = iii;
79 while(l < (LX * LX * LX)) {
80 shu[l] = u[l + e * LX * LX * LX];
81 shdr[l] = dr[l + e * LX * LX * LX];
82 shds[l] = ds[l + e * LX * LX * LX];
83 shdt[l] = dt[l + e * LX * LX * LX];
84 shjacinv[l] = jacinv[l + e * LX * LX * LX];
85 l = l + CHUNKS;
86 }
87
89
90 for (int n = 0; n < nchunks; n++) {
91 const int ijk = iii + n * CHUNKS;
92 const int jk = ijk / LX;
93 const int i = ijk - jk * LX;
94 const int k = jk / LX;
95 const int j = jk - k * LX;
96 if ( i < LX && j < LX && k < LX) {
97 T rtmp = 0.0;
98 T stmp = 0.0;
99 T ttmp = 0.0;
100 for (int l = 0; l < LX; l++) {
101 rtmp += shdx[i + l * LX] * shu[l + j * LX + k * LX * LX];
102 stmp += shdy[j + l * LX] * shu[i + l * LX + k * LX * LX];
103 ttmp += shdz[k + l * LX] * shu[i + j * LX + l * LX * LX];
104 }
105 du[ijk + e * LX * LX * LX] = ((rtmp * shdr[ijk])
106 + (stmp * shds[ijk])
107 + (ttmp * shdt[ijk]))
108 * shjacinv[ijk];
109
110 }
111 }
112}
113
114template< typename T, const int LX, const int EB >
117 const T * __restrict__ u,
125 const int nelv) {
126
127 __shared__ T shu[EB * LX * LX];
128
132
133 static_assert(sizeof(shu) +
134 sizeof(shdx) +
135 sizeof(shdy) +
136 sizeof(shdz)
138 "kstep block exceeds the LDS budget");
139
140 const int eb = (EB == 1) ? 0 : threadIdx.z;
141 const int e_blk = blockIdx.x * EB + eb;
142 /* Threads past the last element still have to reach the barriers in
143 the k loop, so clamp their reads and drop their stores rather than
144 returning early. At EB == 1 this all constant folds away */
145 const bool active = (EB == 1) ? true : (e_blk < nelv);
146 const int e = active ? e_blk : (nelv - 1);
147 const int sh = eb * LX * LX;
148 const int j = threadIdx.y;
149 const int i = threadIdx.x;
150 const int ij = i + j * LX;
151 const int ele = e*LX*LX*LX;
152
153 if (eb == 0) {
154 shdx[ij] = dx[ij];
155 shdy[ij] = dy[ij];
156 shdz[ij] = dz[ij];
157 }
158
164
165 #pragma unroll LX
166 for (int k = 0; k < LX; ++k) {
167 ru[k] = u[ij + k*LX*LX + ele];
168 rdr[k] = dr[ij + k*LX*LX + ele];
169 rds[k] = ds[ij + k*LX*LX + ele];
170 rdt[k] = dt[ij + k*LX*LX + ele];
171 rjacinv[k] = jacinv[ij + k*LX*LX + ele];
172 }
173
175
176 #pragma unroll
177 for (int k = 0; k < LX; ++k) {
178 const int ijk = ij + k*LX*LX;
179 T ttmp = 0.0;
180 shu[sh + ij] = ru[k];
181#pragma unroll
182 for (int l = 0; l < LX; l++) {
183 ttmp += shdz[k+l*LX] * ru[l];
184 }
186
187 T rtmp = 0.0;
188 T stmp = 0.0;
189#pragma unroll
190 for (int l = 0; l < LX; l++) {
191 rtmp += shdx[i+l*LX] * shu[sh + l+j*LX];
192 stmp += shdy[j+l*LX] * shu[sh + i+l*LX];
193 }
194
195 if (active) {
196 du[ijk + ele] = rjacinv[k] * ((rtmp * rdr[k])
197 + (stmp * rds[k])
198 + (ttmp * rdt[k]));
199 }
201 }
202}
203
204
205
218#if defined(__gfx90a__) || defined(__gfx942__)
219
220template< typename T, const int LX, const int NWF, const int TILE >
222 const T * __restrict__ u,
223 const T * __restrict__ dr,
224 const T * __restrict__ ds,
225 const T * __restrict__ dt,
226 const T * __restrict__ dx,
227 const T * __restrict__ dy,
228 const T * __restrict__ dz,
229 const T * __restrict__ jacinv,
230 const int nelv) {
231 const int LX2 = LX * LX;
232 const int LX3 = LX * LX * LX;
233
234 /* NWF wavefronts per block, WPE of them cooperating on one element and the
235 block covering EB elements, see the note in mfma_kernel.h. At LX = 4 the
236 contraction offers one column group, so WPE is 1 and every wavefront gets
237 an element of its own rather than idling. */
238 enum { EB = NEKO_MFMA_EB_N(NWF, LX, sizeof(T)),
239 WPE = NWF / EB,
240 /* Elements the batched single precision tile contracts per
241 call, from the wavefronts of the first element of each
242 batch; one for every other tile, see NEKO_MFMA_NEL_N */
243 NEL = NEKO_MFMA_NEL_N(EB, LX, sizeof(T), TILE) };
244 static_assert(WPE * EB == NWF,
245 "wavefronts per block must split evenly over the elements");
246
247 __shared__ T shdx[LX * LX];
248 __shared__ T shdy[LX * LX];
249 __shared__ T shdz[LX * LX];
250 __shared__ T shu[EB * LX * LX * LX]; // the staged field
251 __shared__ T shr[EB * LX * LX * LX]; // d/dr
252 __shared__ T shs[EB * LX * LX * LX]; // d/ds
253 __shared__ T sht[EB * LX * LX * LX]; // d/dt
254
255 static_assert(sizeof(shdx) + sizeof(shdy) + sizeof(shdz) +
256 sizeof(shu) + sizeof(shr) + sizeof(shs) + sizeof(sht)
258 "mfma block exceeds the shared memory budget");
259
260 const int lane = threadIdx.x; // 0..63 : lane within a wavefront
261 const int wf = threadIdx.y; // 0..NWF-1 : which wavefront
262 const int tid = wf * 64 + lane; // 0..NWF*64-1 : block-wide thread id
263 const int nthr = NWF * 64;
264
265 const int eb = wf / WPE; // which element this wavefront serves
266 const int sub = wf % WPE; // its rank among that element's waves
267 const int gtid = sub * 64 + lane; // thread id within the element group
268 const int gnthr = WPE * 64;
269
270 /* Threads past the last element still have to reach the block wide
271 barriers, so clamp their reads and drop their stores rather than
272 returning early. At EB == 1 the grid covers nelv exactly and this is
273 constant folded away */
274 const int e_blk = blockIdx.x * EB + eb;
275 const bool active = (EB == 1) ? true : (e_blk < nelv);
276 const int e = active ? e_blk : (nelv - 1);
277 const int ele = e * LX3;
278 const int sh = eb * LX3;
279
280 /* Reference derivative matrices, one copy shared by every element */
281 for (int p = tid; p < LX2; p += nthr) {
282 shdx[p] = dx[p];
283 shdy[p] = dy[p];
284 shdz[p] = dz[p];
285 }
286 /* Element-local field, staged by the wavefronts that own it */
287 for (int p = gtid; p < LX3; p += gnthr)
288 shu[sh + p] = u[p + ele];
289
291
292 /* Reference space derivatives ur, us, ut, striped across the cooperating
293 wavefronts by mfma_contract_sel */
294 if (eb % NEL == 0)
295 mfma_contract_sel<T, LX, 0, false, false, WPE, TILE, 0, NEL>::run(
296 shr + sh, shdx, shu + sh, lane, sub);
297 if (eb % NEL == 0)
298 mfma_contract_sel<T, LX, 1, false, false, WPE, TILE, 0, NEL>::run(
299 shs + sh, shdy, shu + sh, lane, sub);
300 if (eb % NEL == 0)
301 mfma_contract_sel<T, LX, 2, false, false, WPE, TILE, 0, NEL>::run(
302 sht + sh, shdz, shu + sh, lane, sub);
303
305
306 if (active) {
307 for (int p = gtid; p < LX3; p += gnthr) {
308 const int gp = p + ele;
309
310 du[gp] = jacinv[gp] * (shr[sh + p] * dr[gp]
311 + shs[sh + p] * ds[gp]
312 + sht[sh + p] * dt[gp]);
313 }
314 }
315}
316
317#endif // __gfx90a__ || __gfx942__
318
319/*
320 * Compile-time dispatch onto the MFMA element kernel. The launch macros in
321 * opr_dudxyz.hip are written for every LX the operator dispatches and for
322 * whatever `real` is, so every combination has to compile; the ones the
323 * strategy does not cover -- LX outside the supported range, a build without
324 * a matrix-core arch -- resolve to this no-op. The autotuner never selects
325 * the strategy for them, so the no-op is unreachable at runtime, see
326 * mfma_lx_supported() and hip_have_mfma() in mfma_kernel.h.
327 */
328template< typename T, const int LX, const int NWF, const int TILE >
330 __device__ static void run(T *, const T *, const T *, const T *, const T *,
331 const T *, const T *, const T *, const T *,
332 const int) {}
333};
334
335#if defined(__gfx90a__) || defined(__gfx942__)
336
337/* Keep in sync with mfma_lx_supported() in mfma_kernel.h */
338#define NEKO_DUDXYZ_MFMA_DISPATCH(TYPE, LXV) \
339 template< const int NWF, const int TILE > \
340 struct dudxyz_mfma_dispatch< TYPE, LXV, NWF, TILE > { \
341 __device__ static void run(TYPE * du, \
342 const TYPE * u, \
343 const TYPE * dr, \
344 const TYPE * ds, \
345 const TYPE * dt, \
346 const TYPE * dx, \
347 const TYPE * dy, \
348 const TYPE * dz, \
349 const TYPE * jacinv, \
350 const int nelv) { \
351 dudxyz_mfma_elem< TYPE, LXV, NWF, TILE >(du, u, dr, ds, dt, dx, dy, dz, \
352 jacinv, nelv); \
353 } \
354 }
355
362NEKO_DUDXYZ_MFMA_DISPATCH(double, 10);
363NEKO_DUDXYZ_MFMA_DISPATCH(double, 11);
364NEKO_DUDXYZ_MFMA_DISPATCH(double, 12);
365
377
378#endif // __gfx90a__ || __gfx942__
379
380/*
381 * Note the bare __launch_bounds__ rather than NEKO_EB_BOUNDS, matching
382 * ax_helm_kernel_mfma: the kstep kernels ask for three waves per SIMD, and
383 * the matrix core kernels were validated without that constraint.
384 */
385template< typename T, const int LX, const int NWF, const int TILE >
388 const T * __restrict__ u,
389 const T * __restrict__ dr,
390 const T * __restrict__ ds,
391 const T * __restrict__ dt,
392 const T * __restrict__ dx,
393 const T * __restrict__ dy,
394 const T * __restrict__ dz,
395 const T * __restrict__ jacinv,
396 const int nelv) {
397
399 jacinv, nelv);
400}
401
402#endif // __MATH_DUDXYZ_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[EB *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
__global__ void const T *__restrict__ const T *__restrict__ dr
const int sh
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dy
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ ds
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__ dt
T rdt[LX]
const int i
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dx
const int ij
T rjacinv[LX]
__shared__ T shdx[LX *LX]
const int e
T rds[LX]
__shared__ T shdz[LX *LX]
const int e_blk
__global__ void dudxyz_kernel_1d(T *__restrict__ du, const T *__restrict__ u, const T *__restrict__ dr, const T *__restrict__ ds, const T *__restrict__ dt, const T *__restrict__ dx, const T *__restrict__ dy, const T *__restrict__ dz, const T *__restrict__ jacinv)
const int ele
__global__ void const T *__restrict__ u
__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 int nelv
const int j
T rdr[LX]
__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__ jacinv
#define NEKO_EB_BOUNDS(NT)
Definition elem_block.h:95
__global__ void __launch_bounds__((LX *LX *EB)) dudxyz_kernel_kstep(T *__restrict__ du
#define NEKO_EB_MAX_LDS
Definition elem_block.h:79
#define NEKO_MFMA_NEL_N(EB, LX, SZ, TILE)
#define NEKO_MFMA_EB_N(NWF, LX, SZ)
static __device__ void run(T *, const T *, const T *, const T *, const T *, const T *, const T *, const T *, const T *, const int)