FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
matvecmult_banded_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_banded_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
15namespace FEAT::LAFEM::Arch
16{
17 template <typename DT_, typename IT_>
18 __global__ void cuda_apply_banded(DT_ * r, const DT_ alpha, const DT_ * x, const DT_ * val, const IT_ * offsets, const Index bands, const Index rows, const Index cols)
19 {
20 // grid strided for loop
21 for(Index i = threadIdx.x + blockDim.x * blockIdx.x; i < rows; i += blockDim.x * gridDim.x)
22 {
23 const Index k1(rows - 1);
24 const Index k2(rows + cols - 1);
25
26 Index start(0);
27
28 while ((start < bands) && (k1 > offsets[start] + i))
29 {
30 ++start;
31 }
32
33 // this may happen if the first rows of the matrix are empty
34 if(bands <= start)
35 continue;
36
37 Index end(start);
38
39 while (end < bands && i + offsets[end] < k2)
40 {
41 ++end;
42 }
43
44 DT_ sum(DT_(0.0));
45 for (Index diag(start); diag < end; ++diag)
46 {
47 sum += val[rows * diag + i] * x[i + offsets[diag] - rows + 1];
48 }
49 r[i] += (sum*alpha);
50 }
51 }
52
53 template <typename DT_, typename IT_>
54 void MatVecMultBandedDense::exec_cuda_impl(DT_ * r, const DT_ alpha, const DT_ * const x, const DT_ * const y,
55 const DT_ * const val, const IT_ * const offsets, const Index bands, const Index rows, const Index cols)
56 {
57 dim3 block(static_cast<unsigned int>(Util::cuda_blocksize_spmv));
58 dim3 grid((static_cast<unsigned int>(rows) + block.x - 1u) / block.x); // round up to next integer
59
60 if(y == nullptr)
61 Memory::memset_cuda(r, 0, rows * sizeof(DT_));
62 else if (r != y)
63 Memory::memcopy_cuda(r, y, rows * sizeof(DT_));
64
65 cuda_apply_banded<<<grid, block>>>(r, alpha, x, val, offsets, bands, rows, cols);
66 }
67
68 template void MatVecMultBandedDense::exec_cuda_impl(float *, const float, const float * const, const float * const, const float * const, const std::uint32_t * const, const Index, const Index, const Index);
69 template void MatVecMultBandedDense::exec_cuda_impl(double *, const double, const double * const, const double * const, const double * const, const std::uint32_t * const, const Index, const Index, const Index);
70 #ifdef FEAT_HAVE_HALFMATH
71 template void MatVecMultBandedDense::exec_cuda_impl(Half *, const Half, const Half * const, const Half * const, const Half * const, const std::uint32_t * const, const Index, const Index, const Index);
72 #endif
73
74 template void MatVecMultBandedDense::exec_cuda_impl(float *, const float, const float * const, const float * const, const float * const, const std::uint64_t * const, const Index, const Index, const Index);
75 template void MatVecMultBandedDense::exec_cuda_impl(double *, const double, const double * const, const double * const, const double * const, const std::uint64_t * const, const Index, const Index, const Index);
76 #ifdef FEAT_HAVE_HALFMATH
77 template void MatVecMultBandedDense::exec_cuda_impl(Half *, const Half, const Half * const, const Half * const, const Half * const, const std::uint64_t * const,const Index, const Index, const Index);
78 #endif
79} // namespace FEAT::LAFEM::Arch