Ginkgo Generated from branch based on master. Ginkgo version 1.8.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
diagonal.hpp
1// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#ifndef GKO_PUBLIC_CORE_MATRIX_DIAGONAL_HPP_
6#define GKO_PUBLIC_CORE_MATRIX_DIAGONAL_HPP_
7
8
9#include <ginkgo/core/base/array.hpp>
10#include <ginkgo/core/base/lin_op.hpp>
11
12
13namespace gko {
14namespace matrix {
15
16
17template <typename ValueType, typename IndexType>
18class Csr;
19
20template <typename ValueType>
21class Dense;
22
23
39template <typename ValueType = default_precision>
41 : public EnableLinOp<Diagonal<ValueType>>,
42 public ConvertibleTo<Csr<ValueType, int32>>,
43 public ConvertibleTo<Csr<ValueType, int64>>,
44 public ConvertibleTo<Diagonal<next_precision<ValueType>>>,
45 public Transposable,
46 public WritableToMatrixData<ValueType, int32>,
47 public WritableToMatrixData<ValueType, int64>,
48 public ReadableFromMatrixData<ValueType, int32>,
49 public ReadableFromMatrixData<ValueType, int64>,
50 public EnableAbsoluteComputation<remove_complex<Diagonal<ValueType>>> {
52 friend class Csr<ValueType, int32>;
53 friend class Csr<ValueType, int64>;
54 friend class Diagonal<to_complex<ValueType>>;
55
56public:
57 using EnableLinOp<Diagonal>::convert_to;
58 using EnableLinOp<Diagonal>::move_to;
59 using ConvertibleTo<Csr<ValueType, int32>>::convert_to;
61 using ConvertibleTo<Csr<ValueType, int64>>::convert_to;
65
66 using value_type = ValueType;
67 using index_type = int64;
72 using absolute_type = remove_complex<Diagonal>;
73
74 friend class Diagonal<next_precision<ValueType>>;
75
76 std::unique_ptr<LinOp> transpose() const override;
77
78 std::unique_ptr<LinOp> conj_transpose() const override;
79
80 void convert_to(Diagonal<next_precision<ValueType>>* result) const override;
81
82 void move_to(Diagonal<next_precision<ValueType>>* result) override;
83
84 void convert_to(Csr<ValueType, int32>* result) const override;
85
86 void move_to(Csr<ValueType, int32>* result) override;
87
88 void convert_to(Csr<ValueType, int64>* result) const override;
89
90 void move_to(Csr<ValueType, int64>* result) override;
91
92 std::unique_ptr<absolute_type> compute_absolute() const override;
93
94 void compute_absolute_inplace() override;
95
101 value_type* get_values() noexcept { return values_.get_data(); }
102
110 const value_type* get_const_values() const noexcept
111 {
112 return values_.get_const_data();
113 }
114
123 {
124 GKO_ASSERT_REVERSE_CONFORMANT(this, b);
125 GKO_ASSERT_EQUAL_ROWS(b, x);
126 GKO_ASSERT_EQUAL_COLS(this, x);
127
128 this->rapply_impl(b.get(), x.get());
129 }
130
141 {
142 GKO_ASSERT_CONFORMANT(this, b);
143 GKO_ASSERT_EQUAL_ROWS(b, x);
144 GKO_ASSERT_EQUAL_ROWS(this, x);
145
146 this->inverse_apply_impl(b.get(), x.get());
147 }
148
149 void read(const mat_data& data) override;
150
151 void read(const mat_data32& data) override;
152
153 void read(const device_mat_data& data) override;
154
155 void read(const device_mat_data32& data) override;
156
157 void read(device_mat_data&& data) override;
158
159 void read(device_mat_data32&& data) override;
160
161 void write(mat_data& data) const override;
162
163 void write(mat_data32& data) const override;
164
173 static std::unique_ptr<Diagonal> create(
174 std::shared_ptr<const Executor> exec, size_type size = 0);
175
190 static std::unique_ptr<Diagonal> create(
191 std::shared_ptr<const Executor> exec, const size_type size,
192 array<value_type> values);
193
198 template <typename InputValueType>
199 GKO_DEPRECATED(
200 "explicitly construct the gko::array argument instead of passing an"
201 "initializer list")
202 static std::unique_ptr<Diagonal> create(
203 std::shared_ptr<const Executor> exec, const size_type size,
204 std::initializer_list<InputValueType> values)
205 {
206 return create(exec, size, array<value_type>{exec, std::move(values)});
207 }
208
219 static std::unique_ptr<const Diagonal> create_const(
220 std::shared_ptr<const Executor> exec, size_type size,
221 gko::detail::const_array_view<ValueType>&& values);
222
223protected:
224 Diagonal(std::shared_ptr<const Executor> exec, size_type size = 0);
225
226 Diagonal(std::shared_ptr<const Executor> exec, const size_type size,
227 array<value_type> values);
228
229 void apply_impl(const LinOp* b, LinOp* x) const override;
230
231 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
232 LinOp* x) const override;
233
234 void rapply_impl(const LinOp* b, LinOp* x) const;
235
236 void inverse_apply_impl(const LinOp* b, LinOp* x) const;
237
238private:
239 array<value_type> values_;
240};
241
242
243} // namespace matrix
244} // namespace gko
245
246
247#endif // GKO_PUBLIC_CORE_MATRIX_DIAGONAL_HPP_
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition polymorphic_object.hpp:471
The EnableAbsoluteComputation mixin provides the default implementations of compute_absolute_linop an...
Definition lin_op.hpp:795
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition lin_op.hpp:880
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:663
The first step in using the Ginkgo library consists of creating an executor.
Definition executor.hpp:616
Definition lin_op.hpp:118
A LinOp implementing this interface can read its data from a matrix_data structure.
Definition lin_op.hpp:606
Linear operators which support transposition should implement the Transposable interface.
Definition lin_op.hpp:434
A LinOp implementing this interface can write its data to a matrix_data structure.
Definition lin_op.hpp:661
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition logger.hpp:25
value_type * get_data() noexcept
Returns a pointer to the block of memory used to store the elements of the array.
Definition array.hpp:674
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:683
This type is a device-side equivalent to matrix_data.
Definition device_matrix_data.hpp:36
CSR is a matrix format which stores only the nonzero coefficients by compressing each row of the matr...
Definition sparsity_csr.hpp:22
This class is a utility which efficiently implements the diagonal matrix (a linear operator which sca...
Definition diagonal.hpp:50
static std::unique_ptr< const Diagonal > create_const(std::shared_ptr< const Executor > exec, size_type size, gko::detail::const_array_view< ValueType > &&values)
Creates a constant (immutable) Diagonal matrix from a constant array.
void rapply(ptr_param< const LinOp > b, ptr_param< LinOp > x) const
Applies the diagonal matrix from the right side to a matrix b, which means scales the columns of b wi...
Definition diagonal.hpp:122
static std::unique_ptr< Diagonal > create(std::shared_ptr< const Executor > exec, size_type size=0)
Creates an Diagonal matrix of the specified size.
void inverse_apply(ptr_param< const LinOp > b, ptr_param< LinOp > x) const
Applies the inverse of the diagonal matrix to a matrix b, which means scales the columns of b with th...
Definition diagonal.hpp:140
value_type * get_values() noexcept
Returns a pointer to the array of values of the matrix.
Definition diagonal.hpp:101
std::unique_ptr< LinOp > conj_transpose() const override
Returns a LinOp representing the conjugate transpose of the Transposable object.
static std::unique_ptr< Diagonal > create(std::shared_ptr< const Executor > exec, const size_type size, array< value_type > values)
Creates a Diagonal matrix from an already allocated (and initialized) array.
std::unique_ptr< absolute_type > compute_absolute() const override
Gets the AbsoluteLinOp.
const value_type * get_const_values() const noexcept
Returns a pointer to the array of values of the matrix.
Definition diagonal.hpp:110
void compute_absolute_inplace() override
Compute absolute inplace on each element.
std::unique_ptr< LinOp > transpose() const override
Returns a LinOp representing the transpose of the Transposable object.
This class is used for function parameters in the place of raw pointers.
Definition utils_helper.hpp:43
T * get() const
Definition utils_helper.hpp:77
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:326
std::int32_t int32
32-bit signed integral type.
Definition types.hpp:103
typename detail::next_precision_impl< T >::type next_precision
Obtains the next type in the singly-linked precision list.
Definition math.hpp:462
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:345
std::int64_t int64
64-bit signed integral type.
Definition types.hpp:109
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:86
This structure is used as an intermediate data type to store a sparse matrix.
Definition matrix_data.hpp:127