spla
Loading...
Searching...
No Matches
tvector.hpp
Go to the documentation of this file.
1/**********************************************************************************/
2/* This file is part of spla project */
3/* https://github.com/SparseLinearAlgebra/spla */
4/**********************************************************************************/
5/* MIT License */
6/* */
7/* Copyright (c) 2023 SparseLinearAlgebra */
8/* */
9/* Permission is hereby granted, free of charge, to any person obtaining a copy */
10/* of this software and associated documentation files (the "Software"), to deal */
11/* in the Software without restriction, including without limitation the rights */
12/* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
13/* copies of the Software, and to permit persons to whom the Software is */
14/* furnished to do so, subject to the following conditions: */
15/* */
16/* The above copyright notice and this permission notice shall be included in all */
17/* copies or substantial portions of the Software. */
18/* */
19/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
20/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
21/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
22/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
23/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
24/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
25/* SOFTWARE. */
26/**********************************************************************************/
27
28#ifndef SPLA_TVECTOR_HPP
29#define SPLA_TVECTOR_HPP
30
31#include <spla/config.hpp>
32#include <spla/vector.hpp>
33
34#include <core/logger.hpp>
35#include <core/tarray.hpp>
36#include <core/tdecoration.hpp>
37#include <core/top.hpp>
38#include <core/ttype.hpp>
39
42
43#include <algorithm>
44#include <random>
45
46namespace spla {
47
59 template<typename T>
60 class TVector final : public Vector {
61 public:
62 explicit TVector(uint n_rows);
63 ~TVector() override = default;
64
65 uint get_n_rows() override;
66 ref_ptr<Type> get_type() override;
67 void set_label(std::string label) override;
68 const std::string& get_label() const override;
69 Status set_format(FormatVector format) override;
70 Status set_fill_value(const ref_ptr<Scalar>& value) override;
71 Status set_reduce(ref_ptr<OpBinary> resolve_duplicates) override;
72 Status set_int(uint row_id, std::int32_t value) override;
73 Status set_uint(uint row_id, std::uint32_t value) override;
74 Status set_float(uint row_id, float value) override;
75 Status get_int(uint row_id, int32_t& value) override;
76 Status get_uint(uint row_id, uint32_t& value) override;
77 Status get_float(uint row_id, float& value) override;
78 Status fill_noize(uint seed) override;
79 Status fill_with(const ref_ptr<Scalar>& value) override;
80 Status build(const ref_ptr<MemView>& keys, const ref_ptr<MemView>& values) override;
81 Status read(ref_ptr<MemView>& keys, ref_ptr<MemView>& values) override;
82 Status clear() override;
83
84 template<typename Decorator>
85 Decorator* get() { return m_storage.template get<Decorator>(); }
86
87 void validate_rw(FormatVector format);
88 void validate_rwd(FormatVector format);
89 void validate_wd(FormatVector format);
90 void validate_ctor(FormatVector format);
91 bool is_valid(FormatVector format) const;
92 T get_fill_value() const { return m_storage.get_fill_value(); }
93
95
96 private:
97 typename StorageManagerVector<T>::Storage m_storage;
98 std::string m_label;
99 };
100
101 template<typename T>
103 m_storage.set_dims(n_rows, 1);
104 }
105
106 template<typename T>
108 return m_storage.get_n_rows();
109 }
110 template<typename T>
112 return get_ttype<T>().template as<Type>();
113 }
114
115 template<typename T>
116 void TVector<T>::set_label(std::string label) {
117 m_label = std::move(label);
118 LOG_MSG(Status::Ok, "set label '" << m_label << "' to " << (void*) this);
119 }
120 template<typename T>
121 const std::string& TVector<T>::get_label() const {
122 return m_label;
123 }
124
125 template<typename T>
127 validate_rw(format);
128 return Status::Ok;
129 }
130 template<typename T>
132 if (value) {
133 m_storage.invalidate();
134
135 if constexpr (std::is_same<T, T_INT>::value) m_storage.set_fill_value(value->as_int());
136 if constexpr (std::is_same<T, T_UINT>::value) m_storage.set_fill_value(value->as_uint());
137 if constexpr (std::is_same<T, T_FLOAT>::value) m_storage.set_fill_value(value->as_float());
138
139 return Status::Ok;
140 }
141
143 }
144 template<typename T>
146 auto reduce = resolve_duplicates.template cast_safe<TOpBinary<T, T, T>>();
147
148 if (reduce) {
149 validate_ctor(FormatVector::CpuDok);
150 auto* vec = get<CpuDokVec<T>>();
151 vec->reduce = reduce->function;
152 return Status::Ok;
153 }
154
156 }
157
158 template<typename T>
159 Status TVector<T>::set_int(uint row_id, std::int32_t value) {
160 if (is_valid(FormatVector::CpuDense)) {
161 validate_rwd(FormatVector::CpuDense);
162 get<CpuDenseVec<T>>()->Ax[row_id] = static_cast<T>(value);
163 return Status::Ok;
164 }
165
166 validate_rwd(FormatVector::CpuDok);
167 cpu_dok_vec_add_element(row_id, static_cast<T>(value), *get<CpuDokVec<T>>());
168 return Status::Ok;
169 }
170 template<typename T>
171 Status TVector<T>::set_uint(uint row_id, std::uint32_t value) {
172 if (is_valid(FormatVector::CpuDense)) {
173 validate_rwd(FormatVector::CpuDense);
174 get<CpuDenseVec<T>>()->Ax[row_id] = static_cast<T>(value);
175 return Status::Ok;
176 }
177
178 validate_rwd(FormatVector::CpuDok);
179 cpu_dok_vec_add_element(row_id, static_cast<T>(value), *get<CpuDokVec<T>>());
180 return Status::Ok;
181 }
182 template<typename T>
183 Status TVector<T>::set_float(uint row_id, float value) {
184 if (is_valid(FormatVector::CpuDense)) {
185 validate_rwd(FormatVector::CpuDense);
186 get<CpuDenseVec<T>>()->Ax[row_id] = static_cast<T>(value);
187 return Status::Ok;
188 }
189
190 validate_rwd(FormatVector::CpuDok);
191 cpu_dok_vec_add_element(row_id, static_cast<T>(value), *get<CpuDokVec<T>>());
192 return Status::Ok;
193 }
194
195 template<typename T>
196 Status TVector<T>::get_int(uint row_id, int32_t& value) {
197 validate_rw(FormatVector::CpuDok);
198
199 const auto& Ax = get<CpuDokVec<T>>()->Ax;
200 const auto entry = Ax.find(row_id);
201 value = m_storage.get_fill_value();
202
203 if (entry != Ax.end()) {
204 value = static_cast<T_INT>(entry->second);
205 }
206
207 return Status::Ok;
208 }
209 template<typename T>
210 Status TVector<T>::get_uint(uint row_id, uint32_t& value) {
211 validate_rw(FormatVector::CpuDok);
212
213 const auto& Ax = get<CpuDokVec<T>>()->Ax;
214 const auto entry = Ax.find(row_id);
215 value = m_storage.get_fill_value();
216
217 if (entry != Ax.end()) {
218 value = static_cast<T_UINT>(entry->second);
219 }
220
221 return Status::Ok;
222 }
223 template<typename T>
224 Status TVector<T>::get_float(uint row_id, float& value) {
225 validate_rw(FormatVector::CpuDok);
226
227 const auto& Ax = get<CpuDokVec<T>>()->Ax;
228 const auto entry = Ax.find(row_id);
229 value = m_storage.get_fill_value();
230
231 if (entry != Ax.end()) {
232 value = static_cast<T_FLOAT>(entry->second);
233 }
234
235 return Status::Ok;
236 }
237
238 template<typename T>
240 validate_wd(FormatVector::CpuDense);
241 auto& Ax = get<CpuDenseVec<T>>()->Ax;
242 auto engine = std::default_random_engine(seed);
243
244 if constexpr (std::is_integral_v<T>) {
245 std::uniform_int_distribution<T> dist;
246 for (auto& x : Ax) x = dist(engine);
247 }
248 if constexpr (std::is_floating_point_v<T>) {
249 std::uniform_real_distribution<T> dist;
250 for (auto& x : Ax) x = dist(engine);
251 }
252
253 return Status::Ok;
254 }
255 template<typename T>
257 assert(value);
258
259 T t = T();
260
261 if constexpr (std::is_same<T, T_INT>::value) t = value->as_int();
262 if constexpr (std::is_same<T, T_UINT>::value) t = value->as_uint();
263 if constexpr (std::is_same<T, T_FLOAT>::value) t = value->as_float();
264
265 validate_wd(FormatVector::CpuDense);
266 auto& Ax = get<CpuDenseVec<T>>()->Ax;
267 std::fill(Ax.begin(), Ax.end(), t);
268
269 return Status::Ok;
270 }
271
272 template<typename T>
274 assert(keys);
275 assert(values);
276
277 const auto key_size = sizeof(uint);
278 const auto value_size = sizeof(T);
279 const auto elements_count = keys->get_size() / key_size;
280
281 if (elements_count != values->get_size() / value_size) {
283 }
284 if (elements_count * key_size != keys->get_size()) {
286 }
287
288 validate_rwd(FormatVector::CpuCoo);
289 CpuCooVec<T>& coo = *get<CpuCooVec<T>>();
290
291 coo.Ai.resize(elements_count);
292 coo.Ax.resize(elements_count);
293 coo.values = uint(elements_count);
294
295 keys->read(0, key_size * elements_count, coo.Ai.data());
296 values->read(0, value_size * elements_count, coo.Ax.data());
297
298 return Status::Ok;
299 }
300 template<typename T>
302 const auto key_size = sizeof(uint);
303 const auto value_size = sizeof(T);
304
305 validate_rw(FormatVector::CpuCoo);
306 CpuCooVec<T>& coo = *get<CpuCooVec<T>>();
307
308 const auto elements_count = coo.Ai.size();
309
310 keys = MemView::make(coo.Ai.data(), key_size * elements_count, false);
311 values = MemView::make(coo.Ax.data(), value_size * elements_count, false);
312
313 return Status::Ok;
314 }
315
316 template<typename T>
318 m_storage.invalidate();
319 return Status::Ok;
320 }
321
322 template<typename T>
324 StorageManagerVector<T>* manager = get_storage_manager();
325 manager->validate_rw(format, m_storage);
326 }
327
328 template<typename T>
330 StorageManagerVector<T>* manager = get_storage_manager();
331 manager->validate_rwd(format, m_storage);
332 }
333
334 template<typename T>
336 StorageManagerVector<T>* manager = get_storage_manager();
337 manager->validate_wd(format, m_storage);
338 }
339
340 template<typename T>
342 StorageManagerVector<T>* manager = get_storage_manager();
343 manager->validate_ctor(format, m_storage);
344 }
345
346 template<typename T>
348 return m_storage.is_valid(format);
349 }
350
351 template<typename T>
353 static std::unique_ptr<StorageManagerVector<T>> storage_manager;
354
355 if (!storage_manager) {
356 storage_manager = std::make_unique<StorageManagerVector<T>>();
357 register_formats_vector(*storage_manager);
358 }
359
360 return storage_manager.get();
361 }
362
367}// namespace spla
368
369#endif//SPLA_TVECTOR_HPP
Named storage format for library vector data objects.
Status of library operation execution.
CPU list-of-coordinates sparse vector representation.
Definition cpu_formats.hpp:90
std::vector< uint > Ai
Definition cpu_formats.hpp:96
std::vector< T > Ax
Definition cpu_formats.hpp:97
Definition cpu_formats.hpp:55
static ref_ptr< MemView > make()
Definition memview.cpp:67
General format converter for vector or matrix decoration storage.
Definition storage_manager.hpp:57
Storage for decorators with data of a particular vector or matrix object.
Definition tdecoration.hpp:70
void set_dims(uint n_rows, uint n_cols)
Definition tdecoration.hpp:92
uint values
Definition tdecoration.hpp:58
Vector interface implementation with type information bound.
Definition tvector.hpp:60
Decorator * get()
Definition tvector.hpp:85
T get_fill_value() const
Definition tvector.hpp:92
~TVector() override=default
Generalized N dimensional vector object.
Definition vector.hpp:48
Automates reference counting and behaves as shared smart pointer.
Definition ref.hpp:117
Status fill_noize(uint seed) override
Definition tvector.hpp:239
Status build(const ref_ptr< MemView > &keys, const ref_ptr< MemView > &values) override
Definition tvector.hpp:273
void validate_rwd(FormatVector format)
Definition tvector.hpp:329
uint get_n_rows() override
Definition tvector.hpp:107
Status set_int(uint row_id, std::int32_t value) override
Definition tvector.hpp:159
void set_label(std::string label) override
Definition tvector.hpp:116
void validate_wd(F format, Storage &storage)
Definition storage_manager.hpp:212
Status set_float(uint row_id, float value) override
Definition tvector.hpp:183
Status get_float(uint row_id, float &value) override
Definition tvector.hpp:224
void validate_rw(F format, Storage &storage)
Definition storage_manager.hpp:128
Status get_int(uint row_id, int32_t &value) override
Definition tvector.hpp:196
ref_ptr< Type > get_type() override
Definition tvector.hpp:111
bool is_valid(FormatVector format) const
Definition tvector.hpp:347
void validate_wd(FormatVector format)
Definition tvector.hpp:335
Status read(ref_ptr< MemView > &keys, ref_ptr< MemView > &values) override
Definition tvector.hpp:301
TVector(uint n_rows)
Definition tvector.hpp:102
Status set_format(FormatVector format) override
Definition tvector.hpp:126
const std::string & get_label() const override
Definition tvector.hpp:121
void validate_ctor(F format, Storage &storage)
Definition storage_manager.hpp:121
void validate_rwd(F format, Storage &storage)
Definition storage_manager.hpp:206
void validate_rw(FormatVector format)
Definition tvector.hpp:323
Status get_uint(uint row_id, uint32_t &value) override
Definition tvector.hpp:210
void cpu_dok_vec_add_element(uint row_id, T element, CpuDokVec< T > &vec)
Definition cpu_format_dok_vec.hpp:85
Status clear() override
Definition tvector.hpp:317
void validate_ctor(FormatVector format)
Definition tvector.hpp:341
Status fill_with(const ref_ptr< Scalar > &value) override
Definition tvector.hpp:256
static StorageManagerVector< T > * get_storage_manager()
Definition tvector.hpp:352
Status set_uint(uint row_id, std::uint32_t value) override
Definition tvector.hpp:171
Status set_fill_value(const ref_ptr< Scalar > &value) override
Definition tvector.hpp:131
Status set_reduce(ref_ptr< OpBinary > resolve_duplicates) override
Definition tvector.hpp:145
float T_FLOAT
Definition type.hpp:60
std::int32_t T_INT
Definition type.hpp:58
std::uint32_t uint
Library index and size type.
Definition config.hpp:56
std::uint32_t T_UINT
Definition type.hpp:59
#define LOG_MSG(status, msg)
Definition logger.hpp:66
Definition algorithm.hpp:37
void register_formats_vector(StorageManagerVector< T > &manager)
Definition storage_manager_vector.hpp:51