Ginkgo  Generated from pipelines/1589998975 branch based on develop. Ginkgo version 1.10.0
A numerical linear algebra library targeting many-core architectures
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 
13 namespace gko {
14 namespace matrix {
15 
16 
17 template <typename ValueType, typename IndexType>
18 class Csr;
19 
20 template <typename ValueType>
21 class Dense;
22 
23 
39 template <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
46  public ConvertibleTo<Diagonal<next_precision<next_precision<ValueType>>>>,
47 #endif
48  public Transposable,
49  public WritableToMatrixData<ValueType, int32>,
50  public WritableToMatrixData<ValueType, int64>,
51  public ReadableFromMatrixData<ValueType, int32>,
52  public ReadableFromMatrixData<ValueType, int64>,
53  public EnableAbsoluteComputation<remove_complex<Diagonal<ValueType>>> {
54  friend class EnablePolymorphicObject<Diagonal, LinOp>;
55  friend class Csr<ValueType, int32>;
56  friend class Csr<ValueType, int64>;
57  friend class Diagonal<to_complex<ValueType>>;
58 
59 public:
62  using ConvertibleTo<Csr<ValueType, int32>>::convert_to;
63  using ConvertibleTo<Csr<ValueType, int32>>::move_to;
64  using ConvertibleTo<Csr<ValueType, int64>>::convert_to;
65  using ConvertibleTo<Csr<ValueType, int64>>::move_to;
66  using ConvertibleTo<Diagonal<next_precision<ValueType>>>::convert_to;
67  using ConvertibleTo<Diagonal<next_precision<ValueType>>>::move_to;
68 
69  using value_type = ValueType;
70  using index_type = int64;
71  using mat_data = matrix_data<ValueType, int64>;
72  using mat_data32 = matrix_data<ValueType, int32>;
73  using device_mat_data = device_matrix_data<ValueType, int64>;
74  using device_mat_data32 = device_matrix_data<ValueType, int32>;
75  using absolute_type = remove_complex<Diagonal>;
76 
77  friend class Diagonal<previous_precision<ValueType>>;
78 
79  std::unique_ptr<LinOp> transpose() const override;
80 
81  std::unique_ptr<LinOp> conj_transpose() const override;
82 
83  void convert_to(Diagonal<next_precision<ValueType>>* result) const override;
84 
85  void move_to(Diagonal<next_precision<ValueType>>* result) override;
86 
87 #if GINKGO_ENABLE_HALF
88  friend class Diagonal<previous_precision<previous_precision<ValueType>>>;
89  using ConvertibleTo<
90  Diagonal<next_precision<next_precision<ValueType>>>>::convert_to;
91  using ConvertibleTo<
92  Diagonal<next_precision<next_precision<ValueType>>>>::move_to;
93 
94  void convert_to(Diagonal<next_precision<next_precision<ValueType>>>* result)
95  const override;
96 
97  void move_to(
98  Diagonal<next_precision<next_precision<ValueType>>>* result) override;
99 #endif
100 
101  void convert_to(Csr<ValueType, int32>* result) const override;
102 
103  void move_to(Csr<ValueType, int32>* result) override;
104 
105  void convert_to(Csr<ValueType, int64>* result) const override;
106 
107  void move_to(Csr<ValueType, int64>* result) override;
108 
109  std::unique_ptr<absolute_type> compute_absolute() const override;
110 
111  void compute_absolute_inplace() override;
112 
118  value_type* get_values() noexcept { return values_.get_data(); }
119 
127  const value_type* get_const_values() const noexcept
128  {
129  return values_.get_const_data();
130  }
131 
140  {
141  GKO_ASSERT_REVERSE_CONFORMANT(this, b);
142  GKO_ASSERT_EQUAL_ROWS(b, x);
143  GKO_ASSERT_EQUAL_COLS(this, x);
144 
145  this->rapply_impl(b.get(), x.get());
146  }
147 
158  {
159  GKO_ASSERT_CONFORMANT(this, b);
160  GKO_ASSERT_EQUAL_ROWS(b, x);
161  GKO_ASSERT_EQUAL_ROWS(this, x);
162 
163  this->inverse_apply_impl(b.get(), x.get());
164  }
165 
166  void read(const mat_data& data) override;
167 
168  void read(const mat_data32& data) override;
169 
170  void read(const device_mat_data& data) override;
171 
172  void read(const device_mat_data32& data) override;
173 
174  void read(device_mat_data&& data) override;
175 
176  void read(device_mat_data32&& data) override;
177 
178  void write(mat_data& data) const override;
179 
180  void write(mat_data32& data) const override;
181 
190  static std::unique_ptr<Diagonal> create(
191  std::shared_ptr<const Executor> exec, size_type size = 0);
192 
207  static std::unique_ptr<Diagonal> create(
208  std::shared_ptr<const Executor> exec, const size_type size,
209  array<value_type> values);
210 
215  template <typename InputValueType>
216  GKO_DEPRECATED(
217  "explicitly construct the gko::array argument instead of passing an"
218  "initializer list")
219  static std::unique_ptr<Diagonal> create(
220  std::shared_ptr<const Executor> exec, const size_type size,
221  std::initializer_list<InputValueType> values)
222  {
223  return create(exec, size, array<value_type>{exec, std::move(values)});
224  }
225 
236  static std::unique_ptr<const Diagonal> create_const(
237  std::shared_ptr<const Executor> exec, size_type size,
238  gko::detail::const_array_view<ValueType>&& values);
239 
240 protected:
241  Diagonal(std::shared_ptr<const Executor> exec, size_type size = 0);
242 
243  Diagonal(std::shared_ptr<const Executor> exec, const size_type size,
244  array<value_type> values);
245 
246  void apply_impl(const LinOp* b, LinOp* x) const override;
247 
248  void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
249  LinOp* x) const override;
250 
251  void rapply_impl(const LinOp* b, LinOp* x) const;
252 
253  void inverse_apply_impl(const LinOp* b, LinOp* x) const;
254 
255 private:
256  array<value_type> values_;
257 };
258 
259 
260 } // namespace matrix
261 } // namespace gko
262 
263 
264 #endif // GKO_PUBLIC_CORE_MATRIX_DIAGONAL_HPP_
gko::matrix::Diagonal::compute_absolute_inplace
void compute_absolute_inplace() override
Compute absolute inplace on each element.
gko::EnablePolymorphicAssignment< ConcreteLinOp >::move_to
void move_to(result_type *result) override
Definition: polymorphic_object.hpp:751
gko::EnablePolymorphicAssignment< ConcreteLinOp >::convert_to
void convert_to(result_type *result) const override
Definition: polymorphic_object.hpp:749
gko::LinOp
Definition: lin_op.hpp:117
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:89
gko::matrix::Diagonal::transpose
std::unique_ptr< LinOp > transpose() const override
Returns a LinOp representing the transpose of the Transposable object.
gko::ptr_param::get
T * get() const
Definition: utils_helper.hpp:75
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
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::array< value_type >
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::compute_absolute
std::unique_ptr< absolute_type > compute_absolute() const override
Gets the AbsoluteLinOp.
gko::matrix::Diagonal
This class is a utility which efficiently implements the diagonal matrix (a linear operator which sca...
Definition: lin_op.hpp:31
gko::ptr_param
This class is used for function parameters in the place of raw pointers.
Definition: utils_helper.hpp:41
gko::array::get_data
value_type * get_data() noexcept
Returns a pointer to the block of memory used to store the elements of the array.
Definition: array.hpp:673
gko::next_precision
next_precision_base< T > next_precision
Obtains the next type in the singly-linked precision list with half.
Definition: math.hpp:445
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:139
gko::int64
std::int64_t int64
64-bit signed integral type.
Definition: types.hpp:112
gko::int32
std::int32_t int32
32-bit signed integral type.
Definition: types.hpp:106
gko::Executor
The first step in using the Ginkgo library consists of creating an executor.
Definition: executor.hpp:615
gko::array::get_const_data
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:682
gko::matrix::Diagonal::get_values
value_type * get_values() noexcept
Returns a pointer to the array of values of the matrix.
Definition: diagonal.hpp:118
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:157
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::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:127
gko::LinOp::LinOp
LinOp(const LinOp &)=default
Copy-constructs a LinOp.
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:279