Ginkgo  Generated from pipelines/3133041809 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
dense.hpp
1 // SPDX-FileCopyrightText: 2026 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #pragma once
6 
7 
8 #include <ginkgo/core/base/lin_op.hpp>
9 #include <ginkgo/core/matrix/device_views.hpp>
10 
11 
12 namespace gko {
13 namespace matrix {
14 
15 
16 template <typename ValueType>
17 class MultiVector;
18 
19 
20 template <typename ValueType, typename IndexType>
21 class Coo;
22 
23 template <typename ValueType, typename IndexType>
24 class Csr;
25 
26 template <typename ValueType>
27 class Diagonal;
28 
29 template <typename ValueType, typename IndexType>
30 class Ell;
31 
32 template <typename ValueType, typename IndexType>
33 class Fbcsr;
34 
35 template <typename ValueType, typename IndexType>
36 class Hybrid;
37 
38 template <typename ValueType, typename IndexType>
39 class Sellp;
40 
41 template <typename ValueType, typename IndexType>
42 class SparsityCsr;
43 
44 
63 template <typename ValueType = default_precision>
64 class Dense : public LinOp,
65  public EnableCloneable<Dense<ValueType>>,
66  public ConvertibleTo<MultiVector<ValueType>>,
67  public ConvertibleTo<Dense<next_precision<ValueType>>>,
68 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
69  public ConvertibleTo<Dense<next_precision<ValueType, 2>>>,
70 #endif
71 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
72  public ConvertibleTo<Dense<next_precision<ValueType, 3>>>,
73 #endif
74  public ConvertibleTo<Coo<ValueType, int32>>,
75  public ConvertibleTo<Coo<ValueType, int64>>,
76  public ConvertibleTo<Csr<ValueType, int32>>,
77  public ConvertibleTo<Csr<ValueType, int64>>,
78  public ConvertibleTo<Ell<ValueType, int32>>,
79  public ConvertibleTo<Ell<ValueType, int64>>,
80  public ConvertibleTo<Fbcsr<ValueType, int32>>,
81  public ConvertibleTo<Fbcsr<ValueType, int64>>,
82  public ConvertibleTo<Hybrid<ValueType, int32>>,
83  public ConvertibleTo<Hybrid<ValueType, int64>>,
84  public ConvertibleTo<Sellp<ValueType, int32>>,
85  public ConvertibleTo<Sellp<ValueType, int64>>,
86  public ConvertibleTo<SparsityCsr<ValueType, int32>>,
87  public ConvertibleTo<SparsityCsr<ValueType, int64>>,
88  public DiagonalExtractable<ValueType>,
89  public ReadableFromMatrixData<ValueType, int32>,
90  public ReadableFromMatrixData<ValueType, int64>,
91  public WritableToMatrixData<ValueType, int32>,
92  public WritableToMatrixData<ValueType, int64>,
93  public Transposable,
94  public ScaledIdentityAddable {
95  friend class EnableCloneable<Dense>;
96  friend class Dense<to_complex<ValueType>>;
97  friend class Dense<previous_precision<ValueType>>;
98  friend class MultiVector<ValueType>;
99  friend class Coo<ValueType, int32>;
100  friend class Coo<ValueType, int64>;
101  friend class Csr<ValueType, int32>;
102  friend class Csr<ValueType, int64>;
103  friend class Ell<ValueType, int32>;
104  friend class Ell<ValueType, int64>;
105  friend class Fbcsr<ValueType, int32>;
106  friend class Fbcsr<ValueType, int64>;
107  friend class Hybrid<ValueType, int32>;
108  friend class Hybrid<ValueType, int64>;
109  friend class Sellp<ValueType, int32>;
110  friend class Sellp<ValueType, int64>;
111  friend class SparsityCsr<ValueType, int32>;
112  friend class SparsityCsr<ValueType, int64>;
113  GKO_ASSERT_SUPPORTED_VALUE_TYPE;
114 
115 public:
118  using ConvertibleTo<MultiVector<ValueType>>::convert_to;
119  using ConvertibleTo<MultiVector<ValueType>>::move_to;
120  using ConvertibleTo<Coo<ValueType, int32>>::convert_to;
121  using ConvertibleTo<Coo<ValueType, int32>>::move_to;
122  using ConvertibleTo<Coo<ValueType, int64>>::convert_to;
123  using ConvertibleTo<Coo<ValueType, int64>>::move_to;
124  using ConvertibleTo<Csr<ValueType, int32>>::convert_to;
125  using ConvertibleTo<Csr<ValueType, int32>>::move_to;
126  using ConvertibleTo<Csr<ValueType, int64>>::convert_to;
127  using ConvertibleTo<Csr<ValueType, int64>>::move_to;
128  using ConvertibleTo<Ell<ValueType, int32>>::convert_to;
129  using ConvertibleTo<Ell<ValueType, int32>>::move_to;
130  using ConvertibleTo<Ell<ValueType, int64>>::convert_to;
131  using ConvertibleTo<Ell<ValueType, int64>>::move_to;
132  using ConvertibleTo<Fbcsr<ValueType, int32>>::convert_to;
133  using ConvertibleTo<Fbcsr<ValueType, int32>>::move_to;
134  using ConvertibleTo<Fbcsr<ValueType, int64>>::convert_to;
135  using ConvertibleTo<Fbcsr<ValueType, int64>>::move_to;
136  using ConvertibleTo<Hybrid<ValueType, int32>>::convert_to;
137  using ConvertibleTo<Hybrid<ValueType, int32>>::move_to;
138  using ConvertibleTo<Hybrid<ValueType, int64>>::convert_to;
139  using ConvertibleTo<Hybrid<ValueType, int64>>::move_to;
140  using ConvertibleTo<Sellp<ValueType, int32>>::convert_to;
141  using ConvertibleTo<Sellp<ValueType, int32>>::move_to;
142  using ConvertibleTo<Sellp<ValueType, int64>>::convert_to;
143  using ConvertibleTo<Sellp<ValueType, int64>>::move_to;
144  using ConvertibleTo<SparsityCsr<ValueType, int32>>::convert_to;
145  using ConvertibleTo<SparsityCsr<ValueType, int32>>::move_to;
146  using ConvertibleTo<SparsityCsr<ValueType, int64>>::convert_to;
147  using ConvertibleTo<SparsityCsr<ValueType, int64>>::move_to;
148 
149  using value_type = ValueType;
150  using index_type = int64;
151  using transposed_type = Dense;
152  using mat_data64 = matrix_data<value_type, int64>;
153  using mat_data32 = matrix_data<value_type, int32>;
154  using mat_data [[deprecated("Use mat_data64 instead.")]] = mat_data64;
155  using device_mat_data64 = device_matrix_data<value_type, int64>;
156  using device_mat_data32 = device_matrix_data<value_type, int32>;
157  using device_mat_data [[deprecated("Use device_mat_data64 instead.")]] =
158  device_mat_data64;
159  using device_view = view::dense<value_type>;
160  using const_device_view = view::dense<const value_type>;
161 
162  using row_major_range = gko::range<gko::accessor::row_major<ValueType, 2>>;
163 
164  void convert_to(MultiVector<ValueType>* result) const override;
165 
166  void move_to(MultiVector<ValueType>* result) override;
167 
168  void convert_to(Dense<next_precision<ValueType>>* result) const override;
169 
170  void move_to(Dense<next_precision<ValueType>>* result) override;
171 
172 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
173  friend class Dense<previous_precision<ValueType, 2>>;
174  using ConvertibleTo<Dense<next_precision<ValueType, 2>>>::convert_to;
175  using ConvertibleTo<Dense<next_precision<ValueType, 2>>>::move_to;
176 
177  void convert_to(Dense<next_precision<ValueType, 2>>* result) const override;
178 
179  void move_to(Dense<next_precision<ValueType, 2>>* result) override;
180 #endif
181 
182 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
183  friend class Dense<previous_precision<ValueType, 3>>;
184  using ConvertibleTo<Dense<next_precision<ValueType, 3>>>::convert_to;
185  using ConvertibleTo<Dense<next_precision<ValueType, 3>>>::move_to;
186 
187  void convert_to(Dense<next_precision<ValueType, 3>>* result) const override;
188 
189  void move_to(Dense<next_precision<ValueType, 3>>* result) override;
190 #endif
191 
192  void convert_to(Coo<ValueType, int32>* result) const override;
193 
194  void move_to(Coo<ValueType, int32>* result) override;
195 
196  void convert_to(Coo<ValueType, int64>* result) const override;
197 
198  void move_to(Coo<ValueType, int64>* result) override;
199 
200  void convert_to(Csr<ValueType, int32>* result) const override;
201 
202  void move_to(Csr<ValueType, int32>* result) override;
203 
204  void convert_to(Csr<ValueType, int64>* result) const override;
205 
206  void move_to(Csr<ValueType, int64>* result) override;
207 
208  void convert_to(Ell<ValueType, int32>* result) const override;
209 
210  void move_to(Ell<ValueType, int32>* result) override;
211 
212  void convert_to(Ell<ValueType, int64>* result) const override;
213 
214  void move_to(Ell<ValueType, int64>* result) override;
215 
216  void convert_to(Fbcsr<ValueType, int32>* result) const override;
217 
218  void move_to(Fbcsr<ValueType, int32>* result) override;
219 
220  void convert_to(Fbcsr<ValueType, int64>* result) const override;
221 
222  void move_to(Fbcsr<ValueType, int64>* result) override;
223 
224  void convert_to(Hybrid<ValueType, int32>* result) const override;
225 
226  void move_to(Hybrid<ValueType, int32>* result) override;
227 
228  void convert_to(Hybrid<ValueType, int64>* result) const override;
229 
230  void move_to(Hybrid<ValueType, int64>* result) override;
231 
232  void convert_to(Sellp<ValueType, int32>* result) const override;
233 
234  void move_to(Sellp<ValueType, int32>* result) override;
235 
236  void convert_to(Sellp<ValueType, int64>* result) const override;
237 
238  void move_to(Sellp<ValueType, int64>* result) override;
239 
240  void convert_to(SparsityCsr<ValueType, int32>* result) const override;
241 
242  void move_to(SparsityCsr<ValueType, int32>* result) override;
243 
244  void convert_to(SparsityCsr<ValueType, int64>* result) const override;
245 
246  void move_to(SparsityCsr<ValueType, int64>* result) override;
247 
248  void read(const mat_data32& data) override;
249 
250  void read(const mat_data64& data) override;
251 
252  void read(const device_mat_data32& data) override;
253 
254  void read(const device_mat_data64& data) override;
255 
256  void read(device_mat_data32&& data) override;
257 
258  void read(device_mat_data64&& data) override;
259 
260  void write(mat_data32& data) const override;
261 
262  void write(mat_data64& data) const override;
263 
264  void validate_data() const override;
265 
266  void fill(ValueType value);
267 
275  void extract_diagonal(ptr_param<Diagonal<ValueType>> output) const;
276 
277  std::unique_ptr<Diagonal<ValueType>> extract_diagonal() const override;
278 
279  [[nodiscard]] std::unique_ptr<LinOp> transpose() const override;
280 
281  [[nodiscard]] std::unique_ptr<LinOp> conj_transpose() const override;
282 
289  void transpose(ptr_param<Dense> output) const;
290 
297  void conj_transpose(ptr_param<Dense> output) const;
298 
305  void add_scaled(ptr_param<const AbstractMultiVector> alpha,
306  ptr_param<const Diagonal<value_type>> diag);
307 
314  void sub_scaled(ptr_param<const AbstractMultiVector> alpha,
315  ptr_param<const Diagonal<value_type>> diag);
316 
317  [[nodiscard]] static std::unique_ptr<Dense> create(
318  std::shared_ptr<const Executor> exec, const dim<2>& size = dim<2>{},
319  size_type stride = 0);
320 
321  [[nodiscard]] static std::unique_ptr<Dense> create(
322  std::shared_ptr<const Executor> exec, const dim<2>& size,
323  array<value_type> values, size_type stride);
324 
325  [[nodiscard]] static std::unique_ptr<const Dense> create_const(
326  std::shared_ptr<const Executor> exec, const dim<2>& size,
327  ::gko::detail::const_array_view<ValueType>&& values, size_type stride);
328 
329  [[nodiscard]] std::unique_ptr<Dense> create_subview(span rows, span cols);
330 
331  [[nodiscard]] std::unique_ptr<const Dense> create_subview(span rows,
332  span cols) const;
333 
334  [[nodiscard]] std::unique_ptr<const Dense> create_const_subview(
335  span rows, span cols) const;
336 
337  [[nodiscard]] std::unique_ptr<const MultiVector<ValueType>>
338  as_const_multivector_view() const;
339 
340  [[nodiscard]] std::unique_ptr<MultiVector<ValueType>> as_multivector_view();
341 
342  [[nodiscard]] device_view get_device_view();
343 
344  [[nodiscard]] const_device_view get_const_device_view() const;
345 
346  ValueType* get_values() noexcept { return values_.get_data(); }
347 
348  const ValueType* get_const_values() const noexcept
349  {
350  return values_.get_const_data();
351  }
352 
353  ValueType& at(size_type row, size_type col);
354 
355  ValueType at(size_type row, size_type col) const;
356 
357  [[nodiscard]] size_type get_stride() const noexcept;
358 
359  [[nodiscard]] size_type get_num_stored_elements() const noexcept;
360 
361  Dense(const Dense& other);
362 
363  Dense(Dense&& other);
364 
365  Dense& operator=(const Dense& other);
366 
367  Dense& operator=(Dense&& other);
368 
369 protected:
370  Dense(std::shared_ptr<const Executor> exec, const dim<2>& size = dim<2>{},
371  size_type stride = 0);
372 
373  Dense(std::shared_ptr<const Executor> exec, const dim<2>& size,
374  array<value_type> values, size_type stride);
375 
376  void apply_impl(const AbstractMultiVector* b,
377  AbstractMultiVector* x) const override;
378 
379  void apply_impl(const AbstractMultiVector* alpha,
380  const AbstractMultiVector* b,
381  const AbstractMultiVector* beta,
382  AbstractMultiVector* x) const override;
383 
384  [[nodiscard]] size_type linearize_index(size_type row,
385  size_type col) const noexcept;
386 
387  void resize(dim<2> new_size);
388 
389  template <typename IndexType>
390  void convert_impl(Coo<ValueType, IndexType>* result) const;
391 
392  template <typename IndexType>
393  void convert_impl(Csr<ValueType, IndexType>* result) const;
394 
395  template <typename IndexType>
396  void convert_impl(Ell<ValueType, IndexType>* result) const;
397 
398  template <typename IndexType>
399  void convert_impl(Fbcsr<ValueType, IndexType>* result) const;
400 
401  template <typename IndexType>
402  void convert_impl(Hybrid<ValueType, IndexType>* result) const;
403 
404  template <typename IndexType>
405  void convert_impl(Sellp<ValueType, IndexType>* result) const;
406 
407  template <typename IndexType>
408  void convert_impl(SparsityCsr<ValueType, IndexType>* result) const;
409 
410 private:
411  size_type stride_;
412  array<value_type> values_;
413 
414  void add_scaled_identity_impl(const AbstractMultiVector* a,
415  const AbstractMultiVector* b) override;
416 };
417 
418 
419 } // namespace matrix
420 
421 
422 namespace detail {
423 
424 
425 template <typename ValueType>
426 struct temporary_clone_helper<matrix::Dense<ValueType>> {
427  static std::unique_ptr<matrix::Dense<ValueType>> create(
428  std::shared_ptr<const Executor> exec, matrix::Dense<ValueType>* ptr,
429  bool copy_data)
430  {
431  if (copy_data) {
432  return gko::clone(std::move(exec), ptr);
433  } else {
434  return matrix::Dense<ValueType>::create(exec, ptr->get_size());
435  }
436  }
437 };
438 
439 
440 } // namespace detail
441 
442 
464 template <typename Matrix, typename... TArgs>
465 std::unique_ptr<Matrix> initialize(
466  size_type stride, std::initializer_list<typename Matrix::value_type> vals,
467  std::shared_ptr<const Executor> exec, TArgs&&... create_args)
468 {
470  size_type num_rows = vals.size();
471  auto tmp = dense::create(exec->get_master(), dim<2>{num_rows, 1}, stride);
472  size_type idx = 0;
473  for (const auto& elem : vals) {
474  tmp->at(idx, 0) = elem;
475  ++idx;
476  }
477  auto mtx = Matrix::create(exec, std::forward<TArgs>(create_args)...);
478  tmp->move_to(mtx);
479  return mtx;
480 }
481 
503 template <typename Matrix, typename... TArgs>
504 std::unique_ptr<Matrix> initialize(
505  std::initializer_list<typename Matrix::value_type> vals,
506  std::shared_ptr<const Executor> exec, TArgs&&... create_args)
507 {
508  return initialize<Matrix>(1, vals, std::move(exec),
509  std::forward<TArgs>(create_args)...);
510 }
511 
512 
534 template <typename Matrix, typename... TArgs>
535 std::unique_ptr<Matrix> initialize(
536  size_type stride,
537  std::initializer_list<std::initializer_list<typename Matrix::value_type>>
538  vals,
539  std::shared_ptr<const Executor> exec, TArgs&&... create_args)
540 {
542  size_type num_rows = vals.size();
543  size_type num_cols = num_rows > 0 ? begin(vals)->size() : 1;
544  auto tmp =
545  dense::create(exec->get_master(), dim<2>{num_rows, num_cols}, stride);
546  size_type ridx = 0;
547  for (const auto& row : vals) {
548  size_type cidx = 0;
549  for (const auto& elem : row) {
550  tmp->at(ridx, cidx) = elem;
551  ++cidx;
552  }
553  ++ridx;
554  }
555  auto mtx = Matrix::create(exec, std::forward<TArgs>(create_args)...);
556  tmp->move_to(mtx);
557  return mtx;
558 }
559 
560 
583 template <typename Matrix, typename... TArgs>
584 std::unique_ptr<Matrix> initialize(
585  std::initializer_list<std::initializer_list<typename Matrix::value_type>>
586  vals,
587  std::shared_ptr<const Executor> exec, TArgs&&... create_args)
588 {
589  return initialize<Matrix>(vals.size() > 0 ? begin(vals)->size() : 0, vals,
590  std::move(exec),
591  std::forward<TArgs>(create_args)...);
592 }
593 
594 
595 } // namespace gko
gko::matrix::permute_mode::rows
The rows will be permuted.
gko::matrix::Dense::transpose
std::unique_ptr< LinOp > transpose() const override
Returns a LinOp representing the transpose of the Transposable object.
gko::matrix::Dense
Dense is a matrix format which explicitly stores all values of the matrix.
Definition: lin_op.hpp:35
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:102
gko::initialize
std::unique_ptr< Matrix > initialize(size_type stride, std::initializer_list< typename Matrix::value_type > vals, std::shared_ptr< const Executor > exec, TArgs &&... create_args)
Creates and initializes a column-vector.
Definition: dense.hpp:465
gko::clone
detail::cloned_type< Pointer > clone(const Pointer &p)
Creates a unique clone of the object pointed to by p.
Definition: utils_helper.hpp:425
gko::range
A range is a multidimensional view of the memory.
Definition: range.hpp:320
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:19
gko::dim< 2 >
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::matrix::Dense::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::matrix::Dense::extract_diagonal
std::unique_ptr< Diagonal< ValueType > > extract_diagonal() const override
Extracts the diagonal entries of the matrix into a vector.
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::matrix::Dense::conj_transpose
std::unique_ptr< LinOp > conj_transpose() const override
Returns a LinOp representing the conjugate transpose of the Transposable object.
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::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::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::matrix::Dense::sub_scaled
void sub_scaled(ptr_param< const AbstractMultiVector > alpha, ptr_param< const Diagonal< value_type >> diag)
Subtracts a scaled diagonal from this A = A - alpha * D.
gko::matrix::Dense::add_scaled
void add_scaled(ptr_param< const AbstractMultiVector > alpha, ptr_param< const Diagonal< value_type >> diag)
Adds a scaled diagonal to this A = A + alpha * D.
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