 |
Ginkgo
Generated from pipelines/2969282958 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
|
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>
22 template <
typename ValueType>
25 template <
typename ValueType,
typename IndexType>
40 template <
typename ValueType = default_precision,
typename IndexType =
int32>
43 public EnableCloneable<Hybrid<ValueType, IndexType>>,
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>>,
51 public ConvertibleTo<Dense<ValueType>>,
52 public ConvertibleTo<Csr<ValueType, IndexType>>,
53 public DiagonalExtractable<ValueType>,
54 public ReadableFromMatrixData<ValueType, IndexType>,
55 public WritableToMatrixData<ValueType, IndexType>,
56 public EnableAbsoluteComputation<
57 remove_complex<Hybrid<ValueType, IndexType>>> {
58 friend class EnableCloneable<
Hybrid>;
59 friend class Dense<ValueType>;
60 friend class Csr<ValueType, IndexType>;
62 GKO_ASSERT_SUPPORTED_VALUE_AND_INDEX_TYPE;
68 Hybrid<next_precision<ValueType>, IndexType>>::convert_to;
69 using ConvertibleTo<Hybrid<next_precision<ValueType>, IndexType>>::move_to;
70 using ConvertibleTo<Dense<ValueType>>::convert_to;
71 using ConvertibleTo<Dense<ValueType>>::move_to;
72 using ConvertibleTo<Csr<ValueType, IndexType>>::convert_to;
73 using ConvertibleTo<Csr<ValueType, IndexType>>::move_to;
76 using value_type = ValueType;
77 using index_type = IndexType;
78 using mat_data = matrix_data<ValueType, IndexType>;
79 using device_mat_data = device_matrix_data<ValueType, IndexType>;
80 using device_view = view::hybrid<value_type, index_type>;
81 using const_device_view = view::hybrid<const value_type, const index_type>;
82 using coo_type = Coo<ValueType, IndexType>;
83 using ell_type = Ell<ValueType, IndexType>;
84 using absolute_type = remove_complex<Hybrid>;
121 size_type* ell_num_stored_elements_per_row,
126 ref_row_nnz = row_nnz;
127 ell_num_stored_elements_per_row_ =
129 coo_nnz_ = this->compute_coo_nnz(ref_row_nnz);
130 *ell_num_stored_elements_per_row = ell_num_stored_elements_per_row_;
141 return ell_num_stored_elements_per_row_;
175 if (row_nnz_val[i] > ell_num_stored_elements_per_row_) {
177 row_nnz_val[i] - ell_num_stored_elements_per_row_;
184 size_type ell_num_stored_elements_per_row_;
200 : num_columns_(num_column)
237 percent_ = std::min(percent_, 1.0);
238 percent_ = std::max(percent_, 0.0);
244 auto row_nnz_val = row_nnz->
get_data();
245 auto num_rows = row_nnz->
get_size();
249 std::sort(row_nnz_val, row_nnz_val + num_rows);
251 auto percent_pos = static_cast<size_type>(num_rows * percent_);
252 return row_nnz_val[percent_pos];
254 return row_nnz_val[num_rows - 1];
286 auto num_rows = row_nnz->
get_size();
289 return std::min(ell_cols,
290 static_cast<size_type>(num_rows * ratio_));
326 (sizeof(ValueType) + 2 * sizeof(IndexType))))
375 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
389 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
411 void read(
const mat_data& data)
override;
413 void read(
const device_mat_data& data)
override;
415 void read(device_mat_data&& data)
override;
419 void write(mat_data& data)
const override;
443 return ell_->get_const_values();
462 return ell_->get_const_col_idxs();
472 return ell_->get_num_stored_elements_per_row();
489 return ell_->get_num_stored_elements();
505 return ell_->val_at(row, idx);
513 return ell_->val_at(row, idx);
528 return ell_->col_at(row, idx);
536 return ell_->col_at(row, idx);
562 return coo_->get_const_values();
581 return coo_->get_const_col_idxs();
600 return coo_->get_const_row_idxs();
610 return coo_->get_num_stored_elements();
627 return coo_->get_num_stored_elements() +
628 ell_->get_num_stored_elements();
648 template <
typename HybType>
649 std::shared_ptr<typename HybType::strategy_type>
get_strategy()
const;
675 static std::unique_ptr<Hybrid>
create(
676 std::shared_ptr<const Executor> exec,
677 std::shared_ptr<strategy_type> strategy =
678 std::make_shared<automatic>());
691 static std::unique_ptr<Hybrid>
create(
692 std::shared_ptr<const Executor> exec,
const dim<2>& size,
693 std::shared_ptr<strategy_type> strategy =
694 std::make_shared<automatic>());
708 static std::unique_ptr<Hybrid>
create(
709 std::shared_ptr<const Executor> exec,
const dim<2>& size,
711 std::shared_ptr<strategy_type> strategy =
712 std::make_shared<automatic>());
726 static std::unique_ptr<Hybrid>
create(
727 std::shared_ptr<const Executor> exec,
const dim<2>& size,
729 std::shared_ptr<strategy_type> strategy);
744 static std::unique_ptr<Hybrid>
create(
745 std::shared_ptr<const Executor> exec,
const dim<2>& size,
748 std::shared_ptr<strategy_type> strategy =
749 std::make_shared<automatic>());
778 Hybrid(std::shared_ptr<const Executor> exec,
const dim<2>& size = {},
781 std::shared_ptr<strategy_type> strategy =
782 std::make_shared<automatic>());
796 void apply_impl(
const LinOp* b,
LinOp* x)
const override;
799 LinOp* x)
const override;
802 std::unique_ptr<ell_type> ell_;
803 std::unique_ptr<coo_type> coo_;
804 std::shared_ptr<strategy_type> strategy_;
808 template <
typename ValueType,
typename IndexType>
809 template <
typename HybType>
810 std::shared_ptr<typename HybType::strategy_type>
814 std::is_same<HybType, Hybrid<
typename HybType::value_type,
815 typename HybType::index_type>>::value,
816 "The given `HybType` type must be of type `matrix::Hybrid`!");
818 std::shared_ptr<typename HybType::strategy_type> strategy;
819 if (std::dynamic_pointer_cast<automatic>(strategy_)) {
820 strategy = std::make_shared<typename HybType::automatic>();
821 }
else if (
auto temp = std::dynamic_pointer_cast<minimal_storage_limit>(
824 if (
sizeof(value_type) ==
sizeof(
typename HybType::value_type) &&
825 sizeof(index_type) ==
sizeof(
typename HybType::index_type)) {
827 std::make_shared<typename HybType::minimal_storage_limit>();
829 strategy = std::make_shared<typename HybType::imbalance_limit>(
830 temp->get_percentage());
832 }
else if (
auto temp = std::dynamic_pointer_cast<imbalance_bounded_limit>(
834 strategy = std::make_shared<typename HybType::imbalance_bounded_limit>(
835 temp->get_percentage(), temp->get_ratio());
836 }
else if (
auto temp =
837 std::dynamic_pointer_cast<imbalance_limit>(strategy_)) {
838 strategy = std::make_shared<typename HybType::imbalance_limit>(
839 temp->get_percentage());
840 }
else if (
auto temp = std::dynamic_pointer_cast<column_limit>(strategy_)) {
841 strategy = std::make_shared<typename HybType::column_limit>(
842 temp->get_num_columns());
844 GKO_NOT_SUPPORTED(strategy_);
854 #endif // GKO_PUBLIC_CORE_MATRIX_HYBRID_HPP_
const value_type * get_const_coo_values() const noexcept
Returns the values of the coo part.
Definition: hybrid.hpp:560
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:534
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:470
const index_type * get_const_coo_row_idxs() const noexcept
Returns the row indexes of the coo part.
Definition: hybrid.hpp:598
minimal_storage_limit()
Creates a minimal_storage_limit strategy.
Definition: hybrid.hpp:323
CSR is a matrix format which stores only the nonzero coefficients by compressing each row of the matr...
Definition: matrix.hpp:30
imbalance_bounded_limit(double percent=0.8, double ratio=0.0001)
Creates a imbalance_bounded_limit strategy.
Definition: hybrid.hpp:279
column_limit is a strategy_type which decides the number of stored elements per row of the ell part b...
Definition: hybrid.hpp:192
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:358
auto get_percentage() const
Get the percent setting.
Definition: hybrid.hpp:263
virtual void read(const matrix_data< ValueType, IndexType > &data)=0
Reads a matrix from a matrix_data structure.
Dense is a matrix format which explicitly stores all values of the matrix.
Definition: dense_cache.hpp:28
strategy_type is to decide how to set the hybrid config.
Definition: hybrid.hpp:96
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:120
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:526
const ell_type * get_ell() const noexcept
Returns the matrix of the ell part.
Definition: hybrid.hpp:544
value_type * get_coo_values() noexcept
Returns the values of the coo part.
Definition: hybrid.hpp:551
index_type * get_ell_col_idxs() noexcept
Returns the column indexes of the ell part.
Definition: hybrid.hpp:451
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:139
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:203
value_type * get_ell_values() noexcept
Returns the values of the ell part.
Definition: hybrid.hpp:432
automatic is a strategy_type which decides the number of stored elements per row of the ell part auto...
Definition: hybrid.hpp:351
size_type get_coo_num_stored_elements() const noexcept
Returns the number of elements explicitly stored in the coo part.
Definition: hybrid.hpp:608
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:101
void read(const mat_data &data) override
Reads a matrix from a matrix_data structure.
index_type * get_coo_row_idxs() noexcept
Returns the row indexes of the coo part.
Definition: hybrid.hpp:589
auto get_percentage() const
Get the percent setting.
Definition: hybrid.hpp:340
size_type get_ell_stride() const noexcept
Returns the stride of the ell part.
Definition: hybrid.hpp:480
const value_type * get_const_ell_values() const noexcept
Returns the values of the ell part.
Definition: hybrid.hpp:441
The Ginkgo namespace.
Definition: abstract_factory.hpp:19
const_device_view get_const_device_view() const
Returns a non-owning const device view of this matrix.
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:329
size_type get_num_stored_elements() const noexcept
Returns the number of elements explicitly stored in the matrix.
Definition: hybrid.hpp:625
index_type * get_coo_col_idxs() noexcept
Returns the column indexes of the coo part.
Definition: hybrid.hpp:570
const index_type * get_const_coo_col_idxs() const noexcept
Returns the column indexes of the coo part.
Definition: hybrid.hpp:579
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.
size_type get_coo_nnz() const noexcept
Returns the number of nonzeros of the coo part.
Definition: hybrid.hpp:149
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:241
imbalance_bounded_limit is a strategy_type which decides the number of stored elements per row of the...
Definition: hybrid.hpp:274
device_view get_device_view()
Returns a non-owning device view of this matrix.
value_type * get_data() noexcept
Returns a pointer to the block of memory used to store the elements of the array.
Definition: array.hpp:686
A LinOp implementing this interface can write its data to a matrix_data structure.
Definition: lin_op.hpp:719
std::shared_ptr< strategy_type > get_strategy() const noexcept
Returns the strategy.
Definition: hybrid.hpp:636
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.
strategy_type()
Creates a strategy_type.
Definition: hybrid.hpp:103
automatic()
Creates an automatic strategy.
Definition: hybrid.hpp:356
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor associated with the array.
Definition: array.hpp:702
const index_type * get_const_ell_col_idxs() const noexcept
Returns the column indexes of the ell part.
Definition: hybrid.hpp:460
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:465
void compute_absolute_inplace() override
Compute absolute inplace on each element.
void convert_to(result_type *result) const override
Converts the implementer to an object of type result_type.
Definition: polymorphic_object.hpp:411
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:472
auto get_num_columns() const
Get the number of columns limit.
Definition: hybrid.hpp:214
std::unique_ptr< absolute_type > compute_absolute() const override
Gets the AbsoluteLinOp.
column_limit(size_type num_column=0)
Creates a column_limit strategy.
Definition: hybrid.hpp:199
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:503
void write(mat_data &data) const override
Writes a matrix to a matrix_data structure.
imbalance_limit is a strategy_type which decides the number of stored elements per row of the ell par...
Definition: hybrid.hpp:227
ELL is a matrix format where stride with explicit zeros is used such that all rows have the same numb...
Definition: csr.hpp:32
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition: polymorphic_object.hpp:147
Hybrid(const Hybrid &)
Copy-assigns a Hybrid matrix.
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:511
HYBRID is a matrix format which splits the matrix into ELLPACK and COO format.
Definition: coo.hpp:32
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:695
size_type get_ell_num_stored_elements() const noexcept
Returns the number of elements explicitly stored in the ell part.
Definition: hybrid.hpp:487
std::unique_ptr< Diagonal< ValueType > > extract_diagonal() const override
Extracts the diagonal entries of the matrix into a vector.
void move_to(result_type *result) override
Converts the implementer to an object of type result_type by moving data from this object.
Definition: polymorphic_object.hpp:413
size_type get_size() const noexcept
Returns the number of elements in the array.
Definition: array.hpp:669
const coo_type * get_coo() const noexcept
Returns the matrix of the coo part.
Definition: hybrid.hpp:618
imbalance_limit(double percent=0.8)
Creates a imbalance_limit strategy.
Definition: hybrid.hpp:235
auto get_percentage() const
Get the percent setting.
Definition: hybrid.hpp:298
minimal_storage_limit is a strategy_type which decides the number of stored elements per row of the e...
Definition: hybrid.hpp:318
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:283
auto get_ratio() const
Get the ratio setting.
Definition: hybrid.hpp:305
LinOp(const LinOp &)=default
Copy-constructs a LinOp.
Hybrid & operator=(const Hybrid &)
Copy-assigns a Hybrid matrix.
constexpr T zero()
Returns the additive identity for T.
Definition: math.hpp:625
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:282
COO stores a matrix in the coordinate matrix format.
Definition: coo.hpp:49