FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
matvecmult_csr_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/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>
14
15#include "cusparse_v2.h"
16#include <kernel/util/cuda_helper.cuh>
17#include "cusparse_helper.cuh"
18
19namespace FEAT::LAFEM::Arch
20{
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)
24 {
25 // since async ops are on stream 0 (i.e. default stream), they are always blocking. This might or might not be a feature
26 if(y == nullptr)
27 {
28 cudaCheck(cudaMemsetAsync(r, 0, sizeof(DT_) * (transposed ? cols : rows)), "SPMV Apply memset");
29 }
30 else if(r != y)
31 {
32 cudaCheck(cudaMemcpyAsync(r, y, sizeof(DT_) * (transposed ? cols : rows), cudaMemcpyDeviceToDevice), "SPMV Apply memcpy");
33 }
34 cusparseCheck(cusparseSpMV(Util::Intern::cusparse_handle, trans, &alpha, descr, dx, &beta, dr, ct, CUSPARSE_SPMV_CSR_ALG1, buffer), "cuSparseCSRSPMV");
35 }
36
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)
40 {
41 const cusparseOperation_t trans = transposed ? CUSPARSE_OPERATION_TRANSPOSE : CUSPARSE_OPERATION_NON_TRANSPOSE;
42
43 cudaDataType dt;
44 cudaDataType ct; // compute type
45 Intern::cusparse_deduce_data_type<DT_>(dt, ct);
46
47 const cusparseIndexType_t it = Intern::cusparse_deduce_index_type<IT_>();
48
49 cusparseSpMatDescr_t descr = 0;
50 cusparseDnVecDescr_t dx = 0;
51 cusparseDnVecDescr_t dr = 0;
52 void* buffer = nullptr;
53 DT_ beta = DT_(1);
54
55 if(workspace)
56 {
57 CuSparseSPMVWorkSpace* sp_ws = static_cast<CuSparseSPMVWorkSpace*>(workspace);
58 descr = sp_ws->csr_mat;
59 // only do this in debug
60 #ifdef DEBUG
61 {
62 // check if ptr match
63 int64_t t_rows, t_cols, t_nnz;
64 void* t_col_ind;
65 void* t_row_off;
66 void* t_values;
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);
78 }
79 #endif
80 // set ptr
81 {
82 cusparseCheck(cusparseCsrSetPointers(descr, (void*)row_ptr, (void*)col_idx, (void*)val), "cusparseSetPointers");
83 }
84 dx = sp_ws->vec_x;
85 {
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
88 if(rows != cols)
89 {
90 Index rsize = transposed ? rows : cols;
91 if(!check_size_dnvec(dx, rsize))
92 {
93 // delete old ptr
94 cusparseCheck(cusparseDestroyDnVec(sp_ws->vec_x), "destroyDnVec");
95 // create new one
96 cusparseCheck(cusparseCreateDnVec(&sp_ws->vec_x, int64_t(rsize), (void*)x, dt), "Cusparse Create dnvec");
97 dx = sp_ws->vec_x;
98 }
99 }
100 cusparseCheck(cusparseDnVecSetValues(dx, (void*)x), "Set x Value");
101 }
102 dr = sp_ws->vec_y;
103 {
104 if(rows != cols)
105 {
106 Index rsize = transposed ? cols : rows;
107 if(!check_size_dnvec(dr, rsize))
108 {
109 // delete old ptr
110 cusparseCheck(cusparseDestroyDnVec(sp_ws->vec_y), "destroyDnVec");
111 // create new one
112 cusparseCheck(cusparseCreateDnVec(&sp_ws->vec_y, int64_t(rsize), (void*)r, dt), "Cusparse Create dnvec");
113 dr = sp_ws->vec_y;
114 }
115 }
116 cusparseCheck(cusparseDnVecSetValues(dr, (void*)r), "Set r Value");
117 }
118 buffer = sp_ws->buffer;
119 }
120 else
121 {
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");
125
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);
129 }
130
131 _apply_spmv_csr_cuda(r, y, descr, dr, dx, trans, alpha, beta, ct, buffer, cols, rows, transposed);
132
133 if(!workspace)
134 {
135 cusparseDestroySpMat(descr);
136 cusparseDestroyDnVec(dx);
137 cusparseDestroyDnVec(dr);
138 }
139
140 }
141
142
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)
145 {
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));
151
152 const cusparseOperation_t trans = transposed ? CUSPARSE_OPERATION_TRANSPOSE : CUSPARSE_OPERATION_NON_TRANSPOSE;
153
154 cudaDataType dt;
155 cudaDataType ct; // compute type
156 Intern::cusparse_deduce_data_type<DT_>(dt, ct);
157
158 const cusparseIndexType_t it = Intern::cusparse_deduce_index_type<IT_>();
159
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;
165
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");
169
170 cusparseDnVecDescr_t dx;
171 cusparseCheck(cusparseCreateDnVec(&dx, int64_t(transposed ? rows : cols), pseudo_ptr, dt), "cusparseCreateDnVec");
172
173 cusparseDnVecDescr_t dr;
174 cusparseCheck(cusparseCreateDnVec(&dr, int64_t(transposed ? cols : rows), pseudo_ptr, dt), "cusparseCreateDnVec");
175
176 // alpha/beta are only inspected for their type here; the actual values used on apply are irrelevant
177 DT_ alpha = DT_(1);
178 DT_ beta = DT_(1);
179
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");
182
183 // allocate the persistent SpMV buffer
184 void* buffer = nullptr;
185 if(buffer_size > 0u)
186 {
187 cudaCheck(cudaMalloc(&buffer, buffer_size), "cudaMalloc");
188 }
189 cusparseCheck(cusparseSpMV_preprocess(Util::Intern::cusparse_handle, trans, &alpha, descr, dx, &beta, dr, ct, CUSPARSE_SPMV_CSR_ALG1, buffer), "cuSparseBCSRSPMV");
190
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>();
195 ws->csr_mat = descr;
196 ws->vec_x = dx;
197 ws->vec_y = dr;
198 ws->buffer = buffer;
199 ws->buffer_size = buffer_size;
200 workspace = std::move(ws);
201 }
202
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);
207 #endif
208
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);
213 #endif
214
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);
219 #endif
220
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);
225 #endif
226} // namespace FEAT::LAFEM::Arch