numerics
Loading...
Searching...
No Matches
vector.cpp
Go to the documentation of this file.
1/// @file core/vector.cpp
2/// @brief Backend dispatch for real-vector ops, and sequential complex-vector ops.
3///
4/// BasicVector<T> member functions are defined inline in vector.hpp (template).
5/// This file only provides:
6/// 1. Backend-dispatched free functions for Vector (= BasicVector<real>)
7/// 2. Sequential free functions for CVector (= BasicVector<cplx>)
8///
9/// Adding a new backend:
10/// 1. Add the enumerator to enum class Backend in include/core/policy.hpp
11/// 2. Create src/core/backends/<name>/ with impl.hpp and vector.cpp
12/// 3. Add `case Backend::<name>:` to each switch below
13/// 4. Register the .cpp in cmake/sources.cmake
14
15#include "core/vector.hpp"
16#include <cmath>
17
18#include "backends/seq/impl.hpp"
20#include "backends/omp/impl.hpp"
21#include "backends/gpu/impl.hpp"
22
23namespace num {
24
25// -- Real-vector dispatch ------------------------------------------------------
26
28 switch (b) {
29 case Backend::seq:
35 }
36}
37
38void add(const Vector& x, const Vector& y, Vector& z, Backend b) {
39 if (b == Backend::gpu) {
40 cuda::add(x.gpu_data(), y.gpu_data(), z.gpu_data(), x.size());
41 } else {
42 backends::seq::add(x, y, z);
43 }
44}
45
46void axpy(real alpha, const Vector& x, Vector& y, Backend b) {
47 switch (b) {
48 case Backend::seq:
50 case Backend::simd: backends::seq::axpy(alpha, x, y); break;
51 case Backend::blas: backends::blas::axpy(alpha, x, y); break;
52 case Backend::omp: backends::omp::axpy(alpha, x, y); break;
53 case Backend::gpu: backends::gpu::axpy(alpha, x, y); break;
54 }
55}
56
57real dot(const Vector& x, const Vector& y, Backend b) {
58 switch (b) {
59 case Backend::seq:
61 case Backend::simd: return backends::seq::dot(x, y);
62 case Backend::blas: return backends::blas::dot(x, y);
63 case Backend::omp: return backends::omp::dot(x, y);
64 case Backend::gpu: return backends::gpu::dot(x, y);
65 }
66 return backends::seq::dot(x, y);
67}
68
69real norm(const Vector& x, Backend b) {
70 switch (b) {
71 case Backend::seq:
73 case Backend::simd: return backends::seq::norm(x);
74 case Backend::blas: return backends::blas::norm(x);
75 case Backend::omp: return backends::seq::norm(x); // no OMP norm
76 case Backend::gpu: return backends::gpu::norm(x);
77 }
78 return backends::seq::norm(x);
79}
80
81// -- Complex-vector (sequential) -----------------------------------------------
82
84 for (idx i = 0; i < v.size(); ++i) v[i] *= alpha;
85}
86
87void axpy(cplx alpha, const CVector& x, CVector& y) {
88 for (idx i = 0; i < x.size(); ++i) y[i] += alpha * x[i];
89}
90
91cplx dot(const CVector& x, const CVector& y) {
92 cplx sum{0, 0};
93 for (idx i = 0; i < x.size(); ++i) sum += std::conj(x[i]) * y[i];
94 return sum;
95}
96
97real norm(const CVector& x) {
98 real sum = 0;
99 for (idx i = 0; i < x.size(); ++i) sum += std::norm(x[i]);
100 return std::sqrt(sum);
101}
102
103} // namespace num
real * gpu_data()
Definition vector.hpp:111
constexpr idx size() const noexcept
Definition vector.hpp:77
real dot(const Vector &x, const Vector &y)
Definition vector.cpp:50
void axpy(real alpha, const Vector &x, Vector &y)
Definition vector.cpp:41
real norm(const Vector &x)
Definition vector.cpp:59
void scale(Vector &v, real alpha)
Definition vector.cpp:32
void axpy(real alpha, const Vector &x, Vector &y)
Definition vector.cpp:22
real dot(const Vector &x, const Vector &y)
Definition vector.cpp:30
real norm(const Vector &x)
Definition vector.cpp:38
void scale(Vector &v, real alpha)
Definition vector.cpp:14
real dot(const Vector &x, const Vector &y)
Definition vector.cpp:29
void scale(Vector &v, real alpha)
Definition vector.cpp:9
void axpy(real alpha, const Vector &x, Vector &y)
Definition vector.cpp:19
real dot(const Vector &x, const Vector &y)
Definition vector.cpp:21
void scale(Vector &v, real alpha)
Definition vector.cpp:9
real norm(const Vector &x)
Definition vector.cpp:27
void add(const Vector &x, const Vector &y, Vector &z)
Definition vector.cpp:13
void axpy(real alpha, const Vector &x, Vector &y)
Definition vector.cpp:17
void add(const real *x, const real *y, real *z, idx n)
z = x + y
double real
Definition types.hpp:10
Backend
Selects which backend handles a linalg operation.
Definition policy.hpp:19
@ gpu
CUDA – custom kernels or cuBLAS.
@ omp
OpenMP parallel blocked loops.
@ blocked
Cache-blocked; compiler auto-vectorizes inner loops.
@ simd
Hand-written SIMD intrinsics (AVX2 or NEON)
@ blas
cblas/LAPACKE – OpenBLAS, MKL, Apple Accelerate
@ seq
Naive textbook loops – always available.
constexpr T ipow(T x) noexcept
Compute x^N at compile time via repeated squaring.
std::size_t idx
Definition types.hpp:11
void scale(Vector &v, real alpha, Backend b=default_backend)
v *= alpha
Definition vector.cpp:27
real dot(const Vector &x, const Vector &y, Backend b=default_backend)
dot product
Definition vector.cpp:57
real norm(const Vector &x, Backend b=default_backend)
Euclidean norm.
Definition vector.cpp:69
std::complex< real > cplx
Definition types.hpp:12
void axpy(real alpha, const Vector &x, Vector &y, Backend b=default_backend)
y += alpha * x
Definition vector.cpp:46
void add(const Vector &x, const Vector &y, Vector &z, Backend b=default_backend)
z = x + y
Definition vector.cpp:38
Private declarations for the BLAS backend. Only included by src/core/vector.cpp and src/core/matrix....
Private declarations for the GPU (CUDA) backend. Only included by src/core/vector....
Vector operations.