Neko 1.99.9
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
cdtp_kernel.h
Go to the documentation of this file.
1#ifndef __MATH_CDTP_KERNEL_H__
2#define __MATH_CDTP_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__ x,
49 const T * __restrict__ dr,
50 const T * __restrict__ ds,
51 const T * __restrict__ dt,
52 const T * __restrict__ dxt,
53 const T * __restrict__ dyt,
54 const T * __restrict__ dzt,
55 const T * __restrict__ w3) {
56
60
61 __shared__ T shtar[LX * LX * LX];
62 __shared__ T shtas[LX * LX * LX];
63 __shared__ T shtat[LX * LX * LX];
64
65 const int e = blockIdx.x;
66 const int iii = threadIdx.x;
67 const int nchunks = (LX * LX * LX - 1) / CHUNKS + 1;
68
69 if (iii < (LX * LX)) {
70 shdxt[iii] = dxt[iii];
71 shdyt[iii] = dyt[iii];
72 shdzt[iii] = dzt[iii];
73 }
74
75 int l = iii;
76 while(l < (LX * LX * LX)) {
77 T wx = x[l + e * LX * LX * LX] * w3[l];
78
79 shtar[l] = wx*dr[l + e * LX * LX * LX];
80 shtas[l] = wx*ds[l + e * LX * LX * LX];
81 shtat[l] = wx*dt[l + e * LX * LX * LX];
82
83 l = l + CHUNKS;
84 }
85
87 for (int n = 0; n < nchunks; n++) {
88 const int ijk = iii + n * CHUNKS;
89 const int jk = ijk / LX;
90 const int i = ijk - jk * LX;
91 const int k = jk / LX;
92 const int j = jk - k * LX;
93 if ( i < LX && j < LX && k < LX && ijk < LX*LX*LX) {
94 T rtmp = 0.0;
95 T stmp = 0.0;
96 T ttmp = 0.0;
97 for (int l = 0; l < LX; l++) {
98 rtmp += shdxt[i + l * LX] * shtar[l+j*LX+k*LX*LX];
99 stmp += shdyt[j + l * LX] * shtas[i+l*LX + k*LX*LX];
100 ttmp += shdzt[k + l * LX] * shtat[i + j*LX + l*LX*LX];
101 }
102 dtx[ijk + e * LX * LX * LX] = ( rtmp + stmp + ttmp );
103
104 }
105 }
106}
107
108template< typename T, const int LX, const int EB >
111 const T * __restrict__ x,
119 const int nelv) {
120
124
127
128 static_assert(sizeof(shdxt) +
129 sizeof(shdyt) +
130 sizeof(shdzt) +
131 sizeof(shtar) +
132 sizeof(shtas)
134 "kstep block exceeds the LDS budget");
135
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 shdxt[ij] = dxt[ij];
155 shdyt[ij] = dyt[ij];
156 shdzt[ij] = dzt[ij];
157 }
158
159
160#pragma unroll LX
161 for (int k = 0; k < LX; ++k) {
162 T wx = x[ij + k*LX*LX + ele] * w3[ij + k*LX*LX];
163
164 rtar[k] = wx *dr[ij + k*LX*LX + ele];
165 rtas[k] = wx *ds[ij + k*LX*LX + ele];
166 rtat[k] = wx *dt[ij + k*LX*LX + ele];
167 }
168
170
171#pragma unroll
172 for (int k = 0; k < LX; ++k) {
173 const int ijk = ij + k*LX*LX;
174 T ttmp = 0.0;
175 shtar[sh + ij] = rtar[k];
176 shtas[sh + ij] = rtas[k];
177#pragma unroll
178 for (int l = 0; l < LX; l++) {
179 ttmp += shdzt[k+l*LX] * rtat[l];
180 }
182
183 T rtmp = 0.0;
184 T stmp = 0.0;
185#pragma unroll
186 for (int l = 0; l < LX; l++) {
187 rtmp += shdxt[i+l*LX] * shtar[sh + l+j*LX];
188 stmp += shdyt[j+l*LX] * shtas[sh + i+l*LX];
189 }
190
191 if (active) {
192 dtx[ijk + ele] = ( rtmp + stmp + ttmp );
193 }
194
196 }
197}
198
199
200
217#if defined(__gfx90a__) || defined(__gfx942__)
218
219template< typename T, const int LX, const int NWF, const int TILE >
221 const T * __restrict__ x,
222 const T * __restrict__ dr,
223 const T * __restrict__ ds,
224 const T * __restrict__ dt,
225 const T * __restrict__ dxt,
226 const T * __restrict__ dyt,
227 const T * __restrict__ dzt,
228 const T * __restrict__ w3,
229 const int nelv) {
230 const int LX2 = LX * LX;
231 const int LX3 = LX * LX * LX;
232
233 /* NWF wavefronts per block, WPE of them cooperating on one element and the
234 block covering EB elements, see the note in mfma_kernel.h. At LX = 4 the
235 contraction offers one column group, so WPE is 1 and every wavefront gets
236 an element of its own rather than idling. */
237 enum { EB = NEKO_MFMA_EB_N(NWF, LX, sizeof(T)),
238 WPE = NWF / EB,
239 /* Elements the batched single precision tile contracts per
240 call, from the wavefronts of the first element of each
241 batch; one for every other tile, see NEKO_MFMA_NEL_N */
242 NEL = NEKO_MFMA_NEL_N(EB, LX, sizeof(T), TILE) };
243 static_assert(WPE * EB == NWF,
244 "wavefronts per block must split evenly over the elements");
245
249 __shared__ T shtar[EB * LX * LX * LX]; // the three weighted fields
250 __shared__ T shtas[EB * LX * LX * LX];
251 __shared__ T shtat[EB * LX * LX * LX];
252 __shared__ T shout[EB * LX * LX * LX]; // the accumulated result
253
254 static_assert(sizeof(shdxt) + sizeof(shdyt) + sizeof(shdzt) +
255 sizeof(shtar) + sizeof(shtas) + sizeof(shtat) +
256 sizeof(shout)
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, already transposed, one copy per block */
281 for (int p = tid; p < LX2; p += nthr) {
282 shdxt[p] = dxt[p];
283 shdyt[p] = dyt[p];
284 shdzt[p] = dzt[p];
285 }
286 /* The three weighted fields, formed before anything is contracted, and the
287 accumulator cleared alongside them */
288 for (int p = gtid; p < LX3; p += gnthr) {
289 const int gp = p + ele;
290 const T wx = x[gp] * w3[p];
291
292 shtar[sh + p] = wx * dr[gp];
293 shtas[sh + p] = wx * ds[gp];
294 shtat[sh + p] = wx * dt[gp];
295 shout[sh + p] = 0.0;
296 }
297
299
300 /* dtx = Dr^T tar + Ds^T tas + Dt^T tat, accumulated in shout */
301 if (eb % NEL == 0)
302 mfma_contract_sel<T, LX, 0, false, true, WPE, TILE, 0, NEL>::run(
303 shout + sh, shdxt, shtar + sh, lane, sub);
305 if (eb % NEL == 0)
306 mfma_contract_sel<T, LX, 1, false, true, WPE, TILE, 0, NEL>::run(
307 shout + sh, shdyt, shtas + sh, lane, sub);
309 if (eb % NEL == 0)
310 mfma_contract_sel<T, LX, 2, false, true, WPE, TILE, 0, NEL>::run(
311 shout + sh, shdzt, shtat + sh, lane, sub);
313
314 if (active) {
315 for (int p = gtid; p < LX3; p += gnthr)
316 dtx[p + ele] = shout[sh + p];
317 }
318}
319
320#endif // __gfx90a__ || __gfx942__
321
322/*
323 * Compile-time dispatch onto the MFMA element kernel. The launch macros in
324 * opr_cdtp.hip are written for every LX the operator dispatches and for
325 * whatever `real` is, so every combination has to compile; the ones the
326 * strategy does not cover -- LX outside the supported range, a build without
327 * a matrix-core arch -- resolve to this no-op. The autotuner never selects
328 * the strategy for them, so the no-op is unreachable at runtime, see
329 * mfma_lx_supported() and hip_have_mfma() in mfma_kernel.h.
330 */
331template< typename T, const int LX, const int NWF, const int TILE >
333 __device__ static void run(T *, const T *, const T *, const T *, const T *,
334 const T *, const T *, const T *, const T *,
335 const int) {}
336};
337
338#if defined(__gfx90a__) || defined(__gfx942__)
339
340/* Keep in sync with mfma_lx_supported() in mfma_kernel.h */
341#define NEKO_CDTP_MFMA_DISPATCH(TYPE, LXV) \
342 template< const int NWF, const int TILE > \
343 struct cdtp_mfma_dispatch< TYPE, LXV, NWF, TILE > { \
344 __device__ static void run(TYPE * dtx, \
345 const TYPE * x, \
346 const TYPE * dr, \
347 const TYPE * ds, \
348 const TYPE * dt, \
349 const TYPE * dxt, \
350 const TYPE * dyt, \
351 const TYPE * dzt, \
352 const TYPE * w3, \
353 const int nelv) { \
354 cdtp_mfma_elem< TYPE, LXV, NWF, TILE >(dtx, x, dr, ds, dt, dxt, dyt, \
355 dzt, w3, nelv); \
356 } \
357 }
358
359NEKO_CDTP_MFMA_DISPATCH(double, 4);
360NEKO_CDTP_MFMA_DISPATCH(double, 5);
361NEKO_CDTP_MFMA_DISPATCH(double, 6);
362NEKO_CDTP_MFMA_DISPATCH(double, 7);
363NEKO_CDTP_MFMA_DISPATCH(double, 8);
364NEKO_CDTP_MFMA_DISPATCH(double, 9);
365NEKO_CDTP_MFMA_DISPATCH(double, 10);
366NEKO_CDTP_MFMA_DISPATCH(double, 11);
367NEKO_CDTP_MFMA_DISPATCH(double, 12);
368
377NEKO_CDTP_MFMA_DISPATCH(float, 10);
378NEKO_CDTP_MFMA_DISPATCH(float, 11);
379NEKO_CDTP_MFMA_DISPATCH(float, 12);
380
381#endif // __gfx90a__ || __gfx942__
382
383/*
384 * Note the bare __launch_bounds__ rather than NEKO_EB_BOUNDS, matching
385 * ax_helm_kernel_mfma: the kstep kernels ask for three waves per SIMD, and
386 * the matrix core kernels were validated without that constraint.
387 */
388template< typename T, const int LX, const int NWF, const int TILE >
391 const T * __restrict__ x,
392 const T * __restrict__ dr,
393 const T * __restrict__ ds,
394 const T * __restrict__ dt,
395 const T * __restrict__ dxt,
396 const T * __restrict__ dyt,
397 const T * __restrict__ dzt,
398 const T * __restrict__ w3,
399 const int nelv) {
400
402 w3, nelv);
403}
404
405#endif // __MATH_CDTP_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)
const bool active
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dyt
T rtas[LX]
const int sh
const int eb
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ ds
__shared__ T shdzt[LX *LX]
const int i
T rtat[LX]
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__ dzt
const int e
__global__ void const T *__restrict__ x
T rtar[LX]
__global__ void const T *__restrict__ const T *__restrict__ dr
const int e_blk
const int ele
__shared__ T shtar[EB *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 int nelv
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dt
const int j
__shared__ T shtas[EB *LX *LX]
__global__ void cdtp_kernel_1d(T *__restrict__ dtx, const T *__restrict__ x, const T *__restrict__ dr, const T *__restrict__ ds, const T *__restrict__ dt, const T *__restrict__ dxt, const T *__restrict__ dyt, const T *__restrict__ dzt, const T *__restrict__ w3)
Definition cdtp_kernel.h:46
__shared__ T shdyt[LX *LX]
__syncthreads()
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dxt
__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__ w3
#define NEKO_EB_BOUNDS(NT)
Definition elem_block.h:95
__global__ void __launch_bounds__((LX *LX *EB)) cdtp_kernel_kstep(T *__restrict__ dtx
#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)