Ginkgo  Generated from pipelines/1330831941 branch based on master. Ginkgo version 1.8.0
A numerical linear algebra library targeting many-core architectures
matrix_assembly_data.hpp
1 // SPDX-FileCopyrightText: 2017 - 2024 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 
16 #include <ginkgo/core/base/dim.hpp>
17 #include <ginkgo/core/base/math.hpp>
18 #include <ginkgo/core/base/matrix_data.hpp>
19 #include <ginkgo/core/base/types.hpp>
20 #include <ginkgo/core/base/utils.hpp>
21 
22 
23 namespace gko {
24 namespace detail {
25 
26 
27 template <typename IndexType>
28 struct symbolic_nonzero_hash {
29  symbolic_nonzero_hash() = default;
30 
31  explicit symbolic_nonzero_hash(size_type num_cols) noexcept
32  : num_cols_{num_cols}
33  {}
34 
35  std::size_t operator()(std::pair<IndexType, IndexType> nnz) const noexcept
36  {
37  return static_cast<std::size_t>(nnz.first) * num_cols_ + nnz.second;
38  }
39 
40  size_type num_cols_;
41 };
42 
43 
44 } // namespace detail
45 
46 
59 template <typename ValueType = default_precision, typename IndexType = int32>
61 public:
62  using value_type = ValueType;
63  using index_type = IndexType;
64 
65  explicit matrix_assembly_data(dim<2> size)
66  : size_{size},
67  nonzeros_(0, detail::symbolic_nonzero_hash<index_type>(size_[1]))
68  {}
69 
79  void add_value(index_type row, index_type col, value_type val)
80  {
81  auto ind = std::make_pair(row, col);
82  nonzeros_[ind] += val;
83  }
84 
93  void set_value(index_type row, index_type col, value_type val)
94  {
95  auto ind = std::make_pair(row, col);
96  nonzeros_[ind] = val;
97  }
98 
106  value_type get_value(index_type row, index_type col)
107  {
108  const auto it = nonzeros_.find(std::make_pair(row, col));
109  if (it == nonzeros_.end()) {
110  return zero<value_type>();
111  } else {
112  return it->second;
113  }
114  }
115 
123  bool contains(index_type row, index_type col)
124  {
125  return nonzeros_.find(std::make_pair(row, col)) != nonzeros_.end();
126  }
127 
129  dim<2> get_size() const noexcept { return size_; }
130 
133  {
134  return nonzeros_.size();
135  }
136 
142  {
143  using output_type = matrix_data<ValueType, IndexType>;
144  using nonzero_type = typename output_type::nonzero_type;
145  using entry_type =
146  std::pair<std::pair<index_type, index_type>, value_type>;
147  output_type data{size_};
148  data.nonzeros.reserve(nonzeros_.size());
149  std::transform(nonzeros_.begin(), nonzeros_.end(),
150  std::back_inserter(data.nonzeros), [](entry_type entry) {
151  return nonzero_type{entry.first.first,
152  entry.first.second,
153  entry.second};
154  });
155  data.sort_row_major();
156  return data;
157  }
158 
159 private:
163  dim<2> size_;
164 
172  std::unordered_map<std::pair<index_type, index_type>, value_type,
173  detail::symbolic_nonzero_hash<index_type>>
174  nonzeros_;
175 };
176 
177 
178 } // namespace gko
179 
180 
181 #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:129
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:123
gko::matrix_assembly_data::get_num_stored_elements
size_type get_num_stored_elements() const noexcept
Definition: matrix_assembly_data.hpp:132
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:108
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:93
gko::dim< 2 >
gko::matrix_data
This structure is used as an intermediate data type to store a sparse matrix.
Definition: matrix_data.hpp:127
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:79
gko::matrix_assembly_data::get_ordered_data
matrix_data< ValueType, IndexType > get_ordered_data() const
Definition: matrix_assembly_data.hpp:141
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:106
gko::matrix_assembly_data
This structure is used as an intermediate type to assemble a sparse matrix.
Definition: matrix_assembly_data.hpp:60