spla
storage_manager_matrix.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_MANAGER_MATRIX_HPP
29 #define SPLA_STORAGE_MANAGER_MATRIX_HPP
30 
32 
33 #include <cpu/cpu_format_coo.hpp>
34 #include <cpu/cpu_format_csr.hpp>
35 #include <cpu/cpu_format_dok.hpp>
36 #include <cpu/cpu_format_lil.hpp>
37 #include <cpu/cpu_formats.hpp>
38 
39 #if defined(SPLA_BUILD_OPENCL)
40  #include <opencl/cl_accelerator.hpp>
41  #include <opencl/cl_format_csr.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 StorageManagerMatrix<T>::Storage;
53 
54  manager.register_constructor(FormatMatrix::CpuLil, [](Storage& s) {
55  s.get_ref(FormatMatrix::CpuLil) = make_ref<CpuLil<T>>();
56  });
57  manager.register_constructor(FormatMatrix::CpuDok, [](Storage& s) {
58  s.get_ref(FormatMatrix::CpuDok) = make_ref<CpuDok<T>>();
59  });
60  manager.register_constructor(FormatMatrix::CpuCoo, [](Storage& s) {
61  s.get_ref(FormatMatrix::CpuCoo) = make_ref<CpuCoo<T>>();
62  });
63  manager.register_constructor(FormatMatrix::CpuCsr, [](Storage& s) {
64  s.get_ref(FormatMatrix::CpuCsr) = make_ref<CpuCsr<T>>();
65  });
66 
67  manager.register_validator_discard(FormatMatrix::CpuLil, [](Storage& s) {
68  auto* lil = s.template get<CpuLil<T>>();
69  cpu_lil_resize(s.get_n_rows(), *lil);
70  cpu_lil_clear(*lil);
71  });
72  manager.register_validator_discard(FormatMatrix::CpuDok, [](Storage& s) {
73  auto* dok = s.template get<CpuDok<T>>();
74  cpu_dok_clear(*dok);
75  });
76  manager.register_validator_discard(FormatMatrix::CpuCoo, [](Storage& s) {
77  auto* coo = s.template get<CpuCoo<T>>();
78  cpu_coo_clear(*coo);
79  });
80 
82  auto* lil = s.template get<CpuLil<T>>();
83  auto* dok = s.template get<CpuDok<T>>();
84  cpu_dok_clear(*dok);
85  cpu_lil_to_dok(s.get_n_rows(), *lil, *dok);
86  });
88  auto* lil = s.template get<CpuLil<T>>();
89  auto* coo = s.template get<CpuCoo<T>>();
90  cpu_coo_resize(lil->values, *coo);
91  cpu_lil_to_coo(s.get_n_rows(), *lil, *coo);
92  });
94  auto* lil = s.template get<CpuLil<T>>();
95  auto* csr = s.template get<CpuCsr<T>>();
96  cpu_csr_resize(s.get_n_rows(), lil->values, *csr);
97  cpu_lil_to_csr(s.get_n_rows(), *lil, *csr);
98  });
99 
101  auto* coo = s.template get<CpuCoo<T>>();
102  auto* lil = s.template get<CpuLil<T>>();
103  cpu_lil_clear(*lil);
104  cpu_lil_resize(s.get_n_rows(), *lil);
105  cpu_coo_to_lil(s.get_n_rows(), *coo, *lil);
106  });
108  auto* coo = s.template get<CpuCoo<T>>();
109  auto* dok = s.template get<CpuDok<T>>();
110  cpu_dok_clear(*dok);
111  cpu_coo_to_dok(*coo, *dok);
112  });
114  auto* coo = s.template get<CpuCoo<T>>();
115  auto* csr = s.template get<CpuCsr<T>>();
116  cpu_csr_resize(s.get_n_rows(), coo->values, *csr);
117  cpu_coo_to_csr(s.get_n_rows(), *coo, *csr);
118  });
119 
121  auto* csr = s.template get<CpuCsr<T>>();
122  auto* dok = s.template get<CpuDok<T>>();
123  cpu_dok_clear(*dok);
124  cpu_csr_to_dok(s.get_n_rows(), *csr, *dok);
125  });
127  auto* csr = s.template get<CpuCsr<T>>();
128  auto* coo = s.template get<CpuCoo<T>>();
129  cpu_coo_resize(csr->values, *coo);
130  cpu_csr_to_coo(s.get_n_rows(), *csr, *coo);
131  });
132 
133 #if defined(SPLA_BUILD_OPENCL)
134  manager.register_constructor(FormatMatrix::AccCsr, [](Storage& s) {
135  s.get_ref(FormatMatrix::AccCsr) = make_ref<CLCsr<T>>();
136  });
137 
139  auto* cpu_csr = s.template get<CpuCsr<T>>();
140  auto* cl_csr = s.template get<CLCsr<T>>();
141  cl_csr_init(s.get_n_rows(), cpu_csr->values, cpu_csr->Ap.data(), cpu_csr->Aj.data(), cpu_csr->Ax.data(), *cl_csr);
142  });
143 
145  auto* cl_acc = get_acc_cl();
146  auto* cl_csr = s.template get<CLCsr<T>>();
147  auto* cpu_csr = s.template get<CpuCsr<T>>();
148  cpu_csr_resize(s.get_n_rows(), cl_csr->values, *cpu_csr);
149  if (!cl_acc->is_img()) {
150  cl_csr_read(s.get_n_rows(), cl_csr->values, cpu_csr->Ap.data(), cpu_csr->Aj.data(), cpu_csr->Ax.data(), *cl_csr, 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_csr_read(s.get_n_rows(), cl_csr->values, cpu_csr->Ap.data(), cpu_csr->Aj.data(), cpu_csr->Ax.data(), *cl_csr, cl_acc->get_queue_default(),
156  CL_MEM_HOST_READ_ONLY | CL_MEM_ALLOC_HOST_PTR);
157  }
158  });
159 #endif
160  }
161 
162 }// namespace spla
163 
164 #endif//SPLA_STORAGE_MANAGER_MATRIX_HPP
CPU list of coordinates matrix format.
Definition: cpu_formats.hpp:148
CPU compressed sparse row matrix format.
Definition: cpu_formats.hpp:166
Dictionary of keys sparse matrix format.
Definition: cpu_formats.hpp:128
CPU list-of-list matrix format for fast incremental build.
Definition: cpu_formats.hpp:107
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 cpu_coo_to_csr(uint n_rows, const CpuCoo< T > &in, CpuCsr< T > &out)
Definition: cpu_format_coo.hpp:96
void cpu_dok_clear(CpuDok< T > &storage)
Definition: cpu_format_dok.hpp:41
void register_converter(F from, F to, Function function)
Definition: storage_manager.hpp:112
void cpu_coo_resize(const uint n_values, CpuCoo< T > &storage)
Definition: cpu_format_coo.hpp:41
void cpu_lil_clear(CpuLil< T > &lil)
Definition: cpu_format_lil.hpp:47
void cl_csr_init(std::size_t n_rows, std::size_t n_values, const uint *Ap, const uint *Aj, const T *Ax, CLCsr< T > &storage)
Definition: cl_format_csr.hpp:41
void cpu_csr_to_dok(uint n_rows, const CpuCsr< T > &in, CpuDok< T > &out)
Definition: cpu_format_csr.hpp:51
void cpu_coo_clear(CpuCoo< T > &in)
Definition: cpu_format_coo.hpp:50
void cpu_lil_to_dok(uint n_rows, const CpuLil< T > &in, CpuDok< T > &out)
Definition: cpu_format_lil.hpp:76
void cpu_coo_to_dok(const CpuCoo< T > &in, CpuDok< T > &out)
Definition: cpu_format_coo.hpp:79
void cpu_coo_to_lil(uint n_rows, const CpuCoo< T > &in, CpuLil< T > &out)
Definition: cpu_format_coo.hpp:58
void cl_csr_read(std::size_t n_rows, std::size_t n_values, uint *Ap, uint *Aj, T *Ax, CLCsr< 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_csr.hpp:80
void cpu_lil_to_csr(uint n_rows, const CpuLil< T > &in, CpuCsr< T > &out)
Definition: cpu_format_lil.hpp:119
void cpu_csr_to_coo(uint n_rows, const CpuCsr< T > &in, CpuCoo< T > &out)
Definition: cpu_format_csr.hpp:68
void cpu_lil_resize(uint n_rows, CpuLil< T > &lil)
Definition: cpu_format_lil.hpp:41
void register_validator_discard(F format, Function function)
Definition: storage_manager.hpp:107
void cpu_csr_resize(const uint n_rows, const uint n_values, CpuCsr< T > &storage)
Definition: cpu_format_csr.hpp:41
void cpu_lil_to_coo(uint n_rows, const CpuLil< T > &in, CpuCoo< T > &out)
Definition: cpu_format_lil.hpp:94
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_matrix(StorageManagerMatrix< T > &manager)
Definition: storage_manager_matrix.hpp:51