numerics 0.1.0
Loading...
Searching...
No Matches
matrix.hpp
Go to the documentation of this file.
1/// @file matrix.hpp
2/// @brief Matrix operations
3#pragma once
4
5#include "core/policy.hpp"
7#include "core/vector.hpp"
8
9namespace num {
10
11/// @brief Dense row-major matrix with optional GPU storage
12class Matrix {
13public:
14 Matrix() : rows_(0), cols_(0), data_(nullptr) {}
16 Matrix(idx rows, idx cols, real val);
17 ~Matrix();
18
19 Matrix(const Matrix &);
20 Matrix(Matrix &&) noexcept;
21 Matrix &operator=(const Matrix &);
22 Matrix &operator=(Matrix &&) noexcept;
23
24 constexpr idx rows() const noexcept { return rows_; }
25 constexpr idx cols() const noexcept { return cols_; }
26 constexpr idx size() const noexcept { return rows_ * cols_; }
27
28 real *data() { return data_.get(); }
29 const real *data() const { return data_.get(); }
30 real &operator()(idx i, idx j) { return data_[i * cols_ + j]; }
31 real operator()(idx i, idx j) const { return data_[i * cols_ + j]; }
32
33 void to_gpu();
34 void to_cpu();
35 real *gpu_data() { return d_data_; }
36 const real *gpu_data() const { return d_data_; }
37 bool on_gpu() const { return d_data_ != nullptr; }
38
39private:
40 idx rows_ = 0, cols_ = 0;
41 std::unique_ptr<real[]> data_;
42 real *d_data_ = nullptr;
43};
44
45/// @brief y = A * x
46void matvec(const Matrix &A, const Vector &x, Vector &y,
48
49/// @brief C = A * B
50void matmul(const Matrix &A, const Matrix &B, Matrix &C,
52
53/// @brief C = alpha*A + beta*B
54void matadd(real alpha, const Matrix &A, real beta, const Matrix &B, Matrix &C,
56
57// Named variants for benchmarking and pedagogy.
58// Expose the progression of optimisation techniques.
59// For production code prefer matmul(A, B, C) or matmul(A, B, C, num::simd).
60
61/// @brief C = A * B (cache-blocked)
62///
63/// Divides A, B, C into BLOCKxBLOCK tiles so the working set fits in L2 cache.
64/// @param block_size Tile edge length (default 64).
65void matmul_blocked(const Matrix &A, const Matrix &B, Matrix &C,
66 idx block_size = 64);
67
68/// @brief C = A * B (register-blocked)
69///
70/// Extends cache blocking with a REGxREG register tile inside each cache tile.
71/// @param block_size Cache tile edge (default 64).
72/// @param reg_size Register tile edge (default 4).
73void matmul_register_blocked(const Matrix &A, const Matrix &B, Matrix &C,
74 idx block_size = 64, idx reg_size = 4);
75
76/// @brief C = A * B (SIMD-accelerated)
77///
78/// Dispatches at compile time: AVX-256 + FMA on x86, NEON on AArch64,
79/// falls back to matmul_blocked if neither is available.
80void matmul_simd(const Matrix &A, const Matrix &B, Matrix &C,
81 idx block_size = 64);
82
83/// @brief y = A * x (SIMD-accelerated)
84void matvec_simd(const Matrix &A, const Vector &x, Vector &y);
85
86} // namespace num
Dense row-major matrix with optional GPU storage.
Definition matrix.hpp:12
constexpr idx size() const noexcept
Definition matrix.hpp:26
bool on_gpu() const
Definition matrix.hpp:37
real operator()(idx i, idx j) const
Definition matrix.hpp:31
real & operator()(idx i, idx j)
Definition matrix.hpp:30
const real * data() const
Definition matrix.hpp:29
real * data()
Definition matrix.hpp:28
constexpr idx rows() const noexcept
Definition matrix.hpp:24
real * gpu_data()
Definition matrix.hpp:35
void to_cpu()
Definition matrix.cpp:87
void to_gpu()
Definition matrix.cpp:80
const real * gpu_data() const
Definition matrix.hpp:36
constexpr idx cols() const noexcept
Definition matrix.hpp:25
Backend enum for linear algebra operations.
void matmul_simd(const Matrix &A, const Matrix &B, Matrix &C, idx block_size=64)
C = A * B (SIMD-accelerated)
Definition matrix.cpp:186
double real
Definition types.hpp:10
Backend
Selects which backend handles a linalg operation.
Definition policy.hpp:19
void matvec_simd(const Matrix &A, const Vector &x, Vector &y)
y = A * x (SIMD-accelerated)
Definition matrix.cpp:190
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:120
void matmul_blocked(const Matrix &A, const Matrix &B, Matrix &C, idx block_size=64)
C = A * B (cache-blocked)
Definition matrix.cpp:171
BasicVector< real > Vector
Real-valued dense vector with full backend dispatch (CPU + GPU)
Definition vector.hpp:130
void matmul(const Matrix &A, const Matrix &B, Matrix &C, Backend b=default_backend)
C = A * B.
Definition matrix.cpp:95
constexpr Backend default_backend
Definition policy.hpp:92
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:145
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:178
Constexpr fixed-size stack-allocated matrix and Givens rotation.
Vector operations.