FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
matvecmult_csr_dense.hpp
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#pragma once
7
8// includes, FEAT
10#include <kernel/backend.hpp>
11#include <kernel/util/memory_arbiter.hpp>
12
13#include <array>
14
15namespace FEAT::LAFEM::Arch
16{
19 {
20 template <typename DT_, typename IT_>
21 static void exec_generic_impl(DT_ * r, const DT_ alpha, const DT_ * const x, const DT_ * const y, const DT_ * const val,
22 const IT_ * const col_idx, const IT_ * const row_ptr, const Index rows, const Index, const Index, const bool);
23
24 static void exec_mkl_impl(float * r, const float alpha, const float * const x, const float * const y, const float * const val, const Index * const col_idx, const Index * const row_ptr, const Index rows, const Index cols, const Index, const bool);
25 static void exec_mkl_impl(double * r, const double alpha, const double * const x, const double * const y, const double * const val, const Index * const col_idx, const Index * const row_ptr, const Index rows, const Index cols, const Index, const bool);
26
27 template <typename DT_, typename IT_>
28 static void exec_mkl_impl(DT_ *, const DT_, const DT_ * const, const DT_ * const, const DT_ * const, const IT_ * const, const IT_ * const, const Index, const Index, const Index, const bool)
29 {
30 XABORTM("LAFEM::Arch::MatVecMultCSRDense::exec_mkl_impl: MKL backend not available!");
31 }
32
33 template <typename DT_, typename IT_>
34 static void 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 bool transposed);
35
36 template <typename DT_, typename IT_>
37 static void exec_generic(Memory::Arbiter& r, const DT_ alpha, const Memory::Arbiter& x, const Memory::Arbiter& y, const Memory::Arbiter& a, const Memory::Arbiter& col_idx, const Memory::Arbiter& row_ptr, const Index rows, const Index cols, const Index nonzeros, const bool transposed)
38 {
44 if(y)
45 {
47 exec_generic_impl(r_view.get_w(), alpha, x_view.get_r(), y_view.get_r(), a_view.get_r(),
48 ci_view.get_r(), rp_view.get_r(), rows, cols, nonzeros, transposed);
49 }
50 else // no y
51 {
52 exec_generic_impl(r_view.get_w(), alpha, x_view.get_r(), (DT_*)nullptr, a_view.get_r(),
53 ci_view.get_r(), rp_view.get_r(), rows, cols, nonzeros, transposed);
54 }
55 }
56
57 template <typename DT_>
58 static void exec_mkl(Memory::Arbiter& r, const DT_ alpha, const Memory::Arbiter& x, const Memory::Arbiter& y, const Memory::Arbiter& a, const Memory::Arbiter& col_idx, const Memory::Arbiter& row_ptr, const Index rows, const Index cols, const Index nonzeros, const bool transposed)
59 {
60 // we only expect MKL to work with indices of the same size as FEAT::Index
66 if(y)
67 {
69 exec_mkl_impl(r_view.get_w(), alpha, x_view.get_r(), y_view.get_r(), a_view.get_r(),
70 ci_view.get_r(), rp_view.get_r(), rows, cols, nonzeros, transposed);
71 }
72 else // no y
73 {
74 exec_mkl_impl(r_view.get_w(), alpha, x_view.get_r(), (DT_*)nullptr, a_view.get_r(),
75 ci_view.get_r(), rp_view.get_r(), rows, cols, nonzeros, transposed);
76 }
77 }
78
79 template <typename DT_, typename IT_>
80 static void exec_cuda(void* workspace, Memory::Arbiter& r, const DT_ alpha, const Memory::Arbiter& x, const Memory::Arbiter& y, const Memory::Arbiter& a, const Memory::Arbiter& col_idx, const Memory::Arbiter& row_ptr, const Index rows, const Index cols, const Index nonzeros, const bool transposed)
81 {
87 if(y)
88 {
90 exec_cuda_impl(workspace, r_view.get_w(), alpha, x_view.get_r(), y_view.get_r(), a_view.get_r(),
91 ci_view.get_r(), rp_view.get_r(), rows, cols, nonzeros, transposed);
92 }
93 else
94 {
95 exec_cuda_impl(workspace, r_view.get_w(), alpha, x_view.get_r(), (DT_*)nullptr, a_view.get_r(),
96 ci_view.get_r(), rp_view.get_r(), rows, cols, nonzeros, transposed);
97 }
98 }
99
100 template <typename DT_, typename IT_>
101 static void exec([[maybe_unused]] std::array<std::shared_ptr<void>, to_underlying(BackendIdx::num_backends)>& workspaces, Memory::Arbiter& r, const DT_ alpha, const Memory::Arbiter& x, const Memory::Arbiter& y, const Memory::Arbiter& a, const Memory::Arbiter& col_idx, const Memory::Arbiter& row_ptr, const Index rows, const Index cols, const Index nonzeros, const bool transposed)
102 {
103 XASSERT(r != x);
104
106 {
108 exec_generic<DT_, IT_>(r, alpha, x, y, a, col_idx, row_ptr, rows, cols, nonzeros, transposed);
109 break;
110
112#ifdef FEAT_HAVE_MKL
113 if constexpr(sizeof(IT_) == sizeof(Index))
114 exec_mkl<DT_>(r, alpha, x, y, a, col_idx, row_ptr, rows, cols, nonzeros, transposed);
115 else
116 {
117 Backend::warn_missing("LAFEM::Arch::MatVecMultCSRDense::exec: MKL backend not available for IT_ != Index");
118 exec_generic<DT_, IT_>(r, alpha, x, y, a, col_idx, row_ptr, rows, cols, nonzeros, transposed);
119 }
120#else
121 XABORTM("LAFEM::Arch::MatVecMultCSRDense::exec: MKL backend not available!");
122#endif
123 break;
124
126#ifdef FEAT_HAVE_CUDA
127 exec_cuda<DT_, IT_>(workspaces[to_underlying(BackendIdx::cuda)].get(), r, alpha, x, y, a, col_idx, row_ptr, rows, cols, nonzeros, transposed);
128#else
129 XABORTM("LAFEM::Arch::MatVecMultCSRDense::exec: CUDA backend not available!");
130#endif
131 break;
132
133 default:
134 XABORTM("LAFEM::Arch::MatVecMultCSRDense::exec: unknown backend!");
135 break;
136 }
137 }
138
149 template <typename DT_, typename IT_>
150 static void 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 bool transposed);
151
152 }; // struct MatVecMultCSRDense
153
154#ifdef FEAT_EICKT
155 extern template void MatVecMultCSRDense::exec_generic_impl(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);
156 extern template void MatVecMultCSRDense::exec_generic_impl(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);
157 extern template void MatVecMultCSRDense::exec_generic_impl(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);
158 extern template void MatVecMultCSRDense::exec_generic_impl(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);
159#endif
160
161} // namespace FEAT::LAFEM::Arch
162
163#ifndef __CUDACC__
164#define KERNEL_LAFEM_ARCH_MATVECMULT_CSR_DENSE_HPP 1
165#include <kernel/lafem/arch/matvecmult_csr_dense_generic.hpp>
166#endif
#define XABORTM(msg)
Abortion macro definition with custom message.
Definition: assertion.hpp:192
#define XASSERT(expr)
Assertion macro definition.
Definition: assertion.hpp:262
FEAT Kernel base header.
static PreferredBackend get_preferred_backend()
get current preferred backend
Definition: backend.cpp:32
static void warn_missing(const char *operation, PreferredBackend preferred_backend, PreferredBackend fallback_backend=PreferredBackend::generic)
issues a warning about unsupported backend usage
Definition: backend.cpp:77
Memory arbiter class.
View view(Location mem_loc, Access access)
Maps a raw view to a memory block.
Typed memory view class.
const T_ * get_r() const
Returns a const typed pointer to the array that grants read access.
T_ * get_w()
Returns a typed pointer to the array that grants write (and read) access.
@ overlap
explicitly allow overlapping writes (see Overlapping Memory Views)
@ read
read-only access
@ write
write-only access (see attention note above)
constexpr auto to_underlying(E e) noexcept
Cast enum to underlying type.
std::uint64_t Index
Index data type.
r[i] <- y[i] + alpha * sum_{j=0..n-1} A[i][j] * x[j]
static void 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 bool transposed)
Sets up a persistent cuSPARSE SpMV workspace for this matrix.