spla
cpu_format_csr.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_CSR_HPP
29 #define SPLA_CPU_FORMAT_CSR_HPP
30 
31 #include <cpu/cpu_formats.hpp>
32 
33 namespace spla {
34 
40  template<typename T>
41  void cpu_csr_resize(const uint n_rows,
42  const uint n_values,
43  CpuCsr<T>& storage) {
44  storage.Ap.resize(n_rows + 1);
45  storage.Aj.resize(n_values);
46  storage.Ax.resize(n_values);
47  storage.values = n_values;
48  }
49 
50  template<typename T>
51  void cpu_csr_to_dok(uint n_rows,
52  const CpuCsr<T>& in,
53  CpuDok<T>& out) {
54  auto& Ap = in.Ap;
55  auto& Aj = in.Aj;
56  auto& Ax = in.Ax;
57 
58  assert(out.Ax.empty());
59 
60  for (uint i = 0; i < n_rows; i++) {
61  for (uint j = Ap[i]; j < Ap[i + 1]; j++) {
62  out.Ax.insert(robin_hood::pair<std::pair<uint, uint>, T>(std::pair<uint, uint>(i, Aj[j]), Ax[j]));
63  }
64  }
65  }
66 
67  template<typename T>
68  void cpu_csr_to_coo(uint n_rows,
69  const CpuCsr<T>& in,
70  CpuCoo<T>& out) {
71  auto& Ap = in.Ap;
72  auto& Aj = in.Aj;
73  auto& Ax = in.Ax;
74 
75  auto& Ri = out.Ai;
76  auto& Rj = out.Aj;
77  auto& Rx = out.Ax;
78 
79  assert(Ri.size() == in.values);
80  assert(Rj.size() == in.values);
81  assert(Rx.size() == in.values);
82 
83  for (uint i = 0; i < n_rows; i++) {
84  for (uint j = Ap[i]; j < Ap[i + 1]; j++) {
85  Ri[j] = i;
86  Rj[j] = Aj[j];
87  Rx[j] = Ax[j];
88  }
89  }
90  }
91 
96 }// namespace spla
97 
98 #endif//SPLA_CPU_FORMAT_CSR_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
uint values
Definition: tdecoration.hpp:58
void cpu_csr_to_dok(uint n_rows, const CpuCsr< T > &in, CpuDok< T > &out)
Definition: cpu_format_csr.hpp:51
void cpu_csr_to_coo(uint n_rows, const CpuCsr< T > &in, CpuCoo< T > &out)
Definition: cpu_format_csr.hpp:68
void cpu_csr_resize(const uint n_rows, const uint n_values, CpuCsr< T > &storage)
Definition: cpu_format_csr.hpp:41
std::uint32_t uint
Library index and size type.
Definition: config.hpp:56
Definition: algorithm.hpp:37