numerics 0.1.0
Loading...
Searching...
No Matches
vector.hpp
Go to the documentation of this file.
1/// @file vector.hpp
2/// @brief Dense vector storage and operations.
3#pragma once
4
6#include "core/policy.hpp"
7#include "core/types.hpp"
8#include <algorithm>
9#include <memory>
10#include <type_traits>
11
12namespace num {
13
14/// @brief Dense owning vector.
15template<typename T>
17 public:
19 : n_(0),
20 data_(nullptr) {}
21
22 explicit BasicVector(idx n)
23 : n_(n),
24 data_(new T[n]()) {}
25
26 BasicVector(idx n, T val)
27 : n_(n),
28 data_(new T[n]) {
29 std::fill_n(data_.get(), n_, val);
30 }
31
32 BasicVector(std::initializer_list<T> init)
33 : n_(init.size()),
34 data_(new T[n_]) {
35 std::copy(init.begin(), init.end(), data_.get());
36 }
37
39 if constexpr (std::is_same_v<T, real>) {
40 if (d_data_)
41 cuda::free(d_data_);
42 }
43 }
44
46 : n_(o.n_),
47 data_(new T[n_]) {
48 std::copy_n(o.data_.get(), n_, data_.get());
49 }
50
52 : n_(o.n_),
53 data_(std::move(o.data_)),
54 d_data_(o.d_data_) {
55 o.n_ = 0;
56 o.d_data_ = nullptr;
57 }
58
60 if (this != &o) {
61 n_ = o.n_;
62 data_.reset(new T[n_]);
63 std::copy_n(o.data_.get(), n_, data_.get());
64 }
65 return *this;
66 }
67
69 if (this != &o) {
70 if constexpr (std::is_same_v<T, real>) {
71 if (d_data_)
72 cuda::free(d_data_);
73 }
74 n_ = o.n_;
75 data_ = std::move(o.data_);
76 d_data_ = o.d_data_;
77 o.n_ = 0;
78 o.d_data_ = nullptr;
79 }
80 return *this;
81 }
82
83 constexpr idx size() const noexcept { return n_; }
84
85 BasicVector& vec() { return *this; }
86 const BasicVector& vec() const { return *this; }
87
88 T* data() { return data_.get(); }
89 const T* data() const { return data_.get(); }
90
91 T& operator[](idx i) { return data_[i]; }
92 T operator[](idx i) const { return data_[i]; }
93
94 T* begin() { return data_.get(); }
95 T* end() { return data_.get() + n_; }
96 const T* begin() const { return data_.get(); }
97 const T* end() const { return data_.get() + n_; }
98
99 void to_gpu() {
100 if constexpr (std::is_same_v<T, real>) {
101 if (!d_data_) {
102 d_data_ = cuda::alloc(n_);
103 cuda::to_device(d_data_, data_.get(), n_);
104 }
105 }
106 }
107
108 void to_cpu() {
109 if constexpr (std::is_same_v<T, real>) {
110 if (d_data_) {
111 cuda::to_host(data_.get(), d_data_, n_);
112 cuda::free(d_data_);
113 d_data_ = nullptr;
114 }
115 }
116 }
117
118 real* gpu_data() { return d_data_; }
119 const real* gpu_data() const { return d_data_; }
120 bool on_gpu() const { return d_data_ != nullptr; }
121
122 private:
123 idx n_;
124 std::unique_ptr<T[]> data_;
125 real* d_data_ = nullptr; // GPU mirror (real-typed); always nullptr for T != real
126};
127
128/// @brief Real-valued dense vector with full backend dispatch (CPU + GPU)
130
131/// @brief Complex-valued dense vector (sequential; no GPU)
133
134/// @brief Compute \f$v \leftarrow \alpha v\f$.
135void scale(Vector& v, real alpha, Backend b = default_backend);
136
137/// @brief Compute \f$z=x+y\f$.
138void add(const Vector& x, const Vector& y, Vector& z, Backend b = default_backend);
139
140/// @brief Compute \f$y \leftarrow y+\alpha x\f$.
141void axpy(real alpha, const Vector& x, Vector& y, Backend b = default_backend);
142
143/// @brief Compute \f$x^T y\f$.
144real dot(const Vector& x, const Vector& y, Backend b = default_backend);
145
146/// @brief Compute \f$\|x\|_2\f$.
147real norm(const Vector& x, Backend b = default_backend);
148
149/// @brief Non-owning view of a flat vector as \f$(x_i,y_i)\f$ pairs.
150struct Vec2View {
152
153 idx size() const noexcept { return v.size() / 2; }
154
155 real& x(idx i) noexcept { return v[2 * i]; }
156 real x(idx i) const noexcept { return v[2 * i]; }
157 real& y(idx i) noexcept { return v[2 * i + 1]; }
158 real y(idx i) const noexcept { return v[2 * i + 1]; }
159};
160
162 const Vector& v;
163
164 idx size() const noexcept { return v.size() / 2; }
165 real x(idx i) const noexcept { return v[2 * i]; }
166 real y(idx i) const noexcept { return v[2 * i + 1]; }
167};
168
169/// @brief v *= alpha
170void scale(CVector& v, cplx alpha);
171
172/// @brief y += alpha * x
173void axpy(cplx alpha, const CVector& x, CVector& y);
174
175/// @brief Conjugate inner product <x, y> = Sigma conj(x_i) * y_i
176cplx dot(const CVector& x, const CVector& y);
177
178/// @brief Euclidean norm sqrt(Sigma |v_i|^2)
179real norm(const CVector& x);
180
181} // namespace num
Dense owning vector.
Definition vector.hpp:16
const T * begin() const
Definition vector.hpp:96
const T * end() const
Definition vector.hpp:97
T & operator[](idx i)
Definition vector.hpp:91
const real * gpu_data() const
Definition vector.hpp:119
BasicVector(idx n, T val)
Definition vector.hpp:26
BasicVector & operator=(BasicVector &&o) noexcept
Definition vector.hpp:68
BasicVector & operator=(const BasicVector &o)
Definition vector.hpp:59
const T * data() const
Definition vector.hpp:89
bool on_gpu() const
Definition vector.hpp:120
T operator[](idx i) const
Definition vector.hpp:92
BasicVector(BasicVector &&o) noexcept
Definition vector.hpp:51
BasicVector(idx n)
Definition vector.hpp:22
const BasicVector & vec() const
Definition vector.hpp:86
real * gpu_data()
Definition vector.hpp:118
BasicVector & vec()
Definition vector.hpp:85
constexpr idx size() const noexcept
Definition vector.hpp:83
BasicVector(const BasicVector &o)
Definition vector.hpp:45
BasicVector(std::initializer_list< T > init)
Definition vector.hpp:32
Backend enum and default backend selection.
Core type definitions.
CUDA kernel wrappers.
void to_device(real *dst, const real *src, idx n)
Copy host to device.
void free(real *ptr)
Free device memory.
real * alloc(idx n)
Allocate device memory.
void to_host(real *dst, const real *src, idx n)
Copy device to host.
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
constexpr Backend default_backend
Definition policy.hpp:53
void add(const Vector &x, const Vector &y, Vector &z, Backend b=default_backend)
Compute .
Definition vector.cpp:36
BasicVector< cplx > CVector
Complex-valued dense vector (sequential; no GPU)
Definition vector.hpp:132
const Vector & v
Definition vector.hpp:162
real x(idx i) const noexcept
Definition vector.hpp:165
real y(idx i) const noexcept
Definition vector.hpp:166
idx size() const noexcept
Definition vector.hpp:164
Non-owning view of a flat vector as pairs.
Definition vector.hpp:150
real & y(idx i) noexcept
Definition vector.hpp:157
real x(idx i) const noexcept
Definition vector.hpp:156
idx size() const noexcept
Definition vector.hpp:153
Vector & v
Definition vector.hpp:151
real & x(idx i) noexcept
Definition vector.hpp:155
real y(idx i) const noexcept
Definition vector.hpp:158