Ginkgo Generated from branch based on master. Ginkgo version 1.7.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
diagonal.hpp
1/*******************************<GINKGO LICENSE>******************************
2Copyright (c) 2017-2023, the Ginkgo authors
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
8
91. Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
11
122. Redistributions in binary form must reproduce the above copyright
13notice, this list of conditions and the following disclaimer in the
14documentation and/or other materials provided with the distribution.
15
163. Neither the name of the copyright holder nor the names of its
17contributors may be used to endorse or promote products derived from
18this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31******************************<GINKGO LICENSE>*******************************/
32
33#ifndef GKO_PUBLIC_CORE_MATRIX_DIAGONAL_HPP_
34#define GKO_PUBLIC_CORE_MATRIX_DIAGONAL_HPP_
35
36
37#include <ginkgo/core/base/array.hpp>
38#include <ginkgo/core/base/lin_op.hpp>
39
40
41namespace gko {
42namespace matrix {
43
44
45template <typename ValueType, typename IndexType>
46class Csr;
47
48template <typename ValueType>
49class Dense;
50
51
67template <typename ValueType = default_precision>
69 : public EnableLinOp<Diagonal<ValueType>>,
70 public EnableCreateMethod<Diagonal<ValueType>>,
71 public ConvertibleTo<Csr<ValueType, int32>>,
72 public ConvertibleTo<Csr<ValueType, int64>>,
73 public ConvertibleTo<Diagonal<next_precision<ValueType>>>,
74 public Transposable,
75 public WritableToMatrixData<ValueType, int32>,
76 public WritableToMatrixData<ValueType, int64>,
77 public ReadableFromMatrixData<ValueType, int32>,
78 public ReadableFromMatrixData<ValueType, int64>,
79 public EnableAbsoluteComputation<remove_complex<Diagonal<ValueType>>> {
81 friend class EnableCreateMethod<Diagonal>;
82 friend class Csr<ValueType, int32>;
83 friend class Csr<ValueType, int64>;
84 friend class Diagonal<to_complex<ValueType>>;
85
86public:
87 using EnableLinOp<Diagonal>::convert_to;
88 using EnableLinOp<Diagonal>::move_to;
89 using ConvertibleTo<Csr<ValueType, int32>>::convert_to;
91 using ConvertibleTo<Csr<ValueType, int64>>::convert_to;
95
96 using value_type = ValueType;
97 using index_type = int64;
102 using absolute_type = remove_complex<Diagonal>;
103
104 friend class Diagonal<next_precision<ValueType>>;
105
106 std::unique_ptr<LinOp> transpose() const override;
107
108 std::unique_ptr<LinOp> conj_transpose() const override;
109
110 void convert_to(Diagonal<next_precision<ValueType>>* result) const override;
111
112 void move_to(Diagonal<next_precision<ValueType>>* result) override;
113
114 void convert_to(Csr<ValueType, int32>* result) const override;
115
116 void move_to(Csr<ValueType, int32>* result) override;
117
118 void convert_to(Csr<ValueType, int64>* result) const override;
119
120 void move_to(Csr<ValueType, int64>* result) override;
121
122 std::unique_ptr<absolute_type> compute_absolute() const override;
123
125
131 value_type* get_values() noexcept { return values_.get_data(); }
132
140 const value_type* get_const_values() const noexcept
141 {
142 return values_.get_const_data();
143 }
144
153 {
154 GKO_ASSERT_REVERSE_CONFORMANT(this, b);
155 GKO_ASSERT_EQUAL_ROWS(b, x);
156 GKO_ASSERT_EQUAL_COLS(this, x);
157
158 this->rapply_impl(b.get(), x.get());
159 }
160
171 {
172 GKO_ASSERT_CONFORMANT(this, b);
173 GKO_ASSERT_EQUAL_ROWS(b, x);
174 GKO_ASSERT_EQUAL_ROWS(this, x);
175
176 this->inverse_apply_impl(b.get(), x.get());
177 }
178
179 void read(const mat_data& data) override;
180
181 void read(const mat_data32& data) override;
182
183 void read(const device_mat_data& data) override;
184
185 void read(const device_mat_data32& data) override;
186
187 void read(device_mat_data&& data) override;
188
189 void read(device_mat_data32&& data) override;
190
191 void write(mat_data& data) const override;
192
193 void write(mat_data32& data) const override;
194
205 static std::unique_ptr<const Diagonal> create_const(
206 std::shared_ptr<const Executor> exec, size_type size,
207 gko::detail::const_array_view<ValueType>&& values)
208 {
209 // cast const-ness away, but return a const object afterwards,
210 // so we can ensure that no modifications take place.
211 return std::unique_ptr<const Diagonal>(new Diagonal{
212 exec, size, gko::detail::array_const_cast(std::move(values))});
213 }
214
215protected:
221 explicit Diagonal(std::shared_ptr<const Executor> exec)
222 : Diagonal(std::move(exec), size_type{})
223 {}
224
231 Diagonal(std::shared_ptr<const Executor> exec, size_type size)
232 : EnableLinOp<Diagonal>(exec, dim<2>{size}), values_(exec, size)
233 {}
234
249 template <typename ValuesArray>
250 Diagonal(std::shared_ptr<const Executor> exec, const size_type size,
251 ValuesArray&& values)
252 : EnableLinOp<Diagonal>(exec, dim<2>(size)),
253 values_{exec, std::forward<ValuesArray>(values)}
254 {
255 GKO_ENSURE_IN_BOUNDS(size - 1, values_.get_num_elems());
256 }
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_
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition polymorphic_object.hpp:499
The EnableAbsoluteComputation mixin provides the default implementations of compute_absolute_linop an...
Definition lin_op.hpp:823
This mixin implements a static create() method on ConcreteType that dynamically allocates the memory,...
Definition polymorphic_object.hpp:776
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition lin_op.hpp:908
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:691
Definition lin_op.hpp:146
A LinOp implementing this interface can read its data from a matrix_data structure.
Definition lin_op.hpp:634
Linear operators which support transposition should implement the Transposable interface.
Definition lin_op.hpp:462
A LinOp implementing this interface can write its data to a matrix_data structure.
Definition lin_op.hpp:689
value_type * get_data() noexcept
Returns a pointer to the block of memory used to store the elements of the array.
Definition array.hpp:646
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:655
size_type get_num_elems() const noexcept
Returns the number of elements in the array.
Definition array.hpp:637
This type is a device-side equivalent to matrix_data.
Definition device_matrix_data.hpp:63
CSR is a matrix format which stores only the nonzero coefficients by compressing each row of the matr...
Definition csr.hpp:146
This class is a utility which efficiently implements the diagonal matrix (a linear operator which sca...
Definition diagonal.hpp:79
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.
Definition diagonal.hpp:205
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:152
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:170
value_type * get_values() noexcept
Returns a pointer to the array of values of the matrix.
Definition diagonal.hpp:131
std::unique_ptr< LinOp > conj_transpose() const override
Returns a LinOp representing the conjugate transpose of the Transposable object.
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:140
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:71
T * get() const
Definition utils_helper.hpp:105
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
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:354
std::int32_t int32
32-bit signed integral type.
Definition types.hpp:137
typename detail::next_precision_impl< T >::type next_precision
Obtains the next type in the singly-linked precision list.
Definition math.hpp:490
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:373
std::int64_t int64
64-bit signed integral type.
Definition types.hpp:143
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:120
A type representing the dimensions of a multidimensional object.
Definition dim.hpp:55
This structure is used as an intermediate data type to store a sparse matrix.
Definition matrix_data.hpp:155