Neko 1.99.9
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
opgrad_kernel.h
Go to the documentation of this file.
1#ifndef __MATH_OPGRAD_KERNEL_H__
2#define __MATH_OPGRAD_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 >
50 const T * __restrict__ u,
51 const T * __restrict__ dx,
52 const T * __restrict__ dy,
53 const T * __restrict__ dz,
54 const T * __restrict__ drdx,
55 const T * __restrict__ dsdx,
56 const T * __restrict__ dtdx,
57 const T * __restrict__ drdy,
58 const T * __restrict__ dsdy,
59 const T * __restrict__ dtdy,
60 const T * __restrict__ drdz,
61 const T * __restrict__ dsdz,
62 const T * __restrict__ dtdz,
63 const T * __restrict__ w3) {
64
65 __shared__ T shu[LX * LX * LX];
66
70
71
72 int i,j,k;
73
74 const int e = blockIdx.x;
75 const int iii = threadIdx.x;
76 const int nchunks = (LX * LX * LX - 1) / CHUNKS + 1;
77
78 if (iii < (LX * LX)) {
79 shdx[iii] = dx[iii];
80 shdy[iii] = dy[iii];
81 shdz[iii] = dz[iii];
82 }
83
84 j = iii;
85 while(j < (LX * LX * LX)) {
86 shu[j] = u[j + e * LX * LX * LX];
87 j = j + CHUNKS;
88 }
89
91
92 for (int n = 0; n < nchunks; n++) {
93 const int ijk = iii + n * CHUNKS;
94 const int jk = ijk / LX;
95 i = ijk - jk * LX;
96 k = jk / LX;
97 j = jk - k * LX;
98 if ( i < LX && j < LX && k < LX ) {
99 T rtmp = 0.0;
100 T stmp = 0.0;
101 T ttmp = 0.0;
102 for (int l = 0; l < LX; l++) {
103 rtmp += shdx[i + l * LX] * shu[l + j * LX + k * LX * LX];
104 stmp += shdy[j + l * LX] * shu[i + l * LX + k * LX * LX];
105 ttmp += shdz[k + l * LX] * shu[i + j * LX + l * LX * LX];
106 }
107
108 ux[ijk + e * LX * LX * LX] = w3[ijk]
109 * (drdx[ijk + e * LX * LX * LX] * rtmp
110 + dsdx[ijk + e * LX * LX * LX] * stmp
111 + dtdx[ijk + e * LX * LX * LX] * ttmp);
112
113 uy[ijk + e * LX * LX * LX] = w3[ijk]
114 * (drdy[ijk + e * LX * LX * LX] * rtmp
115 + dsdy[ijk + e * LX * LX * LX] * stmp
116 + dtdy[ijk + e * LX * LX * LX] * ttmp);
117
118 uz[ijk + e * LX * LX * LX] = w3[ijk]
119 * (drdz[ijk + e * LX * LX * LX] * rtmp
120 + dsdz[ijk + e * LX * LX * LX] * stmp
121 + dtdz[ijk + e * LX * LX * LX] * ttmp);
122
123 }
124 }
125
126}
127
128template< typename T, const int LX, const int EB >
133 const T * __restrict__ u,
147 const int nelv) {
148
149 /* One slice per element in the block */
150 __shared__ T shu[EB * LX * LX];
151
152 /* Element independent, one copy per block */
156
157 static_assert(sizeof(shu) + sizeof(shdx) + sizeof(shdy) + sizeof(shdz)
159 "kstep block exceeds the LDS budget");
160
161 /* Threads past the last element still have to reach the barriers in the k
162 loop, so clamp their reads and drop their stores rather than returning
163 early. At EB == 1 all of this is constant folded away */
164 const int eb = (EB == 1) ? 0 : threadIdx.z;
165 const int e_blk = blockIdx.x * EB + eb;
166 const bool active = (EB == 1) ? true : (e_blk < nelv);
167 const int e = active ? e_blk : (nelv - 1);
168 const int j = threadIdx.y;
169 const int i = threadIdx.x;
170 const int ij = i + j * LX;
171 const int sh = eb * LX * LX;
172 const int ele = e*LX*LX*LX;
173
174 if (eb == 0) {
175 shdx[ij] = dx[ij];
176 shdy[ij] = dy[ij];
177 shdz[ij] = dz[ij];
178 }
179
181
182#pragma unroll LX
183 for (int k = 0; k < LX; ++k) {
184 ru[k] = u[ij + k*LX*LX + ele];
185 }
186
188
189 #pragma unroll
190 for (int k = 0; k < LX; ++k) {
191 const int ijk = ij + k*LX*LX;
192 const T W3 = w3[ijk];
193 T ttmp = 0.0;
194 shu[sh + ij] = ru[k];
195#pragma unroll
196 for (int l = 0; l < LX; l++) {
197 ttmp += shdz[k+l*LX] * ru[l];
198 }
200
201 T rtmp = 0.0;
202 T stmp = 0.0;
203#pragma unroll
204 for (int l = 0; l < LX; l++) {
205 rtmp += shdx[i+l*LX] * shu[sh + l+j*LX];
206 stmp += shdy[j+l*LX] * shu[sh + i+l*LX];
207 }
208
209 if (active) {
210 ux[ijk + ele] = W3 * (drdx[ijk + ele] * rtmp
211 + dsdx[ijk + ele] * stmp
212 + dtdx[ijk + ele] * ttmp);
213
214 uy[ijk + ele] = W3 * (drdy[ijk + ele] * rtmp
215 + dsdy[ijk + ele] * stmp
216 + dtdy[ijk + ele] * ttmp);
217
218 uz[ijk + ele] = W3 * (drdz[ijk + ele] * rtmp
219 + dsdz[ijk + ele] * stmp
220 + dtdz[ijk + ele] * ttmp);
221 }
223 }
224}
225
226
227
241#if defined(__gfx90a__) || defined(__gfx942__)
242
243template< typename T, const int LX, const int NWF, const int TILE >
245 T * __restrict__ uy,
246 T * __restrict__ uz,
247 const T * __restrict__ u,
248 const T * __restrict__ dx,
249 const T * __restrict__ dy,
250 const T * __restrict__ dz,
251 const T * __restrict__ drdx,
252 const T * __restrict__ dsdx,
253 const T * __restrict__ dtdx,
254 const T * __restrict__ drdy,
255 const T * __restrict__ dsdy,
256 const T * __restrict__ dtdy,
257 const T * __restrict__ drdz,
258 const T * __restrict__ dsdz,
259 const T * __restrict__ dtdz,
260 const T * __restrict__ w3,
261 const int nelv) {
262 const int LX2 = LX * LX;
263 const int LX3 = LX * LX * LX;
264
265 /* NWF wavefronts per block, WPE of them cooperating on one element and the
266 block covering EB elements, see the note in mfma_kernel.h. At LX = 4 the
267 contraction offers one column group, so WPE is 1 and every wavefront gets
268 an element of its own rather than idling. */
269 enum { EB = NEKO_MFMA_EB_N(NWF, LX, sizeof(T)),
270 WPE = NWF / EB,
271 /* Elements the batched single precision tile contracts per
272 call, from the wavefronts of the first element of each
273 batch; one for every other tile, see NEKO_MFMA_NEL_N */
274 NEL = NEKO_MFMA_NEL_N(EB, LX, sizeof(T), TILE) };
275 static_assert(WPE * EB == NWF,
276 "wavefronts per block must split evenly over the elements");
277
278 __shared__ T shdx[LX * LX];
279 __shared__ T shdy[LX * LX];
280 __shared__ T shdz[LX * LX];
281 __shared__ T shu[EB * LX * LX * LX]; // the staged field
282 __shared__ T shr[EB * LX * LX * LX]; // d/dr
283 __shared__ T shs[EB * LX * LX * LX]; // d/ds
284 __shared__ T sht[EB * LX * LX * LX]; // d/dt
285
286 static_assert(sizeof(shdx) + sizeof(shdy) + sizeof(shdz) +
287 sizeof(shu) + sizeof(shr) + sizeof(shs) + sizeof(sht)
289 "mfma block exceeds the shared memory budget");
290
291 const int lane = threadIdx.x; // 0..63 : lane within a wavefront
292 const int wf = threadIdx.y; // 0..NWF-1 : which wavefront
293 const int tid = wf * 64 + lane; // 0..NWF*64-1 : block-wide thread id
294 const int nthr = NWF * 64;
295
296 const int eb = wf / WPE; // which element this wavefront serves
297 const int sub = wf % WPE; // its rank among that element's waves
298 const int gtid = sub * 64 + lane; // thread id within the element group
299 const int gnthr = WPE * 64;
300
301 /* Threads past the last element still have to reach the block wide
302 barriers, so clamp their reads and drop their stores rather than
303 returning early. At EB == 1 the grid covers nelv exactly and this is
304 constant folded away */
305 const int e_blk = blockIdx.x * EB + eb;
306 const bool active = (EB == 1) ? true : (e_blk < nelv);
307 const int e = active ? e_blk : (nelv - 1);
308 const int ele = e * LX3;
309 const int sh = eb * LX3;
310
311 /* Reference derivative matrices, one copy shared by every element */
312 for (int p = tid; p < LX2; p += nthr) {
313 shdx[p] = dx[p];
314 shdy[p] = dy[p];
315 shdz[p] = dz[p];
316 }
317 /* Element-local field, staged by the wavefronts that own it */
318 for (int p = gtid; p < LX3; p += gnthr)
319 shu[sh + p] = u[p + ele];
320
322
323 /* Reference space derivatives ur, us, ut, striped across the cooperating
324 wavefronts by mfma_contract_sel */
325 if (eb % NEL == 0)
326 mfma_contract_sel<T, LX, 0, false, false, WPE, TILE, 0, NEL>::run(
327 shr + sh, shdx, shu + sh, lane, sub);
328 if (eb % NEL == 0)
329 mfma_contract_sel<T, LX, 1, false, false, WPE, TILE, 0, NEL>::run(
330 shs + sh, shdy, shu + sh, lane, sub);
331 if (eb % NEL == 0)
332 mfma_contract_sel<T, LX, 2, false, false, WPE, TILE, 0, NEL>::run(
333 sht + sh, shdz, shu + sh, lane, sub);
334
336
337 if (active) {
338 for (int p = gtid; p < LX3; p += gnthr) {
339 const int gp = p + ele;
340 const T W3 = w3[p];
341 const T rr = shr[sh + p], ss = shs[sh + p], tt = sht[sh + p];
342
343 ux[gp] = W3 * (drdx[gp] * rr + dsdx[gp] * ss + dtdx[gp] * tt);
344 uy[gp] = W3 * (drdy[gp] * rr + dsdy[gp] * ss + dtdy[gp] * tt);
345 uz[gp] = W3 * (drdz[gp] * rr + dsdz[gp] * ss + dtdz[gp] * tt);
346 }
347 }
348}
349
350#endif // __gfx90a__ || __gfx942__
351
352/*
353 * Compile-time dispatch onto the MFMA element kernel. The launch macros in
354 * opr_opgrad.hip are written for every LX the operator dispatches and for
355 * whatever `real` is, so every combination has to compile; the ones the
356 * strategy does not cover -- LX outside the supported range, a build without
357 * a matrix-core arch -- resolve to this no-op. The autotuner never selects
358 * the strategy for them, so the no-op is unreachable at runtime, see
359 * mfma_lx_supported() and hip_have_mfma() in mfma_kernel.h.
360 */
361template< typename T, const int LX, const int NWF, const int TILE >
363 __device__ static void run(T *, T *, T *, const T *, const T *, const T *,
364 const T *, const T *, const T *, const T *,
365 const T *, const T *, const T *, const T *,
366 const T *, const T *, const T *, const int) {}
367};
368
369#if defined(__gfx90a__) || defined(__gfx942__)
370
371/* Keep in sync with mfma_lx_supported() in mfma_kernel.h */
372#define NEKO_OPGRAD_MFMA_DISPATCH(TYPE, LXV) \
373 template< const int NWF, const int TILE > \
374 struct opgrad_mfma_dispatch< TYPE, LXV, NWF, TILE > { \
375 __device__ static void run(TYPE * ux, \
376 TYPE * uy, \
377 TYPE * uz, \
378 const TYPE * u, \
379 const TYPE * dx, \
380 const TYPE * dy, \
381 const TYPE * dz, \
382 const TYPE * drdx, \
383 const TYPE * dsdx, \
384 const TYPE * dtdx, \
385 const TYPE * drdy, \
386 const TYPE * dsdy, \
387 const TYPE * dtdy, \
388 const TYPE * drdz, \
389 const TYPE * dsdz, \
390 const TYPE * dtdz, \
391 const TYPE * w3, \
392 const int nelv) { \
393 opgrad_mfma_elem< TYPE, LXV, NWF, TILE >(ux, uy, uz, u, dx, dy, dz, \
394 drdx, dsdx, dtdx, drdy, dsdy, \
395 dtdy, drdz, dsdz, dtdz, w3, \
396 nelv); \
397 } \
398 }
399
406NEKO_OPGRAD_MFMA_DISPATCH(double, 10);
407NEKO_OPGRAD_MFMA_DISPATCH(double, 11);
408NEKO_OPGRAD_MFMA_DISPATCH(double, 12);
409
421
422#endif // __gfx90a__ || __gfx942__
423
424/*
425 * Note the bare __launch_bounds__ rather than NEKO_EB_BOUNDS, matching
426 * ax_helm_kernel_mfma: the kstep kernels ask for three waves per SIMD, and
427 * the matrix core kernels were validated without that constraint.
428 */
429template< typename T, const int LX, const int NWF, const int TILE >
432 T * __restrict__ uy,
433 T * __restrict__ uz,
434 const T * __restrict__ u,
435 const T * __restrict__ dx,
436 const T * __restrict__ dy,
437 const T * __restrict__ dz,
438 const T * __restrict__ drdx,
439 const T * __restrict__ dsdx,
440 const T * __restrict__ dtdx,
441 const T * __restrict__ drdy,
442 const T * __restrict__ dsdy,
443 const T * __restrict__ dtdy,
444 const T * __restrict__ drdz,
445 const T * __restrict__ dsdz,
446 const T * __restrict__ dtdz,
447 const T * __restrict__ w3,
448 const int nelv) {
449
451 dsdx, dtdx, drdy, dsdy, dtdy,
452 drdz, dsdz, dtdz, w3, nelv);
453}
454
455#endif // __MATH_OPGRAD_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]
#define NEKO_EB_BOUNDS(NT)
Definition elem_block.h:95
const bool active
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dsdx
__global__ void T *__restrict__ 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
__global__ void opgrad_kernel_1d(T *__restrict__ ux, T *__restrict__ uy, T *__restrict__ uz, const T *__restrict__ u, 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__ w3)
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ dx
__global__ void T *__restrict__ uy
const int sh
__global__ void T *__restrict__ 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
__global__ void T *__restrict__ 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 T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dz
T ru[LX]
const int eb
__shared__ T shdy[LX *LX]
__global__ void T *__restrict__ 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 T *__restrict__ 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
const int i
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dtdx
const int ij
__shared__ T shdx[LX *LX]
const int e
__global__ void T *__restrict__ 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
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ drdx
__shared__ T shdz[LX *LX]
const int e_blk
__global__ void T *__restrict__ 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
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dy
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ u
const int ele
__global__ void T *__restrict__ 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__ w3
const int j
__syncthreads()
__global__ void T *__restrict__ T *__restrict__ uz
#define NEKO_EB_MAX_LDS
Definition elem_block.h:79
__global__ void __launch_bounds__((LX *LX *EB)) opgrad_kernel_kstep(T *__restrict__ ux
#define NEKO_MFMA_NEL_N(EB, LX, SZ, TILE)
#define NEKO_MFMA_EB_N(NWF, LX, SZ)
static __device__ void run(T *, T *, 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)