FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
matvecmult_bcsr_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_bcsr_dense.hpp>
9#include <kernel/lafem/arch/matvecmult_csr_dense.hpp>
10#include <kernel/util/memory_aux.hpp>
11#include <kernel/util/exception.hpp>
12#include <kernel/util/cuda_util.hpp>
13#include <kernel/util/math.hpp>
14#include <kernel/util/half.hpp>
15
16#include "cusparse_v2.h"
17#include <kernel/util/cuda_helper.cuh>
18#include "cusparse_helper.cuh"
19
20namespace FEAT::LAFEM::Arch
21{
22 template <typename DT_, typename IT_>
23 __global__ void cuda_apply_bcsr(DT_ * r, const DT_ alpha, const DT_ * x, const DT_ * val, const IT_ * col_idx,
24 const IT_ * row_ptr, const Index count, const int block_height, const int block_width)
25 {
26 // grid strided for loop
27 for(Index idx = threadIdx.x + blockDim.x * blockIdx.x; idx < count; idx += blockDim.x * gridDim.x)
28 {
29 /// \todo remove hardcoded number
30 DT_ bsum[10];
31 for (int j(0) ; j < block_height ; ++j)
32 {
33 bsum[j] = DT_(0);
34 }
35 const IT_ end(row_ptr[idx + 1]);
36 for (IT_ i(row_ptr[idx]) ; i < end ; ++i)
37 {
38 for (int h(0) ; h < block_height ; ++h)
39 {
40 for (int w(0) ; w < block_width ; ++w)
41 {
42 bsum[h] += val[i * block_height * block_width + h * block_width + w] * x[col_idx[i] * block_width + w];
43 }
44 }
45 }
46 for (int j(0) ; j < block_height ; ++j)
47 {
48 r[idx * block_height + j] += alpha * bsum[j];
49 }
50 }
51 }
52
53 template <typename DT_, typename IT_>
54 void bcsr_intern_cuda(DT_ * r, const DT_ alpha, const DT_ * const x, const DT_ * const y, const DT_ * const val,
55 const IT_ * const col_idx, const IT_ * const row_ptr, const Index rows, const Index cols, const Index /*nonzeros*/,
56 const int block_height, const int block_width, const bool transposed)
57 {
58 dim3 block(static_cast<unsigned int>(Util::cuda_blocksize_spmv));
59 dim3 grid((static_cast<unsigned int>(rows) + block.x - 1u) / block.x); // round up to next integer
60
61 Index rsize = (transposed ? cols*Index(block_width) : rows*Index(block_height));
62 if(y == nullptr)
63 {
64 cudaCheck(cudaMemsetAsync(r, 0, sizeof(DT_) * rsize), "SPMV Apply memset");
65 }
66 else if (r != y)
67 {
68 cudaCheck(cudaMemcpyAsync(r, y, sizeof(DT_) * rsize, cudaMemcpyDeviceToDevice), "SPMV Apply memcpy");
69 }
70
71 XASSERTM(!transposed, "transposed BCSR matvec for CUDA not implemented");
72 XASSERT(block_height < 10); // see cuda_apply_bcsr implementation
73
74 cuda_apply_bcsr<DT_, IT_><<<grid, block>>>(r, alpha, x, val, col_idx, row_ptr, rows, block_height, block_width);
75
76#ifdef DEBUG
77 cudaDeviceSynchronize();
78 cudaError_t last_error(cudaGetLastError());
79 if (cudaSuccess != last_error)
80 throw InternalError(__func__, __FILE__, __LINE__, "CUDA error occurred in execution!\n" + stringify(cudaGetErrorString(last_error)));
81#endif
82 }
83
84 // maps a scalar data type to its CUDA 2-wide vector type for coalesced 16-byte (double) / 8-byte
85 // (float) loads of the two contiguous values of a 2x1 / 1x2 block. Types without a vector partner
86 // (e.g. Half) report available == false and fall back to scalar loads.
87 template<typename DT_> struct BcsrVec2Trait { static constexpr bool available = false; typedef DT_ type; };
88 template<> struct BcsrVec2Trait<float> { static constexpr bool available = true; typedef float2 type; };
89 template<> struct BcsrVec2Trait<double> { static constexpr bool available = true; typedef double2 type; };
90
91 /**
92 * \brief Compile-time-specialized rectangular BCSR matvec kernel (one thread per block-row).
93 *
94 * In contrast to \c cuda_apply_bcsr, the block dimensions are compile-time template parameters, so
95 * the inner loops unroll, the index arithmetic is static and the per-thread accumulator lives in
96 * registers (no local-memory spill). The y/beta term is merged inside the kernel
97 * (r = (y ? y : 0) + alpha * A*x), so the launcher does not need the separate memset/memcpy pre-pass.
98 *
99 * This is the thread-per-block-row variant, best suited to the "tall" N x 1 blocks (e.g. the 2x1 /
100 * 3x1 gradient block B), which have many short block-rows and therefore already saturate the grid.
101 * For the 2x1 shape the two contiguous block values are loaded as one vectorized transaction.
102 */
103 template <typename DT_, typename IT_, int bh_, int bw_>
104 __global__ void cuda_apply_bcsr_ct(DT_ * r, const DT_ alpha, const DT_ * x, const DT_ * y, const DT_ * val,
105 const IT_ * col_idx, const IT_ * row_ptr, const Index count)
106 {
107 // grid-strided loop, one thread per block-row
108 for(Index idx = threadIdx.x + blockDim.x * blockIdx.x; idx < count; idx += blockDim.x * gridDim.x)
109 {
110 DT_ bsum[bh_];
111 #pragma unroll
112 for(int h(0) ; h < bh_ ; ++h)
113 bsum[h] = DT_(0);
114
115 const IT_ end(row_ptr[idx + 1]);
116 if constexpr (bh_ == 2 && bw_ == 1 && BcsrVec2Trait<DT_>::available)
117 {
118 // 2x1: the two block values val[2i], val[2i+1] are contiguous and share the single x entry
119 typedef typename BcsrVec2Trait<DT_>::type Vec2;
120 const Vec2 * const val2 = reinterpret_cast<const Vec2*>(val);
121 for(IT_ i(row_ptr[idx]) ; i < end ; ++i)
122 {
123 const Vec2 v = val2[i];
124 const DT_ xv = x[col_idx[i]];
125 bsum[0] += v.x * xv;
126 bsum[1] += v.y * xv;
127 }
128 }
129 else
130 {
131 for(IT_ i(row_ptr[idx]) ; i < end ; ++i)
132 {
133 const IT_ col(col_idx[i] * IT_(bw_));
134 const DT_ * const vblk = val + i * (bh_ * bw_);
135 #pragma unroll
136 for(int h(0) ; h < bh_ ; ++h)
137 #pragma unroll
138 for(int w(0) ; w < bw_ ; ++w)
139 bsum[h] += vblk[h * bw_ + w] * x[col + IT_(w)];
140 }
141 }
142
143 if(y != nullptr)
144 {
145 #pragma unroll
146 for(int h(0) ; h < bh_ ; ++h)
147 r[idx * Index(bh_) + Index(h)] = y[idx * Index(bh_) + Index(h)] + alpha * bsum[h];
148 }
149 else
150 {
151 #pragma unroll
152 for(int h(0) ; h < bh_ ; ++h)
153 r[idx * Index(bh_) + Index(h)] = alpha * bsum[h];
154 }
155 }
156 }
157
158 /// launcher for the compile-time-specialized rectangular BCSR matvec kernel (thread per block-row)
159 template <typename DT_, typename IT_, int bh_, int bw_>
160 void bcsr_intern_cuda_ct(DT_ * r, const DT_ alpha, const DT_ * const x, const DT_ * const y, const DT_ * const val,
161 const IT_ * const col_idx, const IT_ * const row_ptr, const Index rows, const bool transposed)
162 {
163 XASSERTM(!transposed, "transposed BCSR matvec for CUDA not implemented");
164 dim3 block(static_cast<unsigned int>(Util::cuda_blocksize_spmv));
165 dim3 grid((static_cast<unsigned int>(rows) + block.x - 1u) / block.x); // round up to next integer
166
167 cuda_apply_bcsr_ct<DT_, IT_, bh_, bw_><<<grid, block>>>(r, alpha, x, y, val, col_idx, row_ptr, rows);
168
169#ifdef DEBUG
170 cudaDeviceSynchronize();
171 cudaError_t last_error(cudaGetLastError());
172 if (cudaSuccess != last_error)
173 throw InternalError(__func__, __FILE__, __LINE__, "CUDA error occurred in execution!\n" + stringify(cudaGetErrorString(last_error)));
174#endif
175 }
176
177 /**
178 * \brief Routes a real BCSR matvec to the compile-time-specialized custom kernels.
179 *
180 * Covers the block shapes that occur in 2D/3D saddle-point systems: the square velocity blocks
181 * (2x2 / 3x3), the "tall" gradient blocks (2x1 / 3x1, thread-per-block-row) and the "wide"
182 * divergence blocks (1x2 / 1x3, warp-per-block-row). Any other shape falls back to the generic
183 * runtime-dimension kernel. Used for the rectangular blocks (which cuSPARSE cannot handle) and, when
184 * cuSPARSE is disabled for BCSR (FEAT_NO_CUSPARSE_BCSR), for the square blocks as well.
185 */
186 template <typename DT_, typename IT_>
187 void bcsr_intern_cuda_dispatch(DT_ * r, const DT_ alpha, const DT_ * const x, const DT_ * const y, const DT_ * const val,
188 const IT_ * const col_idx, const IT_ * const row_ptr, const Index rows, const Index cols, const Index nonzeros,
189 const int block_height, const int block_width, const bool transposed)
190 {
191 switch((block_height << 8) | block_width)
192 {
193 case (2 << 8) | 2: // square velocity block, 2D
194 bcsr_intern_cuda_ct<DT_, IT_, 2, 2>(r, alpha, x, y, val, col_idx, row_ptr, rows, transposed);
195 break;
196 case (3 << 8) | 3: // square velocity block, 3D
197 bcsr_intern_cuda_ct<DT_, IT_, 3, 3>(r, alpha, x, y, val, col_idx, row_ptr, rows, transposed);
198 break;
199 case (2 << 8) | 1: // gradient block B, 2D
200 bcsr_intern_cuda_ct<DT_, IT_, 2, 1>(r, alpha, x, y, val, col_idx, row_ptr, rows, transposed);
201 break;
202 case (3 << 8) | 1: // gradient block B, 3D
203 bcsr_intern_cuda_ct<DT_, IT_, 3, 1>(r, alpha, x, y, val, col_idx, row_ptr, rows, transposed);
204 break;
205 case (1 << 8) | 2: // divergence block D, 2D (warp-per-row)
206 bcsr_intern_cuda_ct<DT_, IT_, 1, 2>(r, alpha, x, y, val, col_idx, row_ptr, rows, transposed);
207 break;
208 case (1 << 8) | 3: // divergence block D, 3D (warp-per-row)
209 bcsr_intern_cuda_ct<DT_, IT_, 1, 3>(r, alpha, x, y, val, col_idx, row_ptr, rows, transposed);
210 break;
211 default:
212 bcsr_intern_cuda<DT_, IT_>(r, alpha, x, y, val, col_idx, row_ptr, rows, cols, nonzeros, block_height, block_width, transposed);
213 break;
214 }
215 }
216
217/// since bsr is only supported with cuda version 13.0.1, we have to branch at this point
218#ifdef FEAT_CUSPARSE_BSR_SPMV_SUPPORTED
219 template<typename DT_>
220 static inline void _apply_spmv_bcsr_cuda(DT_* r, const DT_* y, cusparseSpMatDescr_t descr, cusparseDnVecDescr_t dr, cusparseDnVecDescr_t dx, cusparseOperation_t trans, const DT_ alpha,
221 const DT_ beta, cudaDataType ct, void* buffer, Index cols, Index rows, int bheight, int bwidth, bool transposed)
222 {
223 Index rsize = (transposed ? cols*Index(bwidth) : rows*Index(bheight));
224 // since async ops are on stream 0 (i.e. default stream), they are always blocking. This might or might not be a feature
225 if(y == nullptr)
226 {
227 cudaCheck(cudaMemsetAsync(r, 0, sizeof(DT_) * rsize), "SPMV Apply memset");
228 }
229 else if(r != y)
230 {
231 cudaCheck(cudaMemcpyAsync(r, y, sizeof(DT_) * rsize, cudaMemcpyDeviceToDevice), "SPMV Apply memcpy");
232 }
233 cusparseCheck(cusparseSpMV(Util::Intern::cusparse_handle, trans, &alpha, descr, dx, &beta, dr, ct, CUSPARSE_SPMV_BSR_ALG1, buffer), "cuSparseBCSRSPMV");
234 }
235
236 template <typename DT_, typename IT_>
237 void bcsr_intern_cusparse_v13(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,
238 const Index rows, const Index cols, const Index nonzeros, const int bheight, const int bwidth, const bool transposed)
239 {
240 const cusparseOperation_t trans = transposed ? CUSPARSE_OPERATION_TRANSPOSE : CUSPARSE_OPERATION_NON_TRANSPOSE;
241
242 cudaDataType dt;
243 cudaDataType ct; // compute type
244 Intern::cusparse_deduce_data_type<DT_>(dt, ct);
245
246 const cusparseIndexType_t it = Intern::cusparse_deduce_index_type<IT_>();
247 const cusparseOrder_t order = CUSPARSE_ORDER_ROW;
248
249 cusparseSpMatDescr_t descr = 0;
250 cusparseDnVecDescr_t dx = 0;
251 cusparseDnVecDescr_t dr = 0;
252 void* buffer = nullptr;
253 DT_ beta = DT_(1);
254
255 if(workspace)
256 {
257 CuSparseSPMVWorkSpace* sp_ws = static_cast<CuSparseSPMVWorkSpace*>(workspace);
258 descr = sp_ws->csr_mat;
259 // set ptr
260 {
261 cusparseCheck(cusparseSpMatSetValues(descr, (void*)val), "cusparseSetValues");
262 }
263 dx = sp_ws->vec_x;
264 {
265 // generally check if the vectors have the correct size
266 {
267 Index rsize = transposed ? rows*Index(bheight) : cols*Index(bwidth);
268 if(!check_size_dnvec(dx, rsize))
269 {
270 // delete old ptr
271 cusparseCheck(cusparseDestroyDnVec(sp_ws->vec_x), "destroyDnVec");
272 // create new one
273 cusparseCheck(cusparseCreateDnVec(&sp_ws->vec_x, int64_t(rsize), (void*)x, dt), "Cusparse Create dnvec");
274 dx = sp_ws->vec_x;
275 }
276 }
277 cusparseCheck(cusparseDnVecSetValues(dx, (void*)x), "Set x Value");
278 }
279 dr = sp_ws->vec_y;
280 {
281 if(rows != cols)
282 {
283 Index rsize = transposed ? cols*Index(bwidth) : rows*Index(bheight);
284 if(!check_size_dnvec(dr, rsize))
285 {
286 // delete old ptr
287 cusparseCheck(cusparseDestroyDnVec(sp_ws->vec_y), "destroyDnVec");
288 // create new one
289 cusparseCheck(cusparseCreateDnVec(&sp_ws->vec_y, int64_t(rsize), (void*)r, dt), "Cusparse Create dnvec");
290 dr = sp_ws->vec_y;
291 }
292 }
293 cusparseCheck(cusparseDnVecSetValues(dr, (void*)r), "Set r Value");
294 }
295 buffer = sp_ws->buffer;
296 }
297 else
298 {
299 cusparseCheck(cusparseCreateBsr(&descr, int64_t(rows), int64_t(cols), int64_t(nonzeros), int64_t(bheight), int64_t(bwidth), (void*)row_ptr, (void*)col_idx, (void*)val, it, it, CUSPARSE_INDEX_BASE_ZERO, dt, order), "cusparseCreateBCSR");
300 cusparseCheck(cusparseCreateDnVec(&dx, int64_t(transposed?(rows*Index(bheight)):(cols*Index(bwidth))), (void*)x, dt), "cusparseCreateDnVec");
301 cusparseCheck(cusparseCreateDnVec(&dr, int64_t(transposed?(cols*Index(bwidth)):(rows*Index(bheight))), (void*)r, dt), "cusparseCreateDnVec");
302
303 size_t buffer_size(0);
304 cusparseCheck(cusparseSpMV_bufferSize(Util::Intern::cusparse_handle, trans, &alpha, descr, dx, &beta, dr, ct, CUSPARSE_SPMV_BSR_ALG1, &buffer_size), "cuSparseBufferSize");
305 buffer = Util::cuda_get_static_memory(buffer_size);
306 }
307
308 _apply_spmv_bcsr_cuda(r, y, descr, dr, dx, trans, alpha, beta, ct, buffer, cols, rows, bheight, bwidth, transposed);
309
310 if(!workspace)
311 {
312 cusparseCheck(cusparseDestroySpMat(descr), "cusparseDestroy");
313 cusparseDestroyDnVec(dx);
314 cusparseDestroyDnVec(dr);
315 }
316
317#ifdef DEBUG
318 cudaDeviceSynchronize();
319 cudaCheckLastErr()
320#endif
321 }
322#else
323
324 void cusparse_apply_bcsr(cusparseDirection_t dir, cusparseOperation_t trans,
325 int m, int n, int nnz,
326 const float * alpha, const cusparseMatDescr_t descrA,
327 const float * csrVal, const int * csrRowPtr, const int *csrColInd,
328 int block_dim,
329 const float * x, const float * beta, float * y)
330 {
331 cusparseStatus_t status;
332 status = cusparseSbsrmv(Util::Intern::cusparse_handle, dir, trans, m, n, nnz, alpha, descrA, csrVal, csrRowPtr,
333 csrColInd, block_dim, x, beta, y);
334 if (status != CUSPARSE_STATUS_SUCCESS)
335 throw InternalError(__func__, __FILE__, __LINE__, "cusparsebsrmv failed with status code: " + stringify(status));
336 }
337
338 void cusparse_apply_bcsr(cusparseDirection_t dir, cusparseOperation_t trans,
339 int m, int n, int nnz,
340 const double * alpha, const cusparseMatDescr_t descrA,
341 const double * csrVal, const int * csrRowPtr, const int *csrColInd,
342 int block_dim,
343 const double * x, const double * beta, double * y)
344 {
345 cusparseStatus_t status;
346 status = cusparseDbsrmv(Util::Intern::cusparse_handle, dir, trans, m, n, nnz, alpha, descrA, csrVal, csrRowPtr,
347 csrColInd, block_dim, x, beta, y);
348 if (status != CUSPARSE_STATUS_SUCCESS)
349 throw InternalError(__func__, __FILE__, __LINE__, "cusparsebsrmv failed with status code: " + stringify(status));
350 }
351
352 template <typename DT_, typename IT_>
353 void bcsr_intern_cusparse_v12(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,
354 const Index rows, const Index cols, const Index nonzeros, const int block_size, const bool transposed)
355 {
356 Index rsize = (transposed ? cols : rows) * Index(block_size);
357 if(y == nullptr)
358 {
359 cudaCheck(cudaMemsetAsync(r, 0, sizeof(DT_) * rsize), "SPMV Apply memset");
360 }
361 else if(r != y)
362 {
363 cudaCheck(cudaMemcpyAsync(r, y, sizeof(DT_) * rsize, cudaMemcpyDeviceToDevice), "SPMV Apply memcpy");
364 }
365
366 cusparseMatDescr_t descr=0;
367 cusparseCreateMatDescr(&descr);
368 cusparseSetMatType(descr, CUSPARSE_MATRIX_TYPE_GENERAL);
369 cusparseSetMatIndexBase(descr, CUSPARSE_INDEX_BASE_ZERO);
370
371 cusparseOperation_t trans = (transposed ? CUSPARSE_OPERATION_TRANSPOSE : CUSPARSE_OPERATION_NON_TRANSPOSE);
372
373 DT_ b = DT_(y == nullptr ? 0 : 1);
374 cusparse_apply_bcsr(CUSPARSE_DIRECTION_ROW, trans, (int)rows, (int)cols, (int)nonzeros, &alpha, descr, val, (int*)row_ptr, (int*)col_idx,
375 block_size, x, &b, r);
376
377 cusparseDestroyMatDescr(descr);
378 }
379#endif
380
381 template <typename DT_, typename IT_>
382 void MatVecMultBCSRDense::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, const Index cols, const Index nonzeros, const int block_height, const int block_width, const bool transposed)
383 {
384 if(block_height*block_width == 1)
385 {
386 // standard CSR always uses cuSPARSE
387 MatVecMultCSRDense::exec_cuda_impl<DT_, IT_>(workspace, r, alpha, x, y, val, col_idx, row_ptr, rows, cols, nonzeros, transposed);
388 }
389 else
390 {
391#ifdef FEAT_NO_CUSPARSE_BCSR
392 // cuSPARSE is disabled for real (block_dim>1) BCSR at configure time: always use the custom
393 // compile-time-specialized kernels (both square and rectangular blocks)
394 bcsr_intern_cuda_dispatch<DT_, IT_>(r, alpha, x, y, val, col_idx, row_ptr, rows, cols, nonzeros, block_height, block_width, transposed);
395#else
396 // sadly, only quadratic blocks are allowed in cuSPARSE
397 if(block_height == block_width)
398 {
399 #ifdef FEAT_CUSPARSE_BSR_SPMV_SUPPORTED
400 bcsr_intern_cusparse_v13<DT_, IT_>(workspace, r, alpha, x, y, val, col_idx, row_ptr, rows, cols, nonzeros, block_height, block_width, transposed);
401 #else
402 if(sizeof(IT_) == 4)
403 bcsr_intern_cusparse_v12<DT_, IT_>(r, alpha, x, y, val, col_idx, row_ptr, rows, cols, nonzeros, block_height, transposed);
404 else
405 bcsr_intern_cuda_dispatch<DT_, IT_>(r, alpha, x, y, val, col_idx, row_ptr, rows, cols, nonzeros, block_height, block_width, transposed);
406 #endif
407 }
408 else
409 {
410 // rectangular blocks: cuSPARSE does not support them, so use the custom kernels
411 bcsr_intern_cuda_dispatch<DT_, IT_>(r, alpha, x, y, val, col_idx, row_ptr, rows, cols, nonzeros, block_height, block_width, transposed);
412 }
413#endif
414 }
415 }
416
417 template <typename DT_, typename IT_>
418 void MatVecMultBCSRDense::init_workspace_cuda(std::shared_ptr<void>& workspace, const Memory::Arbiter& val, const Memory::Arbiter& col_idx, const Memory::Arbiter& row_ptr, const Index rows, const Index cols, const Index nonzeros, const int block_height, const int block_width, const bool transposed)
419 {
420 if(block_height*block_width == 1)
421 {
422 MatVecMultCSRDense::init_workspace_cuda<DT_, IT_>(workspace, val, col_idx, row_ptr, rows, cols, nonzeros, transposed);
423 return;
424 }
425#ifdef FEAT_NO_CUSPARSE_BCSR
426 // cuSPARSE disabled for real BCSR: the custom kernels need no persistent workspace
427 return;
428#endif
429#ifdef FEAT_CUSPARSE_BSR_SPMV_SUPPORTED
430 // nothing to do in this case
431 if(block_height != block_width)
432 return;
433 Memory::TypedView<IT_> rp_view(row_ptr.view(Memory::Location::cuda, Memory::Access::read));
434 Memory::TypedView<IT_> ci_view(col_idx.view(Memory::Location::cuda, Memory::Access::read));
435 const cusparseOperation_t trans = transposed ? CUSPARSE_OPERATION_TRANSPOSE : CUSPARSE_OPERATION_NON_TRANSPOSE;
436 const cusparseOrder_t order = CUSPARSE_ORDER_ROW;
437
438 cudaDataType dt;
439 cudaDataType ct; // compute type
440 Intern::cusparse_deduce_data_type<DT_>(dt, ct);
441
442 const cusparseIndexType_t it = Intern::cusparse_deduce_index_type<IT_>();
443
444 // The SpMV buffer size does not depend on the actual vector data, only on the vector sizes and
445 // types. As the concrete x/r device pointers are only known at apply time, we hand cuSPARSE a
446 // pseudo pointer purely to satisfy the descriptor creation.
447 // This can not be a nullptr, since there is an assert statement int the cuda interface
448 void* const pseudo_ptr = (void*)0x048;
449
450 // create the persistent CSR matrix descriptor from the (fixed) matrix arrays
451 cusparseSpMatDescr_t descr = nullptr;
452 // cusparseCheck(cusparseCreateBsr(&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");
453 cusparseCheck(cusparseCreateBsr(&descr, int64_t(rows), int64_t(cols), int64_t(nonzeros), int64_t(block_height), int64_t(block_width), (void*)rp_view.get_r(), (void*)ci_view.get_r(), pseudo_ptr, it, it, CUSPARSE_INDEX_BASE_ZERO, dt, order), "cusparseCreateBCSR");
454
455 cusparseDnVecDescr_t dx;
456 cusparseCheck(cusparseCreateDnVec(&dx, int64_t(transposed ? (rows*Index(block_height)) : (cols*Index(block_width))), pseudo_ptr, dt), "cusparseCreateDnVec");
457
458 cusparseDnVecDescr_t dr;
459 cusparseCheck(cusparseCreateDnVec(&dr, int64_t(transposed ? (cols*Index(block_width)) : (rows*Index(block_height))), pseudo_ptr, dt), "cusparseCreateDnVec");
460
461 // alpha/beta are only inspected for their type here; the actual values used on apply are irrelevant
462 DT_ alpha = DT_(1);
463 DT_ beta = DT_(1);
464
465 size_t buffer_size(0);
466 cusparseCheck(cusparseSpMV_bufferSize(Util::Intern::cusparse_handle, trans, &alpha, descr, dx, &beta, dr, ct, CUSPARSE_SPMV_BSR_ALG1, &buffer_size), "cusparseBuffersize");
467
468 // allocate the persistent SpMV buffer
469 void* buffer = nullptr;
470 if(buffer_size > 0u)
471 {
472 cudaCheck(cudaMalloc(&buffer, buffer_size), "cudaMalloc");
473 }
474 cusparseCheck(cusparseSpMV_preprocess(Util::Intern::cusparse_handle, trans, &alpha, descr, dx, &beta, dr, ct, CUSPARSE_SPMV_BSR_ALG1, buffer), "cuSparseBCSRSPMV");
475
476 // hand ownership of the persistent descriptor and buffer to the type-erased workspace; the
477 // CuSparseSPMVWorkSpace destructor releases both once the shared_ptr is reset.
478 // Note that the Deleter will correctly call the destructor defined by cusparseworkspace
479 auto ws = std::make_shared<CuSparseSPMVWorkSpace>();
480 ws->csr_mat = descr;
481 ws->vec_x = dx;
482 ws->vec_y = dr;
483 ws->buffer = buffer;
484 ws->buffer_size = buffer_size;
485 workspace = std::move(ws);
486 #endif
487 }
488
489 template void MatVecMultBCSRDense::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 int, const int, const bool);
490 template void MatVecMultBCSRDense::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 int, const int, const bool);
491 #ifdef FEAT_HAVE_HALFMATH
492 template void MatVecMultBCSRDense::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 int, const int, const bool);
493 #endif
494
495 template void MatVecMultBCSRDense::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 int, const int, const bool);
496 template void MatVecMultBCSRDense::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 int, const int, const bool);
497 #ifdef FEAT_HAVE_HALFMATH
498 template void MatVecMultBCSRDense::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 int, const int, const bool);
499 #endif
500
501 template void MatVecMultBCSRDense::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 int, const int, const bool);
502 template void MatVecMultBCSRDense::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 int, const int, const bool);
503 #ifdef FEAT_HAVE_HALFMATH
504 template void MatVecMultBCSRDense::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 int, const int, const bool);
505 #endif
506
507 template void MatVecMultBCSRDense::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 int, const int, const bool);
508 template void MatVecMultBCSRDense::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 int, const int, const bool);
509 #ifdef FEAT_HAVE_HALFMATH
510 template void MatVecMultBCSRDense::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 int, const int, const bool);
511 #endif
512} // namespace FEAT::LAFEM::Arch