numerics 0.1.0
Loading...
Searching...
No Matches
matrix.cpp
Go to the documentation of this file.
1/// @file core/matrix.cpp
2/// @brief Backend dispatch for Matrix (= BasicMatrix<double>) operations.
3///
4/// Adding a new backend:
5/// 1. Add the enumerator to enum class Backend in include/core/policy.hpp
6/// 2. Create src/core/backends/<name>/ with impl.hpp and matrix.cpp
7/// 3. Add `case Backend::<name>:` to each switch below
8/// 4. Register the .cpp in cmake/sources.cmake
9
10#include "core/matrix.hpp"
11
13#include "backends/gpu/impl.hpp"
14#include "backends/omp/impl.hpp"
15#include "backends/seq/impl.hpp"
17
18namespace num {
19
20void matmul(const Matrix& A, const Matrix& B, Matrix& C, Backend b) {
21 switch (b) {
22 case Backend::seq:
23 backends::seq::matmul(A, B, C);
24 break;
27 break;
28 case Backend::simd:
29 backends::simd::matmul(A, B, C, 64);
30 break;
31 case Backend::lapack:
32 [[fallthrough]];
33 case Backend::blas:
35 break;
36 case Backend::omp:
37 backends::omp::matmul(A, B, C);
38 break;
39 case Backend::gpu:
40 backends::gpu::matmul(A, B, C);
41 break;
42 }
43}
44
45void matvec(const Matrix& A, const Vector& x, Vector& y, Backend b) {
46 switch (b) {
47 case Backend::seq:
49 backends::seq::matvec(A, x, y);
50 break;
51 case Backend::simd:
53 break;
54 case Backend::lapack:
55 [[fallthrough]];
56 case Backend::blas:
58 break;
59 case Backend::omp:
60 backends::omp::matvec(A, x, y);
61 break;
62 case Backend::gpu:
63 backends::gpu::matvec(A, x, y);
64 break;
65 }
66}
67
68void matadd(real alpha,
69 const Matrix& A,
70 real beta,
71 const Matrix& B,
72 Matrix& C,
73 Backend b) {
74 switch (b) {
75 case Backend::seq:
77 case Backend::simd:
78 backends::seq::matadd(alpha, A, beta, B, C);
79 break;
80 case Backend::lapack:
81 [[fallthrough]];
82 case Backend::blas:
83 backends::blas::matadd(alpha, A, beta, B, C);
84 break;
85 case Backend::omp:
86 backends::omp::matadd(alpha, A, beta, B, C);
87 break;
88 case Backend::gpu:
89 backends::seq::matadd(alpha, A, beta, B, C);
90 break;
91 }
92}
93
94void matmul_blocked(const Matrix& A, const Matrix& B, Matrix& C, idx block_size) {
95 backends::seq::matmul_blocked(A, B, C, block_size);
96}
97
99 const Matrix& B,
100 Matrix& C,
101 idx block_size,
102 idx reg_size) {
103 backends::seq::matmul_register_blocked(A, B, C, block_size, reg_size);
104}
105
106void matmul_simd(const Matrix& A, const Matrix& B, Matrix& C, idx block_size) {
107 backends::simd::matmul(A, B, C, block_size);
108}
109
110void matvec_simd(const Matrix& A, const Vector& x, Vector& y) {
111 backends::simd::matvec(A, x, y);
112}
113
114} // namespace num
Private declarations for the BLAS backend. Only included by src/core/vector.cpp and src/core/matrix....
Private declarations for the GPU (CUDA) backend. Only included by src/core/vector....
Private declarations for the SIMD backend. Only included by src/core/vector.cpp and src/core/matrix....
Dense row-major matrix templated over scalar type T.
void matmul(const Matrix &A, const Matrix &B, Matrix &C)
Definition matrix.cpp:36
void matadd(real alpha, const Matrix &A, real beta, const Matrix &B, Matrix &C)
Definition matrix.cpp:78
void matvec(const Matrix &A, const Vector &x, Vector &y)
Definition matrix.cpp:58
void matmul(const Matrix &A, const Matrix &B, Matrix &C)
Definition matrix.cpp:13
void matvec(const Matrix &A, const Vector &x, Vector &y)
Definition matrix.cpp:21
void matvec(const Matrix &A, const Vector &x, Vector &y)
Definition matrix.cpp:42
void matadd(real alpha, const Matrix &A, real beta, const Matrix &B, Matrix &C)
Definition matrix.cpp:56
void matmul(const Matrix &A, const Matrix &B, Matrix &C)
Definition matrix.cpp:14
void matmul_register_blocked(const Matrix &A, const Matrix &B, Matrix &C, idx block_size, idx reg_size)
Definition matrix.cpp:50
void matmul(const Matrix &A, const Matrix &B, Matrix &C)
Definition matrix.cpp:10
void matvec(const Matrix &A, const Vector &x, Vector &y)
Definition matrix.cpp:20
void matmul_blocked(const Matrix &A, const Matrix &B, Matrix &C, idx block_size)
Definition matrix.cpp:28
void matadd(real alpha, const Matrix &A, real beta, const Matrix &B, Matrix &C)
Definition matrix.cpp:24
void matvec(const Matrix &A, const Vector &x, Vector &y)
Definition matrix.cpp:236
void matmul(const Matrix &A, const Matrix &B, Matrix &C, idx block_size)
Definition matrix.cpp:226
void matmul_simd(const Matrix &A, const Matrix &B, Matrix &C, idx block_size=64)
C = A * B (SIMD-accelerated)
Definition matrix.cpp:106
double real
Definition types.hpp:10
Backend
Definition policy.hpp:7
void matvec_simd(const Matrix &A, const Vector &x, Vector &y)
y = A * x (SIMD-accelerated)
Definition matrix.cpp:110
real beta(real a, real b)
B(a, b) – beta function.
Definition math.hpp:248
std::size_t idx
Definition types.hpp:11
void matvec(const Matrix &A, const Vector &x, Vector &y, Backend b=default_backend)
y = A * x
Definition matrix.cpp:45
void matmul_blocked(const Matrix &A, const Matrix &B, Matrix &C, idx block_size=64)
C = A * B (cache-blocked)
Definition matrix.cpp:94
void matmul(const Matrix &A, const Matrix &B, Matrix &C, Backend b=default_backend)
C = A * B.
Definition matrix.cpp:20
void matadd(real alpha, const Matrix &A, real beta, const Matrix &B, Matrix &C, Backend b=default_backend)
C = alpha*A + beta*B.
Definition matrix.cpp:68
void matmul_register_blocked(const Matrix &A, const Matrix &B, Matrix &C, idx block_size=64, idx reg_size=4)
C = A * B (register-blocked)
Definition matrix.cpp:98