5#ifndef GKO_PUBLIC_CORE_MATRIX_HYBRID_HPP_
6#define GKO_PUBLIC_CORE_MATRIX_HYBRID_HPP_
11#include <ginkgo/core/base/array.hpp>
12#include <ginkgo/core/base/lin_op.hpp>
13#include <ginkgo/core/matrix/coo.hpp>
14#include <ginkgo/core/matrix/csr.hpp>
15#include <ginkgo/core/matrix/ell.hpp>
22template <
typename ValueType>
25template <
typename ValueType,
typename IndexType>
41template <
typename ValueType = default_precision,
typename IndexType =
int32>
44 public ConvertibleTo<Hybrid<next_precision<ValueType>, IndexType>>,
45#if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
46 public ConvertibleTo<Hybrid<next_precision<ValueType, 2>, IndexType>>,
48#if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
49 public ConvertibleTo<Hybrid<next_precision<ValueType, 3>, IndexType>>,
57 remove_complex<Hybrid<ValueType, IndexType>>> {
59 friend class Dense<ValueType>;
60 friend class Csr<ValueType, IndexType>;
76 using value_type = ValueType;
77 using index_type = IndexType;
117 size_type* ell_num_stored_elements_per_row,
122 ref_row_nnz = row_nnz;
123 ell_num_stored_elements_per_row_ =
125 coo_nnz_ = this->compute_coo_nnz(ref_row_nnz);
126 *ell_num_stored_elements_per_row = ell_num_stored_elements_per_row_;
137 return ell_num_stored_elements_per_row_;
171 if (row_nnz_val[i] > ell_num_stored_elements_per_row_) {
173 row_nnz_val[i] - ell_num_stored_elements_per_row_;
180 size_type ell_num_stored_elements_per_row_;
196 : num_columns_(num_column)
233 percent_ = std::min(percent_, 1.0);
234 percent_ = std::max(percent_, 0.0);
240 auto row_nnz_val = row_nnz->
get_data();
241 auto num_rows = row_nnz->
get_size();
245 std::sort(row_nnz_val, row_nnz_val + num_rows);
247 auto percent_pos =
static_cast<size_type>(num_rows * percent_);
248 return row_nnz_val[percent_pos];
250 return row_nnz_val[num_rows - 1];
282 auto num_rows = row_nnz->
get_size();
284 strategy_.compute_ell_num_stored_elements_per_row(row_nnz);
285 return std::min(ell_cols,
286 static_cast<size_type>(num_rows * ratio_));
322 (sizeof(ValueType) + 2 * sizeof(IndexType))))
328 return strategy_.compute_ell_num_stored_elements_per_row(row_nnz);
357 return strategy_.compute_ell_num_stored_elements_per_row(row_nnz);
371#if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
385#if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
407 void read(
const mat_data& data)
override;
409 void read(
const device_mat_data& data)
override;
411 void read(device_mat_data&& data)
override;
413 void write(mat_data& data)
const override;
437 return ell_->get_const_values();
456 return ell_->get_const_col_idxs();
466 return ell_->get_num_stored_elements_per_row();
483 return ell_->get_num_stored_elements();
499 return ell_->val_at(row, idx);
507 return ell_->val_at(row, idx);
522 return ell_->col_at(row, idx);
530 return ell_->col_at(row, idx);
538 const ell_type*
get_ell() const noexcept {
return ell_.get(); }
556 return coo_->get_const_values();
575 return coo_->get_const_col_idxs();
594 return coo_->get_const_row_idxs();
604 return coo_->get_num_stored_elements();
612 const coo_type*
get_coo() const noexcept {
return coo_.get(); }
621 return coo_->get_num_stored_elements() +
622 ell_->get_num_stored_elements();
642 template <
typename HybType>
643 std::shared_ptr<typename HybType::strategy_type>
get_strategy()
const;
656 std::shared_ptr<const Executor> exec,
657 std::shared_ptr<strategy_type> strategy =
658 std::make_shared<automatic>());
672 std::shared_ptr<const Executor> exec,
const dim<2>& size,
673 std::shared_ptr<strategy_type> strategy =
674 std::make_shared<automatic>());
689 std::shared_ptr<const Executor> exec,
const dim<2>& size,
691 std::shared_ptr<strategy_type> strategy =
692 std::make_shared<automatic>());
707 std::shared_ptr<const Executor> exec,
const dim<2>& size,
709 std::shared_ptr<strategy_type> strategy);
725 std::shared_ptr<const Executor> exec,
const dim<2>& size,
728 std::shared_ptr<strategy_type> strategy =
729 std::make_shared<automatic>());
758 Hybrid(std::shared_ptr<const Executor> exec,
const dim<2>& size = {},
761 std::shared_ptr<strategy_type> strategy =
762 std::make_shared<automatic>());
776 void apply_impl(
const LinOp* b,
LinOp* x)
const override;
779 LinOp* x)
const override;
782 std::unique_ptr<ell_type> ell_;
783 std::unique_ptr<coo_type> coo_;
784 std::shared_ptr<strategy_type> strategy_;
788template <
typename ValueType,
typename IndexType>
789template <
typename HybType>
790std::shared_ptr<typename HybType::strategy_type>
794 std::is_same<HybType,
Hybrid<
typename HybType::value_type,
795 typename HybType::index_type>>::value,
796 "The given `HybType` type must be of type `matrix::Hybrid`!");
798 std::shared_ptr<typename HybType::strategy_type> strategy;
799 if (std::dynamic_pointer_cast<automatic>(strategy_)) {
800 strategy = std::make_shared<typename HybType::automatic>();
801 }
else if (
auto temp = std::dynamic_pointer_cast<minimal_storage_limit>(
804 if (
sizeof(value_type) ==
sizeof(
typename HybType::value_type) &&
805 sizeof(index_type) ==
sizeof(
typename HybType::index_type)) {
807 std::make_shared<typename HybType::minimal_storage_limit>();
809 strategy = std::make_shared<typename HybType::imbalance_limit>(
810 temp->get_percentage());
812 }
else if (
auto temp = std::dynamic_pointer_cast<imbalance_bounded_limit>(
814 strategy = std::make_shared<typename HybType::imbalance_bounded_limit>(
815 temp->get_percentage(), temp->get_ratio());
816 }
else if (
auto temp =
817 std::dynamic_pointer_cast<imbalance_limit>(strategy_)) {
818 strategy = std::make_shared<typename HybType::imbalance_limit>(
819 temp->get_percentage());
820 }
else if (
auto temp = std::dynamic_pointer_cast<column_limit>(strategy_)) {
821 strategy = std::make_shared<typename HybType::column_limit>(
822 temp->get_num_columns());
824 GKO_NOT_SUPPORTED(strategy_);
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition polymorphic_object.hpp:479
The EnableAbsoluteComputation mixin provides the default implementations of compute_absolute_linop an...
Definition lin_op.hpp:794
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition lin_op.hpp:879
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:668
Definition lin_op.hpp:117
A LinOp implementing this interface can read its data from a matrix_data structure.
Definition lin_op.hpp:605
A LinOp implementing this interface can write its data to a matrix_data structure.
Definition lin_op.hpp:660
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:166
value_type * get_data() noexcept
Returns a pointer to the block of memory used to store the elements of the array.
Definition array.hpp:687
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor associated with the array.
Definition array.hpp:703
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:696
size_type get_size() const noexcept
Returns the number of elements in the array.
Definition array.hpp:670
This type is a device-side equivalent to matrix_data.
Definition device_matrix_data.hpp:36
COO stores a matrix in the coordinate matrix format.
Definition coo.hpp:65
CSR is a matrix format which stores only the nonzero coefficients by compressing each row of the matr...
Definition csr.hpp:126
Dense is a matrix format which explicitly stores all values of the matrix.
Definition dense.hpp:120
ELL is a matrix format where stride with explicit zeros is used such that all rows have the same numb...
Definition ell.hpp:66
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:354
automatic()
Creates an automatic strategy.
Definition hybrid.hpp:352
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:199
column_limit(size_type num_column=0)
Creates a column_limit strategy.
Definition hybrid.hpp:195
auto get_num_columns() const
Get the number of columns limit.
Definition hybrid.hpp:210
imbalance_bounded_limit is a strategy_type which decides the number of stored elements per row of the...
Definition hybrid.hpp:270
auto get_percentage() const
Get the percent setting.
Definition hybrid.hpp:294
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:279
imbalance_bounded_limit(double percent=0.8, double ratio=0.0001)
Creates a imbalance_bounded_limit strategy.
Definition hybrid.hpp:275
auto get_ratio() const
Get the ratio setting.
Definition hybrid.hpp:301
imbalance_limit is a strategy_type which decides the number of stored elements per row of the ell par...
Definition hybrid.hpp:223
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:237
auto get_percentage() const
Get the percent setting.
Definition hybrid.hpp:259
imbalance_limit(double percent=0.8)
Creates a imbalance_limit strategy.
Definition hybrid.hpp:231
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:325
auto get_percentage() const
Get the percent setting.
Definition hybrid.hpp:336
minimal_storage_limit()
Creates a minimal_storage_limit strategy.
Definition hybrid.hpp:319
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.
strategy_type()
Creates a strategy_type.
Definition hybrid.hpp:99
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:135
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:116
size_type get_coo_nnz() const noexcept
Returns the number of nonzeros of the coo part.
Definition hybrid.hpp:145
HYBRID is a matrix format which splits the matrix into ELLPACK and COO format.
Definition hybrid.hpp:57
size_type get_num_stored_elements() const noexcept
Returns the number of elements explicitly stored in the matrix.
Definition hybrid.hpp:619
static std::unique_ptr< Hybrid > create(std::shared_ptr< const Executor > exec, const dim< 2 > &size, size_type num_stored_elements_per_row, size_type stride, std::shared_ptr< strategy_type > strategy)
Creates an uninitialized Hybrid matrix of the specified size and method.
index_type * get_coo_row_idxs() noexcept
Returns the row indexes of the coo part.
Definition hybrid.hpp:583
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:497
static std::unique_ptr< Hybrid > create(std::shared_ptr< const Executor > exec, const dim< 2 > &size, size_type num_stored_elements_per_row, std::shared_ptr< strategy_type > strategy=std::make_shared< automatic >())
Creates an uninitialized Hybrid matrix of the specified size and method.
size_type get_ell_stride() const noexcept
Returns the stride of the ell part.
Definition hybrid.hpp:474
size_type get_coo_num_stored_elements() const noexcept
Returns the number of elements explicitly stored in the coo part.
Definition hybrid.hpp:602
std::unique_ptr< absolute_type > compute_absolute() const override
Gets the AbsoluteLinOp.
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:528
index_type * get_coo_col_idxs() noexcept
Returns the column indexes of the coo part.
Definition hybrid.hpp:564
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:505
const index_type * get_const_coo_row_idxs() const noexcept
Returns the row indexes of the coo part.
Definition hybrid.hpp:592
Hybrid(Hybrid &&)
Move-assigns a Hybrid matrix.
const value_type * get_const_ell_values() const noexcept
Returns the values of the ell part.
Definition hybrid.hpp:435
const ell_type * get_ell() const noexcept
Returns the matrix of the ell part.
Definition hybrid.hpp:538
std::unique_ptr< Diagonal< ValueType > > extract_diagonal() const override
Extracts the diagonal entries of the matrix into a vector.
value_type * get_ell_values() noexcept
Returns the values of the ell part.
Definition hybrid.hpp:426
static std::unique_ptr< Hybrid > create(std::shared_ptr< const Executor > exec, std::shared_ptr< strategy_type > strategy=std::make_shared< automatic >())
Creates an uninitialized Hybrid matrix of specified method.
size_type get_ell_num_stored_elements() const noexcept
Returns the number of elements explicitly stored in the ell part.
Definition hybrid.hpp:481
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:464
const index_type * get_const_coo_col_idxs() const noexcept
Returns the column indexes of the coo part.
Definition hybrid.hpp:573
Hybrid(const Hybrid &)
Copy-assigns a Hybrid matrix.
index_type * get_ell_col_idxs() noexcept
Returns the column indexes of the ell part.
Definition hybrid.hpp:445
void compute_absolute_inplace() override
Compute absolute inplace on each element.
const index_type * get_const_ell_col_idxs() const noexcept
Returns the column indexes of the ell part.
Definition hybrid.hpp:454
std::shared_ptr< strategy_type > get_strategy() const noexcept
Returns the strategy.
Definition hybrid.hpp:630
const coo_type * get_coo() const noexcept
Returns the matrix of the coo part.
Definition hybrid.hpp:612
static std::unique_ptr< Hybrid > create(std::shared_ptr< const Executor > exec, const dim< 2 > &size, size_type num_stored_elements_per_row, size_type stride, size_type num_nonzeros={}, std::shared_ptr< strategy_type > strategy=std::make_shared< automatic >())
Creates an uninitialized Hybrid matrix of the specified size and method.
value_type * get_coo_values() noexcept
Returns the values of the coo part.
Definition hybrid.hpp:545
static std::unique_ptr< Hybrid > create(std::shared_ptr< const Executor > exec, const dim< 2 > &size, std::shared_ptr< strategy_type > strategy=std::make_shared< automatic >())
Creates an uninitialized Hybrid matrix of the specified size and method.
Hybrid & operator=(const Hybrid &)
Copy-assigns a Hybrid matrix.
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:520
const value_type * get_const_coo_values() const noexcept
Returns the values of the coo part.
Definition hybrid.hpp:554
Hybrid & operator=(Hybrid &&)
Move-assigns a Hybrid matrix.
The matrix namespace.
Definition dense_cache.hpp:24
The Ginkgo namespace.
Definition abstract_factory.hpp:20
typename detail::remove_complex_s< T >::type remove_complex
Obtain the type which removed the complex of complex/scalar type or the template parameter of class b...
Definition math.hpp:264
typename detail::to_complex_s< T >::type to_complex
Obtain the type which adds the complex of complex/scalar type or the template parameter of class by a...
Definition math.hpp:283
constexpr T zero()
Returns the additive identity for T.
Definition math.hpp:626
void write(StreamType &&os, MatrixPtrType &&matrix, layout_type layout=detail::mtx_io_traits< std::remove_cv_t< detail::pointee< MatrixPtrType > > >::default_layout)
Writes a matrix into an output stream in matrix market format.
Definition mtx_io.hpp:295
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:90
std::unique_ptr< MatrixType > read(StreamType &&is, MatrixArgs &&... args)
Reads a matrix stored in matrix market format from an input stream.
Definition mtx_io.hpp:159
typename detail::find_precision_impl< T, -step >::type previous_precision
Obtains the previous move type of T in the singly-linked precision corresponding bfloat16/half.
Definition math.hpp:473
typename detail::find_precision_impl< T, step >::type next_precision
Obtains the next move type of T in the singly-linked precision corresponding bfloat16/half.
Definition math.hpp:466
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