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 // Sparse representations (Coo, Dok) store explicit entries only and are
134 // unaffected by the fill value. Dense representations materialize it, so
135 // they must be rebuilt. If no sparse representation is valid, convert the
136 // data to COO first (using the old fill value) so it is not lost.
137 if (!m_storage.is_valid(FormatVector::CpuCoo) && !m_storage.is_valid(FormatVector::CpuDok)) {
138 validate_rw(FormatVector::CpuCoo);
139 }
140 m_storage.invalidate(FormatVector::CpuDense);
141#if defined(SPLA_BUILD_OPENCL)
142 m_storage.invalidate(FormatVector::AccDense);
143#endif
144
145 if constexpr (std::is_same<T, T_INT>::value) m_storage.set_fill_value(value->as_int());
146 if constexpr (std::is_same<T, T_UINT>::value) m_storage.set_fill_value(value->as_uint());
147 if constexpr (std::is_same<T, T_FLOAT>::value) m_storage.set_fill_value(value->as_float());
148
149 return Status::Ok;
150 }
151
153 }
154 template<typename T>
156 auto reduce = resolve_duplicates.template cast_safe<TOpBinary<T, T, T>>();
157
158 if (reduce) {
159 validate_ctor(FormatVector::CpuDok);
160 auto* vec = get<CpuDokVec<T>>();
161 vec->reduce = reduce->function;
162 return Status::Ok;
163 }
164
166 }
167
168 template<typename T>
169 Status TVector<T>::set_int(uint row_id, std::int32_t value) {
170 if (is_valid(FormatVector::CpuDense)) {
171 validate_rwd(FormatVector::CpuDense);
172 get<CpuDenseVec<T>>()->Ax[row_id] = static_cast<T>(value);
173 return Status::Ok;
174 }
175
176 validate_rwd(FormatVector::CpuDok);
177 cpu_dok_vec_add_element(row_id, static_cast<T>(value), *get<CpuDokVec<T>>());
178 return Status::Ok;
179 }
180 template<typename T>
181 Status TVector<T>::set_uint(uint row_id, std::uint32_t value) {
182 if (is_valid(FormatVector::CpuDense)) {
183 validate_rwd(FormatVector::CpuDense);
184 get<CpuDenseVec<T>>()->Ax[row_id] = static_cast<T>(value);
185 return Status::Ok;
186 }
187
188 validate_rwd(FormatVector::CpuDok);
189 cpu_dok_vec_add_element(row_id, static_cast<T>(value), *get<CpuDokVec<T>>());
190 return Status::Ok;
191 }
192 template<typename T>
193 Status TVector<T>::set_float(uint row_id, float value) {
194 if (is_valid(FormatVector::CpuDense)) {
195 validate_rwd(FormatVector::CpuDense);
196 get<CpuDenseVec<T>>()->Ax[row_id] = static_cast<T>(value);
197 return Status::Ok;
198 }
199
200 validate_rwd(FormatVector::CpuDok);
201 cpu_dok_vec_add_element(row_id, static_cast<T>(value), *get<CpuDokVec<T>>());
202 return Status::Ok;
203 }
204
205 template<typename T>
206 Status TVector<T>::get_int(uint row_id, int32_t& value) {
207 validate_rw(FormatVector::CpuDok);
208
209 const auto& Ax = get<CpuDokVec<T>>()->Ax;
210 const auto entry = Ax.find(row_id);
211 value = m_storage.get_fill_value();
212
213 if (entry != Ax.end()) {
214 value = static_cast<T_INT>(entry->second);
215 }
216
217 return Status::Ok;
218 }
219 template<typename T>
220 Status TVector<T>::get_uint(uint row_id, uint32_t& value) {
221 validate_rw(FormatVector::CpuDok);
222
223 const auto& Ax = get<CpuDokVec<T>>()->Ax;
224 const auto entry = Ax.find(row_id);
225 value = m_storage.get_fill_value();
226
227 if (entry != Ax.end()) {
228 value = static_cast<T_UINT>(entry->second);
229 }
230
231 return Status::Ok;
232 }
233 template<typename T>
234 Status TVector<T>::get_float(uint row_id, float& value) {
235 validate_rw(FormatVector::CpuDok);
236
237 const auto& Ax = get<CpuDokVec<T>>()->Ax;
238 const auto entry = Ax.find(row_id);
239 value = m_storage.get_fill_value();
240
241 if (entry != Ax.end()) {
242 value = static_cast<T_FLOAT>(entry->second);
243 }
244
245 return Status::Ok;
246 }
247
248 template<typename T>
250 validate_wd(FormatVector::CpuDense);
251 auto& Ax = get<CpuDenseVec<T>>()->Ax;
252 auto engine = std::default_random_engine(seed);
253
254 if constexpr (std::is_integral_v<T>) {
255 std::uniform_int_distribution<T> dist;
256 for (auto& x : Ax) x = dist(engine);
257 }
258 if constexpr (std::is_floating_point_v<T>) {
259 std::uniform_real_distribution<T> dist;
260 for (auto& x : Ax) x = dist(engine);
261 }
262
263 return Status::Ok;
264 }
265 template<typename T>
267 assert(value);
268
269 T t = T();
270
271 if constexpr (std::is_same<T, T_INT>::value) t = value->as_int();
272 if constexpr (std::is_same<T, T_UINT>::value) t = value->as_uint();
273 if constexpr (std::is_same<T, T_FLOAT>::value) t = value->as_float();
274
275 validate_wd(FormatVector::CpuDense);
276 auto& Ax = get<CpuDenseVec<T>>()->Ax;
277 std::fill(Ax.begin(), Ax.end(), t);
278
279 return Status::Ok;
280 }
281
282 template<typename T>
284 assert(keys);
285 assert(values);
286
287 const auto key_size = sizeof(uint);
288 const auto value_size = sizeof(T);
289 const auto elements_count = keys->get_size() / key_size;
290
291 if (elements_count != values->get_size() / value_size) {
293 }
294 if (elements_count * key_size != keys->get_size()) {
296 }
297
298 validate_rwd(FormatVector::CpuCoo);
299 CpuCooVec<T>& coo = *get<CpuCooVec<T>>();
300
301 coo.Ai.resize(elements_count);
302 coo.Ax.resize(elements_count);
303 coo.values = uint(elements_count);
304
305 keys->read(0, key_size * elements_count, coo.Ai.data());
306 values->read(0, value_size * elements_count, coo.Ax.data());
307
308 return Status::Ok;
309 }
310 template<typename T>
312 const auto key_size = sizeof(uint);
313 const auto value_size = sizeof(T);
314
315 validate_rw(FormatVector::CpuCoo);
316 CpuCooVec<T>& coo = *get<CpuCooVec<T>>();
317
318 const auto elements_count = coo.Ai.size();
319
320 keys = MemView::make(coo.Ai.data(), key_size * elements_count, false);
321 values = MemView::make(coo.Ax.data(), value_size * elements_count, false);
322
323 return Status::Ok;
324 }
325
326 template<typename T>
328 m_storage.invalidate();
329 return Status::Ok;
330 }
331
332 template<typename T>
334 StorageManagerVector<T>* manager = get_storage_manager();
335 manager->validate_rw(format, m_storage);
336 }
337
338 template<typename T>
340 StorageManagerVector<T>* manager = get_storage_manager();
341 manager->validate_rwd(format, m_storage);
342 }
343
344 template<typename T>
346 StorageManagerVector<T>* manager = get_storage_manager();
347 manager->validate_wd(format, m_storage);
348 }
349
350 template<typename T>
352 StorageManagerVector<T>* manager = get_storage_manager();
353 manager->validate_ctor(format, m_storage);
354 }
355
356 template<typename T>
358 return m_storage.is_valid(format);
359 }
360
361 template<typename T>
363 static std::unique_ptr<StorageManagerVector<T>> storage_manager;
364
365 if (!storage_manager) {
366 storage_manager = std::make_unique<StorageManagerVector<T>>();
367 register_formats_vector(*storage_manager);
368 }
369
370 return storage_manager.get();
371 }
372
377}// namespace spla
378
379#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:93
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:249
Status build(const ref_ptr< MemView > &keys, const ref_ptr< MemView > &values) override
Definition tvector.hpp:283
void validate_rwd(FormatVector format)
Definition tvector.hpp:339
uint get_n_rows() override
Definition tvector.hpp:107
Status set_int(uint row_id, std::int32_t value) override
Definition tvector.hpp:169
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:193
Status get_float(uint row_id, float &value) override
Definition tvector.hpp:234
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:206
ref_ptr< Type > get_type() override
Definition tvector.hpp:111
bool is_valid(FormatVector format) const
Definition tvector.hpp:357
void validate_wd(FormatVector format)
Definition tvector.hpp:345
Status read(ref_ptr< MemView > &keys, ref_ptr< MemView > &values) override
Definition tvector.hpp:311
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:333
Status get_uint(uint row_id, uint32_t &value) override
Definition tvector.hpp:220
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:327
void validate_ctor(FormatVector format)
Definition tvector.hpp:351
Status fill_with(const ref_ptr< Scalar > &value) override
Definition tvector.hpp:266
static StorageManagerVector< T > * get_storage_manager()
Definition tvector.hpp:362
Status set_uint(uint row_id, std::uint32_t value) override
Definition tvector.hpp:181
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:155
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