5#ifndef GKO_PUBLIC_CORE_BASE_MATRIX_ASSEMBLY_DATA_HPP_
6#define GKO_PUBLIC_CORE_BASE_MATRIX_ASSEMBLY_DATA_HPP_
13#include <unordered_map>
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>
26template <
typename IndexType>
27struct symbolic_nonzero_hash {
28 symbolic_nonzero_hash() =
default;
30 explicit symbolic_nonzero_hash(
size_type num_cols) noexcept
34 std::size_t operator()(std::pair<IndexType, IndexType> nnz)
const noexcept
36 return static_cast<std::size_t
>(nnz.first) * num_cols_ + nnz.second;
58template <
typename ValueType = default_precision,
typename IndexType =
int32>
61 using value_type = ValueType;
62 using index_type = IndexType;
66 nonzeros_(0, detail::symbolic_nonzero_hash<index_type>(size_[1]))
78 void add_value(index_type row, index_type col, value_type val)
80 auto ind = std::make_pair(row, col);
81 nonzeros_[ind] += val;
92 void set_value(index_type row, index_type col, value_type val)
94 auto ind = std::make_pair(row, col);
107 const auto it = nonzeros_.find(std::make_pair(row, col));
108 if (it == nonzeros_.end()) {
124 return nonzeros_.find(std::make_pair(row, col)) != nonzeros_.end();
133 return nonzeros_.size();
143 using nonzero_type =
typename output_type::nonzero_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,
154 data.sort_row_major();
171 std::unordered_map<std::pair<index_type, index_type>, value_type,
172 detail::symbolic_nonzero_hash<index_type>>
This structure is used as an intermediate type to assemble a sparse matrix.
Definition matrix_assembly_data.hpp:59
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
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
size_type get_num_stored_elements() const noexcept
Definition matrix_assembly_data.hpp:131
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
value_type get_value(index_type row, index_type col)
Gets the matrix value at (row, col).
Definition matrix_assembly_data.hpp:105
dim< 2 > get_size() const noexcept
Definition matrix_assembly_data.hpp:128
matrix_data< ValueType, IndexType > get_ordered_data() const
Definition matrix_assembly_data.hpp:140
The Ginkgo namespace.
Definition abstract_factory.hpp:20
constexpr T zero()
Returns the additive identity for T.
Definition math.hpp:602
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:89
A type representing the dimensions of a multidimensional object.
Definition dim.hpp:26
This structure is used as an intermediate data type to store a sparse matrix.
Definition matrix_data.hpp:126
std::vector< nonzero_type > nonzeros
A vector of tuples storing the non-zeros of the matrix.
Definition matrix_data.hpp:453