 |
Ginkgo
Generated from pipelines/3136675824 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
|
5 #ifndef GKO_PUBLIC_CORE_BASE_LIN_OP_HPP_
6 #define GKO_PUBLIC_CORE_BASE_LIN_OP_HPP_
10 #include <type_traits>
13 #include <ginkgo/core/base/abstract_factory.hpp>
14 #include <ginkgo/core/base/device_matrix_data.hpp>
15 #include <ginkgo/core/base/dim.hpp>
16 #include <ginkgo/core/base/exception_helpers.hpp>
17 #include <ginkgo/core/base/math.hpp>
18 #include <ginkgo/core/base/matrix_assembly_data.hpp>
19 #include <ginkgo/core/base/matrix_data.hpp>
20 #include <ginkgo/core/base/multivector.hpp>
21 #include <ginkgo/core/base/polymorphic_object.hpp>
22 #include <ginkgo/core/base/types.hpp>
23 #include <ginkgo/core/base/utils.hpp>
24 #include <ginkgo/core/log/logger.hpp>
31 template <
typename ValueType>
34 template <
typename ValueType>
45 struct is_dense_ptr : std::false_type {};
48 struct is_dense_ptr<const T&> : is_dense_ptr<std::decay_t<T>> {};
51 struct is_dense_ptr<T&> : is_dense_ptr<std::decay_t<T>> {};
53 template <
typename ValueType>
54 struct is_dense_ptr<matrix::Dense<ValueType>*> : std::true_type {};
56 template <
typename ValueType>
57 struct is_dense_ptr<const matrix::Dense<ValueType>*> : std::true_type {};
59 template <
typename ValueType>
60 struct is_dense_ptr<std::shared_ptr<matrix::Dense<ValueType>>>
63 template <
typename ValueType>
64 struct is_dense_ptr<std::shared_ptr<const matrix::Dense<ValueType>>>
67 template <
typename ValueType>
68 struct is_dense_ptr<std::unique_ptr<matrix::Dense<ValueType>>>
71 template <
typename ValueType>
72 struct is_dense_ptr<std::unique_ptr<const matrix::Dense<ValueType>>>
80 constexpr
bool is_dense_ptr = detail::is_dense_ptr<T>::value;
179 this->
template log<log::Logger::linop_apply_started>(
this, b.
get(),
181 this->validate_application_parameters(b.
get(), x.
get());
185 this->
template log<log::Logger::linop_apply_completed>(
this, b.
get(),
211 this->
template log<log::Logger::linop_advanced_apply_started>(
213 this->validate_application_parameters(alpha.
get(), b.
get(), beta.
get(),
220 this->
template log<log::Logger::linop_advanced_apply_completed>(
224 template <
typename DenseIn,
typename DenseOut,
225 typename = std::enable_if_t<is_dense_ptr<DenseIn> &&
226 is_dense_ptr<DenseOut>>>
228 "Use apply(ptr_param<const AbstractMultiVector> b, "
229 "ptr_param<AbstractMultiVector> x) by storing vectors as "
230 "matrix::MultiVector")]]
void
231 apply(
const DenseIn& b, DenseOut&& x)
const
233 apply(b->as_const_multivector_view(), x->as_multivector_view());
236 template <
typename DenseAlpha,
typename DenseIn,
typename DenseBeta,
238 typename = std::enable_if_t<
239 is_dense_ptr<DenseAlpha> && is_dense_ptr<DenseIn> &&
240 is_dense_ptr<DenseBeta> && is_dense_ptr<DenseOut>>>
242 "Use apply(ptr_param<const AbstractMultiVector> alpha, ptr_param<const "
243 "AbstractMultiVector> b, ptr_param<const AbstractMultiVector> beta, "
244 "ptr_param<AbstractMultiVector> x) by storing vectors as "
245 "matrix::MultiVector")]]
void
246 apply(
const DenseAlpha& alpha,
const DenseIn& b,
const DenseBeta& beta,
249 apply(alpha->as_const_multivector_view(),
250 b->as_const_multivector_view(), beta->as_const_multivector_view(),
251 x->as_multivector_view());
280 if (
this != &other) {
281 PolymorphicObject::operator=(std::move(other));
282 this->set_size(other.get_size());
297 size_{std::exchange(other.size_,
dim<2>{})}
307 explicit LinOp(std::shared_ptr<const Executor> exec,
308 const dim<2>& size = dim<2>{})
309 : PolymorphicObject(exec), size_{size}
317 void set_size(
const dim<2>& value) noexcept { size_ = value; }
328 virtual void apply_impl(
const AbstractMultiVector* b,
329 AbstractMultiVector* x)
const = 0;
344 virtual void apply_impl(
const AbstractMultiVector* alpha,
345 const AbstractMultiVector* b,
346 const AbstractMultiVector* beta,
347 AbstractMultiVector* x)
const;
356 void validate_application_parameters(
const AbstractMultiVector* b,
357 const AbstractMultiVector* x)
const;
362 void validate_application_parameters(
const LinOp* b,
const LinOp* x)
const
364 GKO_ASSERT_CONFORMANT(
this, b);
365 GKO_ASSERT_EQUAL_ROWS(
this, x);
366 GKO_ASSERT_EQUAL_COLS(b, x);
378 void validate_application_parameters(
const AbstractMultiVector* alpha,
379 const AbstractMultiVector* b,
380 const AbstractMultiVector* beta,
381 const AbstractMultiVector* x)
const
383 this->validate_application_parameters(b, x);
384 GKO_ASSERT_EQUAL_DIMENSIONS(alpha, dim<2>(1, 1));
385 GKO_ASSERT_EQUAL_DIMENSIONS(beta, dim<2>(1, 1));
450 using AbstractFactory::AbstractFactory;
452 std::unique_ptr<LinOp> generate(std::shared_ptr<const LinOp> input)
const
454 this->
template log<log::Logger::linop_factory_generate_started>(
457 std::unique_ptr<LinOp> generated;
458 if (input->get_executor() == exec) {
464 this->
template log<log::Logger::linop_factory_generate_completed>(
465 this, input.get(), generated.get());
536 std::unique_ptr<LinOp>
generate_reuse(std::shared_ptr<const LinOp> input,
566 virtual std::unique_ptr<LinOp> generate_reuse_impl(
567 std::shared_ptr<const LinOp> input,
ReuseData& reuse_data)
const;
605 virtual std::unique_ptr<LinOp>
transpose()
const = 0;
613 virtual std::unique_ptr<LinOp> conj_transpose()
const = 0;
646 template <
typename IndexType>
668 return as<Permutable>(this->row_permute(permutation_indices))
669 ->column_permute(permutation_indices);
689 return as<Permutable>(this->inverse_row_permute(permutation_indices))
690 ->inverse_column_permute(permutation_indices);
706 virtual std::unique_ptr<LinOp> row_permute(
723 virtual std::unique_ptr<LinOp> column_permute(
739 virtual std::unique_ptr<LinOp> inverse_row_permute(
756 virtual std::unique_ptr<LinOp> inverse_column_permute(
767 template <
typename ValueType,
typename IndexType>
770 using value_type = ValueType;
771 using index_type = IndexType;
810 this->
read(data.copy_to_host());
822 template <
typename ValueType,
typename IndexType>
825 using value_type = ValueType;
826 using index_type = IndexType;
849 this->
write(host_data);
873 return preconditioner_;
884 preconditioner_ = new_precond;
888 std::shared_ptr<const LinOp> preconditioner_{};
910 virtual std::unique_ptr<LinOp> extract_diagonal_linop()
const = 0;
921 template <
typename ValueType>
924 using value_type = ValueType;
928 std::unique_ptr<LinOp> extract_diagonal_linop()
const override;
935 virtual std::unique_ptr<matrix::Diagonal<ValueType>> extract_diagonal()
974 template <
typename AbsoluteLinOp>
977 using absolute_type = AbsoluteLinOp;
1036 template <
typename ConcreteFactory,
typename ConcreteLinOp,
1037 typename ParametersType,
typename PolymorphicBase =
LinOpFactory>
1063 template <
typename ConcreteFactory,
typename ConcreteLinOp,
1064 typename ParametersType,
typename ReuseDataType,
1068 ParametersType, PolymorphicBase> {
1071 ParametersType, PolymorphicBase>;
1076 std::shared_ptr<const LinOp> input,
1079 return std::unique_ptr<ConcreteLinOp>(static_cast<ConcreteLinOp*>(
1084 std::unique_ptr<LinOpFactory::ReuseData> create_empty_reuse_data()
1087 return std::make_unique<ReuseDataType>();
1090 void check_reuse_consistent(
1092 const LinOpFactory::ReuseData& reuse_data)
const override
1094 ConcreteLinOp::check_reuse_consistent(
1095 static_cast<const ConcreteFactory*>(
this), input,
1096 *as<ReuseDataType>(&reuse_data));
1100 explicit EnableDefaultReusableLinOpFactory(
1101 std::shared_ptr<const Executor> exec,
1102 const ParametersType& parameters = {})
1103 : base_type(std::move(exec), parameters)
1106 std::unique_ptr<LinOp> generate_reuse_impl(
1107 std::shared_ptr<const LinOp> input,
1108 LinOpFactory::ReuseData& reuse_data)
const override
1110 return std::unique_ptr<LinOp>(
new ConcreteLinOp(
1111 static_cast<const ConcreteFactory*>(
this), std::move(input),
1112 *as<ReuseDataType>(&reuse_data)));
1192 #define GKO_ENABLE_LIN_OP_FACTORY(_lin_op, _parameters_name, _factory_name) \
1194 const _parameters_name##_type& get_##_parameters_name() const \
1196 return _parameters_name##_; \
1199 class _factory_name \
1200 : public ::gko::EnableDefaultLinOpFactory<_factory_name, _lin_op, \
1201 _parameters_name##_type> { \
1202 friend class ::gko::enable_parameters_type<_parameters_name##_type, \
1204 explicit _factory_name(std::shared_ptr<const ::gko::Executor> exec) \
1205 : ::gko::EnableDefaultLinOpFactory<_factory_name, _lin_op, \
1206 _parameters_name##_type>( \
1209 explicit _factory_name(std::shared_ptr<const ::gko::Executor> exec, \
1210 const _parameters_name##_type& parameters) \
1211 : ::gko::EnableDefaultLinOpFactory<_factory_name, _lin_op, \
1212 _parameters_name##_type>( \
1213 std::move(exec), parameters) \
1216 friend ::gko::EnableDefaultLinOpFactory<_factory_name, _lin_op, \
1217 _parameters_name##_type>; \
1221 _parameters_name##_type _parameters_name##_; \
1224 static_assert(true, \
1225 "This assert is used to counter the false positive extra " \
1226 "semi-colon warnings")
1265 #define GKO_ENABLE_LIN_OP_FACTORY_WITH_REUSE(_lin_op, _parameters_name, \
1266 _factory_name, _reuse_data_type) \
1268 const _parameters_name##_type& get_##_parameters_name() const \
1270 return _parameters_name##_; \
1273 class _factory_name : public ::gko::EnableDefaultReusableLinOpFactory< \
1274 _factory_name, _lin_op, _parameters_name##_type, \
1275 _reuse_data_type> { \
1276 friend class ::gko::enable_parameters_type<_parameters_name##_type, \
1278 explicit _factory_name(std::shared_ptr<const ::gko::Executor> exec) \
1279 : ::gko::EnableDefaultReusableLinOpFactory< \
1280 _factory_name, _lin_op, _parameters_name##_type, \
1281 _reuse_data_type>(std::move(exec)) \
1283 explicit _factory_name(std::shared_ptr<const ::gko::Executor> exec, \
1284 const _parameters_name##_type& parameters) \
1285 : ::gko::EnableDefaultReusableLinOpFactory< \
1286 _factory_name, _lin_op, _parameters_name##_type, \
1287 _reuse_data_type>(std::move(exec), parameters) \
1290 friend ::gko::EnableDefaultLinOpFactory<_factory_name, _lin_op, \
1291 _parameters_name##_type>; \
1292 friend ::gko::EnableDefaultReusableLinOpFactory< \
1293 _factory_name, _lin_op, _parameters_name##_type, _reuse_data_type>; \
1297 _parameters_name##_type _parameters_name##_; \
1300 static_assert(true, \
1301 "This assert is used to counter the false positive extra " \
1302 "semi-colon warnings")
1308 #endif // GKO_PUBLIC_CORE_BASE_LIN_OP_HPP_
host_type copy_to_host() const
Copies the device_matrix_data entries to the host to return a regular matrix_data object with the sam...
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:160
Definition: lin_op.hpp:163
EnableDefaultLinOpFactory with LinOpFactory::generate_reuse() support, usually used through GKO_ENABL...
Definition: lin_op.hpp:1066
Dense is a matrix format which explicitly stores all values of the matrix.
Definition: lin_op.hpp:35
A LinOp implementing this interface can be preconditioned.
Definition: lin_op.hpp:862
The AbstractFactory is a generic interface template that enables easy implementation of the abstract ...
Definition: abstract_factory.hpp:77
A PolymorphicObject is the abstract base for all "heavy" objects in Ginkgo that behave polymorphicall...
Definition: polymorphic_object.hpp:46
LinOp(LinOp &&other)
Move-constructs a LinOp.
Definition: lin_op.hpp:295
std::unique_ptr< abstract_product_type > generate(Args &&... args) const
Creates a new product from the given components.
Definition: abstract_factory.hpp:97
virtual std::unique_ptr< LinOp > permute(const array< IndexType > *permutation_indices) const
Returns a LinOp representing the symmetric row and column permutation of the Permutable object.
Definition: lin_op.hpp:665
std::shared_ptr< const Executor > get_executor() const
Returns the executor used to store the device_matrix_data entries.
Definition: device_matrix_data.hpp:151
Linear operators which support transposition should implement the Transposable interface.
Definition: lin_op.hpp:596
static device_matrix_data create_from_host(std::shared_ptr< const Executor > exec, const host_type &data)
Creates a device_matrix_data object from the given host data on the given executor.
The AbsoluteComputable is an interface that allows to get the component wise absolute of a LinOp.
Definition: lin_op.hpp:945
T * get() const
Definition: utils_helper.hpp:77
virtual std::unique_ptr< LinOp > compute_absolute_linop() const =0
Gets the absolute LinOp.
This mixin provides a default implementation of a concrete factory.
Definition: abstract_factory.hpp:154
detail::cloned_type< Pointer > clone(const Pointer &p)
Creates a unique clone of the object pointed to by p.
Definition: utils_helper.hpp:425
Linear operators which support permutation should implement the Permutable interface.
Definition: lin_op.hpp:647
LinOp & operator=(LinOp &&other)
Move-assigns a LinOp.
Definition: lin_op.hpp:278
virtual std::shared_ptr< const LinOp > get_preconditioner() const
Returns the preconditioner operator used by the Preconditionable.
Definition: lin_op.hpp:871
The Ginkgo namespace.
Definition: abstract_factory.hpp:19
virtual void read(const device_matrix_data< ValueType, IndexType > &data)
Reads a matrix from a device_matrix_data structure.
Definition: lin_op.hpp:797
void read(const matrix_assembly_data< ValueType, IndexType > &data)
Reads a matrix from a matrix_assembly_data structure.
Definition: lin_op.hpp:787
This structure is used as an intermediate data type to store a sparse matrix.
Definition: matrix_data.hpp:126
virtual bool apply_uses_initial_guess() const
Returns true if the linear operator uses the data given in x as an initial guess.
Definition: lin_op.hpp:268
virtual std::unique_ptr< LinOp > inverse_permute(const array< IndexType > *permutation_indices) const
Returns a LinOp representing the symmetric inverse row and column permutation of the Permutable objec...
Definition: lin_op.hpp:686
virtual void check_reuse_consistent(const LinOp *input, const ReuseData &reuse_data) const
Throws if input can't be used with reuse_data.
This class is a utility which efficiently implements the diagonal matrix (a linear operator which sca...
Definition: lin_op.hpp:32
std::unique_ptr< ConcreteLinOp > generate_reuse(std::shared_ptr< const LinOp > input, LinOpFactory::ReuseData &reuse_data) const
LinOpFactory::generate_reuse(), returning the concrete LinOp type.
Definition: lin_op.hpp:1075
matrix_data< ValueType, IndexType > get_ordered_data() const
Definition: matrix_assembly_data.hpp:142
virtual void set_preconditioner(std::shared_ptr< const LinOp > new_precond)
Sets the preconditioner operator used by the Preconditionable.
Definition: lin_op.hpp:882
This class is used for function parameters in the place of raw pointers.
Definition: utils_helper.hpp:43
virtual std::unique_ptr< ReuseData > create_empty_reuse_data() const
Creates an empty ReuseData.
std::unique_ptr< LinOp > generate_reuse(std::shared_ptr< const LinOp > input, ReuseData &reuse_data) const
Generates a LinOp like generate(), reusing what earlier calls recorded in reuse_data.
virtual void compute_absolute_inplace()=0
Compute absolute inplace on each element.
virtual void read(device_matrix_data< ValueType, IndexType > &&data)
Reads a matrix from a device_matrix_data structure.
Definition: lin_op.hpp:808
void add_scaled_identity(ptr_param< const AbstractMultiVector > a, ptr_param< const AbstractMultiVector > b)
Scales this and adds another scalar times the identity to it.
A LinOp implementing this interface can read its data from a matrix_data structure.
Definition: lin_op.hpp:768
A LinOp implementing this interface can write its data to a matrix_data structure.
Definition: lin_op.hpp:823
LinOp & operator=(const LinOp &)=default
Copy-assigns a LinOp.
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:299
batch_dim< 2, DimensionType > transpose(const batch_dim< 2, DimensionType > &input)
Returns a batch_dim object with its dimensions swapped for batched operators.
Definition: batch_dim.hpp:118
void apply(ptr_param< const AbstractMultiVector > b, ptr_param< AbstractMultiVector > x) const
Applies a linear operator to a vector (or a sequence of vectors).
Definition: lin_op.hpp:176
Adds the operation M <- a I + b M for matrix M, identity operator I and scalars a and b,...
Definition: lin_op.hpp:999
This structure is used as an intermediate type to assemble a sparse matrix.
Definition: matrix_assembly_data.hpp:59
Opaque base for the data a factory records in generate_reuse() to speed up later generations.
Definition: lin_op.hpp:475
virtual void write(device_matrix_data< ValueType, IndexType > &data) const
Writes a matrix to a device_matrix_data structure.
Definition: lin_op.hpp:846
virtual std::unique_ptr< absolute_type > compute_absolute() const =0
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
void apply(ptr_param< const AbstractMultiVector > alpha, ptr_param< const AbstractMultiVector > b, ptr_param< const AbstractMultiVector > beta, ptr_param< AbstractMultiVector > x) const
Performs the operation x = alpha * op(b) + beta * x.
Definition: lin_op.hpp:206
The EnableAbsoluteComputation mixin provides the default implementations of compute_absolute_linop an...
Definition: lin_op.hpp:975
A LinOpFactory represents a higher order mapping which transforms one linear operator into another.
Definition: lin_op.hpp:447
std::unique_ptr< LinOp > compute_absolute_linop() const override
Gets the absolute LinOp.
Definition: lin_op.hpp:981
Abstract interface class for (multi-)vector types.
Definition: multivector.hpp:81
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition: polymorphic_object.hpp:69
const dim< 2 > & get_size() const noexcept
Returns the size of the operator.
Definition: lin_op.hpp:259
This type is a device-side equivalent to matrix_data.
Definition: device_matrix_data.hpp:36
LinOp(const LinOp &)=default
Copy-constructs a LinOp.