Ginkgo  Generated from pipelines/2662685947 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
diagonal.hpp
1 // SPDX-FileCopyrightText: 2017 - 2026 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 LinOp,
42  public EnableCloneable<Diagonal<ValueType>>,
43  public ConvertibleTo<Csr<ValueType, int32>>,
44  public ConvertibleTo<Csr<ValueType, int64>>,
45  public ConvertibleTo<Diagonal<next_precision<ValueType>>>,
46 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
47  public ConvertibleTo<Diagonal<next_precision<ValueType, 2>>>,
48 #endif
49 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
50  public ConvertibleTo<Diagonal<next_precision<ValueType, 3>>>,
51 #endif
52  public Transposable,
53  public WritableToMatrixData<ValueType, int32>,
54  public WritableToMatrixData<ValueType, int64>,
55  public ReadableFromMatrixData<ValueType, int32>,
56  public ReadableFromMatrixData<ValueType, int64>,
57  public EnableAbsoluteComputation<remove_complex<Diagonal<ValueType>>> {
58  friend class EnableCloneable<Diagonal>;
59  friend class Csr<ValueType, int32>;
60  friend class Csr<ValueType, int64>;
61  friend class Diagonal<to_complex<ValueType>>;
62  GKO_ASSERT_SUPPORTED_VALUE_TYPE;
63 
64 public:
67  using ConvertibleTo<Csr<ValueType, int32>>::convert_to;
68  using ConvertibleTo<Csr<ValueType, int32>>::move_to;
69  using ConvertibleTo<Csr<ValueType, int64>>::convert_to;
70  using ConvertibleTo<Csr<ValueType, int64>>::move_to;
71  using ConvertibleTo<Diagonal<next_precision<ValueType>>>::convert_to;
72  using ConvertibleTo<Diagonal<next_precision<ValueType>>>::move_to;
73 
74  using value_type = ValueType;
75  using index_type = int64;
76  using mat_data = matrix_data<ValueType, int64>;
77  using mat_data32 = matrix_data<ValueType, int32>;
78  using device_mat_data = device_matrix_data<ValueType, int64>;
79  using device_mat_data32 = device_matrix_data<ValueType, int32>;
80  using absolute_type = remove_complex<Diagonal>;
81 
82  friend class Diagonal<previous_precision<ValueType>>;
83 
84  std::unique_ptr<LinOp> transpose() const override;
85 
86  std::unique_ptr<LinOp> conj_transpose() const override;
87 
88  void convert_to(Diagonal<next_precision<ValueType>>* result) const override;
89 
90  void move_to(Diagonal<next_precision<ValueType>>* result) override;
91 
92 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
93  friend class Diagonal<previous_precision<ValueType, 2>>;
94  using ConvertibleTo<Diagonal<next_precision<ValueType, 2>>>::convert_to;
95  using ConvertibleTo<Diagonal<next_precision<ValueType, 2>>>::move_to;
96 
97  void convert_to(
98  Diagonal<next_precision<ValueType, 2>>* result) const override;
99 
100  void move_to(Diagonal<next_precision<ValueType, 2>>* result) override;
101 #endif
102 
103 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
104  friend class Diagonal<previous_precision<ValueType, 3>>;
105  using ConvertibleTo<Diagonal<next_precision<ValueType, 3>>>::convert_to;
106  using ConvertibleTo<Diagonal<next_precision<ValueType, 3>>>::move_to;
107 
108  void convert_to(
109  Diagonal<next_precision<ValueType, 3>>* result) const override;
110 
111  void move_to(Diagonal<next_precision<ValueType, 3>>* result) override;
112 #endif
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 
124  void compute_absolute_inplace() override;
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 
203  static std::unique_ptr<Diagonal> create(
204  std::shared_ptr<const Executor> exec, size_type size = 0);
205 
220  static std::unique_ptr<Diagonal> create(
221  std::shared_ptr<const Executor> exec, const size_type size,
222  array<value_type> values);
223 
228  template <typename InputValueType>
229  GKO_DEPRECATED(
230  "explicitly construct the gko::array argument instead of passing an"
231  "initializer list")
232  static std::unique_ptr<Diagonal> create(
233  std::shared_ptr<const Executor> exec, const size_type size,
234  std::initializer_list<InputValueType> values)
235  {
236  return create(exec, size, array<value_type>{exec, std::move(values)});
237  }
238 
249  static std::unique_ptr<const Diagonal> create_const(
250  std::shared_ptr<const Executor> exec, size_type size,
251  gko::detail::const_array_view<ValueType>&& values);
252 
253 protected:
254  Diagonal(std::shared_ptr<const Executor> exec, size_type size = 0);
255 
256  Diagonal(std::shared_ptr<const Executor> exec, const size_type size,
257  array<value_type> values);
258 
259  void apply_impl(const LinOp* b, LinOp* x) const override;
260 
261  void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
262  LinOp* x) const override;
263 
264  void rapply_impl(const LinOp* b, LinOp* x) const;
265 
266  void inverse_apply_impl(const LinOp* b, LinOp* x) const;
267 
268 private:
269  array<value_type> values_;
270 };
271 
272 
273 } // namespace matrix
274 } // namespace gko
275 
276 
277 #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::LinOp
Definition: lin_op.hpp:117
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:90
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:77
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:43
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:687
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:152
gko::EnableCloneable::convert_to
void convert_to(result_type *result) const override
Converts the implementer to an object of type result_type.
Definition: polymorphic_object.hpp:404
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::int64
std::int64_t int64
64-bit signed integral type.
Definition: types.hpp:113
gko::int32
std::int32_t int32
32-bit signed integral type.
Definition: types.hpp:107
gko::Executor
The first step in using the Ginkgo library consists of creating an executor.
Definition: executor.hpp:616
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:696
gko::matrix::Diagonal::get_values
value_type * get_values() noexcept
Returns a pointer to the array of values of the matrix.
Definition: diagonal.hpp:131
gko::EnableCloneable::move_to
void move_to(result_type *result) override
Converts the implementer to an object of type result_type by moving data from this object.
Definition: polymorphic_object.hpp:406
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:170
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:140
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