5 #ifndef GKO_PUBLIC_CORE_BASE_MATRIX_ASSEMBLY_DATA_HPP_
6 #define GKO_PUBLIC_CORE_BASE_MATRIX_ASSEMBLY_DATA_HPP_
13 #include <unordered_map>
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>
27 template <
typename IndexType>
28 struct symbolic_nonzero_hash {
29 symbolic_nonzero_hash() =
default;
31 explicit symbolic_nonzero_hash(
size_type num_cols) noexcept
35 std::size_t operator()(std::pair<IndexType, IndexType> nnz)
const noexcept
37 return static_cast<std::size_t>(nnz.first) * num_cols_ + nnz.second;
59 template <
typename ValueType = default_precision,
typename IndexType =
int32>
62 using value_type = ValueType;
63 using index_type = IndexType;
67 nonzeros_(0, detail::symbolic_nonzero_hash<index_type>(size_[1]))
79 void add_value(index_type row, index_type col, value_type val)
81 auto ind = std::make_pair(row, col);
82 nonzeros_[ind] += val;
93 void set_value(index_type row, index_type col, value_type val)
95 auto ind = std::make_pair(row, col);
108 const auto it = nonzeros_.find(std::make_pair(row, col));
109 if (it == nonzeros_.end()) {
110 return zero<value_type>();
125 return nonzeros_.find(std::make_pair(row, col)) != nonzeros_.end();
134 return nonzeros_.size();
144 using nonzero_type =
typename output_type::nonzero_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,
155 data.sort_row_major();
172 std::unordered_map<std::pair<index_type, index_type>, value_type,
173 detail::symbolic_nonzero_hash<index_type>>
181 #endif // GKO_PUBLIC_CORE_BASE_MATRIX_ASSEMBLY_DATA_HPP_