1#ifndef __MATH_CDTP_KERNEL_H__
2#define __MATH_CDTP_KERNEL_H__
45template<
typename T, const
int LX, const
int CHUNKS >
47 const T * __restrict__
x,
48 const T * __restrict__
dr,
49 const T * __restrict__
ds,
50 const T * __restrict__
dt,
51 const T * __restrict__
dxt,
52 const T * __restrict__
dyt,
53 const T * __restrict__
dzt,
54 const T * __restrict__
w3) {
56 __shared__ T shdxt[LX * LX];
57 __shared__ T
shdyt[LX * LX];
58 __shared__ T
shdzt[LX * LX];
60 __shared__ T
shtar[LX * LX * LX];
61 __shared__ T
shtas[LX * LX * LX];
62 __shared__ T shtat[LX * LX * LX];
64 const int e = blockIdx.x;
65 const int iii = threadIdx.x;
66 const int nchunks = (LX * LX * LX - 1) / CHUNKS + 1;
68 if (iii < (LX * LX)) {
69 shdxt[iii] =
dxt[iii];
75 while(l < (LX * LX * LX)) {
76 T wx =
x[l +
e * LX * LX * LX] *
w3[l];
78 shtar[l] = wx*
dr[l +
e * LX * LX * LX];
79 shtas[l] = wx*
ds[l +
e * LX * LX * LX];
80 shtat[l] = wx*
dt[l +
e * LX * LX * LX];
86 for (
int n = 0; n < nchunks; n++) {
87 const int ijk = iii + n * CHUNKS;
88 const int jk = ijk / LX;
89 const int i = ijk - jk * LX;
90 const int k = jk / LX;
91 const int j = jk - k * LX;
92 if (
i < LX &&
j < LX && k < LX && ijk < LX*LX*LX) {
96 for (
int l = 0; l < LX; l++) {
97 rtmp += shdxt[
i + l * LX] *
shtar[l+
j*LX+k*LX*LX];
99 ttmp +=
shdzt[k + l * LX] * shtat[
i +
j*LX + l*LX*LX];
101 dtx[ijk +
e * LX * LX * LX] = ( rtmp + stmp + ttmp );
107template<
typename T, const
int LX, const
int EB >
109 cdtp_kernel_kstep(T * __restrict__ dtx,
110 const T * __restrict__
x,
111 const T * __restrict__
dr,
112 const T * __restrict__
ds,
113 const T * __restrict__
dt,
114 const T * __restrict__
dxt,
115 const T * __restrict__
dyt,
116 const T * __restrict__
dzt,
117 const T * __restrict__
w3,
120 __shared__ T shdxt[LX * LX];
127 static_assert(
sizeof(shdxt) +
133 "kstep block exceeds the shared memory budget");
139 const int eb = (EB == 1) ? 0 : threadIdx.z;
147 const int j = threadIdx.y;
148 const int i = threadIdx.x;
160 for (
int k = 0; k < LX; ++k) {
161 T wx =
x[
ij + k*LX*LX +
ele] *
w3[
ij + k*LX*LX];
171 for (
int k = 0; k < LX; ++k) {
172 const int ijk =
ij + k*LX*LX;
177 for (
int l = 0; l < LX; l++) {
185 for (
int l = 0; l < LX; l++) {
186 rtmp += shdxt[
i+l*LX] *
shtar[
sh + l+
j*LX];
191 dtx[ijk +
ele] = ( rtmp + stmp + ttmp );
229#if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ >= 800) && (__CUDA_ARCH__ < 1000)
231template< const
int LX, const
int NW >
232__device__ __forceinline__
233void cdtp_dmma_elem(
double * __restrict__ dtx,
234 const double * __restrict__
x,
235 const double * __restrict__
dr,
236 const double * __restrict__
ds,
237 const double * __restrict__
dt,
238 const double * __restrict__
dxt,
239 const double * __restrict__
dyt,
240 const double * __restrict__
dzt,
241 const double * __restrict__
w3,
246 __shared__ __align__(16) double shdxt[
DMMA_MAT];
254 __shared__ __align__(16)
double shtat[
DMMA_CUBE];
255 __shared__ __align__(16)
double shout[
DMMA_CUBE];
257 static_assert(sizeof(shdxt) +
265 "dmma block exceeds the shared memory budget");
269 PACK = PPA * PPA * PPA,
273 typedef dmma_pack< LX, PPA > pack;
275 const int nthrds = 32 * NW;
276 const int tid = threadIdx.x;
277 const int wf = tid >> 5;
278 const int ebase = pack::ebase();
285 for (
int p = tid; p <
DMMA_CUBE; p += nthrds) {
292 for (
int p = tid; p <
DMMA_MAT; p += nthrds) {
303 for (
int p = tid; p < LX * LX; p += nthrds) {
304 const int i = p % LX;
305 const int l = p / LX;
307 for (
int b = 0; b < PPA; b++) {
308 const int m = (b * LX +
i) +
DMMA_P * (b * LX + l);
315 for (
int p = tid; p < NP; p += nthrds) {
316 const dmma_idx idx = pack::map(p, ebase,
nelv);
318 const int gp = idx.g;
319 const double wx =
x[gp] *
w3[idx.l];
323 shtat[c] = wx *
dt[gp];
331 dmma_contract< 0, false, false, NW >(shout, shdxt,
shtar, wf);
333 dmma_contract< 1, false, true, NW >(shout,
shdyt,
shtas, wf);
335 dmma_contract< 2, false, true, NW >(shout,
shdzt, shtat, wf);
338 for (
int p = tid; p < NP; p += nthrds) {
339 const dmma_idx idx = pack::map(p, ebase,
nelv);
342 dtx[idx.g] = shout[idx.c];
358template<
typename T, const
int LX, const
int NW >
360 __device__
static void run(T * __restrict__,
361 const T * __restrict__,
const T * __restrict__,
362 const T * __restrict__,
const T * __restrict__,
363 const T * __restrict__,
const T * __restrict__,
364 const T * __restrict__,
const T * __restrict__,
368#if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ >= 800) && (__CUDA_ARCH__ < 1000)
371#define NEKO_CDTP_DMMA_DISPATCH(LXV) \
372 template< const int NW > \
373 struct cdtp_dmma_dispatch< double, LXV, NW > { \
374 __device__ static void run(double * __restrict__ dtx, \
375 const double * __restrict__ x, \
376 const double * __restrict__ dr, \
377 const double * __restrict__ ds, \
378 const double * __restrict__ dt, \
379 const double * __restrict__ dxt, \
380 const double * __restrict__ dyt, \
381 const double * __restrict__ dzt, \
382 const double * __restrict__ w3, \
384 cdtp_dmma_elem< LXV, NW >(dtx, x, dr, ds, dt, \
385 dxt, dyt, dzt, w3, nelv); \
389NEKO_CDTP_DMMA_DISPATCH(2);
390NEKO_CDTP_DMMA_DISPATCH(3);
391NEKO_CDTP_DMMA_DISPATCH(4);
392NEKO_CDTP_DMMA_DISPATCH(5);
393NEKO_CDTP_DMMA_DISPATCH(6);
394NEKO_CDTP_DMMA_DISPATCH(7);
395NEKO_CDTP_DMMA_DISPATCH(8);
399template<
typename T, const
int LX, const
int NW >
401cdtp_kernel_dmma(T * __restrict__ dtx,
402 const T * __restrict__
x,
403 const T * __restrict__
dr,
404 const T * __restrict__
ds,
405 const T * __restrict__
dt,
406 const T * __restrict__
dxt,
407 const T * __restrict__
dyt,
408 const T * __restrict__
dzt,
409 const T * __restrict__
w3,
445#if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ >= 900) && \
446 (__CUDA_ARCH__ < 1000) && NEKO_TMA_TOOLKIT
448template< const
int LX, const
int NW >
449__device__ __forceinline__
450void cdtp_dmma_tma_elem(
double * __restrict__ dtx,
451 const double * __restrict__
x,
452 const double * __restrict__
dr,
453 const double * __restrict__
ds,
454 const double * __restrict__
dt,
455 const double * __restrict__
dxt,
456 const double * __restrict__
dyt,
457 const double * __restrict__
dzt,
458 const double * __restrict__
w3) {
462 __shared__ __align__(128) double shdxt[
DMMA_MAT];
467 __shared__ __align__(128)
double shx[
DMMA_CUBE];
470 __shared__ __align__(128)
double shtat[
DMMA_CUBE];
471 __shared__ __align__(128)
double shout[
DMMA_CUBE];
473 __shared__ __align__(8)
unsigned long long bar_a;
474 __shared__ __align__(8)
unsigned long long bar_b;
476 static_assert(sizeof(shdxt) +
487 "cdtp dmma tma block exceeds the shared memory budget");
492 static_assert(LX ==
DMMA_P,
493 "the dmma tma variant stages whole cubes only");
495 enum { CUBE_BYTES =
DMMA_CUBE * (int)
sizeof(double) };
497 const int nthrds = 32 * NW;
498 const int tid = threadIdx.x;
499 const int wf = tid >> 5;
500 const int ebase = blockIdx.x *
DMMA_CUBE;
503 tma_barrier_init(&bar_a, 1);
504 tma_barrier_init(&bar_b, 1);
509 for (
int p = tid; p <
DMMA_MAT; p += nthrds) {
521 tma_expect(&bar_a, 2 * CUBE_BYTES);
522 tma_load(shx,
x + ebase, CUBE_BYTES, &bar_a);
523 tma_load(
shtar,
dr + ebase, CUBE_BYTES, &bar_a);
525 tma_expect(&bar_b, 2 * CUBE_BYTES);
526 tma_load(
shtas,
ds + ebase, CUBE_BYTES, &bar_b);
527 tma_load(shtat,
dt + ebase, CUBE_BYTES, &bar_b);
535 for (
int p = tid; p <
DMMA_CUBE; p += nthrds) {
541 dmma_contract< 0, false, false, NW >(shout, shdxt,
shtar, wf);
546 for (
int p = tid; p <
DMMA_CUBE; p += nthrds) {
547 const double wx = shx[p] *
w3[p];
555 dmma_contract< 1, false, true, NW >(shout,
shdyt,
shtas, wf);
557 dmma_contract< 2, false, true, NW >(shout,
shdzt, shtat, wf);
566 tma_store(dtx + ebase, shout, CUBE_BYTES);
583template<
typename T, const
int LX, const
int NW >
585 __device__
static void run(T * __restrict__,
586 const T * __restrict__,
const T * __restrict__,
587 const T * __restrict__,
const T * __restrict__,
588 const T * __restrict__,
const T * __restrict__,
589 const T * __restrict__,
const T * __restrict__) { }
592#if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ >= 900) && \
593 (__CUDA_ARCH__ < 1000) && NEKO_TMA_TOOLKIT
596#define NEKO_CDTP_DMMA_TMA_DISPATCH(LXV) \
597 template< const int NW > \
598 struct cdtp_dmma_tma_dispatch< double, LXV, NW > { \
599 __device__ static void run(double * __restrict__ dtx, \
600 const double * __restrict__ x, \
601 const double * __restrict__ dr, \
602 const double * __restrict__ ds, \
603 const double * __restrict__ dt, \
604 const double * __restrict__ dxt, \
605 const double * __restrict__ dyt, \
606 const double * __restrict__ dzt, \
607 const double * __restrict__ w3) { \
608 cdtp_dmma_tma_elem< LXV, NW >(dtx, x, dr, ds, dt, \
609 dxt, dyt, dzt, w3); \
613NEKO_CDTP_DMMA_TMA_DISPATCH(8);
617template<
typename T, const
int LX, const
int NW >
619cdtp_kernel_dmma_tma(T * __restrict__ dtx,
620 const T * __restrict__
x,
621 const T * __restrict__
dr,
622 const T * __restrict__
ds,
623 const T * __restrict__
dt,
624 const T * __restrict__
dxt,
625 const T * __restrict__
dyt,
626 const T * __restrict__
dzt,
627 const T * __restrict__
w3) {
__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)
static __device__ void run(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)
static __device__ void run(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__)