33 #ifndef GKO_CORE_MATRIX_HYBRID_HPP_ 34 #define GKO_CORE_MATRIX_HYBRID_HPP_ 40 #include <ginkgo/core/base/array.hpp> 41 #include <ginkgo/core/base/lin_op.hpp> 42 #include <ginkgo/core/matrix/coo.hpp> 43 #include <ginkgo/core/matrix/ell.hpp> 50 template <
typename ValueType>
66 template <
typename ValueType = default_precision,
typename IndexType =
int32>
67 class Hybrid :
public EnableLinOp<Hybrid<ValueType, IndexType>>,
68 public EnableCreateMethod<Hybrid<ValueType, IndexType>>,
69 public ConvertibleTo<Dense<ValueType>>,
70 public ReadableFromMatrixData<ValueType, IndexType>,
71 public WritableToMatrixData<ValueType, IndexType> {
72 friend class EnableCreateMethod<Hybrid>;
73 friend class EnablePolymorphicObject<Hybrid, LinOp>;
74 friend class Dense<ValueType>;
80 using value_type = ValueType;
81 using index_type = IndexType;
82 using mat_data = matrix_data<ValueType, IndexType>;
83 using coo_type = Coo<ValueType, IndexType>;
84 using ell_type = Ell<ValueType, IndexType>;
118 size_type *ell_num_stored_elements_per_row,
123 ref_row_nnz = row_nnz;
124 ell_num_stored_elements_per_row_ =
126 coo_nnz_ = this->compute_coo_nnz(ref_row_nnz);
127 *ell_num_stored_elements_per_row = ell_num_stored_elements_per_row_;
138 return ell_num_stored_elements_per_row_;
172 if (row_nnz_val[i] > ell_num_stored_elements_per_row_) {
174 row_nnz_val[i] - ell_num_stored_elements_per_row_;
181 size_type ell_num_stored_elements_per_row_;
197 : num_columns_(num_column)
227 percent_ = std::min(percent_, 1.0f);
228 percent_ = std::max(percent_, 0.0f);
234 auto row_nnz_val = row_nnz->
get_data();
236 std::sort(row_nnz_val, row_nnz_val + num_rows);
238 auto percent_pos =
static_cast<size_type>(num_rows * percent_);
239 return row_nnz_val[percent_pos];
241 return row_nnz_val[num_rows - 1];
268 strategy_.compute_ell_num_stored_elements_per_row(row_nnz);
269 return std::min(ell_cols,
270 static_cast<size_type>(num_rows * ratio_));
292 (sizeof(ValueType) + 2 * sizeof(IndexType))))
298 return strategy_.compute_ell_num_stored_elements_per_row(row_nnz);
320 return strategy_.compute_ell_num_stored_elements_per_row(row_nnz);
351 return ell_->get_const_values();
370 return ell_->get_const_col_idxs();
380 return ell_->get_num_stored_elements_per_row();
397 return ell_->get_num_stored_elements();
413 return ell_->val_at(row, idx);
421 return ell_->val_at(row, idx);
436 return ell_->col_at(row, idx);
444 return ell_->col_at(row, idx);
470 return coo_->get_const_values();
489 return coo_->get_const_col_idxs();
508 return coo_->get_const_row_idxs();
518 return coo_->get_num_stored_elements();
535 return coo_->get_num_stored_elements() +
536 ell_->get_num_stored_elements();
558 if (&other ==
this) {
562 this->coo_->copy_from(other.get_coo());
563 this->ell_->copy_from(other.get_ell());
577 std::shared_ptr<const Executor> exec,
578 std::shared_ptr<strategy_type> strategy = std::make_shared<automatic>())
579 :
Hybrid(std::move(exec),
dim<2>{}, std::move(strategy))
592 std::shared_ptr<const Executor> exec,
const dim<2> &size,
593 std::shared_ptr<strategy_type> strategy = std::make_shared<automatic>())
594 :
Hybrid(std::move(exec), size, size[1], std::move(strategy))
608 std::shared_ptr<const Executor> exec,
const dim<2> &size,
610 std::shared_ptr<strategy_type> strategy = std::make_shared<automatic>())
611 :
Hybrid(std::move(exec), size, num_stored_elements_per_row, size[0],
612 {}, std::move(strategy))
625 Hybrid(std::shared_ptr<const Executor> exec,
const dim<2> &size,
627 std::shared_ptr<strategy_type> strategy)
628 :
Hybrid(std::move(exec), size, num_stored_elements_per_row, stride, {},
644 std::shared_ptr<const Executor> exec,
const dim<2> &size,
647 std::shared_ptr<strategy_type> strategy = std::make_shared<automatic>())
649 ell_(std::move(ell_type::create(
650 exec, size, num_stored_elements_per_row, stride))),
651 coo_(std::move(coo_type::create(exec, size, num_nonzeros))),
652 strategy_(std::move(strategy))
655 void apply_impl(
const LinOp *b,
LinOp *x)
const override;
658 LinOp *x)
const override;
661 std::shared_ptr<ell_type> ell_;
662 std::shared_ptr<coo_type> coo_;
663 std::shared_ptr<strategy_type> strategy_;
671 #endif // GKO_CORE_MATRIX_HYBRID_HPP_ index_type & ell_col_at(size_type row, size_type idx) noexcept
Returns the idx-th column index of the row-th row in the ell part.
Definition: hybrid.hpp:434
Hybrid & operator=(const Hybrid &other)
Copies data from another Hybrid.
Definition: hybrid.hpp:556
size_type get_num_elems() const noexcept
Returns the number of elements in the Array.
Definition: array.hpp:388
value_type * get_coo_values() noexcept
Returns the values of the coo part.
Definition: hybrid.hpp:459
index_type * get_coo_row_idxs() noexcept
Returns the row indexes of the coo part.
Definition: hybrid.hpp:497
imbalance_bounded_limit(float percent=0.8, float ratio=0.0001)
Creates a imbalance_bounded_limit strategy.
Definition: hybrid.hpp:259
ELL is a matrix format where stride with explicit zeros is used such that all rows have the same numb...
Definition: csr.hpp:53
value_type * get_ell_values() noexcept
Returns the values of the ell part.
Definition: hybrid.hpp:340
constexpr T zero()
Returns the additive identity for T.
Definition: math.hpp:292
size_type get_ell_num_stored_elements_per_row() const noexcept
Returns the number of stored elements per row of ell part.
Definition: hybrid.hpp:378
size_type compute_ell_num_stored_elements_per_row(Array< size_type > *row_nnz) const override
Computes the number of stored elements per row of the ell part.
Definition: hybrid.hpp:295
const ell_type * get_ell() const noexcept
Returns the matrix of the ell part.
Definition: hybrid.hpp:452
size_type compute_ell_num_stored_elements_per_row(Array< size_type > *row_nnz) const override
Computes the number of stored elements per row of the ell part.
Definition: hybrid.hpp:200
size_type get_coo_num_stored_elements() const noexcept
Returns the number of elements explicitly stored in the coo part.
Definition: hybrid.hpp:516
size_type get_num_stored_elements() const noexcept
Returns the number of elements explicitly stored in the matrix.
Definition: hybrid.hpp:533
size_type get_ell_stride() const noexcept
Returns the stride of the ell part.
Definition: hybrid.hpp:388
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:94
index_type * get_ell_col_idxs() noexcept
Returns the column indexes of the ell part.
Definition: hybrid.hpp:359
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor associated with the array.
Definition: array.hpp:413
const value_type * get_const_data() const noexcept
Returns a constant pointer to the block of memory used to store the elements of the Array...
Definition: array.hpp:406
The Ginkgo namespace.
Definition: abstract_factory.hpp:45
const value_type * get_const_ell_values() const noexcept
Returns the values of the ell part.
Definition: hybrid.hpp:349
void read(const mat_data &data) override
Reads a matrix from a matrix_data structure.
index_type * get_coo_col_idxs() noexcept
Returns the column indexes of the coo part.
Definition: hybrid.hpp:478
imbalance_limit(float percent=0.8)
Creates a imbalance_limit strategy.
Definition: hybrid.hpp:225
size_type compute_ell_num_stored_elements_per_row(Array< size_type > *row_nnz) const override
Computes the number of stored elements per row of the ell part.
Definition: hybrid.hpp:231
std::shared_ptr< strategy_type > get_strategy() const noexcept
Returns the strategy.
Definition: hybrid.hpp:544
strategy_type is to decide how to set the hybrid config.
Definition: hybrid.hpp:95
minimal_storage_limit is a stratgy_type which decides the number of stored elements per row of the el...
Definition: hybrid.hpp:284
column_limit is a strategy_type which decides the number of stored elements per row of the ell part b...
Definition: hybrid.hpp:189
strategy_type()
Creates a strategy_type.
Definition: hybrid.hpp:100
size_type compute_ell_num_stored_elements_per_row(Array< size_type > *row_nnz) const override
Computes the number of stored elements per row of the ell part.
Definition: hybrid.hpp:317
imbalance_bounded_limit is a stratgy_type which decides the number of stored elements per row of the ...
Definition: hybrid.hpp:254
Dense is a matrix format which explicitly stores all values of the matrix.
Definition: coo.hpp:55
Definition: lin_op.hpp:134
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition: lin_op.hpp:509
void convert_to(result_type *result) const override
Definition: polymorphic_object.hpp:558
void move_to(result_type *result) override
Definition: polymorphic_object.hpp:560
virtual size_type compute_ell_num_stored_elements_per_row(Array< size_type > *row_nnz) const =0
Computes the number of stored elements per row of the ell part.
void compute_hybrid_config(const Array< size_type > &row_nnz, size_type *ell_num_stored_elements_per_row, size_type *coo_nnz)
Computes the config of the Hybrid matrix (ell_num_stored_elements_per_row and coo_nnz).
Definition: hybrid.hpp:117
COO stores a matrix in the coordinate matrix format.
Definition: coo.hpp:73
void write(mat_data &data) const override
Writes a matrix to a matrix_data structure.
automatic is a stratgy_type which decides the number of stored elements per row of the ell part autom...
Definition: hybrid.hpp:310
const index_type * get_const_coo_col_idxs() const noexcept
Returns the column indexes of the coo part.
Definition: hybrid.hpp:487
value_type ell_val_at(size_type row, size_type idx) const noexcept
Returns the idx-th non-zero element of the row-th row in the ell part.
Definition: hybrid.hpp:419
size_type get_ell_num_stored_elements() const noexcept
Returns the number of elements explicitly stored in the ell part.
Definition: hybrid.hpp:395
minimal_storage_limit()
Creates a minimal_storage_limit strategy.
Definition: hybrid.hpp:289
const index_type * get_const_ell_col_idxs() const noexcept
Returns the column indexes of the ell part.
Definition: hybrid.hpp:368
const size_type get_coo_nnz() const noexcept
Returns the number of nonzeros of the coo part.
Definition: hybrid.hpp:146
column_limit(size_type num_column=0)
Creates a column_limit strategy.
Definition: hybrid.hpp:196
value_type & ell_val_at(size_type row, size_type idx) noexcept
Returns the idx-th non-zero element of the row-th row in the ell part.
Definition: hybrid.hpp:411
const coo_type * get_coo() const noexcept
Returns the matrix of the coo part.
Definition: hybrid.hpp:526
const value_type * get_const_coo_values() const noexcept
Returns the values of the coo part.
Definition: hybrid.hpp:468
value_type * get_data() noexcept
Returns a pointer to the block of memory used to store the elements of the Array. ...
Definition: array.hpp:397
const index_type * get_const_coo_row_idxs() const noexcept
Returns the row indexes of the coo part.
Definition: hybrid.hpp:506
size_type compute_ell_num_stored_elements_per_row(Array< size_type > *row_nnz) const override
Computes the number of stored elements per row of the ell part.
Definition: hybrid.hpp:263
const size_type get_ell_num_stored_elements_per_row() const noexcept
Returns the number of stored elements per row of the ell part.
Definition: hybrid.hpp:136
This structure is used as an intermediate data type to store a sparse matrix.
Definition: matrix_data.hpp:102
HYBRID is a matrix format which splits the matrix into ELLPACK and COO format.
Definition: dense.hpp:63
imbalance_limit is a strategy_type which decides the number of stored elements per row of the ell par...
Definition: hybrid.hpp:217
automatic()
Creates an automatic strategy.
Definition: hybrid.hpp:315
index_type ell_col_at(size_type row, size_type idx) const noexcept
Returns the idx-th column index of the row-th row in the ell part.
Definition: hybrid.hpp:442