 |
Ginkgo
Generated from pipelines/2837190956 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
|
5 #ifndef GKO_PUBLIC_CORE_MATRIX_CSR_HPP_
6 #define GKO_PUBLIC_CORE_MATRIX_CSR_HPP_
9 #include <ginkgo/core/base/array.hpp>
10 #include <ginkgo/core/base/index_set.hpp>
11 #include <ginkgo/core/base/lin_op.hpp>
12 #include <ginkgo/core/base/math.hpp>
13 #include <ginkgo/core/matrix/device_views.hpp>
14 #include <ginkgo/core/matrix/permutation.hpp>
15 #include <ginkgo/core/matrix/scaled_permutation.hpp>
22 template <
typename ValueType>
25 template <
typename ValueType>
28 template <
typename ValueType,
typename IndexType>
31 template <
typename ValueType,
typename IndexType>
34 template <
typename ValueType,
typename IndexType>
37 template <
typename ValueType,
typename IndexType>
40 template <
typename ValueType,
typename IndexType>
43 template <
typename ValueType,
typename IndexType>
46 template <
typename ValueType,
typename IndexType>
49 template <
typename ValueType,
typename IndexType>
59 enum class spmv_strategy {
153 template <
typename ValueType = default_precision,
typename IndexType =
int32>
154 class Csr :
public LinOp,
156 public ConvertibleTo<Csr<next_precision<ValueType>, IndexType>>,
157 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
158 public ConvertibleTo<Csr<next_precision<ValueType, 2>, IndexType>>,
160 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
161 public ConvertibleTo<Csr<next_precision<ValueType, 3>, IndexType>>,
176 remove_complex<Csr<ValueType, IndexType>>>,
179 friend class Coo<ValueType, IndexType>;
180 friend class Dense<ValueType>;
181 friend class Diagonal<ValueType>;
182 friend class Ell<ValueType, IndexType>;
183 friend class Hybrid<ValueType, IndexType>;
184 friend class Sellp<ValueType, IndexType>;
185 friend class SparsityCsr<ValueType, IndexType>;
186 friend class Fbcsr<ValueType, IndexType>;
187 friend class CsrBuilder<ValueType, IndexType>;
189 GKO_ASSERT_SUPPORTED_VALUE_AND_INDEX_TYPE;
212 using value_type = ValueType;
213 using index_type = IndexType;
214 using transposed_type = Csr<ValueType, IndexType>;
218 using device_view = view::csr<value_type, index_type>;
219 using const_device_view = view::csr<const value_type, const index_type>;
221 class GKO_DEPRECATED(
222 "please use enum
gko::matrix::csr::spmv_strategy::<strategy>")
228 virtual csr::spmv_strategy get_enum()
const = 0;
231 class GKO_DEPRECATED(
232 "please use enum
gko::matrix::csr::spmv_strategy::classical")
classical
235 csr::spmv_strategy get_enum()
const override
237 return csr::spmv_strategy::classical;
241 class GKO_DEPRECATED(
242 "please use enum
gko::matrix::csr::spmv_strategy::merge_path")
245 csr::spmv_strategy get_enum()
const override
247 return csr::spmv_strategy::merge_path;
251 class GKO_DEPRECATED(
252 "please use enum
gko::matrix::csr::spmv_strategy::sparselib")
cusparse
255 csr::spmv_strategy get_enum()
const override
257 return csr::spmv_strategy::sparselib;
261 class GKO_DEPRECATED(
262 "please use enum
gko::matrix::csr::spmv_strategy::sparselib")
sparselib
265 csr::spmv_strategy get_enum()
const override
267 return csr::spmv_strategy::sparselib;
271 class GKO_DEPRECATED(
272 "please use enum
gko::matrix::csr::spmv_strategy::load_balance")
277 csr::spmv_strategy get_enum()
const override
279 return csr::spmv_strategy::load_balance;
283 class GKO_DEPRECATED(
284 "please use enum
gko::matrix::csr::spmv_strategy::automatic")
289 csr::spmv_strategy get_enum()
const override
291 return csr::spmv_strategy::automatic;
303 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
315 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
363 std::unique_ptr<LinOp>
transpose()
const override;
415 std::unique_ptr<lookup_data>
internal;
490 std::unique_ptr<lookup_data>
internal;
578 std::unique_ptr<lookup_data>
internal;
648 std::unique_ptr<Permutation<IndexType>> value_permutation;
697 bool invert =
false)
const;
741 bool invert =
false)
const;
773 bool invert =
false)
const;
775 std::unique_ptr<LinOp>
permute(
810 bool is_sorted_by_column_index()
const;
934 strategy_ = strategy;
947 GKO_ASSERT_EQUAL_DIMENSIONS(alpha,
dim<2>(1, 1));
960 GKO_ASSERT_EQUAL_DIMENSIONS(alpha,
dim<2>(1, 1));
974 static std::unique_ptr<Csr>
create(std::shared_ptr<const Executor> exec,
975 csr::spmv_strategy strategy);
988 static std::unique_ptr<Csr>
create(
989 std::shared_ptr<const Executor> exec,
const dim<2>& size = {},
991 csr::spmv_strategy strategy = csr::spmv_strategy::automatic);
1013 static std::unique_ptr<Csr>
create(
1014 std::shared_ptr<const Executor> exec,
const dim<2>& size,
1015 array<value_type> values, array<index_type> col_idxs,
1016 array<index_type> row_ptrs,
1017 csr::spmv_strategy strategy = csr::spmv_strategy::automatic);
1023 template <
typename InputValueType,
typename InputColumnIndexType,
1024 typename InputRowPtrType>
1026 "explicitly construct the gko::array argument instead of passing "
1027 "initializer lists")
1029 std::shared_ptr<const
Executor> exec, const
dim<2>& size,
1030 std::initializer_list<InputValueType> values,
1031 std::initializer_list<InputColumnIndexType> col_idxs,
1032 std::initializer_list<InputRowPtrType> row_ptrs)
1056 std::shared_ptr<const Executor> exec,
const dim<2>& size,
1057 gko::detail::const_array_view<ValueType>&& values,
1058 gko::detail::const_array_view<IndexType>&& col_idxs,
1059 gko::detail::const_array_view<IndexType>&& row_ptrs,
1060 csr::spmv_strategy strategy = csr::spmv_strategy::automatic);
1062 GKO_BEGIN_DISABLE_DEPRECATION_WARNINGS
1068 [[deprecated(
"please use enum version")]]
static std::unique_ptr<Csr>
1069 create(std::shared_ptr<const Executor> exec,
1070 std::shared_ptr<strategy_type> strategy);
1077 [[deprecated(
"please use enum version")]]
static std::unique_ptr<Csr>
1078 create(std::shared_ptr<const Executor> exec,
const dim<2>& size,
1088 [[deprecated(
"please use enum version")]]
static std::unique_ptr<const Csr>
1090 gko::detail::const_array_view<ValueType>&& values,
1091 gko::detail::const_array_view<IndexType>&& col_idxs,
1092 gko::detail::const_array_view<IndexType>&& row_ptrs,
1093 std::shared_ptr<strategy_type> strategy);
1095 GKO_END_DISABLE_DEPRECATION_WARNINGS
1125 const span& row_span,
const span& column_span)
const;
1152 Csr(std::shared_ptr<const Executor> exec,
const dim<2>& size = {},
1154 csr::spmv_strategy strategy = csr::spmv_strategy::automatic);
1156 Csr(std::shared_ptr<const Executor> exec,
const dim<2>& size,
1157 array<value_type> values, array<index_type> col_idxs,
1158 array<index_type> row_ptrs,
1159 csr::spmv_strategy strategy = csr::spmv_strategy::automatic);
1161 void apply_impl(
const LinOp* b,
LinOp* x)
const override;
1163 void apply_impl(
const LinOp* alpha,
const LinOp* b,
const LinOp* beta,
1164 LinOp* x)
const override;
1177 virtual void scale_impl(
const LinOp* alpha);
1185 virtual void inv_scale_impl(
const LinOp* alpha);
1194 csr::spmv_strategy get_actual_strategy() const noexcept;
1197 csr::spmv_strategy strategy_;
1198 array<value_type> values_;
1199 array<index_type> col_idxs_;
1200 array<index_type> row_ptrs_;
1201 array<index_type> srow_;
1202 index_type max_nnz_per_row_;
1204 void add_scaled_identity_impl(const
LinOp* a, const
LinOp* b) override;
1212 #endif // GKO_PUBLIC_CORE_MATRIX_CSR_HPP_
const index_type * get_const_srow() const noexcept
Returns the starting rows.
Definition: csr.hpp:895
Csr & operator=(const Csr &)
Copy-assigns a Csr matrix.
index_type * get_col_idxs() noexcept
Returns the column indexes of the matrix.
Definition: csr.hpp:848
Fixed-block compressed sparse row storage matrix format.
Definition: csr.hpp:47
CSR is a matrix format which stores only the nonzero coefficients by compressing each row of the matr...
Definition: matrix.hpp:30
const index_type * get_const_row_ptrs() const noexcept
Returns the row pointers of the matrix.
Definition: csr.hpp:876
void validate_data() const override
Throws gko::InvalidData exception if we found the data inside the object does not fulfill certain pro...
std::unique_ptr< Csr > multiply(ptr_param< const Csr > other) const
Computes the sparse matrix product this * other on the executor of this matrix.
Definition: lin_op.hpp:117
std::pair< std::unique_ptr< Csr >, scale_add_reuse_info > add_scale_reuse(ptr_param< const Dense< value_type >> scale_this, ptr_param< const Dense< value_type >> scale_other, ptr_param< const Csr > mtx_other) const
Computes the sparse matrix sum scale_this * this + scale_other * mtx_add on the executor of this matr...
std::pair< std::unique_ptr< Csr >, permuting_reuse_info > permute_reuse(ptr_param< const Permutation< index_type >> permutation, permute_mode mode=permute_mode::symmetric) const
Computes the operations necessary to propagate changed values from a matrix A to a permuted matrix.
Dense is a matrix format which explicitly stores all values of the matrix.
Definition: dense_cache.hpp:28
Class describing the internal lookup structures created by multiply_add_reuse to recompute a sparse m...
Definition: csr.hpp:454
std::unique_ptr< LinOp > inverse_row_permute(const array< IndexType > *inverse_permutation_indices) const override
Returns a LinOp representing the row permutation of the inverse permuted object.
SparsityCsr is a matrix format which stores only the sparsity pattern of a sparse matrix by compressi...
Definition: csr.hpp:41
std::pair< std::unique_ptr< Csr >, multiply_reuse_info > multiply_reuse(ptr_param< const Csr > other) const
Computes the sparse matrix product this * other on the executor of this matrix, and necessary data fo...
Events get reported to loggers attached to the triggering object and propagating loggers (Logger::nee...
void update_values(ptr_param< const Csr > mtx, ptr_param< const Dense< value_type >> scale_mult, ptr_param< const Csr > mtx_mult, ptr_param< const Dense< value_type >> scale_add, ptr_param< const Csr > mtx_add, ptr_param< Csr > out) const
Recomputes the sparse matrix-matrix product out = scale_mult * mtx * mtx_mult + scale_add * mtx_add w...
void scale(ptr_param< const LinOp > alpha)
Scales the matrix with a scalar.
Definition: csr.hpp:944
const_device_view get_const_device_view() const
Returns a non-owning const device view of this matrix.
Linear operators which support transposition should implement the Transposable interface.
Definition: lin_op.hpp:392
std::unique_ptr< LinOp > column_permute(const array< IndexType > *permutation_indices) const override
Returns a LinOp representing the column permutation of the Permutable object.
ScaledPermutation is a matrix combining a permutation with scaling factors.
Definition: scaled_permutation.hpp:35
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:101
index_type * get_srow() noexcept
Returns the starting rows.
Definition: csr.hpp:886
std::pair< std::unique_ptr< Csr >, permuting_reuse_info > transpose_reuse() const
Computes the necessary data to update a transposed matrix from its original matrix.
Permutation is a matrix format that represents a permutation matrix, i.e.
Definition: permutation.hpp:110
permuting_reuse_info()
Creates an empty reuse info.
std::unique_ptr< LinOp > row_permute(const array< IndexType > *permutation_indices) const override
Returns a LinOp representing the row permutation of the Permutable object.
Linear operators which support permutation should implement the Permutable interface.
Definition: lin_op.hpp:443
std::unique_ptr< LinOp > transpose() const override
Returns a LinOp representing the transpose of the Transposable object.
The Ginkgo namespace.
Definition: abstract_factory.hpp:19
static std::unique_ptr< Csr > create(std::shared_ptr< const Executor > exec, csr::spmv_strategy strategy)
Creates an uninitialized CSR matrix of the specified size.
void inv_scale(ptr_param< const LinOp > alpha)
Scales the matrix with the inverse of a scalar.
Definition: csr.hpp:957
std::unique_ptr< Diagonal< ValueType > > extract_diagonal() const override
Extracts the diagonal entries of the matrix into a vector.
std::unique_ptr< Csr > multiply_add(ptr_param< const Dense< value_type >> scale_mult, ptr_param< const Csr > mtx_mult, ptr_param< const Dense< value_type >> scale_add, ptr_param< const Csr > mtx_add) const
Computes the sparse matrix product scale_mult * this * mtx_mult + scale_add * mtx_add on the executor...
std::unique_ptr< LinOp > inverse_permute(const array< IndexType > *inverse_permutation_indices) const override
Returns a LinOp representing the symmetric inverse row and column permutation of the Permutable objec...
index_type * get_row_ptrs() noexcept
Returns the row pointers of the matrix.
Definition: csr.hpp:867
A span is a lightweight structure used to create sub-ranges from other ranges.
Definition: range.hpp:45
This structure is used as an intermediate data type to store a sparse matrix.
Definition: matrix_data.hpp:126
std::unique_ptr< Csr > permute(ptr_param< const Permutation< index_type >> permutation, permute_mode mode=permute_mode::symmetric) const
Creates a permuted copy of this matrix with the given permutation .
An index set class represents an ordered set of intervals.
Definition: index_set.hpp:55
void set_strategy(csr::spmv_strategy strategy)
Set the strategy.
Definition: csr.hpp:932
This class is used for function parameters in the place of raw pointers.
Definition: utils_helper.hpp:43
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 read its data from a matrix_data structure.
Definition: lin_op.hpp:564
std::unique_ptr< LinOp > conj_transpose() const override
Returns a LinOp representing the conjugate transpose of the Transposable object.
A LinOp implementing this interface can write its data to a matrix_data structure.
Definition: lin_op.hpp:619
The rows and columns will be permuted.
const value_type * get_const_values() const noexcept
Returns the values of the matrix.
Definition: csr.hpp:826
mode
The mode for the residual norm criterion.
Definition: residual_norm.hpp:37
size_type get_num_stored_elements() const noexcept
Returns the number of elements explicitly stored in the matrix.
Definition: csr.hpp:915
std::unique_ptr< Csr< ValueType, IndexType > > create_submatrix(const index_set< IndexType > &row_index_set, const index_set< IndexType > &column_index_set) const
Creates a submatrix from this Csr matrix given row and column index_set objects.
Adds the operation M <- a I + b M for matrix M, identity operator I and scalars a and b,...
Definition: lin_op.hpp:776
A struct describing a transformation of the matrix that reorders the values of the matrix into the tr...
Definition: csr.hpp:630
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 sort_by_column_index()
Sorts all (value, col_idx) pairs in each row by column index.
std::unique_ptr< Csr > scale_add(ptr_param< const Dense< value_type >> scale_this, ptr_param< const Dense< value_type >> scale_other, ptr_param< const Csr > mtx_other) const
Computes the sparse matrix sum scale_this * this + scale_other * mtx_add on the executor of this matr...
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
csr::spmv_strategy get_strategy() const noexcept
Returns the strategy.
void update_values(ptr_param< const Csr > input, ptr_param< Csr > output) const
Propagates the values from an input matrix to the transformed matrix.
This mixin is used to enable a default Cloneable::clone() implementation and similar for objects that...
Definition: polymorphic_object.hpp:376
void update_values(ptr_param< const Dense< value_type >> scale1, ptr_param< const Csr > mtx1, ptr_param< const Dense< value_type >> scale2, ptr_param< const Csr > mtx2, ptr_param< Csr > out) const
Recomputes the sparse matrix-matrix sum out = scale1 * mtx1 + scale2 * mtx2 when only the values of m...
device_view get_device_view()
Returns a non-owning device view of this matrix.
ELL is a matrix format where stride with explicit zeros is used such that all rows have the same numb...
Definition: csr.hpp:32
std::unique_ptr< const Dense< ValueType > > create_const_value_view() const
Creates a const Dense view of the value array of this matrix as a column vector of dimensions nnz x 1...
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition: polymorphic_object.hpp:147
std::unique_ptr< absolute_type > compute_absolute() const override
Gets the AbsoluteLinOp.
detail::temporary_clone< detail::pointee< Ptr > > make_temporary_clone(std::shared_ptr< const Executor > exec, Ptr &&ptr)
Creates a temporary_clone.
Definition: temporary_clone.hpp:208
std::pair< std::unique_ptr< Csr >, multiply_add_reuse_info > multiply_add_reuse(ptr_param< const Dense< value_type >> scale_mult, ptr_param< const Csr > mtx_mult, ptr_param< const Dense< value_type >> scale_add, ptr_param< const Csr > mtx_add) const
Computes the sparse matrix product scale_mult * this * mtx_mult + scale_add * mtx_add on the executor...
The first step in using the Ginkgo library consists of creating an executor.
Definition: executor.hpp:615
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
void write(mat_data &data) const override
Writes a matrix to a matrix_data structure.
permute_mode
Specifies how a permutation will be applied to a matrix.
Definition: permutation.hpp:42
void update_values(ptr_param< const Csr > mtx1, ptr_param< const Csr > mtx2, ptr_param< Csr > out) const
Recomputes the sparse matrix-matrix product out = mtx1 * mtx2 when only the values of mtx1 and mtx2 c...
SELL-P is a matrix format similar to ELL format.
Definition: csr.hpp:38
const index_type * get_const_col_idxs() const noexcept
Returns the column indexes of the matrix.
Definition: csr.hpp:857
The EnableAbsoluteComputation mixin provides the default implementations of compute_absolute_linop an...
Definition: lin_op.hpp:752
static std::unique_ptr< const Csr > create_const(std::shared_ptr< const Executor > exec, const dim< 2 > &size, gko::detail::const_array_view< ValueType > &&values, gko::detail::const_array_view< IndexType > &&col_idxs, gko::detail::const_array_view< IndexType > &&row_ptrs, csr::spmv_strategy strategy=csr::spmv_strategy::automatic)
Creates a constant (immutable) Csr matrix from a set of constant arrays.
std::unique_ptr< LinOp > inverse_column_permute(const array< IndexType > *inverse_permutation_indices) const override
Returns a LinOp representing the row permutation of the inverse permuted object.
Csr(const Csr &)
Copy-constructs a Csr matrix.
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition: polymorphic_object.hpp:69
size_type get_size() const noexcept
Returns the number of elements in the array.
Definition: array.hpp:669
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:263
void compute_absolute_inplace() override
Compute absolute inplace on each element.
std::unique_ptr< Csr > scale_permute(ptr_param< const ScaledPermutation< value_type, index_type >> permutation, permute_mode=permute_mode::symmetric) const
Creates a scaled and permuted copy of this matrix.
This type is a device-side equivalent to matrix_data.
Definition: device_matrix_data.hpp:36
void read(const mat_data &data) override
Reads a matrix from a matrix_data structure.
Non-owning view of a matrix::Csr to be used inside device kernels.
Definition: device_views.hpp:98
std::unique_ptr< Dense< ValueType > > create_value_view()
Creates a Dense view of the value array of this matrix as a column vector of dimensions nnz x 1.
value_type * get_values() noexcept
Returns the values of the matrix.
Definition: csr.hpp:817
Class describing the internal lookup structures created by multiply_reuse(const Csr*) to recompute a ...
Definition: csr.hpp:386
size_type get_num_srow_elements() const noexcept
Returns the number of the srow stored elements (involved warps)
Definition: csr.hpp:905
Class describing the internal lookup structures created by scale_add_reuse to recompute a sparse matr...
Definition: csr.hpp:546
LinOp(const LinOp &)=default
Copy-constructs a LinOp.
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