FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
matmatmult_dense_dense.cu
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.
5
6// includes, FEAT
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>
12
13#include <cublas_v2.h>
14#include <cublasLt.h>
15#include <cusparse_v2.h>
16
17namespace FEAT::LAFEM::Arch
18{
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)
21 {
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!");
24
25 cublasStatus_t status;
26
27 //XASSERTM(z != nullptr, "case z = nullptr not implemented yet");
28 if(z == nullptr)
29 Memory::memset_cuda(r, 0, sizeof(DT_)*rows*cols);
30
31 // inspired by https://github.com/NVIDIA/CUDALibrarySamples/blob/master/cuBLASLt/LtSgemm/sample_cublasLt_LtSgemm.cu
32
33 cublasLtMatmulDesc_t operationDesc = NULL;
34 cublasLtMatrixLayout_t Rdesc = NULL, Adesc = NULL, Bdesc = NULL, Cdesc = NULL;
35 cublasLtMatmulPreference_t preference = NULL;
36
37 int algo_selector = -1;
38
39 cudaDataType dt;
40 cublasComputeType_t ct;
41 if (typeid(DT_) == typeid(double))
42 {
43 dt = CUDA_R_64F;
44 ct = CUBLAS_COMPUTE_64F;
45 algo_selector = (rows > 1 && cols > 1 && inner > 1) ? 0 : 1;
46 }
47 else if (typeid(DT_) == typeid(float))
48 {
49 dt = CUDA_R_32F;
50 #if __CUDA_ARCH__ < 800
51 ct = CUBLAS_COMPUTE_32F;
52 #else
53 ct = CUBLAS_COMPUTE_32F_FAST_TF32;
54 #endif
55 algo_selector = (rows > 1 && cols > 1 && inner > 1) ? 2 : 3;
56 }
57 #ifdef FEAT_HAVE_HALFMATH
58 else if (typeid(DT_) == typeid(Half))
59 {
60 dt = CUDA_R_16F;
61 ct = CUBLAS_COMPUTE_16F;
62 algo_selector = (rows > 1 && cols > 1 && inner > 1) ? 4 : 5;
63 }
64 #endif
65 else
66 throw InternalError(__func__, __FILE__, __LINE__, "unsupported data type!");
67
68 status = cublasLtMatmulDescCreate(&operationDesc, ct, dt);
69 if (status != CUBLAS_STATUS_SUCCESS)
70 throw InternalError(__func__, __FILE__, __LINE__, "cuda error: " + stringify(cublasGetStatusString(status)));
71
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));
83 if (r!=z)
84 {
85 cublasLtMatrixLayoutCreate(&Cdesc, dt, rows, cols, cols);
86 cublasLtMatrixLayoutSetAttribute(Cdesc, CUBLASLT_MATRIX_LAYOUT_ORDER, &matrix_order, sizeof(cublasLtOrder_t));
87 }
88 else // r==z -> in-place multiplication
89 {
90 Cdesc = Rdesc;
91 }
92
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))
96 {
97 int num_algos = 0;
98 cublasLtMatmulHeuristicResult_t heuristic_algos = {};
99
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)));
106
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)));
110
111 if (num_algos == 0)
112 throw InternalError(__func__, __FILE__, __LINE__, "no algo supports our matrices!");
113
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;
116 }
117
118 cublasLtMatmulAlgo_t * algo = &(FEAT::Util::Intern::cublas_lt_algo_matmat[algo_selector]);
119
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)));
126
127 #ifdef DEBUG
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)));
132 #endif
133 }
134
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);
137 #endif
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