Ginkgo  Generated from pipelines/2662685947 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
dense.hpp
1 // SPDX-FileCopyrightText: 2017 - 2026 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_MATRIX_DENSE_HPP_
6 #define GKO_PUBLIC_CORE_MATRIX_DENSE_HPP_
7 
8 
9 #include <initializer_list>
10 #include <type_traits>
11 
12 #include <ginkgo/core/base/array.hpp>
13 #include <ginkgo/core/base/exception_helpers.hpp>
14 #include <ginkgo/core/base/executor.hpp>
15 #include <ginkgo/core/base/lin_op.hpp>
16 #include <ginkgo/core/base/range_accessors.hpp>
17 #include <ginkgo/core/base/types.hpp>
18 #include <ginkgo/core/base/utils.hpp>
19 #include <ginkgo/core/matrix/device_views.hpp>
20 #include <ginkgo/core/matrix/permutation.hpp>
21 #include <ginkgo/core/matrix/scaled_permutation.hpp>
22 
23 
24 namespace gko {
25 namespace experimental {
26 namespace distributed {
27 
28 
29 template <typename ValueType>
30 class Vector;
31 
32 
33 namespace detail {
34 
35 
36 template <typename ValueType>
37 class VectorCache;
38 
39 
40 } // namespace detail
41 } // namespace distributed
42 } // namespace experimental
43 
44 
45 namespace matrix {
46 
47 
48 template <typename ValueType, typename IndexType>
49 class Coo;
50 
51 template <typename ValueType, typename IndexType>
52 class Csr;
53 
54 template <typename ValueType>
55 class Diagonal;
56 
57 template <typename ValueType, typename IndexType>
58 class Ell;
59 
60 template <typename ValueType, typename IndexType>
61 class Fbcsr;
62 
63 template <typename ValueType, typename IndexType>
64 class Hybrid;
65 
66 template <typename ValueType, typename IndexType>
67 class Sellp;
68 
69 template <typename ValueType, typename IndexType>
70 class SparsityCsr;
71 
72 
88 template <typename ValueType = default_precision>
89 class Dense
90  : public LinOp,
91  public EnableCloneable<Dense<ValueType>>,
92  public ConvertibleTo<Dense<next_precision<ValueType>>>,
93 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
94  public ConvertibleTo<Dense<next_precision<ValueType, 2>>>,
95 #endif
96 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
97  public ConvertibleTo<Dense<next_precision<ValueType, 3>>>,
98 #endif
99  public ConvertibleTo<Coo<ValueType, int32>>,
100  public ConvertibleTo<Coo<ValueType, int64>>,
101  public ConvertibleTo<Csr<ValueType, int32>>,
102  public ConvertibleTo<Csr<ValueType, int64>>,
103  public ConvertibleTo<Ell<ValueType, int32>>,
104  public ConvertibleTo<Ell<ValueType, int64>>,
105  public ConvertibleTo<Fbcsr<ValueType, int32>>,
106  public ConvertibleTo<Fbcsr<ValueType, int64>>,
107  public ConvertibleTo<Hybrid<ValueType, int32>>,
108  public ConvertibleTo<Hybrid<ValueType, int64>>,
109  public ConvertibleTo<Sellp<ValueType, int32>>,
110  public ConvertibleTo<Sellp<ValueType, int64>>,
111  public ConvertibleTo<SparsityCsr<ValueType, int32>>,
112  public ConvertibleTo<SparsityCsr<ValueType, int64>>,
113  public DiagonalExtractable<ValueType>,
114  public ReadableFromMatrixData<ValueType, int32>,
115  public ReadableFromMatrixData<ValueType, int64>,
116  public WritableToMatrixData<ValueType, int32>,
117  public WritableToMatrixData<ValueType, int64>,
118  public Transposable,
119  public Permutable<int32>,
120  public Permutable<int64>,
121  public EnableAbsoluteComputation<remove_complex<Dense<ValueType>>>,
122  public ScaledIdentityAddable {
123  friend class EnableCloneable<Dense>;
124  friend class Coo<ValueType, int32>;
125  friend class Coo<ValueType, int64>;
126  friend class Csr<ValueType, int32>;
127  friend class Csr<ValueType, int64>;
128  friend class Diagonal<ValueType>;
129  friend class Ell<ValueType, int32>;
130  friend class Ell<ValueType, int64>;
131  friend class Fbcsr<ValueType, int32>;
132  friend class Fbcsr<ValueType, int64>;
133  friend class Hybrid<ValueType, int32>;
134  friend class Hybrid<ValueType, int64>;
135  friend class Sellp<ValueType, int32>;
136  friend class Sellp<ValueType, int64>;
137  friend class SparsityCsr<ValueType, int32>;
138  friend class SparsityCsr<ValueType, int64>;
139  friend class Dense<to_complex<ValueType>>;
140  friend class experimental::distributed::Vector<ValueType>;
141  friend class experimental::distributed::detail::VectorCache<ValueType>;
142  GKO_ASSERT_SUPPORTED_VALUE_TYPE;
143 
144 public:
147  using ConvertibleTo<Dense<next_precision<ValueType>>>::convert_to;
148  using ConvertibleTo<Dense<next_precision<ValueType>>>::move_to;
149  using ConvertibleTo<Coo<ValueType, int32>>::convert_to;
150  using ConvertibleTo<Coo<ValueType, int32>>::move_to;
151  using ConvertibleTo<Coo<ValueType, int64>>::convert_to;
152  using ConvertibleTo<Coo<ValueType, int64>>::move_to;
153  using ConvertibleTo<Csr<ValueType, int32>>::convert_to;
154  using ConvertibleTo<Csr<ValueType, int32>>::move_to;
155  using ConvertibleTo<Csr<ValueType, int64>>::convert_to;
156  using ConvertibleTo<Csr<ValueType, int64>>::move_to;
157  using ConvertibleTo<Ell<ValueType, int32>>::convert_to;
158  using ConvertibleTo<Ell<ValueType, int32>>::move_to;
159  using ConvertibleTo<Ell<ValueType, int64>>::convert_to;
160  using ConvertibleTo<Ell<ValueType, int64>>::move_to;
161  using ConvertibleTo<Fbcsr<ValueType, int32>>::convert_to;
162  using ConvertibleTo<Fbcsr<ValueType, int32>>::move_to;
163  using ConvertibleTo<Fbcsr<ValueType, int64>>::convert_to;
164  using ConvertibleTo<Fbcsr<ValueType, int64>>::move_to;
165  using ConvertibleTo<Hybrid<ValueType, int32>>::convert_to;
166  using ConvertibleTo<Hybrid<ValueType, int32>>::move_to;
167  using ConvertibleTo<Hybrid<ValueType, int64>>::convert_to;
168  using ConvertibleTo<Hybrid<ValueType, int64>>::move_to;
169  using ConvertibleTo<Sellp<ValueType, int32>>::convert_to;
170  using ConvertibleTo<Sellp<ValueType, int32>>::move_to;
171  using ConvertibleTo<Sellp<ValueType, int64>>::convert_to;
172  using ConvertibleTo<Sellp<ValueType, int64>>::move_to;
173  using ConvertibleTo<SparsityCsr<ValueType, int32>>::convert_to;
174  using ConvertibleTo<SparsityCsr<ValueType, int32>>::move_to;
175  using ConvertibleTo<SparsityCsr<ValueType, int64>>::convert_to;
176  using ConvertibleTo<SparsityCsr<ValueType, int64>>::move_to;
179 
180  using value_type = ValueType;
181  using index_type = int64;
182  using transposed_type = Dense<value_type>;
183  using mat_data = matrix_data<value_type, int64>;
184  using mat_data32 = matrix_data<value_type, int32>;
185  using device_mat_data = device_matrix_data<value_type, int64>;
186  using device_mat_data32 = device_matrix_data<value_type, int32>;
187  using absolute_type = remove_complex<Dense>;
188  using real_type = absolute_type;
189  using complex_type = to_complex<Dense>;
190  using device_view = matrix::view::dense<value_type>;
191  using const_device_view = matrix::view::dense<const value_type>;
192 
193  using row_major_range = gko::range<gko::accessor::row_major<ValueType, 2>>;
194 
201  static std::unique_ptr<Dense> create_with_config_of(
203  {
204  // De-referencing `other` before calling the functions (instead of
205  // using operator `->`) is currently required to be compatible with
206  // CUDA 10.1.
207  // Otherwise, it results in a compile error.
208  return (*other).create_with_same_config();
209  }
210 
222  static std::unique_ptr<Dense> create_with_type_of(
223  ptr_param<const Dense> other, std::shared_ptr<const Executor> exec,
224  const dim<2>& size = dim<2>{})
225  {
226  // See create_with_config_of()
227  return (*other).create_with_type_of_impl(exec, size, size[1]);
228  }
229 
238  static std::unique_ptr<Dense> create_with_type_of(
239  ptr_param<const Dense> other, std::shared_ptr<const Executor> exec,
240  const dim<2>& size, size_type stride)
241  {
242  // See create_with_config_of()
243  return (*other).create_with_type_of_impl(exec, size, stride);
244  }
245 
256  static std::unique_ptr<Dense> create_with_type_of(
257  ptr_param<const Dense> other, std::shared_ptr<const Executor> exec,
258  const dim<2>& size, const dim<2>& local_size, size_type stride)
259  {
260  // See create_with_config_of()
261  return (*other).create_with_type_of_impl(exec, size, stride);
262  }
263 
272  static std::unique_ptr<Dense> create_view_of(ptr_param<Dense> other)
273  {
274  return other->create_view_of_impl();
275  }
276 
284  static std::unique_ptr<const Dense> create_const_view_of(
286  {
287  return other->create_const_view_of_impl();
288  }
289 
290  friend class Dense<previous_precision<ValueType>>;
291 
292  void convert_to(Dense<next_precision<ValueType>>* result) const override;
293 
294  void move_to(Dense<next_precision<ValueType>>* result) override;
295 
296 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
297  friend class Dense<previous_precision<ValueType, 2>>;
300 
301  void convert_to(Dense<next_precision<ValueType, 2>>* result) const override;
302 
303  void move_to(Dense<next_precision<ValueType, 2>>* result) override;
304 #endif
305 
306 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
307  friend class Dense<previous_precision<ValueType, 3>>;
310 
311  void convert_to(Dense<next_precision<ValueType, 3>>* result) const override;
312 
313  void move_to(Dense<next_precision<ValueType, 3>>* result) override;
314 #endif
315 
316  void convert_to(Coo<ValueType, int32>* result) const override;
317 
318  void move_to(Coo<ValueType, int32>* result) override;
319 
320  void convert_to(Coo<ValueType, int64>* result) const override;
321 
322  void move_to(Coo<ValueType, int64>* result) override;
323 
324  void convert_to(Csr<ValueType, int32>* result) const override;
325 
326  void move_to(Csr<ValueType, int32>* result) override;
327 
328  void convert_to(Csr<ValueType, int64>* result) const override;
329 
330  void move_to(Csr<ValueType, int64>* result) override;
331 
332  void convert_to(Ell<ValueType, int32>* result) const override;
333 
334  void move_to(Ell<ValueType, int32>* result) override;
335 
336  void convert_to(Ell<ValueType, int64>* result) const override;
337 
338  void move_to(Ell<ValueType, int64>* result) override;
339 
340  void convert_to(Fbcsr<ValueType, int32>* result) const override;
341 
342  void move_to(Fbcsr<ValueType, int32>* result) override;
343 
344  void convert_to(Fbcsr<ValueType, int64>* result) const override;
345 
346  void move_to(Fbcsr<ValueType, int64>* result) override;
347 
348  void convert_to(Hybrid<ValueType, int32>* result) const override;
349 
350  void move_to(Hybrid<ValueType, int32>* result) override;
351 
352  void convert_to(Hybrid<ValueType, int64>* result) const override;
353 
354  void move_to(Hybrid<ValueType, int64>* result) override;
355 
356  void convert_to(Sellp<ValueType, int32>* result) const override;
357 
358  void move_to(Sellp<ValueType, int32>* result) override;
359 
360  void convert_to(Sellp<ValueType, int64>* result) const override;
361 
362  void move_to(Sellp<ValueType, int64>* result) override;
363 
364  void convert_to(SparsityCsr<ValueType, int32>* result) const override;
365 
366  void move_to(SparsityCsr<ValueType, int32>* result) override;
367 
368  void convert_to(SparsityCsr<ValueType, int64>* result) const override;
369 
370  void move_to(SparsityCsr<ValueType, int64>* result) override;
371 
372  void read(const mat_data& data) override;
373 
374  void read(const mat_data32& data) override;
375 
376  void read(const device_mat_data& data) override;
377 
378  void read(const device_mat_data32& data) override;
379 
380  void read(device_mat_data&& data) override;
381 
382  void read(device_mat_data32&& data) override;
383 
384  void write(mat_data& data) const override;
385 
386  void write(mat_data32& data) const override;
387 
388  std::unique_ptr<LinOp> transpose() const override;
389 
390  std::unique_ptr<LinOp> conj_transpose() const override;
391 
398  void transpose(ptr_param<Dense> output) const;
399 
406  void conj_transpose(ptr_param<Dense> output) const;
407 
413  void fill(const ValueType value);
414 
429  std::unique_ptr<Dense> permute(
430  ptr_param<const Permutation<int32>> permutation,
432 
436  std::unique_ptr<Dense> permute(
437  ptr_param<const Permutation<int64>> permutation,
439 
445  void permute(ptr_param<const Permutation<int32>> permutation,
446  ptr_param<Dense> output, permute_mode mode) const;
447 
452  void permute(ptr_param<const Permutation<int64>> permutation,
453  ptr_param<Dense> output, permute_mode mode) const;
454 
468  std::unique_ptr<Dense> permute(
469  ptr_param<const Permutation<int32>> row_permutation,
470  ptr_param<const Permutation<int32>> column_permutation,
471  bool invert = false) const;
472 
477  std::unique_ptr<Dense> permute(
478  ptr_param<const Permutation<int64>> row_permutation,
479  ptr_param<const Permutation<int64>> column_permutation,
480  bool invert = false) const;
481 
488  void permute(ptr_param<const Permutation<int32>> row_permutation,
489  ptr_param<const Permutation<int32>> column_permutation,
490  ptr_param<Dense> output, bool invert = false) const;
491 
496  void permute(ptr_param<const Permutation<int64>> row_permutation,
497  ptr_param<const Permutation<int64>> column_permutation,
498  ptr_param<Dense> output, bool invert = false) const;
499 
509  std::unique_ptr<Dense> scale_permute(
512 
517  std::unique_ptr<Dense> scale_permute(
520 
527  void scale_permute(
529  ptr_param<Dense> output, permute_mode mode) const;
530 
535  void scale_permute(
537  ptr_param<Dense> output, permute_mode mode) const;
538 
551  std::unique_ptr<Dense> scale_permute(
552  ptr_param<const ScaledPermutation<value_type, int32>> row_permutation,
554  column_permutation,
555  bool invert = false) const;
556 
561  std::unique_ptr<Dense> scale_permute(
562  ptr_param<const ScaledPermutation<value_type, int64>> row_permutation,
564  column_permutation,
565  bool invert = false) const;
566 
573  void scale_permute(
574  ptr_param<const ScaledPermutation<value_type, int32>> row_permutation,
576  column_permutation,
577  ptr_param<Dense> output, bool invert = false) const;
578 
584  void scale_permute(
585  ptr_param<const ScaledPermutation<value_type, int64>> row_permutation,
587  column_permutation,
588  ptr_param<Dense> output, bool invert = false) const;
589 
590  std::unique_ptr<LinOp> permute(
591  const array<int32>* permutation_indices) const override;
592 
593  std::unique_ptr<LinOp> permute(
594  const array<int64>* permutation_indices) const override;
595 
605  void permute(const array<int32>* permutation_indices,
606  ptr_param<Dense> output) const;
607 
611  void permute(const array<int64>* permutation_indices,
612  ptr_param<Dense> output) const;
613 
614  std::unique_ptr<LinOp> inverse_permute(
615  const array<int32>* permutation_indices) const override;
616 
617  std::unique_ptr<LinOp> inverse_permute(
618  const array<int64>* permutation_indices) const override;
619 
630  void inverse_permute(const array<int32>* permutation_indices,
631  ptr_param<Dense> output) const;
632 
636  void inverse_permute(const array<int64>* permutation_indices,
637  ptr_param<Dense> output) const;
638 
639  std::unique_ptr<LinOp> row_permute(
640  const array<int32>* permutation_indices) const override;
641 
642  std::unique_ptr<LinOp> row_permute(
643  const array<int64>* permutation_indices) const override;
644 
654  void row_permute(const array<int32>* permutation_indices,
655  ptr_param<Dense> output) const;
656 
660  void row_permute(const array<int64>* permutation_indices,
661  ptr_param<Dense> output) const;
662 
673  std::unique_ptr<Dense> row_gather(const array<int32>* gather_indices) const;
674 
678  std::unique_ptr<Dense> row_gather(const array<int64>* gather_indices) const;
679 
692  void row_gather(const array<int32>* gather_indices,
693  ptr_param<LinOp> row_collection) const;
694 
698  void row_gather(const array<int64>* gather_indices,
699  ptr_param<LinOp> row_collection) const;
700 
715  const array<int32>* gather_indices,
717  ptr_param<LinOp> row_collection) const;
718 
724  const array<int64>* gather_indices,
726  ptr_param<LinOp> row_collection) const;
727 
728  std::unique_ptr<LinOp> column_permute(
729  const array<int32>* permutation_indices) const override;
730 
731  std::unique_ptr<LinOp> column_permute(
732  const array<int64>* permutation_indices) const override;
733 
743  void column_permute(const array<int32>* permutation_indices,
744  ptr_param<Dense> output) const;
745 
749  void column_permute(const array<int64>* permutation_indices,
750  ptr_param<Dense> output) const;
751 
752  std::unique_ptr<LinOp> inverse_row_permute(
753  const array<int32>* permutation_indices) const override;
754 
755  std::unique_ptr<LinOp> inverse_row_permute(
756  const array<int64>* permutation_indices) const override;
757 
767  void inverse_row_permute(const array<int32>* permutation_indices,
768  ptr_param<Dense> output) const;
769 
773  void inverse_row_permute(const array<int64>* permutation_indices,
774  ptr_param<Dense> output) const;
775 
776  std::unique_ptr<LinOp> inverse_column_permute(
777  const array<int32>* permutation_indices) const override;
778 
779  std::unique_ptr<LinOp> inverse_column_permute(
780  const array<int64>* permutation_indices) const override;
781 
791  void inverse_column_permute(const array<int32>* permutation_indices,
792  ptr_param<Dense> output) const;
793 
797  void inverse_column_permute(const array<int64>* permutation_indices,
798  ptr_param<Dense> output) const;
799 
800  std::unique_ptr<Diagonal<ValueType>> extract_diagonal() const override;
801 
809  void extract_diagonal(ptr_param<Diagonal<ValueType>> output) const;
810 
811  std::unique_ptr<absolute_type> compute_absolute() const override;
812 
820  void compute_absolute(ptr_param<absolute_type> output) const;
821 
822  void compute_absolute_inplace() override;
823 
828  std::unique_ptr<complex_type> make_complex() const;
829 
835  void make_complex(ptr_param<complex_type> result) const;
836 
841  std::unique_ptr<real_type> get_real() const;
842 
846  void get_real(ptr_param<real_type> result) const;
847 
852  std::unique_ptr<real_type> get_imag() const;
853 
858  void get_imag(ptr_param<real_type> result) const;
859 
865  value_type* get_values() noexcept { return values_.get_data(); }
866 
874  const value_type* get_const_values() const noexcept
875  {
876  return values_.get_const_data();
877  }
878 
884  size_type get_stride() const noexcept { return stride_; }
885 
892  {
893  return values_.get_size();
894  }
895 
896  device_view get_device_view();
897 
898  const_device_view get_const_device_view() const;
899 
910  value_type& at(size_type row, size_type col) noexcept
911  {
912  return values_.get_data()[linearize_index(row, col)];
913  }
914 
918  value_type at(size_type row, size_type col) const noexcept
919  {
920  return values_.get_const_data()[linearize_index(row, col)];
921  }
922 
937  ValueType& at(size_type idx) noexcept
938  {
939  return values_.get_data()[linearize_index(idx)];
940  }
941 
945  ValueType at(size_type idx) const noexcept
946  {
947  return values_.get_const_data()[linearize_index(idx)];
948  }
949 
959  void scale(ptr_param<const LinOp> alpha);
960 
970  void inv_scale(ptr_param<const LinOp> alpha);
971 
983 
995 
1004  void compute_dot(ptr_param<const LinOp> b, ptr_param<LinOp> result) const;
1005 
1018  array<char>& tmp) const;
1019 
1029  ptr_param<LinOp> result) const;
1030 
1043  array<char>& tmp) const;
1044 
1052  void compute_norm2(ptr_param<LinOp> result) const;
1053 
1064  void compute_norm2(ptr_param<LinOp> result, array<char>& tmp) const;
1065 
1073  void compute_norm1(ptr_param<LinOp> result) const;
1074 
1085  void compute_norm1(ptr_param<LinOp> result, array<char>& tmp) const;
1086 
1095  void compute_squared_norm2(ptr_param<LinOp> result) const;
1096 
1108  void compute_squared_norm2(ptr_param<LinOp> result, array<char>& tmp) const;
1109 
1117  void compute_mean(ptr_param<LinOp> result) const;
1118 
1129  void compute_mean(ptr_param<LinOp> result, array<char>& tmp) const;
1130 
1141  std::unique_ptr<Dense> create_submatrix(const span& rows,
1142  const span& columns,
1143  const size_type stride)
1144  {
1145  return this->create_submatrix_impl(rows, columns, stride);
1146  }
1147 
1154  std::unique_ptr<Dense> create_submatrix(const span& rows,
1155  const span& columns)
1156  {
1157  return create_submatrix(rows, columns, this->get_stride());
1158  }
1159 
1160 
1169  std::unique_ptr<Dense> create_submatrix(const local_span& rows,
1170  const local_span& columns,
1171  dim<2> size)
1172  {
1173  dim<2> deduced_size{rows.length(), columns.length()};
1174  GKO_ASSERT_EQUAL_DIMENSIONS(deduced_size, size);
1175  return create_submatrix(rows, columns, this->get_stride());
1176  }
1177 
1185  std::unique_ptr<real_type> create_real_view();
1186 
1190  std::unique_ptr<const real_type> create_real_view() const;
1191 
1204  static std::unique_ptr<Dense> create(std::shared_ptr<const Executor> exec,
1205  const dim<2>& size = {},
1206  size_type stride = 0);
1207 
1224  static std::unique_ptr<Dense> create(std::shared_ptr<const Executor> exec,
1225  const dim<2>& size,
1226  array<value_type> values,
1227  size_type stride);
1228 
1233  template <typename InputValueType>
1234  GKO_DEPRECATED(
1235  "explicitly construct the gko::array argument instead of passing an"
1236  "initializer list")
1237  static std::unique_ptr<Dense> create(
1238  std::shared_ptr<const Executor> exec, const dim<2>& size,
1239  std::initializer_list<InputValueType> values, size_type stride)
1240  {
1241  return create(exec, size, array<value_type>{exec, std::move(values)},
1242  stride);
1243  }
1244 
1256  static std::unique_ptr<const Dense> create_const(
1257  std::shared_ptr<const Executor> exec, const dim<2>& size,
1258  gko::detail::const_array_view<ValueType>&& values, size_type stride);
1259 
1265  Dense& operator=(const Dense&);
1266 
1272  Dense& operator=(Dense&&);
1273 
1278  Dense(const Dense&);
1279 
1284  Dense(Dense&&);
1285 
1286 protected:
1287  Dense(std::shared_ptr<const Executor> exec, const dim<2>& size = {},
1288  size_type stride = 0);
1289 
1290  Dense(std::shared_ptr<const Executor> exec, const dim<2>& size,
1291  array<value_type> values, size_type stride);
1292 
1299  virtual std::unique_ptr<Dense> create_with_same_config() const
1300  {
1301  return Dense::create(this->get_executor(), this->get_size(),
1302  this->get_stride());
1303  }
1304 
1312  virtual std::unique_ptr<Dense> create_with_type_of_impl(
1313  std::shared_ptr<const Executor> exec, const dim<2>& size,
1314  size_type stride) const
1315  {
1316  return Dense::create(exec, size, stride);
1317  }
1318 
1325  virtual std::unique_ptr<Dense> create_view_of_impl()
1326  {
1327  auto exec = this->get_executor();
1328  return Dense::create(
1329  exec, this->get_size(),
1331  this->get_values()),
1332  this->get_stride());
1333  }
1334 
1341  virtual std::unique_ptr<const Dense> create_const_view_of_impl() const
1342  {
1343  auto exec = this->get_executor();
1344  return Dense::create_const(
1345  exec, this->get_size(),
1347  this->get_const_values()),
1348  this->get_stride());
1349  }
1350 
1351  template <typename IndexType>
1352  void convert_impl(Coo<ValueType, IndexType>* result) const;
1353 
1354  template <typename IndexType>
1355  void convert_impl(Csr<ValueType, IndexType>* result) const;
1356 
1357  template <typename IndexType>
1358  void convert_impl(Ell<ValueType, IndexType>* result) const;
1359 
1360  template <typename IndexType>
1361  void convert_impl(Fbcsr<ValueType, IndexType>* result) const;
1362 
1363  template <typename IndexType>
1364  void convert_impl(Hybrid<ValueType, IndexType>* result) const;
1365 
1366  template <typename IndexType>
1367  void convert_impl(Sellp<ValueType, IndexType>* result) const;
1368 
1369  template <typename IndexType>
1370  void convert_impl(SparsityCsr<ValueType, IndexType>* result) const;
1371 
1378  virtual void scale_impl(const LinOp* alpha);
1379 
1386  virtual void inv_scale_impl(const LinOp* alpha);
1387 
1394  virtual void add_scaled_impl(const LinOp* alpha, const LinOp* b);
1395 
1402  virtual void sub_scaled_impl(const LinOp* alpha, const LinOp* b);
1403 
1410  virtual void compute_dot_impl(const LinOp* b, LinOp* result) const;
1411 
1418  virtual void compute_conj_dot_impl(const LinOp* b, LinOp* result) const;
1419 
1426  virtual void compute_norm2_impl(LinOp* result) const;
1427 
1434  virtual void compute_norm1_impl(LinOp* result) const;
1435 
1442  virtual void compute_squared_norm2_impl(LinOp* result) const;
1443 
1447  virtual void compute_mean_impl(LinOp* result) const;
1448 
1457  void resize(gko::dim<2> new_size);
1458 
1466  virtual std::unique_ptr<Dense> create_submatrix_impl(
1467  const span& rows, const span& columns, const size_type stride);
1468 
1469  void apply_impl(const LinOp* b, LinOp* x) const override;
1470 
1471  void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
1472  LinOp* x) const override;
1473 
1474  size_type linearize_index(size_type row, size_type col) const noexcept
1475  {
1476  return row * stride_ + col;
1477  }
1478 
1479  size_type linearize_index(size_type idx) const noexcept
1480  {
1481  return linearize_index(idx / this->get_size()[1],
1482  idx % this->get_size()[1]);
1483  }
1484 
1485  template <typename IndexType>
1486  void permute_impl(const Permutation<IndexType>* permutation,
1487  permute_mode mode, Dense* output) const;
1488 
1489  template <typename IndexType>
1490  void permute_impl(const Permutation<IndexType>* row_permutation,
1491  const Permutation<IndexType>* col_permutation,
1492  bool invert, Dense* output) const;
1493 
1494  template <typename IndexType>
1495  void scale_permute_impl(
1496  const ScaledPermutation<ValueType, IndexType>* permutation,
1497  permute_mode mode, Dense* output) const;
1498 
1499  template <typename IndexType>
1500  void scale_permute_impl(
1501  const ScaledPermutation<ValueType, IndexType>* row_permutation,
1502  const ScaledPermutation<ValueType, IndexType>* column_permutation,
1503  bool invert, Dense* output) const;
1504 
1505  template <typename OutputType, typename IndexType>
1506  void row_gather_impl(const array<IndexType>* row_idxs,
1507  Dense<OutputType>* row_collection) const;
1508 
1509  template <typename OutputType, typename IndexType>
1510  void row_gather_impl(const Dense<ValueType>* alpha,
1511  const array<IndexType>* row_idxs,
1512  const Dense<ValueType>* beta,
1513  Dense<OutputType>* row_collection) const;
1514 
1515 private:
1516  size_type stride_;
1517  array<value_type> values_;
1518 
1519  void add_scaled_identity_impl(const LinOp* a, const LinOp* b) override;
1520 };
1521 
1522 
1523 } // namespace matrix
1524 
1525 
1526 namespace detail {
1527 
1528 
1529 template <typename ValueType>
1530 struct temporary_clone_helper<matrix::Dense<ValueType>> {
1531  static std::unique_ptr<matrix::Dense<ValueType>> create(
1532  std::shared_ptr<const Executor> exec, matrix::Dense<ValueType>* ptr,
1533  bool copy_data)
1534  {
1535  if (copy_data) {
1536  return gko::clone(std::move(exec), ptr);
1537  } else {
1538  return matrix::Dense<ValueType>::create(exec, ptr->get_size());
1539  }
1540  }
1541 };
1542 
1543 
1544 } // namespace detail
1545 
1546 
1554 template <typename VecPtr>
1555 std::unique_ptr<matrix::Dense<typename detail::pointee<VecPtr>::value_type>>
1556 make_dense_view(VecPtr&& vector)
1557 {
1558  using value_type = typename detail::pointee<VecPtr>::value_type;
1560 }
1561 
1562 
1570 template <typename VecPtr>
1571 std::unique_ptr<
1572  const matrix::Dense<typename detail::pointee<VecPtr>::value_type>>
1573 make_const_dense_view(VecPtr&& vector)
1574 {
1575  using value_type = typename detail::pointee<VecPtr>::value_type;
1577 }
1578 
1579 
1600 template <typename Matrix, typename... TArgs>
1601 std::unique_ptr<Matrix> initialize(
1602  size_type stride, std::initializer_list<typename Matrix::value_type> vals,
1603  std::shared_ptr<const Executor> exec, TArgs&&... create_args)
1604 {
1606  size_type num_rows = vals.size();
1607  auto tmp = dense::create(exec->get_master(), dim<2>{num_rows, 1}, stride);
1608  size_type idx = 0;
1609  for (const auto& elem : vals) {
1610  tmp->at(idx) = elem;
1611  ++idx;
1612  }
1613  auto mtx = Matrix::create(exec, std::forward<TArgs>(create_args)...);
1614  tmp->move_to(mtx);
1615  return mtx;
1616 }
1617 
1638 template <typename Matrix, typename... TArgs>
1639 std::unique_ptr<Matrix> initialize(
1640  std::initializer_list<typename Matrix::value_type> vals,
1641  std::shared_ptr<const Executor> exec, TArgs&&... create_args)
1642 {
1643  return initialize<Matrix>(1, vals, std::move(exec),
1644  std::forward<TArgs>(create_args)...);
1645 }
1646 
1647 
1668 template <typename Matrix, typename... TArgs>
1669 std::unique_ptr<Matrix> initialize(
1670  size_type stride,
1671  std::initializer_list<std::initializer_list<typename Matrix::value_type>>
1672  vals,
1673  std::shared_ptr<const Executor> exec, TArgs&&... create_args)
1674 {
1676  size_type num_rows = vals.size();
1677  size_type num_cols = num_rows > 0 ? begin(vals)->size() : 1;
1678  auto tmp =
1679  dense::create(exec->get_master(), dim<2>{num_rows, num_cols}, stride);
1680  size_type ridx = 0;
1681  for (const auto& row : vals) {
1682  size_type cidx = 0;
1683  for (const auto& elem : row) {
1684  tmp->at(ridx, cidx) = elem;
1685  ++cidx;
1686  }
1687  ++ridx;
1688  }
1689  auto mtx = Matrix::create(exec, std::forward<TArgs>(create_args)...);
1690  tmp->move_to(mtx);
1691  return mtx;
1692 }
1693 
1694 
1716 template <typename Matrix, typename... TArgs>
1717 std::unique_ptr<Matrix> initialize(
1718  std::initializer_list<std::initializer_list<typename Matrix::value_type>>
1719  vals,
1720  std::shared_ptr<const Executor> exec, TArgs&&... create_args)
1721 {
1722  return initialize<Matrix>(vals.size() > 0 ? begin(vals)->size() : 0, vals,
1723  std::move(exec),
1724  std::forward<TArgs>(create_args)...);
1725 }
1726 
1727 
1728 } // namespace gko
1729 
1730 
1731 #endif // GKO_PUBLIC_CORE_MATRIX_DENSE_HPP_
gko::matrix::Dense::permute
std::unique_ptr< Dense > permute(ptr_param< const Permutation< int32 >> permutation, permute_mode mode=permute_mode::symmetric) const
Creates a permuted copy of this matrix with the given permutation .
gko::matrix::Dense::inverse_column_permute
std::unique_ptr< LinOp > inverse_column_permute(const array< int32 > *permutation_indices) const override
Returns a LinOp representing the row permutation of the inverse permuted object.
gko::matrix::Dense::add_scaled
void add_scaled(ptr_param< const LinOp > alpha, ptr_param< const LinOp > b)
Adds b scaled by alpha to the matrix (aka: BLAS axpy).
gko::matrix::Fbcsr
Fixed-block compressed sparse row storage matrix format.
Definition: csr.hpp:46
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::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::permute_mode::columns
The columns will be permuted.
gko::ReadableFromMatrixData< ValueType, int32 >::read
virtual void read(const matrix_data< ValueType, int32 > &data)=0
Reads a matrix from a matrix_data structure.
gko::matrix::Dense
Dense is a matrix format which explicitly stores all values of the matrix.
Definition: dense_cache.hpp:28
gko::matrix::SparsityCsr
SparsityCsr is a matrix format which stores only the sparsity pattern of a sparse matrix by compressi...
Definition: csr.hpp:40
gko::matrix::Dense::at
ValueType at(size_type idx) const noexcept
Returns a single element of the matrix.
Definition: dense.hpp:945
gko::matrix::Dense::row_gather
std::unique_ptr< Dense > row_gather(const array< int32 > *gather_indices) const
Create a Dense matrix consisting of the given rows from this matrix.
gko::matrix::Dense::make_complex
std::unique_ptr< complex_type > make_complex() const
Creates a complex copy of the original matrix.
gko::matrix::Dense::create
static std::unique_ptr< Dense > create(std::shared_ptr< const Executor > exec, const dim< 2 > &size={}, size_type stride=0)
Creates an uninitialized Dense matrix of the specified size.
gko::matrix::Dense::create_with_type_of
static std::unique_ptr< Dense > create_with_type_of(ptr_param< const Dense > other, std::shared_ptr< const Executor > exec, const dim< 2 > &size, size_type stride)
Definition: dense.hpp:238
gko::matrix::Dense::scale
void scale(ptr_param< const LinOp > alpha)
Scales the matrix with a scalar (aka: BLAS scal).
gko::matrix::ScaledPermutation
ScaledPermutation is a matrix combining a permutation with scaling factors.
Definition: scaled_permutation.hpp:36
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:90
gko::matrix::Dense::get_real
std::unique_ptr< real_type > get_real() const
Creates a new real matrix and extracts the real part of the original matrix into that.
gko::matrix::Dense::inv_scale
void inv_scale(ptr_param< const LinOp > alpha)
Scales the matrix with the inverse of a scalar.
gko::matrix::Permutation
Permutation is a matrix format that represents a permutation matrix, i.e.
Definition: csr.hpp:52
gko::matrix::Dense::fill
void fill(const ValueType value)
Fill the dense matrix with a given value.
gko::make_const_array_view
detail::const_array_view< ValueType > make_const_array_view(std::shared_ptr< const Executor > exec, size_type size, const ValueType *data)
Helper function to create a const array view deducing the value type.
Definition: array.hpp:820
gko::matrix::Dense::compute_dot
void compute_dot(ptr_param< const LinOp > b, ptr_param< LinOp > result) const
Computes the column-wise dot product of this matrix and b.
gko::matrix::Dense::create_submatrix
std::unique_ptr< Dense > create_submatrix(const span &rows, const span &columns, const size_type stride)
Create a submatrix from the original matrix.
Definition: dense.hpp:1141
gko::matrix::Dense::create_with_config_of
static std::unique_ptr< Dense > create_with_config_of(ptr_param< const Dense > other)
Creates a Dense matrix with the same size and stride as another Dense matrix.
Definition: dense.hpp:201
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:1601
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::matrix::Dense::at
value_type & at(size_type row, size_type col) noexcept
Returns a single element of the matrix.
Definition: dense.hpp:910
gko::matrix::Dense::get_stride
size_type get_stride() const noexcept
Returns the stride of the matrix.
Definition: dense.hpp:884
gko::matrix::Dense::create_view_of
static std::unique_ptr< Dense > create_view_of(ptr_param< Dense > other)
Creates a Dense matrix, where the underlying array is a view of another Dense matrix' array.
Definition: dense.hpp:272
gko::range
A range is a multidimensional view of the memory.
Definition: range.hpp:304
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::matrix::Dense::get_num_stored_elements
size_type get_num_stored_elements() const noexcept
Returns the number of elements explicitly stored in the matrix.
Definition: dense.hpp:891
gko::matrix::Dense::sub_scaled
void sub_scaled(ptr_param< const LinOp > alpha, ptr_param< const LinOp > b)
Subtracts b scaled by alpha from the matrix (aka: BLAS axpy).
gko::matrix::Dense::get_values
value_type * get_values() noexcept
Returns a pointer to the array of values of the matrix.
Definition: dense.hpp:865
gko::array
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition: array.hpp:26
gko::matrix::Dense::compute_norm2
void compute_norm2(ptr_param< LinOp > result) const
Computes the column-wise Euclidean (L^2) norm of this matrix.
gko::matrix::Dense::row_permute
std::unique_ptr< LinOp > row_permute(const array< int32 > *permutation_indices) const override
Returns a LinOp representing the row permutation of the Permutable object.
gko::matrix::Dense::create_with_type_of
static std::unique_ptr< Dense > create_with_type_of(ptr_param< const Dense > other, std::shared_ptr< const Executor > exec, const dim< 2 > &size=dim< 2 >{})
Creates a Dense matrix with the same type as another Dense matrix but on a different executor and wit...
Definition: dense.hpp:222
gko::span
A span is a lightweight structure used to create sub-ranges from other ranges.
Definition: range.hpp:46
gko::dim< 2 >
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::matrix::Dense::create_const
static std::unique_ptr< const Dense > create_const(std::shared_ptr< const Executor > exec, const dim< 2 > &size, gko::detail::const_array_view< ValueType > &&values, size_type stride)
Creates a constant (immutable) Dense matrix from a constant array.
gko::ptr_param
This class is used for function parameters in the place of raw pointers.
Definition: utils_helper.hpp:43
gko::matrix::Dense::at
value_type at(size_type row, size_type col) const noexcept
Returns a single element of the matrix.
Definition: dense.hpp:918
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::Dense::compute_mean
void compute_mean(ptr_param< LinOp > result) const
Computes the column-wise arithmetic mean of this matrix.
gko::matrix::Dense::inverse_row_permute
std::unique_ptr< LinOp > inverse_row_permute(const array< int32 > *permutation_indices) const override
Returns a LinOp representing the row permutation of the inverse permuted object.
gko::matrix::permute_mode::symmetric
The rows and columns will be permuted.
gko::matrix::Dense::get_imag
std::unique_ptr< real_type > get_imag() const
Creates a new real matrix and extracts the imaginary part of the original matrix into that.
gko::matrix::Dense::compute_squared_norm2
void compute_squared_norm2(ptr_param< LinOp > result) const
Computes the square of the column-wise Euclidean (L^2) norm of this matrix.
gko::matrix::Dense::operator=
Dense & operator=(const Dense &)
Copy-assigns a Dense matrix.
gko::matrix::Dense::inverse_permute
std::unique_ptr< LinOp > inverse_permute(const array< int32 > *permutation_indices) const override
Returns a LinOp representing the symmetric inverse row and column permutation of the Permutable objec...
gko::matrix::Dense::create_with_type_of
static std::unique_ptr< Dense > create_with_type_of(ptr_param< const Dense > other, std::shared_ptr< const Executor > exec, const dim< 2 > &size, const dim< 2 > &local_size, size_type stride)
Definition: dense.hpp:256
gko::stop::mode
mode
The mode for the residual norm criterion.
Definition: residual_norm.hpp:37
gko::matrix::Dense::scale_permute
std::unique_ptr< Dense > scale_permute(ptr_param< const ScaledPermutation< value_type, int32 >> permutation, permute_mode mode=permute_mode::symmetric) const
Creates a scaled and permuted copy of this matrix.
gko::matrix::Dense::compute_norm1
void compute_norm1(ptr_param< LinOp > result) const
Computes the column-wise (L^1) norm of this matrix.
gko::matrix::Dense::get_const_values
const value_type * get_const_values() const noexcept
Returns a pointer to the array of values of the matrix.
Definition: dense.hpp:874
gko::make_array_view
array< ValueType > make_array_view(std::shared_ptr< const Executor > exec, size_type size, ValueType *data)
Helper function to create an array view deducing the value type.
Definition: array.hpp:801
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::next_precision
typename detail::find_precision_impl< T, step >::type next_precision
Obtains the next move type of T in the singly-linked precision corresponding bfloat16/half.
Definition: math.hpp:466
gko::matrix::Dense::compute_absolute_inplace
void compute_absolute_inplace() override
Compute absolute inplace on each element.
gko::matrix::Dense::column_permute
std::unique_ptr< LinOp > column_permute(const array< int32 > *permutation_indices) const override
Returns a LinOp representing the column permutation of the Permutable object.
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::matrix::Dense::create_const_view_of
static std::unique_ptr< const Dense > create_const_view_of(ptr_param< const Dense > other)
Creates a immutable Dense matrix, where the underlying array is a view of another Dense matrix' array...
Definition: dense.hpp:284
gko::matrix::Dense::at
ValueType & at(size_type idx) noexcept
Returns a single element of the matrix.
Definition: dense.hpp:937
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:113
gko::matrix::Dense::compute_conj_dot
void compute_conj_dot(ptr_param< const LinOp > b, ptr_param< LinOp > result) const
Computes the column-wise dot product of conj(this matrix) and b.
gko::matrix::Ell
ELL is a matrix format where stride with explicit zeros is used such that all rows have the same numb...
Definition: csr.hpp:31
gko::ConvertibleTo
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition: polymorphic_object.hpp:140
gko::matrix::Dense::create_submatrix
std::unique_ptr< Dense > create_submatrix(const span &rows, const span &columns)
Create a submatrix from the original matrix.
Definition: dense.hpp:1154
gko::int32
std::int32_t int32
32-bit signed integral type.
Definition: types.hpp:107
gko::matrix::Dense::compute_absolute
std::unique_ptr< absolute_type > compute_absolute() const override
Gets the AbsoluteLinOp.
gko::make_dense_view
std::unique_ptr< matrix::Dense< typename detail::pointee< VecPtr >::value_type > > make_dense_view(VecPtr &&vector)
Creates a view of a given Dense vector.
Definition: dense.hpp:1556
gko::Executor
The first step in using the Ginkgo library consists of creating an executor.
Definition: executor.hpp:616
gko::matrix::Hybrid
HYBRID is a matrix format which splits the matrix into ELLPACK and COO format.
Definition: coo.hpp:33
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::permute_mode
permute_mode
Specifies how a permutation will be applied to a matrix.
Definition: permutation.hpp:42
gko::matrix::Dense::Dense
Dense(const Dense &)
Copy-constructs a Dense matrix.
gko::matrix::Sellp
SELL-P is a matrix format similar to ELL format.
Definition: csr.hpp:37
gko::make_const_dense_view
std::unique_ptr< const matrix::Dense< typename detail::pointee< VecPtr >::value_type > > make_const_dense_view(VecPtr &&vector)
Creates a view of a given Dense vector.
Definition: dense.hpp:1573
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::PolymorphicObject::get_executor
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition: polymorphic_object.hpp:62
gko::array::get_size
size_type get_size() const noexcept
Returns the number of elements in the array.
Definition: array.hpp:670
gko::LinOp::get_size
const dim< 2 > & get_size() const noexcept
Returns the size of the operator.
Definition: lin_op.hpp:169
gko::local_span
A span that is used exclusively for local numbering.
Definition: range.hpp:138
gko::matrix::Dense::create_submatrix
std::unique_ptr< Dense > create_submatrix(const local_span &rows, const local_span &columns, dim< 2 > size)
Create a submatrix from the original matrix.
Definition: dense.hpp:1169
gko::matrix::Dense::create_real_view
std::unique_ptr< real_type > create_real_view()
Create a real view of the (potentially) complex original matrix.
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:283
gko::matrix::Coo
COO stores a matrix in the coordinate matrix format.
Definition: coo.hpp:51