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