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#include "core/vector.hpp"
6#include <cmath>
7
10#include "backends/omp/impl.hpp"
11#include "backends/seq/impl.hpp"
12
13namespace num {
14
15void scale(Vector& v, real alpha, Backend b) {
16 switch (b) {
17 case Backend::seq:
19 case Backend::simd:
20 backends::seq::scale(v, alpha);
21 break;
22 case Backend::lapack:
23 [[fallthrough]];
24 case Backend::blas:
25 backends::blas::scale(v, alpha);
26 break;
27 case Backend::omp:
28 backends::omp::scale(v, alpha);
29 break;
30 case Backend::gpu:
31 backends::gpu::scale(v, alpha);
32 break;
33 }
34}
35
36void add(const Vector& x, const Vector& y, Vector& z, Backend b) {
37 if (b == Backend::gpu) {
38 cuda::add(x.gpu_data(), y.gpu_data(), z.gpu_data(), x.size());
39 } else {
40 backends::seq::add(x, y, z);
41 }
42}
43
44void axpy(real alpha, const Vector& x, Vector& y, Backend b) {
45 switch (b) {
46 case Backend::seq:
48 case Backend::simd:
49 backends::seq::axpy(alpha, x, y);
50 break;
51 case Backend::lapack:
52 [[fallthrough]];
53 case Backend::blas:
54 backends::blas::axpy(alpha, x, y);
55 break;
56 case Backend::omp:
57 backends::omp::axpy(alpha, x, y);
58 break;
59 case Backend::gpu:
60 backends::gpu::axpy(alpha, x, y);
61 break;
62 }
63}
64
65real dot(const Vector& x, const Vector& y, Backend b) {
66 switch (b) {
67 case Backend::seq:
69 case Backend::simd:
70 return backends::seq::dot(x, y);
71 case Backend::lapack:
72 [[fallthrough]];
73 case Backend::blas:
74 return backends::blas::dot(x, y);
75 case Backend::omp:
76 return backends::omp::dot(x, y);
77 case Backend::gpu:
78 return backends::gpu::dot(x, y);
79 }
80 return backends::seq::dot(x, y);
81}
82
83real norm(const Vector& x, Backend b) {
84 switch (b) {
85 case Backend::seq:
87 case Backend::simd:
88 return backends::seq::norm(x);
89 case Backend::lapack:
90 [[fallthrough]];
91 case Backend::blas:
92 return backends::blas::norm(x);
93 case Backend::omp:
94 return backends::seq::norm(x); // no OMP norm
95 case Backend::gpu:
96 return backends::gpu::norm(x);
97 }
98 return backends::seq::norm(x);
99}
100
101void scale(CVector& v, cplx alpha) {
102 for (idx i = 0; i < v.size(); ++i)
103 v[i] *= alpha;
104}
105
106void axpy(cplx alpha, const CVector& x, CVector& y) {
107 for (idx i = 0; i < x.size(); ++i)
108 y[i] += alpha * x[i];
109}
110
111cplx dot(const CVector& x, const CVector& y) {
112 cplx sum{0, 0};
113 for (idx i = 0; i < x.size(); ++i)
114 sum += std::conj(x[i]) * y[i];
115 return sum;
116}
117
118real norm(const CVector& x) {
119 real sum = 0;
120 for (idx i = 0; i < x.size(); ++i)
121 sum += std::norm(x[i]);
122 return std::sqrt(sum);
123}
124
125} // namespace num
real * gpu_data()
Definition vector.hpp:118
constexpr idx size() const noexcept
Definition vector.hpp:83
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:22
void scale(Vector &v, real alpha)
Definition vector.cpp:9
real norm(const Vector &x)
Definition vector.cpp:26
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:18
void add(const real *x, const real *y, real *z, idx n)
z = x + y
double real
Definition types.hpp:10
Backend
Definition policy.hpp:7
std::size_t idx
Definition types.hpp:11
void scale(Vector &v, real alpha, Backend b=default_backend)
Compute .
Definition vector.cpp:15
real dot(const Vector &x, const Vector &y, Backend b=default_backend)
Compute .
Definition vector.cpp:65
real norm(const Vector &x, Backend b=default_backend)
Compute .
Definition vector.cpp:83
std::complex< real > cplx
Definition types.hpp:12
void axpy(real alpha, const Vector &x, Vector &y, Backend b=default_backend)
Compute .
Definition vector.cpp:44
void add(const Vector &x, const Vector &y, Vector &z, Backend b=default_backend)
Compute .
Definition vector.cpp:36
Dense vector storage and operations.