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_sparse_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_, typename IT_>
20 void MatMatMultSparseDense::exec_cuda_impl(DT_ * r, const DT_ alpha, const DT_ * const val, const IT_ * const col_idx, const IT_ * const row_ptr, const Index nonzeros,
21 const DT_ * y, const DT_ * z, const Index rows, const Index cols, const Index inner)
23 XASSERT((r == z) || (z == nullptr));
25 cudaDataType ct; //compute type
26 if (typeid(DT_) == typeid(double))
31 else if (typeid(DT_) == typeid(float))
36 #ifdef FEAT_HAVE_HALFMATH
37 else if (typeid(DT_) == typeid(Half))
40 ct = CUDA_R_32F; //cusparseSpMM does not support computation in half, yet
45 throw InternalError(__func__, __FILE__, __LINE__, "unsupported data type!");
48 cusparseIndexType_t it;
50 it = CUSPARSE_INDEX_32I;
51 else if(sizeof(IT_) == 8u)
52 it = CUSPARSE_INDEX_64I;
55 throw InternalError(__func__, __FILE__, __LINE__, "unsupported index type!");
58 cusparseStatus_t status;
60 cusparseDnMatDescr_t descr_r=0;
61 status = cusparseCreateDnMat(&descr_r, rows, cols, cols, (void*)r, dt, CUSPARSE_ORDER_ROW);
62 if (status != CUSPARSE_STATUS_SUCCESS)
63 throw InternalError(__func__, __FILE__, __LINE__, "cuda error: " + stringify(cusparseGetErrorString(status)));
65 cusparseSpMatDescr_t descr_x=0;
66 status = cusparseCreateCsr(&descr_x, rows, inner, nonzeros, (void*)row_ptr, (void*)col_idx, (void*)val, it, it, CUSPARSE_INDEX_BASE_ZERO, dt);
67 if (status != CUSPARSE_STATUS_SUCCESS)
68 throw InternalError(__func__, __FILE__, __LINE__, "cuda error: " + stringify(cusparseGetErrorString(status)));
70 cusparseDnMatDescr_t descr_y=0;
71 status = cusparseCreateDnMat(&descr_y, inner, cols, cols, (void*)y, dt, CUSPARSE_ORDER_ROW);
72 if (status != CUSPARSE_STATUS_SUCCESS)
73 throw InternalError(__func__, __FILE__, __LINE__, "cuda error: " + stringify(cusparseGetErrorString(status)));
75 cusparseOperation_t trans = CUSPARSE_OPERATION_NON_TRANSPOSE;
76 size_t buffer_size(0);
77 DT_ beta = (z == nullptr ? DT_(0) : DT_(1));
78 status = cusparseSpMM_bufferSize(Util::Intern::cusparse_handle, trans, trans, &alpha, descr_x, descr_y, &beta, descr_r, ct, CUSPARSE_SPMM_CSR_ALG2, &buffer_size);
79 if (status != CUSPARSE_STATUS_SUCCESS)
80 throw InternalError(__func__, __FILE__, __LINE__, "cusparsecsrmvex_buffersize failed with status code: " + stringify(cusparseGetErrorString(status)));
82 void* buffer = Util::cuda_get_static_memory(buffer_size);
84 status = cusparseSpMM(Util::Intern::cusparse_handle, trans, trans, &alpha, descr_x, descr_y, &beta, descr_r, ct, CUSPARSE_SPMM_CSR_ALG2, buffer);
85 if (status != CUSPARSE_STATUS_SUCCESS)
86 throw InternalError(__func__, __FILE__, __LINE__, "cusparseSpMM failed with status code: " + stringify(cusparseGetErrorString(status)));
88 cusparseDestroyDnMat(descr_r);
89 cusparseDestroySpMat(descr_x);
90 cusparseDestroyDnMat(descr_y);
93 cudaDeviceSynchronize();
94 cudaError_t last_error(cudaGetLastError());
95 if (cudaSuccess != last_error)
96 throw InternalError(__func__, __FILE__, __LINE__, "CUDA error occurred in execution!\n" + stringify(cudaGetErrorString(last_error)));
100 #ifdef FEAT_HAVE_HALFMATH
101 template void MatMatMultSparseDense::exec_cuda_impl(Half *, const Half, const Half * const, const std::uint32_t * const, const std::uint32_t * const, const Index, const Half *, const Half *, const Index, const Index, const Index);
103 template void MatMatMultSparseDense::exec_cuda_impl(float *, const float, const float * const, const std::uint32_t * const, const std::uint32_t * const, const Index, const float *, const float *, const Index, const Index, const Index);
104 template void MatMatMultSparseDense::exec_cuda_impl(double *, const double, const double * const, const std::uint32_t * const, const std::uint32_t * const, const Index, const double *, const double *, const Index, const Index, const Index);
105 #ifdef FEAT_HAVE_HALFMATH
106 template void MatMatMultSparseDense::exec_cuda_impl(Half *, const Half, const Half * const, const std::uint64_t * const, const std::uint64_t * const, const Index, const Half *, const Half *, const Index, const Index, const Index);
108 template void MatMatMultSparseDense::exec_cuda_impl(float *, const float, const float * const, const std::uint64_t * const, const std::uint64_t * const, const Index, const float *, const float *, const Index, const Index, const Index);
109 template void MatMatMultSparseDense::exec_cuda_impl(double *, const double, const double * const, const std::uint64_t * const, const std::uint64_t * const, const Index, const double *, const double *, const Index, const Index, const Index);
110} // namespace FEAT::LAFEM::Arch