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 >
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),
270 WPE = NWF / EB };
271 static_assert(WPE * EB == NWF,
272 "wavefronts per block must split evenly over the elements");
273
274 __shared__ T shdx[LX * LX];
275 __shared__ T shdy[LX * LX];
276 __shared__ T shdz[LX * LX];
277 __shared__ T shu[EB * LX * LX * LX]; // the staged field
278 __shared__ T shr[EB * LX * LX * LX]; // d/dr
279 __shared__ T shs[EB * LX * LX * LX]; // d/ds
280 __shared__ T sht[EB * LX * LX * LX]; // d/dt
281
282 static_assert(sizeof(shdx) + sizeof(shdy) + sizeof(shdz) +
283 sizeof(shu) + sizeof(shr) + sizeof(shs) + sizeof(sht)
285 "mfma block exceeds the shared memory budget");
286
287 const int lane = threadIdx.x; // 0..63 : lane within a wavefront
288 const int wf = threadIdx.y; // 0..NWF-1 : which wavefront
289 const int tid = wf * 64 + lane; // 0..NWF*64-1 : block-wide thread id
290 const int nthr = NWF * 64;
291
292 const int eb = wf / WPE; // which element this wavefront serves
293 const int sub = wf % WPE; // its rank among that element's waves
294 const int gtid = sub * 64 + lane; // thread id within the element group
295 const int gnthr = WPE * 64;
296
297 /* Threads past the last element still have to reach the block wide
298 barriers, so clamp their reads and drop their stores rather than
299 returning early. At EB == 1 the grid covers nelv exactly and this is
300 constant folded away */
301 const int e_blk = blockIdx.x * EB + eb;
302 const bool active = (EB == 1) ? true : (e_blk < nelv);
303 const int e = active ? e_blk : (nelv - 1);
304 const int ele = e * LX3;
305 const int sh = eb * LX3;
306
307 /* Reference derivative matrices, one copy shared by every element */
308 for (int p = tid; p < LX2; p += nthr) {
309 shdx[p] = dx[p];
310 shdy[p] = dy[p];
311 shdz[p] = dz[p];
312 }
313 /* Element-local field, staged by the wavefronts that own it */
314 for (int p = gtid; p < LX3; p += gnthr)
315 shu[sh + p] = u[p + ele];
316
318
319 /* Reference space derivatives ur, us, ut, striped across the cooperating
320 wavefronts by mfma_contract_sel */
321 mfma_contract_sel<T, LX, 0, false, false, WPE>::run(shr + sh, shdx,
322 shu + sh, lane, sub);
323 mfma_contract_sel<T, LX, 1, false, false, WPE>::run(shs + sh, shdy,
324 shu + sh, lane, sub);
325 mfma_contract_sel<T, LX, 2, false, false, WPE>::run(sht + sh, shdz,
326 shu + sh, lane, sub);
327
329
330 if (active) {
331 for (int p = gtid; p < LX3; p += gnthr) {
332 const int gp = p + ele;
333 const T W3 = w3[p];
334 const T rr = shr[sh + p], ss = shs[sh + p], tt = sht[sh + p];
335
336 ux[gp] = W3 * (drdx[gp] * rr + dsdx[gp] * ss + dtdx[gp] * tt);
337 uy[gp] = W3 * (drdy[gp] * rr + dsdy[gp] * ss + dtdy[gp] * tt);
338 uz[gp] = W3 * (drdz[gp] * rr + dsdz[gp] * ss + dtdz[gp] * tt);
339 }
340 }
341}
342
343#endif // __gfx90a__ || __gfx942__
344
345/*
346 * Compile-time dispatch onto the MFMA element kernel. The launch macros in
347 * opr_opgrad.hip are written for every LX the operator dispatches and for
348 * whatever `real` is, so every combination has to compile; the ones the
349 * strategy does not cover -- LX outside the supported range, a build without
350 * a matrix-core arch -- resolve to this no-op. The autotuner never selects
351 * the strategy for them, so the no-op is unreachable at runtime, see
352 * mfma_lx_supported() and hip_have_mfma() in mfma_kernel.h.
353 */
354template< typename T, const int LX, const int NWF >
356 __device__ static void run(T *, T *, T *, const T *, const T *, const T *,
357 const T *, const T *, const T *, const T *,
358 const T *, const T *, const T *, const T *,
359 const T *, const T *, const T *, const int) {}
360};
361
362#if defined(__gfx90a__) || defined(__gfx942__)
363
364/* Keep in sync with mfma_lx_supported() in mfma_kernel.h */
365#define NEKO_OPGRAD_MFMA_DISPATCH(TYPE, LXV) \
366 template< const int NWF > \
367 struct opgrad_mfma_dispatch< TYPE, LXV, NWF > { \
368 __device__ static void run(TYPE * ux, \
369 TYPE * uy, \
370 TYPE * uz, \
371 const TYPE * u, \
372 const TYPE * dx, \
373 const TYPE * dy, \
374 const TYPE * dz, \
375 const TYPE * drdx, \
376 const TYPE * dsdx, \
377 const TYPE * dtdx, \
378 const TYPE * drdy, \
379 const TYPE * dsdy, \
380 const TYPE * dtdy, \
381 const TYPE * drdz, \
382 const TYPE * dsdz, \
383 const TYPE * dtdz, \
384 const TYPE * w3, \
385 const int nelv) { \
386 opgrad_mfma_elem< TYPE, LXV, NWF >(ux, uy, uz, u, dx, dy, dz, drdx, \
387 dsdx, dtdx, drdy, dsdy, dtdy, \
388 drdz, dsdz, dtdz, w3, nelv); \
389 } \
390 }
391
398NEKO_OPGRAD_MFMA_DISPATCH(double, 10);
399NEKO_OPGRAD_MFMA_DISPATCH(double, 11);
400NEKO_OPGRAD_MFMA_DISPATCH(double, 12);
401
411
412#endif // __gfx90a__ || __gfx942__
413
414/*
415 * Note the bare __launch_bounds__ rather than NEKO_EB_BOUNDS, matching
416 * ax_helm_kernel_mfma: the kstep kernels ask for three waves per SIMD, and
417 * the matrix core kernels were validated without that constraint.
418 */
419template< typename T, const int LX, const int NWF >
422 T * __restrict__ uy,
423 T * __restrict__ uz,
424 const T * __restrict__ u,
425 const T * __restrict__ dx,
426 const T * __restrict__ dy,
427 const T * __restrict__ dz,
428 const T * __restrict__ drdx,
429 const T * __restrict__ dsdx,
430 const T * __restrict__ dtdx,
431 const T * __restrict__ drdy,
432 const T * __restrict__ dsdy,
433 const T * __restrict__ dtdy,
434 const T * __restrict__ drdz,
435 const T * __restrict__ dsdz,
436 const T * __restrict__ dtdz,
437 const T * __restrict__ w3,
438 const int nelv) {
439
441 dsdx, dtdx, drdy, dsdy, dtdy, drdz,
442 dsdz, dtdz, w3, nelv);
443}
444
445#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[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), 3) opgrad_kernel_kstep(T *__restrict__ ux
#define NEKO_MFMA_EB_N(NWF, LX)
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)