1#ifndef __MATH_CDTP_KERNEL_H__
2#define __MATH_CDTP_KERNEL_H__
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) {
57 __shared__ T shdxt[LX * LX];
58 __shared__ T
shdyt[LX * LX];
59 __shared__ T
shdzt[LX * LX];
61 __shared__ T
shtar[LX * LX * LX];
62 __shared__ T
shtas[LX * LX * LX];
63 __shared__ T shtat[LX * LX * LX];
65 const int e = blockIdx.x;
66 const int iii = threadIdx.x;
67 const int nchunks = (LX * LX * LX - 1) / CHUNKS + 1;
69 if (iii < (LX * LX)) {
70 shdxt[iii] =
dxt[iii];
76 while(l < (LX * LX * LX)) {
77 T wx =
x[l +
e * LX * LX * LX] *
w3[l];
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];
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) {
97 for (
int l = 0; l < LX; l++) {
98 rtmp += shdxt[
i + l * LX] *
shtar[l+
j*LX+k*LX*LX];
100 ttmp +=
shdzt[k + l * LX] * shtat[
i +
j*LX + l*LX*LX];
102 dtx[ijk +
e * LX * LX * LX] = ( rtmp + stmp + ttmp );
108template<
typename T, const
int LX, const
int EB >
110 cdtp_kernel_kstep(T * __restrict__ dtx,
111 const T * __restrict__
x,
112 const T * __restrict__
dr,
113 const T * __restrict__
ds,
114 const T * __restrict__
dt,
115 const T * __restrict__
dxt,
116 const T * __restrict__
dyt,
117 const T * __restrict__
dzt,
118 const T * __restrict__
w3,
121 __shared__ T shdxt[LX * LX];
128 static_assert(
sizeof(shdxt) +
134 "kstep block exceeds the LDS budget");
140 const int eb = (EB == 1) ? 0 : threadIdx.z;
148 const int j = threadIdx.y;
149 const int i = threadIdx.x;
161 for (
int k = 0; k < LX; ++k) {
162 T wx =
x[
ij + k*LX*LX +
ele] *
w3[
ij + k*LX*LX];
172 for (
int k = 0; k < LX; ++k) {
173 const int ijk =
ij + k*LX*LX;
178 for (
int l = 0; l < LX; l++) {
186 for (
int l = 0; l < LX; l++) {
187 rtmp += shdxt[
i+l*LX] *
shtar[
sh + l+
j*LX];
192 dtx[ijk +
ele] = ( rtmp + stmp + ttmp );
217#if defined(__gfx90a__) || defined(__gfx942__)
219template<
typename T, const
int LX, const
int NWF, const
int TILE >
220__device__
void cdtp_mfma_elem(T * __restrict__ dtx,
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,
230 const int LX2 = LX * LX;
231 const int LX3 = LX * LX * LX;
243 static_assert(WPE * EB == NWF,
244 "wavefronts per block must split evenly over the elements");
246 __shared__ T shdxt[LX * LX];
247 __shared__ T
shdyt[LX * LX];
248 __shared__ T
shdzt[LX * LX];
249 __shared__ T
shtar[EB * LX * LX * LX];
250 __shared__ T
shtas[EB * LX * LX * LX];
251 __shared__ T shtat[EB * LX * LX * LX];
252 __shared__ T shout[EB * LX * LX * LX];
254 static_assert(
sizeof(shdxt) +
sizeof(
shdyt) +
sizeof(
shdzt) +
255 sizeof(
shtar) +
sizeof(
shtas) +
sizeof(shtat) +
258 "mfma block exceeds the shared memory budget");
260 const int lane = threadIdx.x;
261 const int wf = threadIdx.y;
262 const int tid = wf * 64 + lane;
263 const int nthr = NWF * 64;
265 const int eb = wf / WPE;
266 const int sub = wf % WPE;
267 const int gtid = sub * 64 + lane;
268 const int gnthr = WPE * 64;
274 const int e_blk = blockIdx.x * EB +
eb;
277 const int ele =
e * LX3;
278 const int sh =
eb * LX3;
281 for (
int p = tid; p < LX2; p += nthr) {
288 for (
int p = gtid; p < LX3; p += gnthr) {
289 const int gp = p +
ele;
290 const T wx =
x[gp] *
w3[p];
294 shtat[
sh + p] = wx *
dt[gp];
302 mfma_contract_sel<T, LX, 0, false, true, WPE, TILE, 0, NEL>::run(
303 shout +
sh, shdxt,
shtar +
sh, lane, sub);
306 mfma_contract_sel<T, LX, 1, false, true, WPE, TILE, 0, NEL>::run(
310 mfma_contract_sel<T, LX, 2, false, true, WPE, TILE, 0, NEL>::run(
311 shout +
sh,
shdzt, shtat +
sh, lane, sub);
315 for (
int p = gtid; p < LX3; p += gnthr)
316 dtx[p +
ele] = shout[
sh + p];
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 *,
338#if defined(__gfx90a__) || defined(__gfx942__)
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, \
354 cdtp_mfma_elem< TYPE, LXV, NWF, TILE >(dtx, x, dr, ds, dt, dxt, dyt, \
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);
369NEKO_CDTP_MFMA_DISPATCH(
float, 2);
370NEKO_CDTP_MFMA_DISPATCH(
float, 3);
371NEKO_CDTP_MFMA_DISPATCH(
float, 4);
372NEKO_CDTP_MFMA_DISPATCH(
float, 5);
373NEKO_CDTP_MFMA_DISPATCH(
float, 6);
374NEKO_CDTP_MFMA_DISPATCH(
float, 7);
375NEKO_CDTP_MFMA_DISPATCH(
float, 8);
376NEKO_CDTP_MFMA_DISPATCH(
float, 9);
377NEKO_CDTP_MFMA_DISPATCH(
float, 10);
378NEKO_CDTP_MFMA_DISPATCH(
float, 11);
379NEKO_CDTP_MFMA_DISPATCH(
float, 12);
388template<
typename T, const
int LX, const
int NWF, const
int TILE >
390cdtp_kernel_mfma(T * __restrict__ dtx,
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,
401 cdtp_mfma_dispatch< T, LX, NWF, TILE >::run(dtx,
x,
dr,
ds,
dt,
dxt,
dyt,
dzt,
__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 T *__restrict__ const T *__restrict__ const T *__restrict__ const int *__restrict__ const int nelv
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dyt
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ ds
__shared__ T shdzt[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__ dzt
__global__ void const T *__restrict__ x
__global__ void const T *__restrict__ const T *__restrict__ dr
__shared__ T shtar[EB *LX *LX]
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dt
__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)
__shared__ T shdyt[LX *LX]
__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)
__global__ void __launch_bounds__((LX *LX *EB)) cdtp_kernel_kstep(T *__restrict__ dtx
#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)