numerics
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/vector.hpp"
6#include "core/policy.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) {}
17 ~Matrix();
18
19 Matrix(const Matrix&);
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_, cols_;
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, Backend b = default_backend);
47
48/// @brief C = A * B
49void matmul(const Matrix& A, const Matrix& B, Matrix& C, Backend b = default_backend);
50
51/// @brief C = alpha*A + beta*B
52void matadd(real alpha, const Matrix& A, real beta, const Matrix& B, Matrix& C,
54
55// -- Named variants for benchmarking and pedagogy ----------------------------
56// These expose the progression of optimisation techniques. For production
57// code, prefer matmul(A, B, C) or matmul(A, B, C, num::simd) etc.
58
59/// @brief C = A * B (cache-blocked)
60///
61/// Divides A, B, C into BLOCKxBLOCK tiles so the working set fits in L2 cache.
62/// @param block_size Tile edge length (default 64).
63void matmul_blocked(const Matrix& A, const Matrix& B, Matrix& C,
64 idx block_size = 64);
65
66/// @brief C = A * B (register-blocked)
67///
68/// Extends cache blocking with a REGxREG register tile inside each cache tile.
69/// @param block_size Cache tile edge (default 64).
70/// @param reg_size Register tile edge (default 4).
71void matmul_register_blocked(const Matrix& A, const Matrix& B, Matrix& C,
72 idx block_size = 64, idx reg_size = 4);
73
74/// @brief C = A * B (SIMD-accelerated)
75///
76/// Dispatches at compile time: AVX-256 + FMA on x86, NEON on AArch64,
77/// falls back to matmul_blocked if neither is available.
78void matmul_simd(const Matrix& A, const Matrix& B, Matrix& C,
79 idx block_size = 64);
80
81/// @brief y = A * x (SIMD-accelerated)
82void matvec_simd(const Matrix& A, const Vector& x, Vector& y);
83
84} // 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:75
void to_gpu()
Definition matrix.cpp:68
const real * gpu_data() const
Definition matrix.hpp:36
constexpr idx cols() const noexcept
Definition matrix.hpp:25
void matmul_simd(const Matrix &A, const Matrix &B, Matrix &C, idx block_size=64)
C = A * B (SIMD-accelerated)
Definition matrix.cpp:126
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:130
real beta(real a, real b)
B(a, b) – beta function.
Definition math.hpp:242
constexpr T ipow(T x) noexcept
Compute x^N at compile time via repeated squaring.
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:94
void matmul_blocked(const Matrix &A, const Matrix &B, Matrix &C, idx block_size=64)
C = A * B (cache-blocked)
Definition matrix.cpp:117
BasicVector< real > Vector
Real-valued dense vector with full backend dispatch (CPU + GPU)
Definition vector.hpp:122
void matmul(const Matrix &A, const Matrix &B, Matrix &C, Backend b=default_backend)
C = A * B.
Definition matrix.cpp:83
constexpr Backend default_backend
Definition policy.hpp:57
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:105
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:121
Backend enum for linear algebra operations.
Constexpr fixed-size stack-allocated matrix and Givens rotation.
Vector operations.