1#ifndef __MATH_DMMA_KERNEL_H__
2#define __MATH_DMMA_KERNEL_H__
101#include <cuda_runtime.h>
123#define NEKO_DMMA_CANDIDATES 3
124#define NEKO_DMMA_NW(C) (1 << ((C) + 1))
125#define NEKO_DMMA_NTHRDS(C) dim3(32 * NEKO_DMMA_NW(C), 1, 1)
147#define NEKO_DMMA_PPA(LX) ((DMMA_P % (LX) == 0) ? (DMMA_P / (LX)) : 1)
148#define NEKO_DMMA_PACK(LX) \
149 (NEKO_DMMA_PPA(LX) * NEKO_DMMA_PPA(LX) * NEKO_DMMA_PPA(LX))
150#define NEKO_DMMA_NBLCKS(NELV, LX) \
151 dim3(((NELV) + NEKO_DMMA_PACK(LX) - 1) / NEKO_DMMA_PACK(LX), 1, 1)
157 const char *
v = getenv(
"NEKO_DMMA_NW");
175#define NEKO_TUNE_LOG_DMMA(LX, T3) \
177 for (int c = 0; c < NEKO_DMMA_CANDIDATES; c++) { \
178 if ((T3)[c] >= NEKO_TUNE_INIT) { continue; } \
180 sprintf(lbl_, "DMMA %dw %de", \
181 NEKO_DMMA_NW(c), NEKO_DMMA_PACK(LX)); \
182 sprintf(neko_log_buf, "%-13s: %9.2f us/call", lbl_, \
183 NEKO_TUNE_US((T3)[c], iters)); \
184 log_message(neko_log_buf); \
199#ifndef NEKO_DMMA_ARCH_COMPILED
200#if defined(__CUDA_ARCH_LIST__)
203 const int arch[] = { __CUDA_ARCH_LIST__ };
205 for (
int i = 0;
i < (int) (
sizeof(arch)/
sizeof(arch[0]));
i++) {
206 if (arch[
i] >= 800 && arch[
i] < 1000) {
221 return (NEKO_DMMA_ARCH_COMPILED != 0);
232 static int cached = -1;
240 }
else if (cudaGetDevice(&dev) == cudaSuccess &&
241 cudaGetDeviceProperties(&prop, dev) == cudaSuccess) {
242 cached = ((prop.major == 8 && prop.minor == 0) ||
243 (prop.major == 9)) ? 1 : 0;
278template< const
int LX >
281 return (
sizeof(
real) == 8) && (LX >= 2) && (LX <=
DMMA_P);
300template< const
int LX >
303 return (
sizeof(
real) == 8) && (LX >= 4) && (LX <=
DMMA_P);
306#if defined(__CUDA_ARCH__) && (__CUDA_ARCH__ >= 800) && (__CUDA_ARCH__ < 1000)
334template< const
int LX, const
int PPA >
336 enum { PACK = PPA * PPA * PPA,
341 __device__ __forceinline__
static int ebase() {
342 return blockIdx.x * PACK;
345 __device__ __forceinline__
static dmma_idx map(
const int p,
const int ebase,
347 const int q = p / LX3;
348 const int r = p - q * LX3;
349 const int i = r % LX;
350 const int jk = r / LX;
351 const int j = jk % LX;
352 const int k = jk / LX;
353 const int qa = q % PPA;
354 const int qb = (q / PPA) % PPA;
355 const int qc = q / (PPA * PPA);
356 const int eq = ebase + q;
360 x.g = r + (eq <
nelv ? eq :
nelv - 1) * LX3;
362 x.live = (eq <
nelv);
373template< const
int LX >
374struct dmma_pack< LX, 1 > {
379 __device__ __forceinline__
static int ebase() {
380 return blockIdx.x * LX3;
383 __device__ __forceinline__
static dmma_idx map(
const int p,
const int ebase,
385 const int i = p % LX;
386 const int jk = p / LX;
387 const int j = jk % LX;
388 const int k = jk / LX;
406template< const
int AXIS >
410struct dmma_view< 0 > {
411 typedef nvcuda::wmma::col_major layout;
412 enum { LDM =
DMMA_SI, COL_MAJOR = 1 };
413 __device__ __forceinline__
static int base(
const int t) {
419struct dmma_view< 1 > {
420 typedef nvcuda::wmma::row_major layout;
421 enum { LDM =
DMMA_SI, COL_MAJOR = 0 };
422 __device__ __forceinline__
static int base(
const int t) {
428struct dmma_view< 2 > {
429 typedef nvcuda::wmma::row_major layout;
430 enum { LDM =
DMMA_SJ, COL_MAJOR = 0 };
431 __device__ __forceinline__
static int base(
const int t) {
442template< const
bool TRANSPOSE >
446struct dmma_amat< false > {
447 typedef nvcuda::wmma::col_major layout;
448 __device__ __forceinline__
static int koff(
const int l0) {
454struct dmma_amat< true > {
455 typedef nvcuda::wmma::row_major layout;
456 __device__ __forceinline__
static int koff(
const int l0) {
473template<
const int AXIS,
const bool TRANSPOSE,
const bool ACCUM,
475__device__ __forceinline__
476void dmma_contract(
double * __restrict__ out,
477 const double * __restrict__ dmat,
478 const double * __restrict__ in,
481 namespace wmma = nvcuda::wmma;
482 typedef dmma_view< AXIS > view;
483 typedef dmma_amat< TRANSPOSE > amat;
485 const wmma::layout_t mem_layout =
486 view::COL_MAJOR ? wmma::mem_col_major : wmma::mem_row_major;
494 wmma::load_matrix_sync(a[ks], dmat + amat::koff(ks *
DMMA_KS),
DMMA_P);
500 for (
int p = 0; p < npass; p++) {
501 const int t = wf + p * NW;
504 wmma::fragment< wmma::accumulator, DMMA_P, DMMA_P, DMMA_KS, double > acc;
507 wmma::load_matrix_sync(acc, out + view::base(t), view::LDM, mem_layout);
509 wmma::fill_fragment(acc, 0.0);
515 const int koff = view::COL_MAJOR ? l0 : (l0 * (int) view::LDM);
517 typename view::layout > b;
519 wmma::load_matrix_sync(b, in + view::base(t) + koff, view::LDM);
520 wmma::mma_sync(acc, a[ks], b, acc);
523 wmma::store_matrix_sync(out + view::base(t), acc, view::LDM, mem_layout);
__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 T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ v
__global__ void const T *__restrict__ x
#define NEKO_DMMA_CANDIDATES
static bool dmma_arch_compiled()
static bool dmma_vector_lx_supported()
static bool cuda_have_dmma()
static bool dmma_lx_supported()
static int neko_dmma_pin()