numerics
Loading...
Searching...
No Matches
matrix.cpp
Go to the documentation of this file.
1/// @file core/backends/blas/matrix.cpp
2/// @brief BLAS backend -- cblas level-2/3 matrix operations
3///
4/// Delegates to the system BLAS (OpenBLAS, MKL, Apple Accelerate, ...).
5/// When NUMERICS_HAS_BLAS is not defined, falls back to the blocked backend
6/// and emits a one-time stderr warning.
7
8#include "core/matrix.hpp"
9#include "../seq/impl.hpp"
10#include <cstdio>
11
12#ifdef NUMERICS_HAS_BLAS
13# include <cblas.h>
14#endif
15
16namespace {
17void warn_blas_unavailable() {
18#ifndef NUMERICS_HAS_BLAS
19 static bool warned = false;
20 if (!warned) {
21 warned = true;
22 std::fprintf(stderr,
23 "[numerics] WARNING: Backend::blas requested but BLAS was not found at "
24 "configure time.\n"
25 " Falling back to Backend::blocked (cache-blocked).\n"
26 " Install OpenBLAS and reconfigure: "
27 "apt install libopenblas-dev | brew install openblas\n");
28 }
29#endif
30}
31} // namespace
32
33namespace num::backends::blas {
34
35void matmul(const Matrix& A, const Matrix& B, Matrix& C) {
37#ifdef NUMERICS_HAS_BLAS
39 static_cast<int>(A.rows()),
40 static_cast<int>(B.cols()),
41 static_cast<int>(A.cols()),
42 1.0, A.data(), static_cast<int>(A.cols()),
43 B.data(), static_cast<int>(B.cols()),
44 0.0, C.data(), static_cast<int>(C.cols()));
45#else
47#endif
48}
49
50void matvec(const Matrix& A, const Vector& x, Vector& y) {
52#ifdef NUMERICS_HAS_BLAS
54 static_cast<int>(A.rows()),
55 static_cast<int>(A.cols()),
56 1.0, A.data(), static_cast<int>(A.cols()),
57 x.data(), 1,
58 0.0, y.data(), 1);
59#else
61#endif
62}
63
64void matadd(real alpha, const Matrix& A, real beta, const Matrix& B, Matrix& C) {
66#ifdef NUMERICS_HAS_BLAS
67 cblas_dcopy(static_cast<int>(A.size()), A.data(), 1, C.data(), 1);
68 cblas_dscal(static_cast<int>(C.size()), alpha, C.data(), 1);
69 cblas_daxpy(static_cast<int>(B.size()), beta, B.data(), 1, C.data(), 1);
70#else
72#endif
73}
74
75} // namespace num::backends::blas
Dense row-major matrix with optional GPU storage.
Definition matrix.hpp:12
Matrix operations.
void matmul(const Matrix &A, const Matrix &B, Matrix &C)
Definition matrix.cpp:35
void matadd(real alpha, const Matrix &A, real beta, const Matrix &B, Matrix &C)
Definition matrix.cpp:64
void matvec(const Matrix &A, const Vector &x, Vector &y)
Definition matrix.cpp:50
void matvec(const Matrix &A, const Vector &x, Vector &y)
Definition matrix.cpp:24
void matmul_blocked(const Matrix &A, const Matrix &B, Matrix &C, idx block_size)
Definition matrix.cpp:73
void matadd(real alpha, const Matrix &A, real beta, const Matrix &B, Matrix &C)
Definition matrix.cpp:32
double real
Definition types.hpp:10
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.