Ginkgo  Generated from pipelines/3136675824 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 
35 template <typename ValueType = default_precision>
36 class Diagonal
37  : public LinOp,
38  public EnableCloneable<Diagonal<ValueType>>,
39  public ConvertibleTo<Csr<ValueType, int32>>,
40  public ConvertibleTo<Csr<ValueType, int64>>,
41  public ConvertibleTo<Diagonal<next_precision<ValueType>>>,
42 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
43  public ConvertibleTo<Diagonal<next_precision<ValueType, 2>>>,
44 #endif
45 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
46  public ConvertibleTo<Diagonal<next_precision<ValueType, 3>>>,
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 EnableCloneable<Diagonal>;
55  friend class Csr<ValueType, int32>;
56  friend class Csr<ValueType, int64>;
57  friend class Diagonal<to_complex<ValueType>>;
58  GKO_ASSERT_SUPPORTED_VALUE_TYPE;
59 
60 public:
61  using LinOp::apply;
64  using ConvertibleTo<Csr<ValueType, int32>>::convert_to;
65  using ConvertibleTo<Csr<ValueType, int32>>::move_to;
66  using ConvertibleTo<Csr<ValueType, int64>>::convert_to;
67  using ConvertibleTo<Csr<ValueType, int64>>::move_to;
68  using ConvertibleTo<Diagonal<next_precision<ValueType>>>::convert_to;
69  using ConvertibleTo<Diagonal<next_precision<ValueType>>>::move_to;
70 
71  using value_type = ValueType;
72  using index_type = int64;
73  using mat_data = matrix_data<ValueType, int64>;
74  using mat_data32 = matrix_data<ValueType, int32>;
75  using device_mat_data = device_matrix_data<ValueType, int64>;
76  using device_mat_data32 = device_matrix_data<ValueType, int32>;
77  using absolute_type = remove_complex<Diagonal>;
78 
79  friend class Diagonal<previous_precision<ValueType>>;
80 
81  std::unique_ptr<LinOp> transpose() const override;
82 
83  std::unique_ptr<LinOp> conj_transpose() const override;
84 
85  void convert_to(Diagonal<next_precision<ValueType>>* result) const override;
86 
87  void move_to(Diagonal<next_precision<ValueType>>* result) override;
88 
89 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
90  friend class Diagonal<previous_precision<ValueType, 2>>;
91  using ConvertibleTo<Diagonal<next_precision<ValueType, 2>>>::convert_to;
92  using ConvertibleTo<Diagonal<next_precision<ValueType, 2>>>::move_to;
93 
94  void convert_to(
95  Diagonal<next_precision<ValueType, 2>>* result) const override;
96 
97  void move_to(Diagonal<next_precision<ValueType, 2>>* result) override;
98 #endif
99 
100 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
101  friend class Diagonal<previous_precision<ValueType, 3>>;
102  using ConvertibleTo<Diagonal<next_precision<ValueType, 3>>>::convert_to;
103  using ConvertibleTo<Diagonal<next_precision<ValueType, 3>>>::move_to;
104 
105  void convert_to(
106  Diagonal<next_precision<ValueType, 3>>* result) const override;
107 
108  void move_to(Diagonal<next_precision<ValueType, 3>>* result) override;
109 #endif
110 
111  void convert_to(Csr<ValueType, int32>* result) const override;
112 
113  void move_to(Csr<ValueType, int32>* result) override;
114 
115  void convert_to(Csr<ValueType, int64>* result) const override;
116 
117  void move_to(Csr<ValueType, int64>* result) override;
118 
119  std::unique_ptr<absolute_type> compute_absolute() const override;
120 
121  void compute_absolute_inplace() override;
122 
128  value_type* get_values() noexcept { return values_.get_data(); }
129 
137  const value_type* get_const_values() const noexcept
138  {
139  return values_.get_const_data();
140  }
141 
151 
152  void rapply(ptr_param<const Csr<ValueType, int32>> b,
154 
155  void rapply(ptr_param<const Csr<ValueType, int64>> b,
157 
169 
172 
175 
176  void apply(ptr_param<const Csr<ValueType, int32>> b,
178 
179  void apply(ptr_param<const Csr<ValueType, int64>> b,
181 
182  void read(const mat_data& data) override;
183 
184  void read(const mat_data32& data) override;
185 
186  void read(const device_mat_data& data) override;
187 
188  void read(const device_mat_data32& data) override;
189 
190  void read(device_mat_data&& data) override;
191 
192  void read(device_mat_data32&& data) override;
193 
196 
197  void write(mat_data& data) const override;
198 
199  void write(mat_data32& data) const override;
200 
201  void validate_data() const override;
202 
211  static std::unique_ptr<Diagonal> create(
212  std::shared_ptr<const Executor> exec, size_type size = 0);
213 
228  static std::unique_ptr<Diagonal> create(
229  std::shared_ptr<const Executor> exec, const size_type size,
230  array<value_type> values);
231 
236  template <typename InputValueType>
237  GKO_DEPRECATED(
238  "explicitly construct the gko::array argument instead of passing an"
239  "initializer list")
240  static std::unique_ptr<Diagonal> create(
241  std::shared_ptr<const Executor> exec, const size_type size,
242  std::initializer_list<InputValueType> values)
243  {
244  return create(exec, size, array<value_type>{exec, std::move(values)});
245  }
246 
257  static std::unique_ptr<const Diagonal> create_const(
258  std::shared_ptr<const Executor> exec, size_type size,
259  gko::detail::const_array_view<ValueType>&& values);
260 
261 protected:
262  Diagonal(std::shared_ptr<const Executor> exec, size_type size = 0);
263 
264  Diagonal(std::shared_ptr<const Executor> exec, const size_type size,
265  array<value_type> values);
266 
267  void apply_impl(const AbstractMultiVector* b,
268  AbstractMultiVector* x) const override;
269 
270 private:
271  array<value_type> values_;
272 };
273 
274 
275 } // namespace matrix
276 } // namespace gko
277 
278 
279 #endif // GKO_PUBLIC_CORE_MATRIX_DIAGONAL_HPP_
gko::matrix::Diagonal::inverse_apply
void inverse_apply(ptr_param< const AbstractMultiVector > b, ptr_param< AbstractMultiVector > x) const
Applies the inverse of the diagonal matrix to a matrix b, which means scales the columns of b with th...
gko::matrix::Diagonal::compute_absolute_inplace
void compute_absolute_inplace() override
Compute absolute inplace on each element.
gko::matrix::Csr
CSR is a matrix format which stores only the nonzero coefficients by compressing each row of the matr...
Definition: matrix.hpp:30
gko::matrix::Diagonal::validate_data
void validate_data() const override
Throws gko::InvalidData exception if we found the data inside the object does not fulfill certain pro...
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:102
gko::matrix::Diagonal::transpose
std::unique_ptr< LinOp > transpose() const override
Returns a LinOp representing the transpose of the Transposable object.
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:19
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:32
gko::ptr_param
This class is used for function parameters in the place of raw pointers.
Definition: utils_helper.hpp:43
gko::matrix::Diagonal::rapply
void rapply(ptr_param< const AbstractMultiVector > b, ptr_param< AbstractMultiVector > x) const
Applies the diagonal matrix from the right side to a matrix b, which means scales the columns of b wi...
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:686
gko::WritableToMatrixData
A LinOp implementing this interface can write its data to a matrix_data structure.
Definition: lin_op.hpp:823
gko::LinOp::apply
void apply(ptr_param< const AbstractMultiVector > b, ptr_param< AbstractMultiVector > x) const
Applies a linear operator to a vector (or a sequence of vectors).
Definition: lin_op.hpp:176
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:407
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:472
gko::int64
std::int64_t int64
64-bit signed integral type.
Definition: types.hpp:125
gko::int32
std::int32_t int32
32-bit signed integral type.
Definition: types.hpp:119
gko::Executor
The first step in using the Ginkgo library consists of creating an executor.
Definition: executor.hpp:617
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:695
gko::matrix::Diagonal::get_values
value_type * get_values() noexcept
Returns a pointer to the array of values of the matrix.
Definition: diagonal.hpp:128
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:409
gko::AbstractMultiVector
Abstract interface class for (multi-)vector types.
Definition: multivector.hpp:81
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:137
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:282