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/matvecmult_csr_dense.hpp>
9#include <kernel/util/memory_aux.hpp>
10#include <kernel/util/exception.hpp>
11#include <kernel/util/cuda_util.hpp>
12#include <kernel/util/math.hpp>
13#include <kernel/util/half.hpp>
15#include "cusparse_v2.h"
16#include <kernel/util/cuda_helper.cuh>
17#include "cusparse_helper.cuh"
19namespace FEAT::LAFEM::Arch
21 template<typename DT_>
22 static inline void _apply_spmv_csr_cuda(DT_* r, const DT_* y, cusparseSpMatDescr_t descr, cusparseDnVecDescr_t dr, cusparseDnVecDescr_t dx, cusparseOperation_t trans, const DT_ alpha,
23 const DT_ beta, cudaDataType ct, void* buffer, Index cols, Index rows, bool transposed)
25 // since async ops are on stream 0 (i.e. default stream), they are always blocking. This might or might not be a feature
28 cudaCheck(cudaMemsetAsync(r, 0, sizeof(DT_) * (transposed ? cols : rows)), "SPMV Apply memset");
32 cudaCheck(cudaMemcpyAsync(r, y, sizeof(DT_) * (transposed ? cols : rows), cudaMemcpyDeviceToDevice), "SPMV Apply memcpy");
34 cusparseCheck(cusparseSpMV(Util::Intern::cusparse_handle, trans, &alpha, descr, dx, &beta, dr, ct, CUSPARSE_SPMV_CSR_ALG1, buffer), "cuSparseCSRSPMV");
37 template <typename DT_, typename IT_>
38 void MatVecMultCSRDense::exec_cuda_impl(void* workspace, DT_ * r, const DT_ alpha, const DT_ * const x, const DT_ * const y, const DT_ * const val, const IT_ * const col_idx, const IT_ * const row_ptr, const Index rows,
39 const Index cols, const Index nonzeros, const bool transposed)
41 const cusparseOperation_t trans = transposed ? CUSPARSE_OPERATION_TRANSPOSE : CUSPARSE_OPERATION_NON_TRANSPOSE;
44 cudaDataType ct; // compute type
45 Intern::cusparse_deduce_data_type<DT_>(dt, ct);
47 const cusparseIndexType_t it = Intern::cusparse_deduce_index_type<IT_>();
49 cusparseSpMatDescr_t descr = 0;
50 cusparseDnVecDescr_t dx = 0;
51 cusparseDnVecDescr_t dr = 0;
52 void* buffer = nullptr;
57 CuSparseSPMVWorkSpace* sp_ws = static_cast<CuSparseSPMVWorkSpace*>(workspace);
58 descr = sp_ws->csr_mat;
59 // only do this in debug
63 int64_t t_rows, t_cols, t_nnz;
67 cusparseIndexType_t t_row_type, t_col_type;
68 cusparseIndexBase_t t_idxbase;
69 cudaDataType t_valtype;
70 cusparseCheck(cusparseCsrGet(descr, &t_rows, &t_cols, &t_nnz, &t_row_off, &t_col_ind, &t_values, &t_row_type, &t_col_type, &t_idxbase, &t_valtype), "cusparseCSRGet");
71 // check if structure is the same
72 XASSERT(Index(t_rows) == rows);
73 XASSERT(Index(t_cols) == cols);
74 XASSERT(Index(t_nnz) == nonzeros);
75 XASSERT(t_col_ind == (void*)col_idx);
76 XASSERT(t_row_off == (void*)row_ptr);
77 XASSERT(dt == t_valtype);
82 cusparseCheck(cusparseCsrSetPointers(descr, (void*)row_ptr, (void*)col_idx, (void*)val), "cusparseSetPointers");
86 // if the matrix is rectangular, we have to check if the vector descriptor tracks the correct size
87 // due to transpose vs. non_transpose
90 Index rsize = transposed ? rows : cols;
91 if(!check_size_dnvec(dx, rsize))
94 cusparseCheck(cusparseDestroyDnVec(sp_ws->vec_x), "destroyDnVec");
96 cusparseCheck(cusparseCreateDnVec(&sp_ws->vec_x, int64_t(rsize), (void*)x, dt), "Cusparse Create dnvec");
100 cusparseCheck(cusparseDnVecSetValues(dx, (void*)x), "Set x Value");
106 Index rsize = transposed ? cols : rows;
107 if(!check_size_dnvec(dr, rsize))
110 cusparseCheck(cusparseDestroyDnVec(sp_ws->vec_y), "destroyDnVec");
112 cusparseCheck(cusparseCreateDnVec(&sp_ws->vec_y, int64_t(rsize), (void*)r, dt), "Cusparse Create dnvec");
116 cusparseCheck(cusparseDnVecSetValues(dr, (void*)r), "Set r Value");
118 buffer = sp_ws->buffer;
122 cusparseCheck(cusparseCreateCsr(&descr, int64_t(rows), int64_t(cols), int64_t(nonzeros), (void*)row_ptr, (void*)col_idx, (void*)val, it, it, CUSPARSE_INDEX_BASE_ZERO, dt), "cusparseCreateCSR");
123 cusparseCheck(cusparseCreateDnVec(&dx, int64_t(transposed?rows:cols), (void*)x, dt), "cusparseCreateDnVec");
124 cusparseCheck(cusparseCreateDnVec(&dr, int64_t(transposed?cols:rows), (void*)r, dt), "cusparseCreateDnVec");
126 size_t buffer_size(0);
127 cusparseCheck(cusparseSpMV_bufferSize(Util::Intern::cusparse_handle, trans, &alpha, descr, dx, &beta, dr, ct, CUSPARSE_SPMV_CSR_ALG1, &buffer_size), "cuSparseBufferSize");
128 buffer = Util::cuda_get_static_memory(buffer_size);
131 _apply_spmv_csr_cuda(r, y, descr, dr, dx, trans, alpha, beta, ct, buffer, cols, rows, transposed);
135 cusparseDestroySpMat(descr);
136 cusparseDestroyDnVec(dx);
137 cusparseDestroyDnVec(dr);
143 template <typename DT_, typename IT_>
144 void MatVecMultCSRDense::init_workspace_cuda(std::shared_ptr<void>& workspace, const Memory::Arbiter& DOXY(val), const Memory::Arbiter& col_idx, const Memory::Arbiter& row_ptr, const Index rows, const Index cols, const Index nonzeros, const bool transposed)
146 // fetch the persistent device pointers of the matrix arrays; these are assumed to stay valid
147 // for the whole lifetime of the workspace (see the warning in the header).
148 Memory::TypedView<IT_> rp_view(row_ptr.view(Memory::Location::cuda, Memory::Access::read));
149 Memory::TypedView<IT_> ci_view(col_idx.view(Memory::Location::cuda, Memory::Access::read));
150 // Memory::TypedView<DT_> a_view(val.view(Memory::Location::cuda, Memory::Access::read));
152 const cusparseOperation_t trans = transposed ? CUSPARSE_OPERATION_TRANSPOSE : CUSPARSE_OPERATION_NON_TRANSPOSE;
155 cudaDataType ct; // compute type
156 Intern::cusparse_deduce_data_type<DT_>(dt, ct);
158 const cusparseIndexType_t it = Intern::cusparse_deduce_index_type<IT_>();
160 // The SpMV buffer size does not depend on the actual vector data, only on the vector sizes and
161 // types. As the concrete x/r device pointers are only known at apply time, we hand cuSPARSE a
162 // pseudo pointer purely to satisfy the descriptor creation.
163 // This can not be a nullptr, since there is an assert statement int the cuda interface
164 void* const pseudo_ptr = (void*)0x048;
166 // create the persistent CSR matrix descriptor from the (fixed) matrix arrays
167 cusparseSpMatDescr_t descr = nullptr;
168 cusparseCheck(cusparseCreateCsr(&descr, int64_t(rows), int64_t(cols), int64_t(nonzeros), (void*)rp_view.get_r(), (void*)ci_view.get_r(), pseudo_ptr, it, it, CUSPARSE_INDEX_BASE_ZERO, dt), "cusparseCreateCsr");
170 cusparseDnVecDescr_t dx;
171 cusparseCheck(cusparseCreateDnVec(&dx, int64_t(transposed ? rows : cols), pseudo_ptr, dt), "cusparseCreateDnVec");
173 cusparseDnVecDescr_t dr;
174 cusparseCheck(cusparseCreateDnVec(&dr, int64_t(transposed ? cols : rows), pseudo_ptr, dt), "cusparseCreateDnVec");
176 // alpha/beta are only inspected for their type here; the actual values used on apply are irrelevant
180 size_t buffer_size(0);
181 cusparseCheck(cusparseSpMV_bufferSize(Util::Intern::cusparse_handle, trans, &alpha, descr, dx, &beta, dr, ct, CUSPARSE_SPMV_CSR_ALG1, &buffer_size), "cusparseBuffersize");
183 // allocate the persistent SpMV buffer
184 void* buffer = nullptr;
187 cudaCheck(cudaMalloc(&buffer, buffer_size), "cudaMalloc");
189 cusparseCheck(cusparseSpMV_preprocess(Util::Intern::cusparse_handle, trans, &alpha, descr, dx, &beta, dr, ct, CUSPARSE_SPMV_CSR_ALG1, buffer), "cuSparseBCSRSPMV");
191 // hand ownership of the persistent descriptor and buffer to the type-erased workspace; the
192 // CuSparseSPMVWorkSpace destructor releases both once the shared_ptr is reset.
193 // Note that the Deleter will correctly call the destructor defined by cusparseworkspace
194 auto ws = std::make_shared<CuSparseSPMVWorkSpace>();
199 ws->buffer_size = buffer_size;
200 workspace = std::move(ws);
203 template void MatVecMultCSRDense::exec_cuda_impl(void*, float *, const float, const float * const, const float * const, const float * const, const std::uint32_t * const, const std::uint32_t * const, const Index, const Index, const Index, const bool);
204 template void MatVecMultCSRDense::exec_cuda_impl(void*, double *, const double, const double * const, const double * const, const double * const, const std::uint32_t * const, const std::uint32_t * const, const Index, const Index, const Index, const bool);
205 #ifdef FEAT_HAVE_HALFMATH
206 template void MatVecMultCSRDense::exec_cuda_impl(void*, Half *, const Half, const Half * const, const Half * const, const Half * const, const std::uint32_t * const, const std::uint32_t * const, const Index, const Index, const Index, const bool);
209 template void MatVecMultCSRDense::exec_cuda_impl(void*, float *, const float, const float * const, const float * const, const float * const, const std::uint64_t * const, const std::uint64_t * const, const Index, const Index, const Index, const bool);
210 template void MatVecMultCSRDense::exec_cuda_impl(void*,double *, const double, const double * const, const double * const, const double * const, const std::uint64_t * const, const std::uint64_t * const, const Index, const Index, const Index, const bool);
211 #ifdef FEAT_HAVE_HALFMATH
212 template void MatVecMultCSRDense::exec_cuda_impl(void*, Half *, const Half, const Half * const, const Half * const, const Half * const, const std::uint64_t * const, const std::uint64_t * const, const Index, const Index, const Index, const bool);
215 template void MatVecMultCSRDense::init_workspace_cuda<float, std::uint32_t>(std::shared_ptr<void>&, const Memory::Arbiter&, const Memory::Arbiter&, const Memory::Arbiter&, const Index, const Index, const Index, const bool);
216 template void MatVecMultCSRDense::init_workspace_cuda<double, std::uint32_t>(std::shared_ptr<void>&, const Memory::Arbiter&, const Memory::Arbiter&, const Memory::Arbiter&, const Index, const Index, const Index, const bool);
217 #ifdef FEAT_HAVE_HALFMATH
218 template void MatVecMultCSRDense::init_workspace_cuda<Half, std::uint32_t>(std::shared_ptr<void>&, const Memory::Arbiter&, const Memory::Arbiter&, const Memory::Arbiter&, const Index, const Index, const Index, const bool);
221 template void MatVecMultCSRDense::init_workspace_cuda<float, std::uint64_t>(std::shared_ptr<void>&, const Memory::Arbiter&, const Memory::Arbiter&, const Memory::Arbiter&, const Index, const Index, const Index, const bool);
222 template void MatVecMultCSRDense::init_workspace_cuda<double, std::uint64_t>(std::shared_ptr<void>&, const Memory::Arbiter&, const Memory::Arbiter&, const Memory::Arbiter&, const Index, const Index, const Index, const bool);
223 #ifdef FEAT_HAVE_HALFMATH
224 template void MatVecMultCSRDense::init_workspace_cuda<Half, std::uint64_t>(std::shared_ptr<void>&, const Memory::Arbiter&, const Memory::Arbiter&, const Memory::Arbiter&, const Index, const Index, const Index, const bool);
226} // namespace FEAT::LAFEM::Arch