Ginkgo Generated from branch based on master. Ginkgo version 1.7.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
matrix_assembly_data.hpp
1/*******************************<GINKGO LICENSE>******************************
2Copyright (c) 2017-2023, the Ginkgo authors
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
8
91. Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
11
122. Redistributions in binary form must reproduce the above copyright
13notice, this list of conditions and the following disclaimer in the
14documentation and/or other materials provided with the distribution.
15
163. Neither the name of the copyright holder nor the names of its
17contributors may be used to endorse or promote products derived from
18this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31******************************<GINKGO LICENSE>*******************************/
32
33#ifndef GKO_PUBLIC_CORE_BASE_MATRIX_ASSEMBLY_DATA_HPP_
34#define GKO_PUBLIC_CORE_BASE_MATRIX_ASSEMBLY_DATA_HPP_
35
36
37#include <algorithm>
38#include <iterator>
39#include <numeric>
40#include <tuple>
41#include <unordered_map>
42
43
44#include <ginkgo/core/base/dim.hpp>
45#include <ginkgo/core/base/math.hpp>
46#include <ginkgo/core/base/matrix_data.hpp>
47#include <ginkgo/core/base/types.hpp>
48#include <ginkgo/core/base/utils.hpp>
49
50
51namespace gko {
52namespace detail {
53
54
55template <typename IndexType>
56struct symbolic_nonzero_hash {
57 symbolic_nonzero_hash() = default;
58
59 explicit symbolic_nonzero_hash(size_type num_cols) noexcept
60 : num_cols_{num_cols}
61 {}
62
63 std::size_t operator()(std::pair<IndexType, IndexType> nnz) const noexcept
64 {
65 return static_cast<std::size_t>(nnz.first) * num_cols_ + nnz.second;
66 }
67
68 size_type num_cols_;
69};
70
71
72} // namespace detail
73
74
87template <typename ValueType = default_precision, typename IndexType = int32>
89public:
90 using value_type = ValueType;
91 using index_type = IndexType;
92
93 explicit matrix_assembly_data(dim<2> size)
94 : size_{size},
95 nonzeros_(0, detail::symbolic_nonzero_hash<index_type>(size_[1]))
96 {}
97
107 void add_value(index_type row, index_type col, value_type val)
108 {
109 auto ind = std::make_pair(row, col);
110 nonzeros_[ind] += val;
111 }
112
121 void set_value(index_type row, index_type col, value_type val)
122 {
123 auto ind = std::make_pair(row, col);
124 nonzeros_[ind] = val;
125 }
126
134 value_type get_value(index_type row, index_type col)
135 {
136 const auto it = nonzeros_.find(std::make_pair(row, col));
137 if (it == nonzeros_.end()) {
138 return zero<value_type>();
139 } else {
140 return it->second;
141 }
142 }
143
151 bool contains(index_type row, index_type col)
152 {
153 return nonzeros_.find(std::make_pair(row, col)) != nonzeros_.end();
154 }
155
157 dim<2> get_size() const noexcept { return size_; }
158
161 {
162 return nonzeros_.size();
163 }
164
170 {
172 using nonzero_type = typename output_type::nonzero_type;
173 using entry_type =
174 std::pair<std::pair<index_type, index_type>, value_type>;
175 output_type data{size_};
176 data.nonzeros.reserve(nonzeros_.size());
177 std::transform(nonzeros_.begin(), nonzeros_.end(),
178 std::back_inserter(data.nonzeros), [](entry_type entry) {
179 return nonzero_type{entry.first.first,
180 entry.first.second,
181 entry.second};
182 });
183 data.ensure_row_major_order();
184 return data;
185 }
186
187private:
191 dim<2> size_;
192
200 std::unordered_map<std::pair<index_type, index_type>, value_type,
201 detail::symbolic_nonzero_hash<index_type>>
202 nonzeros_;
203};
204
205
206} // namespace gko
207
208
209#endif // GKO_PUBLIC_CORE_BASE_MATRIX_ASSEMBLY_DATA_HPP_
This structure is used as an intermediate type to assemble a sparse matrix.
Definition matrix_assembly_data.hpp:88
void add_value(index_type row, index_type col, value_type val)
Sets the matrix value at (row, col).
Definition matrix_assembly_data.hpp:107
void set_value(index_type row, index_type col, value_type val)
Sets the matrix value at (row, col).
Definition matrix_assembly_data.hpp:121
size_type get_num_stored_elements() const noexcept
Definition matrix_assembly_data.hpp:160
bool contains(index_type row, index_type col)
Returns true iff the matrix contains an entry at (row, col).
Definition matrix_assembly_data.hpp:151
value_type get_value(index_type row, index_type col)
Gets the matrix value at (row, col).
Definition matrix_assembly_data.hpp:134
dim< 2 > get_size() const noexcept
Definition matrix_assembly_data.hpp:157
matrix_data< ValueType, IndexType > get_ordered_data() const
Definition matrix_assembly_data.hpp:169
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:120
A type representing the dimensions of a multidimensional object.
Definition dim.hpp:55
This structure is used as an intermediate data type to store a sparse matrix.
Definition matrix_data.hpp:155