Ginkgo  Generated from pipelines/1589998975 branch based on develop. Ginkgo version 1.10.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 #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 public:
61  using value_type = ValueType;
62  using index_type = IndexType;
63 
64  explicit matrix_assembly_data(dim<2> size)
65  : size_{size},
66  nonzeros_(0, detail::symbolic_nonzero_hash<index_type>(size_[1]))
67  {}
68 
78  void add_value(index_type row, index_type col, value_type val)
79  {
80  auto ind = std::make_pair(row, col);
81  nonzeros_[ind] += val;
82  }
83 
92  void set_value(index_type row, index_type col, value_type val)
93  {
94  auto ind = std::make_pair(row, col);
95  nonzeros_[ind] = val;
96  }
97 
105  value_type get_value(index_type row, index_type col)
106  {
107  const auto it = nonzeros_.find(std::make_pair(row, col));
108  if (it == nonzeros_.end()) {
109  return zero<value_type>();
110  } else {
111  return it->second;
112  }
113  }
114 
122  bool contains(index_type row, index_type col)
123  {
124  return nonzeros_.find(std::make_pair(row, col)) != nonzeros_.end();
125  }
126 
128  dim<2> get_size() const noexcept { return size_; }
129 
132  {
133  return nonzeros_.size();
134  }
135 
141  {
142  using output_type = matrix_data<ValueType, IndexType>;
143  using nonzero_type = typename output_type::nonzero_type;
144  using entry_type =
145  std::pair<std::pair<index_type, index_type>, value_type>;
146  output_type data{size_};
147  data.nonzeros.reserve(nonzeros_.size());
148  std::transform(nonzeros_.begin(), nonzeros_.end(),
149  std::back_inserter(data.nonzeros), [](entry_type entry) {
150  return nonzero_type{entry.first.first,
151  entry.first.second,
152  entry.second};
153  });
154  data.sort_row_major();
155  return data;
156  }
157 
158 private:
162  dim<2> size_;
163 
171  std::unordered_map<std::pair<index_type, index_type>, value_type,
172  detail::symbolic_nonzero_hash<index_type>>
173  nonzeros_;
174 };
175 
176 
177 } // namespace gko
178 
179 
180 #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:128
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:122
gko::matrix_assembly_data::get_num_stored_elements
size_type get_num_stored_elements() const noexcept
Definition: matrix_assembly_data.hpp:131
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:89
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:92
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:78
gko::matrix_assembly_data::get_ordered_data
matrix_data< ValueType, IndexType > get_ordered_data() const
Definition: matrix_assembly_data.hpp:140
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:105
gko::matrix_assembly_data
This structure is used as an intermediate type to assemble a sparse matrix.
Definition: matrix_assembly_data.hpp:59