spla
storage_manager_vector.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_STORAGE_MAANGER_VECTOR_HPP
29 #define SPLA_STORAGE_MAANGER_VECTOR_HPP
30 
32 
36 #include <cpu/cpu_formats.hpp>
37 
38 #if defined(SPLA_BUILD_OPENCL)
39  #include <opencl/cl_accelerator.hpp>
42  #include <opencl/cl_formats.hpp>
43 #endif
44 
45 namespace spla {
46 
47  template<typename T>
49 
50  template<typename T>
52  using Storage = typename StorageManagerVector<T>::Storage;
53 
54  manager.register_constructor(FormatVector::CpuDok, [](Storage& s) {
56  });
57  manager.register_constructor(FormatVector::CpuCoo, [](Storage& s) {
59  });
60  manager.register_constructor(FormatVector::CpuDense, [](Storage& s) {
62  cpu_dense_vec_resize(s.get_n_rows(), *s.template get<CpuDenseVec<T>>());
63  });
64 
65  manager.register_validator_discard(FormatVector::CpuDok, [](Storage& s) {
66  cpu_dok_vec_clear(*s.template get<CpuDokVec<T>>());
67  });
68  manager.register_validator_discard(FormatVector::CpuCoo, [](Storage& s) {
69  cpu_coo_vec_clear(*s.template get<CpuCooVec<T>>());
70  });
71  manager.register_validator(FormatVector::CpuDense, [](Storage& s) {
72  cpu_dense_vec_fill(s.get_fill_value(), *s.template get<CpuDenseVec<T>>());
73  });
74 
76  auto* dok = s.template get<CpuDokVec<T>>();
77  auto* coo = s.template get<CpuCooVec<T>>();
78  cpu_coo_vec_resize(dok->values, *coo);
79  cpu_dok_vec_to_coo(*dok, *coo);
80  });
82  auto* dok = s.template get<CpuDokVec<T>>();
83  auto* dense = s.template get<CpuDenseVec<T>>();
84  cpu_dense_vec_fill(s.get_fill_value(), *dense);
85  cpu_dok_vec_to_dense(s.get_n_rows(), *dok, *dense);
86  });
88  auto* coo = s.template get<CpuCooVec<T>>();
89  auto* dok = s.template get<CpuDokVec<T>>();
90  cpu_dok_vec_clear(*dok);
91  cpu_coo_vec_to_dok(*coo, *dok);
92  });
94  auto* dense = s.template get<CpuDenseVec<T>>();
95  auto* dok = s.template get<CpuDokVec<T>>();
96  cpu_dok_vec_clear(*dok);
97  cpu_dense_vec_to_dok(s.get_n_rows(), s.get_fill_value(), *dense, *dok);
98  });
99 
100 
101 #if defined(SPLA_BUILD_OPENCL)
102  manager.register_constructor(FormatVector::AccCoo, [](Storage& s) {
103  s.get_ref(FormatVector::AccCoo) = make_ref<CLCooVec<T>>();
104  });
105  manager.register_constructor(FormatVector::AccDense, [](Storage& s) {
106  s.get_ref(FormatVector::AccDense) = make_ref<CLDenseVec<T>>();
107  auto* cl_dense = s.template get<CLDenseVec<T>>();
108  cl_dense_vec_resize(s.get_n_rows(), *cl_dense);
109  });
110 
111  manager.register_validator(FormatVector::AccCoo, [](Storage& s) {
112  auto* cl_coo = s.template get<CLCooVec<T>>();
113  cl_coo_vec_clear(*cl_coo);
114  });
115  manager.register_validator(FormatVector::AccDense, [](Storage& s) {
116  auto* cl_dense = s.template get<CLDenseVec<T>>();
117  cl_dense_vec_fill_value(s.get_n_rows(), s.get_fill_value(), *cl_dense);
118  });
119 
121  auto* cpu_dense = s.template get<CpuDenseVec<T>>();
122  auto* cl_dense = s.template get<CLDenseVec<T>>();
123  cl_dense_vec_init(s.get_n_rows(), cpu_dense->Ax.data(), *cl_dense);
124  });
126  auto* cl_acc = get_acc_cl();
127  auto* cl_dense = s.template get<CLDenseVec<T>>();
128  auto* cpu_dense = s.template get<CpuDenseVec<T>>();
129  if (!cl_acc->is_img()) {
130  cl_dense_vec_read(s.get_n_rows(), cpu_dense->Ax.data(), *cl_dense, cl_acc->get_queue_default());
131  } else {
132  // On Imagination Technologies devices copying data to staging buffer created with CL_MEM_READ_ONLY flag does not affect this buffer.
133  // According to the [documentation](https://registry.khronos.org/OpenCL/specs/3.0-unified/html/OpenCL_API.html#clEnqueueCopyBuffer),
134  // this flag should not affect copying, but you have to create a buffer without this flag to keep it correct.
135  cl_dense_vec_read(s.get_n_rows(), cpu_dense->Ax.data(), *cl_dense, cl_acc->get_queue_default(),
136  CL_MEM_HOST_READ_ONLY | CL_MEM_ALLOC_HOST_PTR);
137  }
138  });
140  auto* cpu_coo = s.template get<CpuCooVec<T>>();
141  auto* cl_coo = s.template get<CLCooVec<T>>();
142  cl_coo_vec_init(cpu_coo->values, cpu_coo->Ai.data(), cpu_coo->Ax.data(), *cl_coo);
143  });
145  auto* cl_acc = get_acc_cl();
146  auto* cl_coo = s.template get<CLCooVec<T>>();
147  auto* cpu_coo = s.template get<CpuCooVec<T>>();
148  cpu_coo_vec_resize(cl_coo->values, *cpu_coo);
149  if (!cl_acc->is_img()) {
150  cl_coo_vec_read(cl_coo->values, cpu_coo->Ai.data(), cpu_coo->Ax.data(), *cl_coo, cl_acc->get_queue_default());
151  } else {
152  // On Imagination Technologies devices copying data to staging buffer created with CL_MEM_READ_ONLY flag does not affect this buffer.
153  // According to the [documentation](https://registry.khronos.org/OpenCL/specs/3.0-unified/html/OpenCL_API.html#clEnqueueCopyBuffer),
154  // this flag should not affect copying, but you have to create a buffer without this flag to keep it correct.
155  cl_coo_vec_read(cl_coo->values, cpu_coo->Ai.data(), cpu_coo->Ax.data(), *cl_coo, cl_acc->get_queue_default(),
156  CL_MEM_HOST_READ_ONLY | CL_MEM_ALLOC_HOST_PTR);
157  }
158  });
160  auto* cl_acc = get_acc_cl();
161  auto* cl_coo = s.template get<CLCooVec<T>>();
162  auto* cl_dense = s.template get<CLDenseVec<T>>();
163  cl_coo_vec_to_dense(s.get_n_rows(), s.get_fill_value(), *cl_coo, *cl_dense, cl_acc->get_queue_default());
164  });
166  auto* cl_acc = get_acc_cl();
167  auto* cl_dense = s.template get<CLDenseVec<T>>();
168  auto* cl_coo = s.template get<CLCooVec<T>>();
169  cl_dense_vec_to_coo(s.get_n_rows(), s.get_fill_value(), *cl_dense, *cl_coo, cl_acc->get_queue_default());
170  });
171 #endif
172  }
173 
174 }// namespace spla
175 
176 #endif//SPLA_STORAGE_MAANGER_VECTOR_HPP
CPU list-of-coordinates sparse vector representation.
Definition: cpu_formats.hpp:90
CPU one-dim array for dense vector representation.
Definition: cpu_formats.hpp:74
Definition: cpu_formats.hpp:55
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 cl_coo_vec_read(const std::size_t n_values, uint *Ai, T *Ax, const CLCooVec< T > &storage, cl::CommandQueue &queue, cl_mem_flags staging_flags=CL_MEM_READ_ONLY|CL_MEM_HOST_READ_ONLY|CL_MEM_ALLOC_HOST_PTR, bool blocking=true)
Definition: cl_format_coo_vec.hpp:103
void cpu_coo_vec_clear(CpuCooVec< T > &vec)
Definition: cpu_format_coo_vec.hpp:69
void cpu_dok_vec_to_dense(const uint n_rows, const CpuDokVec< T > &in, CpuDenseVec< T > &out)
Definition: cpu_format_dok_vec.hpp:72
void cl_dense_vec_to_coo(const std::size_t n_rows, const T fill_value, const CLDenseVec< T > &in, CLCooVec< T > &out, cl::CommandQueue &queue)
Definition: cl_format_dense_vec.hpp:90
void register_converter(F from, F to, Function function)
Definition: storage_manager.hpp:112
void cl_coo_vec_to_dense(const std::size_t n_rows, const T fill_value, const CLCooVec< T > &in, CLDenseVec< T > &out, cl::CommandQueue &queue)
Definition: cl_format_coo_vec.hpp:128
void cl_coo_vec_clear(CLCooVec< T > &storage)
Definition: cl_format_coo_vec.hpp:96
void cpu_coo_vec_resize(const uint n_values, CpuCooVec< T > &vec)
Definition: cpu_format_coo_vec.hpp:61
void cl_dense_vec_read(const std::size_t n_rows, T *values, CLDenseVec< T > &storage, cl::CommandQueue &queue, cl_mem_flags staging_flags=CL_MEM_READ_ONLY|CL_MEM_HOST_READ_ONLY|CL_MEM_ALLOC_HOST_PTR, bool blocking=true)
Definition: cl_format_dense_vec.hpp:76
void cl_dense_vec_init(const std::size_t n_rows, const T *values, CLDenseVec< T > &storage)
Definition: cl_format_dense_vec.hpp:63
void cl_dense_vec_fill_value(const std::size_t n_rows, const T value, CLDenseVec< T > &storage)
Definition: cl_format_dense_vec.hpp:56
void cl_coo_vec_init(const std::size_t n_values, const uint *Ai, const T *Ax, CLCooVec< T > &storage)
Definition: cl_format_coo_vec.hpp:45
void cpu_dense_vec_fill(const T fill_value, CpuDenseVec< T > &vec)
Definition: cpu_format_dense_vec.hpp:48
void cpu_dok_vec_clear(CpuDokVec< T > &vec)
Definition: cpu_format_dok_vec.hpp:99
void cpu_dense_vec_resize(const uint n_rows, CpuDenseVec< T > &vec)
Definition: cpu_format_dense_vec.hpp:41
void cpu_dok_vec_to_coo(const CpuDokVec< T > &in, CpuCooVec< T > &out)
Definition: cpu_format_dok_vec.hpp:43
void cl_dense_vec_resize(const std::size_t n_rows, CLDenseVec< T > &storage)
Definition: cl_format_dense_vec.hpp:46
void register_validator(F format, Function function)
Definition: storage_manager.hpp:97
void cpu_dense_vec_to_dok(const uint n_rows, const T fill_value, const CpuDenseVec< T > &in, CpuDokVec< T > &out)
Definition: cpu_format_dense_vec.hpp:54
void register_validator_discard(F format, Function function)
Definition: storage_manager.hpp:107
void cpu_coo_vec_to_dok(const CpuCooVec< T > &in, CpuDokVec< T > &out)
Definition: cpu_format_coo_vec.hpp:76
void register_constructor(F format, Function function)
Definition: storage_manager.hpp:92
ref_ptr< T > make_ref(TArgs &&... args)
Definition: ref.hpp:246
Definition: algorithm.hpp:37
void register_formats_vector(StorageManagerVector< T > &manager)
Definition: storage_manager_vector.hpp:51