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 >
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),
239 WPE = NWF / EB };
240 static_assert(WPE * EB == NWF,
241 "wavefronts per block must split evenly over the elements");
242
243 __shared__ T shdx[LX * LX];
244 __shared__ T shdy[LX * LX];
245 __shared__ T shdz[LX * LX];
246 __shared__ T shu[EB * LX * LX * LX]; // the staged field
247 __shared__ T shr[EB * LX * LX * LX]; // d/dr
248 __shared__ T shs[EB * LX * LX * LX]; // d/ds
249 __shared__ T sht[EB * LX * LX * LX]; // d/dt
250
251 static_assert(sizeof(shdx) + sizeof(shdy) + sizeof(shdz) +
252 sizeof(shu) + sizeof(shr) + sizeof(shs) + sizeof(sht)
254 "mfma block exceeds the shared memory budget");
255
256 const int lane = threadIdx.x; // 0..63 : lane within a wavefront
257 const int wf = threadIdx.y; // 0..NWF-1 : which wavefront
258 const int tid = wf * 64 + lane; // 0..NWF*64-1 : block-wide thread id
259 const int nthr = NWF * 64;
260
261 const int eb = wf / WPE; // which element this wavefront serves
262 const int sub = wf % WPE; // its rank among that element's waves
263 const int gtid = sub * 64 + lane; // thread id within the element group
264 const int gnthr = WPE * 64;
265
266 /* Threads past the last element still have to reach the block wide
267 barriers, so clamp their reads and drop their stores rather than
268 returning early. At EB == 1 the grid covers nelv exactly and this is
269 constant folded away */
270 const int e_blk = blockIdx.x * EB + eb;
271 const bool active = (EB == 1) ? true : (e_blk < nelv);
272 const int e = active ? e_blk : (nelv - 1);
273 const int ele = e * LX3;
274 const int sh = eb * LX3;
275
276 /* Reference derivative matrices, one copy shared by every element */
277 for (int p = tid; p < LX2; p += nthr) {
278 shdx[p] = dx[p];
279 shdy[p] = dy[p];
280 shdz[p] = dz[p];
281 }
282 /* Element-local field, staged by the wavefronts that own it */
283 for (int p = gtid; p < LX3; p += gnthr)
284 shu[sh + p] = u[p + ele];
285
287
288 /* Reference space derivatives ur, us, ut, striped across the cooperating
289 wavefronts by mfma_contract_sel */
290 mfma_contract_sel<T, LX, 0, false, false, WPE>::run(shr + sh, shdx,
291 shu + sh, lane, sub);
292 mfma_contract_sel<T, LX, 1, false, false, WPE>::run(shs + sh, shdy,
293 shu + sh, lane, sub);
294 mfma_contract_sel<T, LX, 2, false, false, WPE>::run(sht + sh, shdz,
295 shu + sh, lane, sub);
296
298
299 if (active) {
300 for (int p = gtid; p < LX3; p += gnthr) {
301 const int gp = p + ele;
302
303 du[gp] = jacinv[gp] * (shr[sh + p] * dr[gp]
304 + shs[sh + p] * ds[gp]
305 + sht[sh + p] * dt[gp]);
306 }
307 }
308}
309
310#endif // __gfx90a__ || __gfx942__
311
312/*
313 * Compile-time dispatch onto the MFMA element kernel. The launch macros in
314 * opr_dudxyz.hip are written for every LX the operator dispatches and for
315 * whatever `real` is, so every combination has to compile; the ones the
316 * strategy does not cover -- LX outside the supported range, a build without
317 * a matrix-core arch -- resolve to this no-op. The autotuner never selects
318 * the strategy for them, so the no-op is unreachable at runtime, see
319 * mfma_lx_supported() and hip_have_mfma() in mfma_kernel.h.
320 */
321template< typename T, const int LX, const int NWF >
323 __device__ static void run(T *, const T *, const T *, const T *, const T *,
324 const T *, const T *, const T *, const T *,
325 const int) {}
326};
327
328#if defined(__gfx90a__) || defined(__gfx942__)
329
330/* Keep in sync with mfma_lx_supported() in mfma_kernel.h */
331#define NEKO_DUDXYZ_MFMA_DISPATCH(TYPE, LXV) \
332 template< const int NWF > \
333 struct dudxyz_mfma_dispatch< TYPE, LXV, NWF > { \
334 __device__ static void run(TYPE * du, \
335 const TYPE * u, \
336 const TYPE * dr, \
337 const TYPE * ds, \
338 const TYPE * dt, \
339 const TYPE * dx, \
340 const TYPE * dy, \
341 const TYPE * dz, \
342 const TYPE * jacinv, \
343 const int nelv) { \
344 dudxyz_mfma_elem< TYPE, LXV, NWF >(du, u, dr, ds, dt, dx, dy, dz, \
345 jacinv, nelv); \
346 } \
347 }
348
355NEKO_DUDXYZ_MFMA_DISPATCH(double, 10);
356NEKO_DUDXYZ_MFMA_DISPATCH(double, 11);
357NEKO_DUDXYZ_MFMA_DISPATCH(double, 12);
358
368
369#endif // __gfx90a__ || __gfx942__
370
371/*
372 * Note the bare __launch_bounds__ rather than NEKO_EB_BOUNDS, matching
373 * ax_helm_kernel_mfma: the kstep kernels ask for three waves per SIMD, and
374 * the matrix core kernels were validated without that constraint.
375 */
376template< typename T, const int LX, const int NWF >
379 const T * __restrict__ u,
380 const T * __restrict__ dr,
381 const T * __restrict__ ds,
382 const T * __restrict__ dt,
383 const T * __restrict__ dx,
384 const T * __restrict__ dy,
385 const T * __restrict__ dz,
386 const T * __restrict__ jacinv,
387 const int nelv) {
388
390 jacinv, nelv);
391}
392
393#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[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), 3) dudxyz_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 int)