spla
cpu_format_coo.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_CPU_FORMAT_COO_HPP
29 #define SPLA_CPU_FORMAT_COO_HPP
30 
31 #include <cpu/cpu_formats.hpp>
32 
33 namespace spla {
34 
40  template<typename T>
41  void cpu_coo_resize(const uint n_values,
42  CpuCoo<T>& storage) {
43  storage.Ai.resize(n_values);
44  storage.Aj.resize(n_values);
45  storage.Ax.resize(n_values);
46  storage.values = n_values;
47  }
48 
49  template<typename T>
51  in.Ai.clear();
52  in.Aj.clear();
53  in.Ax.clear();
54  in.values = 0;
55  }
56 
57  template<typename T>
58  void cpu_coo_to_lil(uint n_rows,
59  const CpuCoo<T>& in,
60  CpuLil<T>& out) {
61  auto& Rr = out.Ar;
62 
63  auto& Ai = in.Ai;
64  auto& Aj = in.Aj;
65  auto& Ax = in.Ax;
66 
67  assert(Rr.size() == n_rows);
68 
69  for (uint k = 0; k < in.values; k++) {
70  const uint i = Ai[k];
71  const uint j = Aj[k];
72  const T x = Ax[k];
73 
74  Rr[i].emplace_back(j, x);
75  }
76  }
77 
78  template<typename T>
79  void cpu_coo_to_dok(const CpuCoo<T>& in,
80  CpuDok<T>& out) {
81  auto& Rx = out.Ax;
82 
83  auto& Ai = in.Ai;
84  auto& Aj = in.Aj;
85  auto& Ax = in.Ax;
86 
87  assert(Rx.empty());
88 
89  for (uint i = 0; i < in.values; i++) {
90  typename CpuDok<T>::Key key{Ai[i], Aj[i]};
91  Rx[key] = Ax[i];
92  }
93  }
94 
95  template<typename T>
96  void cpu_coo_to_csr(uint n_rows,
97  const CpuCoo<T>& in,
98  CpuCsr<T>& out) {
99  auto& Rp = out.Ap;
100  auto& Rj = out.Aj;
101  auto& Rx = out.Ax;
102  auto& Ai = in.Ai;
103  auto& Aj = in.Aj;
104  auto& Ax = in.Ax;
105 
106  assert(Rp.size() == n_rows + 1);
107  assert(Rj.size() == in.values);
108  assert(Rx.size() == in.values);
109 
110  std::fill(Rp.begin(), Rp.end(), 0u);
111 
112  for (uint k = 0; k < in.values; ++k) {
113  Rp[Ai[k]] += 1;
114  }
115 
116  std::exclusive_scan(Rp.begin(), Rp.end(), Rp.begin(), 0, std::plus<>());
117  assert(Rp[n_rows] == in.values);
118 
119  for (uint k = 0; k < in.values; ++k) {
120  Rj[k] = Aj[k];
121  Rx[k] = Ax[k];
122  }
123  }
124 
129 }// namespace spla
130 
131 #endif//SPLA_CPU_FORMAT_COO_HPP
CPU list of coordinates matrix format.
Definition: cpu_formats.hpp:148
std::vector< uint > Aj
Definition: cpu_formats.hpp:155
std::vector< T > Ax
Definition: cpu_formats.hpp:156
std::vector< uint > Ai
Definition: cpu_formats.hpp:154
CPU compressed sparse row matrix format.
Definition: cpu_formats.hpp:166
std::vector< uint > Ap
Definition: cpu_formats.hpp:172
std::vector< uint > Aj
Definition: cpu_formats.hpp:173
std::vector< T > Ax
Definition: cpu_formats.hpp:174
Dictionary of keys sparse matrix format.
Definition: cpu_formats.hpp:128
robin_hood::unordered_flat_map< Key, T, pair_hash > Ax
Definition: cpu_formats.hpp:137
std::pair< uint, uint > Key
Definition: cpu_formats.hpp:134
CPU list-of-list matrix format for fast incremental build.
Definition: cpu_formats.hpp:107
std::vector< Row > Ar
Definition: cpu_formats.hpp:117
uint values
Definition: tdecoration.hpp:58
void cpu_coo_to_csr(uint n_rows, const CpuCoo< T > &in, CpuCsr< T > &out)
Definition: cpu_format_coo.hpp:96
void cpu_coo_resize(const uint n_values, CpuCoo< T > &storage)
Definition: cpu_format_coo.hpp:41
void cpu_coo_clear(CpuCoo< T > &in)
Definition: cpu_format_coo.hpp:50
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
std::uint32_t uint
Library index and size type.
Definition: config.hpp:56
Definition: algorithm.hpp:37