Ginkgo  Generated from pipelines/2171896597 branch based on develop. Ginkgo version 1.11.0
A numerical linear algebra library targeting many-core architectures
matrix_assembly_data.hpp
1 // SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_BASE_MATRIX_ASSEMBLY_DATA_HPP_
6 #define GKO_PUBLIC_CORE_BASE_MATRIX_ASSEMBLY_DATA_HPP_
7 
8 
9 #include <algorithm>
10 #include <iterator>
11 #include <numeric>
12 #include <tuple>
13 #include <unordered_map>
14 
15 #include <ginkgo/core/base/dim.hpp>
16 #include <ginkgo/core/base/math.hpp>
17 #include <ginkgo/core/base/matrix_data.hpp>
18 #include <ginkgo/core/base/types.hpp>
19 #include <ginkgo/core/base/utils.hpp>
20 
21 
22 namespace gko {
23 namespace detail {
24 
25 
26 template <typename IndexType>
27 struct symbolic_nonzero_hash {
28  symbolic_nonzero_hash() = default;
29 
30  explicit symbolic_nonzero_hash(size_type num_cols) noexcept
31  : num_cols_{num_cols}
32  {}
33 
34  std::size_t operator()(std::pair<IndexType, IndexType> nnz) const noexcept
35  {
36  return static_cast<std::size_t>(nnz.first) * num_cols_ + nnz.second;
37  }
38 
39  size_type num_cols_;
40 };
41 
42 
43 } // namespace detail
44 
45 
58 template <typename ValueType = default_precision, typename IndexType = int32>
60  GKO_ASSERT_SUPPORTED_VALUE_TYPE;
61 
62 public:
63  using value_type = ValueType;
64  using index_type = IndexType;
65 
66  explicit matrix_assembly_data(dim<2> size)
67  : size_{size},
68  nonzeros_(0, detail::symbolic_nonzero_hash<index_type>(size_[1]))
69  {}
70 
80  void add_value(index_type row, index_type col, value_type val)
81  {
82  auto ind = std::make_pair(row, col);
83  nonzeros_[ind] += val;
84  }
85 
94  void set_value(index_type row, index_type col, value_type val)
95  {
96  auto ind = std::make_pair(row, col);
97  nonzeros_[ind] = val;
98  }
99 
107  value_type get_value(index_type row, index_type col)
108  {
109  const auto it = nonzeros_.find(std::make_pair(row, col));
110  if (it == nonzeros_.end()) {
111  return zero<value_type>();
112  } else {
113  return it->second;
114  }
115  }
116 
124  bool contains(index_type row, index_type col)
125  {
126  return nonzeros_.find(std::make_pair(row, col)) != nonzeros_.end();
127  }
128 
130  dim<2> get_size() const noexcept { return size_; }
131 
134  {
135  return nonzeros_.size();
136  }
137 
143  {
144  using output_type = matrix_data<ValueType, IndexType>;
145  using nonzero_type = typename output_type::nonzero_type;
146  using entry_type =
147  std::pair<std::pair<index_type, index_type>, value_type>;
148  output_type data{size_};
149  data.nonzeros.reserve(nonzeros_.size());
150  std::transform(nonzeros_.begin(), nonzeros_.end(),
151  std::back_inserter(data.nonzeros), [](entry_type entry) {
152  return nonzero_type{entry.first.first,
153  entry.first.second,
154  entry.second};
155  });
156  data.sort_row_major();
157  return data;
158  }
159 
160 private:
164  dim<2> size_;
165 
173  std::unordered_map<std::pair<index_type, index_type>, value_type,
174  detail::symbolic_nonzero_hash<index_type>>
175  nonzeros_;
176 };
177 
178 
179 } // namespace gko
180 
181 
182 #endif // GKO_PUBLIC_CORE_BASE_MATRIX_ASSEMBLY_DATA_HPP_
gko::matrix_assembly_data::get_size
dim< 2 > get_size() const noexcept
Definition: matrix_assembly_data.hpp:130
gko::matrix_assembly_data::contains
bool contains(index_type row, index_type col)
Returns true iff the matrix contains an entry at (row, col).
Definition: matrix_assembly_data.hpp:124
gko::matrix_assembly_data::get_num_stored_elements
size_type get_num_stored_elements() const noexcept
Definition: matrix_assembly_data.hpp:133
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:90
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::matrix_assembly_data::set_value
void set_value(index_type row, index_type col, value_type val)
Sets the matrix value at (row, col).
Definition: matrix_assembly_data.hpp:94
gko::dim< 2 >
gko::matrix_data
This structure is used as an intermediate data type to store a sparse matrix.
Definition: matrix_data.hpp:126
gko::matrix_assembly_data::add_value
void add_value(index_type row, index_type col, value_type val)
Sets the matrix value at (row, col).
Definition: matrix_assembly_data.hpp:80
gko::matrix_assembly_data::get_ordered_data
matrix_data< ValueType, IndexType > get_ordered_data() const
Definition: matrix_assembly_data.hpp:142
gko::matrix_assembly_data::get_value
value_type get_value(index_type row, index_type col)
Gets the matrix value at (row, col).
Definition: matrix_assembly_data.hpp:107
gko::matrix_assembly_data
This structure is used as an intermediate type to assemble a sparse matrix.
Definition: matrix_assembly_data.hpp:59