spla
Loading...
Searching...
No Matches
cpu_format_lil.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_LIL_HPP
29#define SPLA_CPU_FORMAT_LIL_HPP
30
31#include <cpu/cpu_formats.hpp>
32
33namespace spla {
34
40 template<typename T>
41 void cpu_lil_resize(uint n_rows,
42 CpuLil<T>& lil) {
43 lil.Ar.resize(n_rows);
44 }
45
46 template<typename T>
48 for (auto& row : lil.Ar) {
49 row.clear();
50 }
51 lil.values = 0;
52 }
53
54 template<typename T>
56 uint col_id,
57 T element,
58 CpuLil<T>& lil) {
59 using Entry = typename CpuLil<T>::Entry;
60 auto& row = lil.Ar[row_id];
61
62 auto where = std::upper_bound(row.begin(), row.end(), Entry{col_id, element}, [](const Entry& val, const Entry& point) {
63 return val.first <= point.first;
64 });
65
66 if (where != row.end() && (*where).first == col_id) {
67 (*where).second = lil.reduce((*where).second, element);
68 return;
69 }
70
71 row.insert(where, Entry{col_id, element});
72 lil.values += 1;
73 }
74
75 template<typename T>
76 void cpu_lil_to_dok(uint n_rows,
77 const CpuLil<T>& in,
78 CpuDok<T>& out) {
79 auto& Rx = out.Ax;
80 auto& Ar = in.Ar;
81
82 assert(Rx.empty());
83
84 for (uint i = 0; i < n_rows; i++) {
85 const auto& row = Ar[i];
86 for (uint j = 0; j < row.size(); j++) {
87 typename CpuDok<T>::Key key{i, row[j].first};
88 Rx[key] = row[j].second;
89 }
90 }
91 }
92
93 template<typename T>
94 void cpu_lil_to_coo(uint n_rows,
95 const CpuLil<T>& in,
96 CpuCoo<T>& out) {
97 auto& Ri = out.Ai;
98 auto& Rj = out.Aj;
99 auto& Rx = out.Ax;
100 auto& Ar = in.Ar;
101
102 assert(Ri.size() == in.values);
103 assert(Rj.size() == in.values);
104 assert(Rx.size() == in.values);
105
106 uint k = 0;
107 for (uint i = 0; i < n_rows; i++) {
108 const auto& row = Ar[i];
109 for (uint j = 0; j < row.size(); j++) {
110 Ri[k] = i;
111 Rj[k] = row[j].first;
112 Rx[k] = row[j].second;
113 k += 1;
114 }
115 }
116 }
117
118 template<typename T>
119 void cpu_lil_to_csr(uint n_rows,
120 const CpuLil<T>& in,
121 CpuCsr<T>& out) {
122 auto& Rp = out.Ap;
123 auto& Rj = out.Aj;
124 auto& Rx = out.Ax;
125 auto& Ar = in.Ar;
126
127 assert(Rp.size() == n_rows + 1);
128 assert(Rj.size() == in.values);
129 assert(Rx.size() == in.values);
130
131 for (uint i = 0; i < n_rows; i++) {
132 Rp[i] = Ar[i].size();
133 }
134
135 std::exclusive_scan(Rp.begin(), Rp.end(), Rp.begin(), 0, std::plus<>());
136 assert(Rp[n_rows] == in.values);
137
138 uint k = 0;
139 for (uint i = 0; i < n_rows; i++) {
140 const auto& row = Ar[i];
141 for (uint j = 0; j < row.size(); j++) {
142 Rj[k] = row[j].first;
143 Rx[k] = row[j].second;
144 k += 1;
145 }
146 }
147 }
148
153}// namespace spla
154
155#endif//SPLA_CPU_FORMAT_LIL_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::pair< uint, T > Entry
Definition cpu_formats.hpp:113
std::vector< Row > Ar
Definition cpu_formats.hpp:117
Reduce reduce
Definition cpu_formats.hpp:118
uint values
Definition tdecoration.hpp:58
void cpu_lil_clear(CpuLil< T > &lil)
Definition cpu_format_lil.hpp:47
void cpu_lil_to_dok(uint n_rows, const CpuLil< T > &in, CpuDok< T > &out)
Definition cpu_format_lil.hpp:76
void cpu_lil_to_csr(uint n_rows, const CpuLil< T > &in, CpuCsr< T > &out)
Definition cpu_format_lil.hpp:119
void cpu_lil_add_element(uint row_id, uint col_id, T element, CpuLil< T > &lil)
Definition cpu_format_lil.hpp:55
void cpu_lil_resize(uint n_rows, CpuLil< T > &lil)
Definition cpu_format_lil.hpp:41
void cpu_lil_to_coo(uint n_rows, const CpuLil< T > &in, CpuCoo< T > &out)
Definition cpu_format_lil.hpp:94
std::uint32_t uint
Library index and size type.
Definition config.hpp:56
Definition algorithm.hpp:37