Ginkgo: ginkgo/core/matrix/diagonal.hpp Source File

| Ginkgo Generated from branch based on main. Ginkgo version 1.11.0 A numerical linear algebra library targeting many-core architectures

Loading...
Searching...
No Matches
diagonal.hpp
1// SPDX-FileCopyrightText: 2017 - 2025 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>
40 class Diagonal
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#if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
46 public ConvertibleTo<Diagonal<next_precision<ValueType, 2>>>,
47#endif
48#if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
49 public ConvertibleTo<Diagonal<next_precision<ValueType, 3>>>,
50#endif
51 public Transposable,
52 public WritableToMatrixData<ValueType, int32>,
53 public WritableToMatrixData<ValueType, int64>,
54 public ReadableFromMatrixData<ValueType, int32>,
55 public ReadableFromMatrixData<ValueType, int64>,
56 public EnableAbsoluteComputation<remove_complex<Diagonal<ValueType>>> {
57 friend class EnablePolymorphicObject<Diagonal, LinOp>;
58 friend class Csr<ValueType, int32>;
59 friend class Csr<ValueType, int64>;
60 friend class Diagonal<to_complex<ValueType>>;
61 GKO_ASSERT_SUPPORTED_VALUE_TYPE;
62
63public:
64 using EnableLinOp<Diagonal>::convert_to;
65 using EnableLinOp<Diagonal>::move_to;
66 using ConvertibleTo<Csr<ValueType, int32>>::convert_to;
67 using ConvertibleTo<Csr<ValueType, int32>>::move_to;
68 using ConvertibleTo<Csr<ValueType, int64>>::convert_to;
69 using ConvertibleTo<Csr<ValueType, int64>>::move_to;
70 using ConvertibleTo<Diagonal<next_precision<ValueType>>>::convert_to;
71 using ConvertibleTo<Diagonal<next_precision<ValueType>>>::move_to;
72
73 using value_type = ValueType;
74 using index_type = int64;
75 using mat_data = matrix_data<ValueType, int64>;
76 using mat_data32 = matrix_data<ValueType, int32>;
77 using device_mat_data = device_matrix_data<ValueType, int64>;
78 using device_mat_data32 = device_matrix_data<ValueType, int32>;
79 using absolute_type = remove_complex<Diagonal>;
80
81 friend class Diagonal<previous_precision<ValueType>>;
82
83 std::unique_ptr<LinOp> transpose() const override;
84
85 std::unique_ptr<LinOp> conj_transpose() const override;
86
87 void convert_to(Diagonal<next_precision<ValueType>>* result) const override;
88
89 void move_to(Diagonal<next_precision<ValueType>>* result) override;
90
91#if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
92 friend class Diagonal<previous_precision<ValueType, 2>>;
93 using ConvertibleTo<Diagonal<next_precision<ValueType, 2>>>::convert_to;
94 using ConvertibleTo<Diagonal<next_precision<ValueType, 2>>>::move_to;
95
96 void convert_to(
97 Diagonal<next_precision<ValueType, 2>>* result) const override;
98
99 void move_to(Diagonal<next_precision<ValueType, 2>>* result) override;
100#endif
101
102#if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
103 friend class Diagonal<previous_precision<ValueType, 3>>;
104 using ConvertibleTo<Diagonal<next_precision<ValueType, 3>>>::convert_to;
105 using ConvertibleTo<Diagonal<next_precision<ValueType, 3>>>::move_to;
106
107 void convert_to(
108 Diagonal<next_precision<ValueType, 3>>* result) const override;
109
110 void move_to(Diagonal<next_precision<ValueType, 3>>* result) override;
111#endif
112
113 void convert_to(Csr<ValueType, int32>* result) const override;
114
115 void move_to(Csr<ValueType, int32>* result) override;
116
117 void convert_to(Csr<ValueType, int64>* result) const override;
118
119 void move_to(Csr<ValueType, int64>* result) override;
120
121 std::unique_ptr<absolute_type> compute_absolute() const override;
122
123 void compute_absolute_inplace() override;
124
130 value_type* get_values() noexcept { return values_.get_data(); }
131
139 const value_type* get_const_values() const noexcept
140 {
141 return values_.get_const_data();
142 }
143
151 void rapply(ptr_param<const LinOp> b, ptr_param<LinOp> x) const
152 {
153 GKO_ASSERT_REVERSE_CONFORMANT(this, b);
154 GKO_ASSERT_EQUAL_ROWS(b, x);
155 GKO_ASSERT_EQUAL_COLS(this, x);
156
157 this->rapply_impl(b.get(), x.get());
158 }
159
169 void inverse_apply(ptr_param<const LinOp> b, ptr_param<LinOp> x) const
170 {
171 GKO_ASSERT_CONFORMANT(this, b);
172 GKO_ASSERT_EQUAL_ROWS(b, x);
173 GKO_ASSERT_EQUAL_ROWS(this, x);
174
175 this->inverse_apply_impl(b.get(), x.get());
176 }
177
178 void read(const mat_data& data) override;
179
180 void read(const mat_data32& data) override;
181
182 void read(const device_mat_data& data) override;
183
184 void read(const device_mat_data32& data) override;
185
186 void read(device_mat_data&& data) override;
187
188 void read(device_mat_data32&& data) override;
189
190 void write(mat_data& data) const override;
191
192 void write(mat_data32& data) const override;
193
202 static std::unique_ptr<Diagonal> create(
203 std::shared_ptr<const Executor> exec, size_type size = 0);
204
219 static std::unique_ptr<Diagonal> create(
220 std::shared_ptr<const Executor> exec, const size_type size,
221 array<value_type> values);
222
227 template <typename InputValueType>
228 GKO_DEPRECATED(
229 "explicitly construct the gko::array argument instead of passing an"
230 "initializer list")
231 static std::unique_ptr<Diagonal> create(
232 std::shared_ptr<const Executor> exec, const size_type size,
233 std::initializer_list<InputValueType> values)
234 {
235 return create(exec, size, array<value_type>{exec, std::move(values)});
236 }
237
248 static std::unique_ptr<const Diagonal> create_const(
249 std::shared_ptr<const Executor> exec, size_type size,
250 gko::detail::const_array_view<ValueType>&& values);
251
252protected:
253 Diagonal(std::shared_ptr<const Executor> exec, size_type size = 0);
254
255 Diagonal(std::shared_ptr<const Executor> exec, const size_type size,
256 array<value_type> values);
257
258 void apply_impl(const LinOp* b, LinOp* x) const override;
259
260 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
261 LinOp* x) const override;
262
263 void rapply_impl(const LinOp* b, LinOp* x) const;
264
265 void inverse_apply_impl(const LinOp* b, LinOp* x) const;
266
267private:
268 array<value_type> values_;
269};
270
271
272} // namespace matrix
273} // namespace gko
274
275
276#endif // GKO_PUBLIC_CORE_MATRIX_DIAGONAL_HPP_
gko::EnableAbsoluteComputation
The EnableAbsoluteComputation mixin provides the default implementations of compute_absolute_linop an...
Definition lin_op.hpp:794
gko::EnableLinOp
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition lin_op.hpp:879
gko::EnablePolymorphicObject
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:668
gko::Executor
The first step in using the Ginkgo library consists of creating an executor.
Definition executor.hpp:615
gko::LinOp
Definition lin_op.hpp:117
gko::ReadableFromMatrixData
A LinOp implementing this interface can read its data from a matrix_data structure.
Definition lin_op.hpp:605
gko::Transposable
Linear operators which support transposition should implement the Transposable interface.
Definition lin_op.hpp:433
gko::WritableToMatrixData
A LinOp implementing this interface can write its data to a matrix_data structure.
Definition lin_op.hpp:660
gko::array
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:166
gko::device_matrix_data
This type is a device-side equivalent to matrix_data.
Definition device_matrix_data.hpp:36
gko::matrix::Csr
CSR is a matrix format which stores only the nonzero coefficients by compressing each row of the matr...
Definition csr.hpp:126
gko::matrix::Dense
Dense is a matrix format which explicitly stores all values of the matrix.
Definition dense.hpp:120
gko::matrix::Diagonal::create_const
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.
gko::matrix::Diagonal::rapply
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:151
gko::matrix::Diagonal::create
static std::unique_ptr< Diagonal > create(std::shared_ptr< const Executor > exec, size_type size=0)
Creates an Diagonal matrix of the specified size.
gko::matrix::Diagonal::inverse_apply
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:169
gko::matrix::Diagonal::get_values
value_type * get_values() noexcept
Returns a pointer to the array of values of the matrix.
Definition diagonal.hpp:130
gko::matrix::Diagonal::conj_transpose
std::unique_ptr< LinOp > conj_transpose() const override
Returns a LinOp representing the conjugate transpose of the Transposable object.
gko::matrix::Diagonal::create
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.
gko::matrix::Diagonal::compute_absolute
std::unique_ptr< absolute_type > compute_absolute() const override
Gets the AbsoluteLinOp.
gko::matrix::Diagonal::get_const_values
const value_type * get_const_values() const noexcept
Returns a pointer to the array of values of the matrix.
Definition diagonal.hpp:139
gko::matrix::Diagonal::compute_absolute_inplace
void compute_absolute_inplace() override
Compute absolute inplace on each element.
gko::matrix::Diagonal::transpose
std::unique_ptr< LinOp > transpose() const override
Returns a LinOp representing the transpose of the Transposable object.
gko::ptr_param
This class is used for function parameters in the place of raw pointers.
Definition utils_helper.hpp:41
gko::ptr_param::get
T * get() const
Definition utils_helper.hpp:75
gko::matrix
The matrix namespace.
Definition dense_cache.hpp:24
gko
The Ginkgo namespace.
Definition abstract_factory.hpp:20
gko::remove_complex
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
gko::int32
std::int32_t int32
32-bit signed integral type.
Definition types.hpp:107
gko::to_complex
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
gko::int64
std::int64_t int64
64-bit signed integral type.
Definition types.hpp:113
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:90
gko::read
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
gko::previous_precision
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
gko::next_precision
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
gko::write
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
std
STL namespace.
gko::matrix_data
This structure is used as an intermediate data type to store a sparse matrix.
Definition matrix_data.hpp:126