numerics
Loading...
Searching...
No Matches
matrix.cpp
Go to the documentation of this file.
1/// @file core/backends/omp/matrix.cpp
2/// @brief OpenMP backend -- parallelised matrix operations
3///
4/// The (ii, jj) cache-tile pairs are distributed across threads.
5/// Each thread owns a distinct C tile so there are no data races.
6/// Falls back to sequential when NUMERICS_HAS_OMP is not defined.
7
8#include "core/matrix.hpp"
9#include "../seq/impl.hpp"
10#include <algorithm>
11
12namespace num::backends::omp {
13
14void matmul(const Matrix& A, const Matrix& B, Matrix& C) {
15#ifdef NUMERICS_HAS_OMP
16 constexpr idx BS = 64;
17 const idx M = A.rows(), K = A.cols(), N = B.cols();
18 std::fill_n(C.data(), M * N, real(0));
19
20# pragma omp parallel for schedule(dynamic) collapse(2)
21 for (idx ii = 0; ii < M; ii += BS) {
22 for (idx jj = 0; jj < N; jj += BS) {
23 const idx i_lim = std::min(ii + BS, M);
24 const idx j_lim = std::min(jj + BS, N);
25 for (idx kk = 0; kk < K; kk += BS) {
26 const idx k_lim = std::min(kk + BS, K);
27 for (idx i = ii; i < i_lim; ++i) {
28 for (idx k = kk; k < k_lim; ++k) {
29 const real a_ik = A(i, k);
30 for (idx j = jj; j < j_lim; ++j)
31 C(i, j) += a_ik * B(k, j);
32 }
33 }
34 }
35 }
36 }
37#else
39#endif
40}
41
42void matvec(const Matrix& A, const Vector& x, Vector& y) {
43#ifdef NUMERICS_HAS_OMP
44# pragma omp parallel for schedule(static)
45 for (idx i = 0; i < A.rows(); ++i) {
46 real sum = 0;
47 for (idx j = 0; j < A.cols(); ++j)
48 sum += A(i, j) * x[j];
49 y[i] = sum;
50 }
51#else
53#endif
54}
55
56void matadd(real alpha, const Matrix& A, real beta, const Matrix& B, Matrix& C) {
57#ifdef NUMERICS_HAS_OMP
58 const idx n = A.size();
59# pragma omp parallel for schedule(static)
60 for (idx i = 0; i < n; ++i)
61 C.data()[i] = alpha * A.data()[i] + beta * B.data()[i];
62#else
64#endif
65}
66
67} // namespace num::backends::omp
Dense row-major matrix with optional GPU storage.
Definition matrix.hpp:12
Matrix operations.
void matvec(const Matrix &A, const Vector &x, Vector &y)
Definition matrix.cpp:42
void matadd(real alpha, const Matrix &A, real beta, const Matrix &B, Matrix &C)
Definition matrix.cpp:56
void matmul(const Matrix &A, const Matrix &B, Matrix &C)
Definition matrix.cpp:14
void matmul(const Matrix &A, const Matrix &B, Matrix &C)
Definition matrix.cpp:14
void matvec(const Matrix &A, const Vector &x, Vector &y)
Definition matrix.cpp:24
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.
std::size_t idx
Definition types.hpp:11