numerics 0.1.0
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
3/// ops.
4///
5/// BasicVector<T> member functions are defined inline in vector.hpp (template).
6/// This file only provides:
7/// 1. Backend-dispatched free functions for Vector (= BasicVector<real>)
8/// 2. Sequential free functions for CVector (= BasicVector<cplx>)
9///
10/// Adding a new backend:
11/// 1. Add the enumerator to enum class Backend in include/core/policy.hpp
12/// 2. Create src/core/backends/<name>/ with impl.hpp and vector.cpp
13/// 3. Add `case Backend::<name>:` to each switch below
14/// 4. Register the .cpp in cmake/sources.cmake
15
16#include "core/vector.hpp"
17#include <cmath>
18
19#include "backends/seq/impl.hpp"
21#include "backends/omp/impl.hpp"
22#include "backends/gpu/impl.hpp"
23
24namespace num {
25
26// -- Real-vector dispatch
27// ------------------------------------------------------
28
29void scale(Vector& v, real alpha, Backend b) {
30 switch (b) {
31 case Backend::seq:
33 case Backend::simd:
34 backends::seq::scale(v, alpha);
35 break;
36 case Backend::lapack:
37 [[fallthrough]];
38 case Backend::blas:
39 backends::blas::scale(v, alpha);
40 break;
41 case Backend::omp:
42 backends::omp::scale(v, alpha);
43 break;
44 case Backend::gpu:
45 backends::gpu::scale(v, alpha);
46 break;
47 }
48}
49
50void add(const Vector& x, const Vector& y, Vector& z, Backend b) {
51 if (b == Backend::gpu) {
52 cuda::add(x.gpu_data(), y.gpu_data(), z.gpu_data(), x.size());
53 } else {
54 backends::seq::add(x, y, z);
55 }
56}
57
58void axpy(real alpha, const Vector& x, Vector& y, Backend b) {
59 switch (b) {
60 case Backend::seq:
62 case Backend::simd:
63 backends::seq::axpy(alpha, x, y);
64 break;
65 case Backend::lapack:
66 [[fallthrough]];
67 case Backend::blas:
68 backends::blas::axpy(alpha, x, y);
69 break;
70 case Backend::omp:
71 backends::omp::axpy(alpha, x, y);
72 break;
73 case Backend::gpu:
74 backends::gpu::axpy(alpha, x, y);
75 break;
76 }
77}
78
79real dot(const Vector& x, const Vector& y, Backend b) {
80 switch (b) {
81 case Backend::seq:
83 case Backend::simd:
84 return backends::seq::dot(x, y);
85 case Backend::lapack:
86 [[fallthrough]];
87 case Backend::blas:
88 return backends::blas::dot(x, y);
89 case Backend::omp:
90 return backends::omp::dot(x, y);
91 case Backend::gpu:
92 return backends::gpu::dot(x, y);
93 }
94 return backends::seq::dot(x, y);
95}
96
97real norm(const Vector& x, Backend b) {
98 switch (b) {
99 case Backend::seq:
100 case Backend::blocked:
101 case Backend::simd:
102 return backends::seq::norm(x);
103 case Backend::lapack:
104 [[fallthrough]];
105 case Backend::blas:
106 return backends::blas::norm(x);
107 case Backend::omp:
108 return backends::seq::norm(x); // no OMP norm
109 case Backend::gpu:
110 return backends::gpu::norm(x);
111 }
112 return backends::seq::norm(x);
113}
114
115// -- Complex-vector (sequential)
116// -----------------------------------------------
117
118void scale(CVector& v, cplx alpha) {
119 for (idx i = 0; i < v.size(); ++i)
120 v[i] *= alpha;
121}
122
123void axpy(cplx alpha, const CVector& x, CVector& y) {
124 for (idx i = 0; i < x.size(); ++i)
125 y[i] += alpha * x[i];
126}
127
128cplx dot(const CVector& x, const CVector& y) {
129 cplx sum{0, 0};
130 for (idx i = 0; i < x.size(); ++i)
131 sum += std::conj(x[i]) * y[i];
132 return sum;
133}
134
135real norm(const CVector& x) {
136 real sum = 0;
137 for (idx i = 0; i < x.size(); ++i)
138 sum += std::norm(x[i]);
139 return std::sqrt(sum);
140}
141
142} // namespace num
real * gpu_data()
Definition vector.hpp:118
constexpr idx size() const noexcept
Definition vector.hpp:80
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....
real dot(const Vector &x, const Vector &y)
Definition vector.cpp:51
void axpy(real alpha, const Vector &x, Vector &y)
Definition vector.cpp:42
real norm(const Vector &x)
Definition vector.cpp:60
void scale(Vector &v, real alpha)
Definition vector.cpp:33
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:31
void scale(Vector &v, real alpha)
Definition vector.cpp:9
void axpy(real alpha, const Vector &x, Vector &y)
Definition vector.cpp:20
real dot(const Vector &x, const Vector &y)
Definition vector.cpp:24
void scale(Vector &v, real alpha)
Definition vector.cpp:9
real norm(const Vector &x)
Definition vector.cpp:31
void add(const Vector &x, const Vector &y, Vector &z)
Definition vector.cpp:14
void axpy(real alpha, const Vector &x, Vector &y)
Definition vector.cpp:19
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 – OpenBLAS, MKL, Apple Accelerate (Level-1/2/3)
@ lapack
LAPACKE – industry-standard factorizations, SVD, eigen.
@ seq
Naive textbook loops – always available.
std::size_t idx
Definition types.hpp:11
void scale(Vector &v, real alpha, Backend b=default_backend)
v *= alpha
Definition vector.cpp:29
real dot(const Vector &x, const Vector &y, Backend b=default_backend)
dot product
Definition vector.cpp:79
real norm(const Vector &x, Backend b=default_backend)
Euclidean norm.
Definition vector.cpp:97
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:58
void add(const Vector &x, const Vector &y, Vector &z, Backend b=default_backend)
z = x + y
Definition vector.cpp:50
Vector operations.