1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
7#include <kernel/base_header.hpp>
8#include <kernel/lafem/arch/matmatmult_dense_dense.hpp>
9#include <kernel/util/exception.hpp>
10#include <kernel/util/cuda_util.hpp>
11#include <kernel/util/half.hpp>
15#include <cusparse_v2.h>
17namespace FEAT::LAFEM::Arch
19 template <typename DT_>
20 void MatMatMultDenseDense::exec_cuda_impl(DT_ * r, const DT_ alpha, const DT_ * const x, const DT_ * const y, const DT_ * const z, const Index rows, const Index cols, const Index inner)
22 if (r==y || r==x || x==y || z==x || z==y)
23 throw InternalError(__func__, __FILE__, __LINE__, "cuda MatMatMultDenseDense does not allow r==y or r==x or x==y or z==x or z==y!");
25 cublasStatus_t status;
27 //XASSERTM(z != nullptr, "case z = nullptr not implemented yet");
29 Memory::memset_cuda(r, 0, sizeof(DT_)*rows*cols);
31 // inspired by https://github.com/NVIDIA/CUDALibrarySamples/blob/master/cuBLASLt/LtSgemm/sample_cublasLt_LtSgemm.cu
33 cublasLtMatmulDesc_t operationDesc = NULL;
34 cublasLtMatrixLayout_t Rdesc = NULL, Adesc = NULL, Bdesc = NULL, Cdesc = NULL;
35 cublasLtMatmulPreference_t preference = NULL;
37 int algo_selector = -1;
40 cublasComputeType_t ct;
41 if (typeid(DT_) == typeid(double))
44 ct = CUBLAS_COMPUTE_64F;
45 algo_selector = (rows > 1 && cols > 1 && inner > 1) ? 0 : 1;
47 else if (typeid(DT_) == typeid(float))
50 #if __CUDA_ARCH__ < 800
51 ct = CUBLAS_COMPUTE_32F;
53 ct = CUBLAS_COMPUTE_32F_FAST_TF32;
55 algo_selector = (rows > 1 && cols > 1 && inner > 1) ? 2 : 3;
57 #ifdef FEAT_HAVE_HALFMATH
58 else if (typeid(DT_) == typeid(Half))
61 ct = CUBLAS_COMPUTE_16F;
62 algo_selector = (rows > 1 && cols > 1 && inner > 1) ? 4 : 5;
66 throw InternalError(__func__, __FILE__, __LINE__, "unsupported data type!");
68 status = cublasLtMatmulDescCreate(&operationDesc, ct, dt);
69 if (status != CUBLAS_STATUS_SUCCESS)
70 throw InternalError(__func__, __FILE__, __LINE__, "cuda error: " + stringify(cublasGetStatusString(status)));
72 cublasLtOrder_t matrix_order = CUBLASLT_ORDER_ROW;
73 status = cublasLtMatrixLayoutCreate(&Rdesc, dt, rows, cols, cols);
74 if (status != CUBLAS_STATUS_SUCCESS)
75 throw InternalError(__func__, __FILE__, __LINE__, "cuda error: " + stringify(cublasGetStatusString(status)));
76 status = cublasLtMatrixLayoutSetAttribute(Rdesc, CUBLASLT_MATRIX_LAYOUT_ORDER, &matrix_order, sizeof(cublasLtOrder_t));
77 if (status != CUBLAS_STATUS_SUCCESS)
78 throw InternalError(__func__, __FILE__, __LINE__, "cuda error: " + stringify(cublasGetStatusString(status)));
79 cublasLtMatrixLayoutCreate(&Adesc, dt, rows, inner, inner);
80 cublasLtMatrixLayoutSetAttribute(Adesc, CUBLASLT_MATRIX_LAYOUT_ORDER, &matrix_order, sizeof(cublasLtOrder_t));
81 cublasLtMatrixLayoutCreate(&Bdesc, dt, inner, cols, cols);
82 cublasLtMatrixLayoutSetAttribute(Bdesc, CUBLASLT_MATRIX_LAYOUT_ORDER, &matrix_order, sizeof(cublasLtOrder_t));
85 cublasLtMatrixLayoutCreate(&Cdesc, dt, rows, cols, cols);
86 cublasLtMatrixLayoutSetAttribute(Cdesc, CUBLASLT_MATRIX_LAYOUT_ORDER, &matrix_order, sizeof(cublasLtOrder_t));
88 else // r==z -> in-place multiplication
93 cublasLtMatmulHeuristicResult_t algo_check_result;
94 if (! FEAT::Util::Intern::cublas_lt_algo_matmat_initialized[algo_selector] ||
95 CUBLAS_STATUS_SUCCESS != cublasLtMatmulAlgoCheck((cublasLtHandle_t)Util::Intern::cublas_handle, operationDesc, Adesc, Bdesc, Cdesc, Rdesc, &(FEAT::Util::Intern::cublas_lt_algo_matmat[algo_selector]), &algo_check_result))
98 cublasLtMatmulHeuristicResult_t heuristic_algos = {};
100 status = cublasLtMatmulPreferenceCreate(&preference);
101 if (status != CUBLAS_STATUS_SUCCESS)
102 throw InternalError(__func__, __FILE__, __LINE__, "cuda error: " + stringify(cublasGetStatusString(status)));
103 //status = cublasLtMatmulPreferenceSetAttribute(preference, CUBLASLT_MATMUL_PREF_MAX_WORKSPACE_BYTES, &(FEAT::Util::Intern::cuda_workspace_size), sizeof(FEAT::Util::Intern::cuda_workspace_size));
104 if (status != CUBLAS_STATUS_SUCCESS)
105 throw InternalError(__func__, __FILE__, __LINE__, "cuda error: " + stringify(cublasGetStatusString(status)));
107 status = cublasLtMatmulAlgoGetHeuristic((cublasLtHandle_t)Util::Intern::cublas_handle, operationDesc, Adesc, Bdesc, Cdesc, Rdesc, preference, 1, &heuristic_algos, &num_algos);
108 if (status != CUBLAS_STATUS_SUCCESS)
109 throw InternalError(__func__, __FILE__, __LINE__, "cuda error: " + stringify(cublasGetStatusString(status)));
112 throw InternalError(__func__, __FILE__, __LINE__, "no algo supports our matrices!");
114 FEAT::Util::Intern::cublas_lt_algo_matmat[algo_selector] = heuristic_algos.algo;
115 FEAT::Util::Intern::cublas_lt_algo_matmat_initialized[algo_selector] = true;
118 cublasLtMatmulAlgo_t * algo = &(FEAT::Util::Intern::cublas_lt_algo_matmat[algo_selector]);
120 DT_ beta = DT_(z != nullptr ? 1 : 0);
121 const DT_* zr = (z != nullptr ? z : r);
122 //status = cublasLtMatmul((cublasLtHandle_t)Util::Intern::cublas_handle, operationDesc, &alpha, x, Adesc, y, Bdesc, &beta, z, Cdesc, r, Rdesc, algo, FEAT::Util::Intern::cuda_workspace, FEAT::Util::Intern::cuda_workspace_size, 0);
123 status = cublasLtMatmul((cublasLtHandle_t)Util::Intern::cublas_handle, operationDesc, &alpha, x, Adesc, y, Bdesc, &beta, zr, Cdesc, r, Rdesc, algo, NULL, 0, 0);
124 if (status != CUBLAS_STATUS_SUCCESS)
125 throw InternalError(__func__, __FILE__, __LINE__, "cuda error: " + stringify(cublasGetStatusString(status)));
128 cudaDeviceSynchronize();
129 cudaError_t last_error(cudaGetLastError());
130 if (cudaSuccess != last_error)
131 throw InternalError(__func__, __FILE__, __LINE__, "CUDA error occurred in execution!\n" + stringify(cudaGetErrorString(last_error)));
135 #ifdef FEAT_HAVE_HALFMATH
136 template void MatMatMultDenseDense::exec_cuda_impl(Half *, const Half, const Half * const, const Half * const, const Half * const, const Index, const Index, const Index);
138 template void MatMatMultDenseDense::exec_cuda_impl(float *, const float, const float * const, const float * const, const float * const, const Index, const Index, const Index);
139 template void MatMatMultDenseDense::exec_cuda_impl(double *, const double, const double * const, const double * const, const double * const, const Index, const Index, const Index);
140} // namespace FEAT::LAFEM::Arch