numerics 0.1.0
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(
23 stderr,
24 "[numerics] WARNING: Backend::blas requested but BLAS was not "
25 "found at "
26 "configure time.\n"
27 " Falling back to Backend::blocked (cache-blocked).\n"
28 " Install OpenBLAS and reconfigure: "
29 "apt install libopenblas-dev | brew install openblas\n");
30 }
31#endif
32}
33} // namespace
34
35namespace num::backends::blas {
36
37void matmul(const Matrix& A, const Matrix& B, Matrix& C) {
38 warn_blas_unavailable();
39#ifdef NUMERICS_HAS_BLAS
40 cblas_dgemm(CblasRowMajor,
41 CblasNoTrans,
42 CblasNoTrans,
43 static_cast<int>(A.rows()),
44 static_cast<int>(B.cols()),
45 static_cast<int>(A.cols()),
46 1.0,
47 A.data(),
48 static_cast<int>(A.cols()),
49 B.data(),
50 static_cast<int>(B.cols()),
51 0.0,
52 C.data(),
53 static_cast<int>(C.cols()));
54#else
56#endif
57}
58
59void matvec(const Matrix& A, const Vector& x, Vector& y) {
60 warn_blas_unavailable();
61#ifdef NUMERICS_HAS_BLAS
62 cblas_dgemv(CblasRowMajor,
63 CblasNoTrans,
64 static_cast<int>(A.rows()),
65 static_cast<int>(A.cols()),
66 1.0,
67 A.data(),
68 static_cast<int>(A.cols()),
69 x.data(),
70 1,
71 0.0,
72 y.data(),
73 1);
74#else
76#endif
77}
78
79void matadd(real alpha,
80 const Matrix& A,
81 real beta,
82 const Matrix& B,
83 Matrix& C) {
84 warn_blas_unavailable();
85#ifdef NUMERICS_HAS_BLAS
86 cblas_dcopy(static_cast<int>(A.size()), A.data(), 1, C.data(), 1);
87 cblas_dscal(static_cast<int>(C.size()), alpha, C.data(), 1);
88 cblas_daxpy(static_cast<int>(B.size()), beta, B.data(), 1, C.data(), 1);
89#else
90 num::backends::seq::matadd(alpha, A, beta, B, C);
91#endif
92}
93
94} // namespace num::backends::blas
Dense row-major matrix with optional GPU storage.
Definition matrix.hpp:12
constexpr idx size() const noexcept
Definition matrix.hpp:26
real * data()
Definition matrix.hpp:28
constexpr idx rows() const noexcept
Definition matrix.hpp:24
constexpr idx cols() const noexcept
Definition matrix.hpp:25
Matrix operations.
void matmul(const Matrix &A, const Matrix &B, Matrix &C)
Definition matrix.cpp:37
void matadd(real alpha, const Matrix &A, real beta, const Matrix &B, Matrix &C)
Definition matrix.cpp:79
void matvec(const Matrix &A, const Vector &x, Vector &y)
Definition matrix.cpp:59
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:77
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:248