Ginkgo  Generated from pipelines/1330831941 branch based on master. Ginkgo version 1.8.0
A numerical linear algebra library targeting many-core architectures
dense.hpp
1 // SPDX-FileCopyrightText: 2017 - 2024 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 
13 #include <ginkgo/core/base/array.hpp>
14 #include <ginkgo/core/base/exception_helpers.hpp>
15 #include <ginkgo/core/base/executor.hpp>
16 #include <ginkgo/core/base/lin_op.hpp>
17 #include <ginkgo/core/base/range_accessors.hpp>
18 #include <ginkgo/core/base/types.hpp>
19 #include <ginkgo/core/base/utils.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 }
34 } // namespace experimental
35 
36 
37 namespace matrix {
38 
39 
40 template <typename ValueType, typename IndexType>
41 class Coo;
42 
43 template <typename ValueType, typename IndexType>
44 class Csr;
45 
46 template <typename ValueType>
47 class Diagonal;
48 
49 template <typename ValueType, typename IndexType>
50 class Ell;
51 
52 template <typename ValueType, typename IndexType>
53 class Fbcsr;
54 
55 template <typename ValueType, typename IndexType>
56 class Hybrid;
57 
58 template <typename ValueType, typename IndexType>
59 class Sellp;
60 
61 template <typename ValueType, typename IndexType>
62 class SparsityCsr;
63 
64 
80 template <typename ValueType = default_precision>
81 class Dense
82  : public EnableLinOp<Dense<ValueType>>,
83  public ConvertibleTo<Dense<next_precision<ValueType>>>,
84  public ConvertibleTo<Coo<ValueType, int32>>,
85  public ConvertibleTo<Coo<ValueType, int64>>,
86  public ConvertibleTo<Csr<ValueType, int32>>,
87  public ConvertibleTo<Csr<ValueType, int64>>,
88  public ConvertibleTo<Ell<ValueType, int32>>,
89  public ConvertibleTo<Ell<ValueType, int64>>,
90  public ConvertibleTo<Fbcsr<ValueType, int32>>,
91  public ConvertibleTo<Fbcsr<ValueType, int64>>,
92  public ConvertibleTo<Hybrid<ValueType, int32>>,
93  public ConvertibleTo<Hybrid<ValueType, int64>>,
94  public ConvertibleTo<Sellp<ValueType, int32>>,
95  public ConvertibleTo<Sellp<ValueType, int64>>,
96  public ConvertibleTo<SparsityCsr<ValueType, int32>>,
97  public ConvertibleTo<SparsityCsr<ValueType, int64>>,
98  public DiagonalExtractable<ValueType>,
99  public ReadableFromMatrixData<ValueType, int32>,
100  public ReadableFromMatrixData<ValueType, int64>,
101  public WritableToMatrixData<ValueType, int32>,
102  public WritableToMatrixData<ValueType, int64>,
103  public Transposable,
104  public Permutable<int32>,
105  public Permutable<int64>,
106  public EnableAbsoluteComputation<remove_complex<Dense<ValueType>>>,
107  public ScaledIdentityAddable {
108  friend class EnablePolymorphicObject<Dense, LinOp>;
109  friend class Coo<ValueType, int32>;
110  friend class Coo<ValueType, int64>;
111  friend class Csr<ValueType, int32>;
112  friend class Csr<ValueType, int64>;
113  friend class Diagonal<ValueType>;
114  friend class Ell<ValueType, int32>;
115  friend class Ell<ValueType, int64>;
116  friend class Fbcsr<ValueType, int32>;
117  friend class Fbcsr<ValueType, int64>;
118  friend class Hybrid<ValueType, int32>;
119  friend class Hybrid<ValueType, int64>;
120  friend class Sellp<ValueType, int32>;
121  friend class Sellp<ValueType, int64>;
122  friend class SparsityCsr<ValueType, int32>;
123  friend class SparsityCsr<ValueType, int64>;
124  friend class Dense<to_complex<ValueType>>;
125  friend class experimental::distributed::Vector<ValueType>;
126 
127 public:
130  using ConvertibleTo<Dense<next_precision<ValueType>>>::convert_to;
131  using ConvertibleTo<Dense<next_precision<ValueType>>>::move_to;
132  using ConvertibleTo<Coo<ValueType, int32>>::convert_to;
133  using ConvertibleTo<Coo<ValueType, int32>>::move_to;
134  using ConvertibleTo<Coo<ValueType, int64>>::convert_to;
135  using ConvertibleTo<Coo<ValueType, int64>>::move_to;
136  using ConvertibleTo<Csr<ValueType, int32>>::convert_to;
137  using ConvertibleTo<Csr<ValueType, int32>>::move_to;
138  using ConvertibleTo<Csr<ValueType, int64>>::convert_to;
139  using ConvertibleTo<Csr<ValueType, int64>>::move_to;
140  using ConvertibleTo<Ell<ValueType, int32>>::convert_to;
141  using ConvertibleTo<Ell<ValueType, int32>>::move_to;
142  using ConvertibleTo<Ell<ValueType, int64>>::convert_to;
143  using ConvertibleTo<Ell<ValueType, int64>>::move_to;
144  using ConvertibleTo<Fbcsr<ValueType, int32>>::convert_to;
145  using ConvertibleTo<Fbcsr<ValueType, int32>>::move_to;
146  using ConvertibleTo<Fbcsr<ValueType, int64>>::convert_to;
147  using ConvertibleTo<Fbcsr<ValueType, int64>>::move_to;
148  using ConvertibleTo<Hybrid<ValueType, int32>>::convert_to;
149  using ConvertibleTo<Hybrid<ValueType, int32>>::move_to;
150  using ConvertibleTo<Hybrid<ValueType, int64>>::convert_to;
151  using ConvertibleTo<Hybrid<ValueType, int64>>::move_to;
152  using ConvertibleTo<Sellp<ValueType, int32>>::convert_to;
153  using ConvertibleTo<Sellp<ValueType, int32>>::move_to;
154  using ConvertibleTo<Sellp<ValueType, int64>>::convert_to;
155  using ConvertibleTo<Sellp<ValueType, int64>>::move_to;
156  using ConvertibleTo<SparsityCsr<ValueType, int32>>::convert_to;
157  using ConvertibleTo<SparsityCsr<ValueType, int32>>::move_to;
158  using ConvertibleTo<SparsityCsr<ValueType, int64>>::convert_to;
159  using ConvertibleTo<SparsityCsr<ValueType, int64>>::move_to;
162 
163  using value_type = ValueType;
164  using index_type = int64;
165  using transposed_type = Dense<ValueType>;
166  using mat_data = matrix_data<ValueType, int64>;
167  using mat_data32 = matrix_data<ValueType, int32>;
168  using device_mat_data = device_matrix_data<ValueType, int64>;
169  using device_mat_data32 = device_matrix_data<ValueType, int32>;
170  using absolute_type = remove_complex<Dense>;
171  using real_type = absolute_type;
172  using complex_type = to_complex<Dense>;
173 
174  using row_major_range = gko::range<gko::accessor::row_major<ValueType, 2>>;
175 
182  static std::unique_ptr<Dense> create_with_config_of(
184  {
185  // De-referencing `other` before calling the functions (instead of
186  // using operator `->`) is currently required to be compatible with
187  // CUDA 10.1.
188  // Otherwise, it results in a compile error.
189  return (*other).create_with_same_config();
190  }
191 
203  static std::unique_ptr<Dense> create_with_type_of(
204  ptr_param<const Dense> other, std::shared_ptr<const Executor> exec,
205  const dim<2>& size = dim<2>{})
206  {
207  // See create_with_config_of()
208  return (*other).create_with_type_of_impl(exec, size, size[1]);
209  }
210 
219  static std::unique_ptr<Dense> create_with_type_of(
220  ptr_param<const Dense> other, std::shared_ptr<const Executor> exec,
221  const dim<2>& size, size_type stride)
222  {
223  // See create_with_config_of()
224  return (*other).create_with_type_of_impl(exec, size, stride);
225  }
226 
237  static std::unique_ptr<Dense> create_with_type_of(
238  ptr_param<const Dense> other, std::shared_ptr<const Executor> exec,
239  const dim<2>& size, const dim<2>& local_size, size_type stride)
240  {
241  // See create_with_config_of()
242  return (*other).create_with_type_of_impl(exec, size, stride);
243  }
244 
253  static std::unique_ptr<Dense> create_view_of(ptr_param<Dense> other)
254  {
255  return other->create_view_of_impl();
256  }
257 
265  static std::unique_ptr<const Dense> create_const_view_of(
267  {
268  return other->create_const_view_of_impl();
269  }
270 
271  friend class Dense<next_precision<ValueType>>;
272 
273  void convert_to(Dense<next_precision<ValueType>>* result) const override;
274 
275  void move_to(Dense<next_precision<ValueType>>* result) override;
276 
277  void convert_to(Coo<ValueType, int32>* result) const override;
278 
279  void move_to(Coo<ValueType, int32>* result) override;
280 
281  void convert_to(Coo<ValueType, int64>* result) const override;
282 
283  void move_to(Coo<ValueType, int64>* result) override;
284 
285  void convert_to(Csr<ValueType, int32>* result) const override;
286 
287  void move_to(Csr<ValueType, int32>* result) override;
288 
289  void convert_to(Csr<ValueType, int64>* result) const override;
290 
291  void move_to(Csr<ValueType, int64>* result) override;
292 
293  void convert_to(Ell<ValueType, int32>* result) const override;
294 
295  void move_to(Ell<ValueType, int32>* result) override;
296 
297  void convert_to(Ell<ValueType, int64>* result) const override;
298 
299  void move_to(Ell<ValueType, int64>* result) override;
300 
301  void convert_to(Fbcsr<ValueType, int32>* result) const override;
302 
303  void move_to(Fbcsr<ValueType, int32>* result) override;
304 
305  void convert_to(Fbcsr<ValueType, int64>* result) const override;
306 
307  void move_to(Fbcsr<ValueType, int64>* result) override;
308 
309  void convert_to(Hybrid<ValueType, int32>* result) const override;
310 
311  void move_to(Hybrid<ValueType, int32>* result) override;
312 
313  void convert_to(Hybrid<ValueType, int64>* result) const override;
314 
315  void move_to(Hybrid<ValueType, int64>* result) override;
316 
317  void convert_to(Sellp<ValueType, int32>* result) const override;
318 
319  void move_to(Sellp<ValueType, int32>* result) override;
320 
321  void convert_to(Sellp<ValueType, int64>* result) const override;
322 
323  void move_to(Sellp<ValueType, int64>* result) override;
324 
325  void convert_to(SparsityCsr<ValueType, int32>* result) const override;
326 
327  void move_to(SparsityCsr<ValueType, int32>* result) override;
328 
329  void convert_to(SparsityCsr<ValueType, int64>* result) const override;
330 
331  void move_to(SparsityCsr<ValueType, int64>* result) override;
332 
333  void read(const mat_data& data) override;
334 
335  void read(const mat_data32& data) override;
336 
337  void read(const device_mat_data& data) override;
338 
339  void read(const device_mat_data32& data) override;
340 
341  void read(device_mat_data&& data) override;
342 
343  void read(device_mat_data32&& data) override;
344 
345  void write(mat_data& data) const override;
346 
347  void write(mat_data32& data) const override;
348 
349  std::unique_ptr<LinOp> transpose() const override;
350 
351  std::unique_ptr<LinOp> conj_transpose() const override;
352 
359  void transpose(ptr_param<Dense> output) const;
360 
367  void conj_transpose(ptr_param<Dense> output) const;
368 
374  void fill(const ValueType value);
375 
390  std::unique_ptr<Dense> permute(
391  ptr_param<const Permutation<int32>> permutation,
393 
397  std::unique_ptr<Dense> permute(
398  ptr_param<const Permutation<int64>> permutation,
400 
406  void permute(ptr_param<const Permutation<int32>> permutation,
407  ptr_param<Dense> output, permute_mode mode) const;
408 
413  void permute(ptr_param<const Permutation<int64>> permutation,
414  ptr_param<Dense> output, permute_mode mode) const;
415 
429  std::unique_ptr<Dense> permute(
430  ptr_param<const Permutation<int32>> row_permutation,
431  ptr_param<const Permutation<int32>> column_permutation,
432  bool invert = false) const;
433 
438  std::unique_ptr<Dense> permute(
439  ptr_param<const Permutation<int64>> row_permutation,
440  ptr_param<const Permutation<int64>> column_permutation,
441  bool invert = false) const;
442 
449  void permute(ptr_param<const Permutation<int32>> row_permutation,
450  ptr_param<const Permutation<int32>> column_permutation,
451  ptr_param<Dense> output, bool invert = false) const;
452 
457  void permute(ptr_param<const Permutation<int64>> row_permutation,
458  ptr_param<const Permutation<int64>> column_permutation,
459  ptr_param<Dense> output, bool invert = false) const;
460 
470  std::unique_ptr<Dense> scale_permute(
473 
478  std::unique_ptr<Dense> scale_permute(
481 
488  void scale_permute(
490  ptr_param<Dense> output, permute_mode mode) const;
491 
496  void scale_permute(
498  ptr_param<Dense> output, permute_mode mode) const;
499 
512  std::unique_ptr<Dense> scale_permute(
513  ptr_param<const ScaledPermutation<value_type, int32>> row_permutation,
515  column_permutation,
516  bool invert = false) const;
517 
522  std::unique_ptr<Dense> scale_permute(
523  ptr_param<const ScaledPermutation<value_type, int64>> row_permutation,
525  column_permutation,
526  bool invert = false) const;
527 
534  void scale_permute(
535  ptr_param<const ScaledPermutation<value_type, int32>> row_permutation,
537  column_permutation,
538  ptr_param<Dense> output, bool invert = false) const;
539 
545  void scale_permute(
546  ptr_param<const ScaledPermutation<value_type, int64>> row_permutation,
548  column_permutation,
549  ptr_param<Dense> output, bool invert = false) const;
550 
551  std::unique_ptr<LinOp> permute(
552  const array<int32>* permutation_indices) const override;
553 
554  std::unique_ptr<LinOp> permute(
555  const array<int64>* permutation_indices) const override;
556 
566  void permute(const array<int32>* permutation_indices,
567  ptr_param<Dense> output) const;
568 
572  void permute(const array<int64>* permutation_indices,
573  ptr_param<Dense> output) const;
574 
575  std::unique_ptr<LinOp> inverse_permute(
576  const array<int32>* permutation_indices) const override;
577 
578  std::unique_ptr<LinOp> inverse_permute(
579  const array<int64>* permutation_indices) const override;
580 
591  void inverse_permute(const array<int32>* permutation_indices,
592  ptr_param<Dense> output) const;
593 
597  void inverse_permute(const array<int64>* permutation_indices,
598  ptr_param<Dense> output) const;
599 
600  std::unique_ptr<LinOp> row_permute(
601  const array<int32>* permutation_indices) const override;
602 
603  std::unique_ptr<LinOp> row_permute(
604  const array<int64>* permutation_indices) const override;
605 
615  void row_permute(const array<int32>* permutation_indices,
616  ptr_param<Dense> output) const;
617 
621  void row_permute(const array<int64>* permutation_indices,
622  ptr_param<Dense> output) const;
623 
634  std::unique_ptr<Dense> row_gather(const array<int32>* gather_indices) const;
635 
639  std::unique_ptr<Dense> row_gather(const array<int64>* gather_indices) const;
640 
653  void row_gather(const array<int32>* gather_indices,
654  ptr_param<LinOp> row_collection) const;
655 
659  void row_gather(const array<int64>* gather_indices,
660  ptr_param<LinOp> row_collection) const;
661 
676  const array<int32>* gather_indices,
678  ptr_param<LinOp> row_collection) const;
679 
685  const array<int64>* gather_indices,
687  ptr_param<LinOp> row_collection) const;
688 
689  std::unique_ptr<LinOp> column_permute(
690  const array<int32>* permutation_indices) const override;
691 
692  std::unique_ptr<LinOp> column_permute(
693  const array<int64>* permutation_indices) const override;
694 
704  void column_permute(const array<int32>* permutation_indices,
705  ptr_param<Dense> output) const;
706 
710  void column_permute(const array<int64>* permutation_indices,
711  ptr_param<Dense> output) const;
712 
713  std::unique_ptr<LinOp> inverse_row_permute(
714  const array<int32>* permutation_indices) const override;
715 
716  std::unique_ptr<LinOp> inverse_row_permute(
717  const array<int64>* permutation_indices) const override;
718 
728  void inverse_row_permute(const array<int32>* permutation_indices,
729  ptr_param<Dense> output) const;
730 
734  void inverse_row_permute(const array<int64>* permutation_indices,
735  ptr_param<Dense> output) const;
736 
737  std::unique_ptr<LinOp> inverse_column_permute(
738  const array<int32>* permutation_indices) const override;
739 
740  std::unique_ptr<LinOp> inverse_column_permute(
741  const array<int64>* permutation_indices) const override;
742 
752  void inverse_column_permute(const array<int32>* permutation_indices,
753  ptr_param<Dense> output) const;
754 
758  void inverse_column_permute(const array<int64>* permutation_indices,
759  ptr_param<Dense> output) const;
760 
761  std::unique_ptr<Diagonal<ValueType>> extract_diagonal() const override;
762 
770  void extract_diagonal(ptr_param<Diagonal<ValueType>> output) const;
771 
772  std::unique_ptr<absolute_type> compute_absolute() const override;
773 
781  void compute_absolute(ptr_param<absolute_type> output) const;
782 
783  void compute_absolute_inplace() override;
784 
789  std::unique_ptr<complex_type> make_complex() const;
790 
796  void make_complex(ptr_param<complex_type> result) const;
797 
802  std::unique_ptr<real_type> get_real() const;
803 
807  void get_real(ptr_param<real_type> result) const;
808 
813  std::unique_ptr<real_type> get_imag() const;
814 
819  void get_imag(ptr_param<real_type> result) const;
820 
826  value_type* get_values() noexcept { return values_.get_data(); }
827 
835  const value_type* get_const_values() const noexcept
836  {
837  return values_.get_const_data();
838  }
839 
845  size_type get_stride() const noexcept { return stride_; }
846 
853  {
854  return values_.get_size();
855  }
856 
867  value_type& at(size_type row, size_type col) noexcept
868  {
869  return values_.get_data()[linearize_index(row, col)];
870  }
871 
875  value_type at(size_type row, size_type col) const noexcept
876  {
877  return values_.get_const_data()[linearize_index(row, col)];
878  }
879 
894  ValueType& at(size_type idx) noexcept
895  {
896  return values_.get_data()[linearize_index(idx)];
897  }
898 
902  ValueType at(size_type idx) const noexcept
903  {
904  return values_.get_const_data()[linearize_index(idx)];
905  }
906 
916  void scale(ptr_param<const LinOp> alpha);
917 
927  void inv_scale(ptr_param<const LinOp> alpha);
928 
940 
952 
962 
975  array<char>& tmp) const;
976 
986  ptr_param<LinOp> result) const;
987 
1000  array<char>& tmp) const;
1001 
1009  void compute_norm2(ptr_param<LinOp> result) const;
1010 
1021  void compute_norm2(ptr_param<LinOp> result, array<char>& tmp) const;
1022 
1030  void compute_norm1(ptr_param<LinOp> result) const;
1031 
1042  void compute_norm1(ptr_param<LinOp> result, array<char>& tmp) const;
1043 
1052  void compute_squared_norm2(ptr_param<LinOp> result) const;
1053 
1065  void compute_squared_norm2(ptr_param<LinOp> result, array<char>& tmp) const;
1066 
1074  void compute_mean(ptr_param<LinOp> result) const;
1075 
1086  void compute_mean(ptr_param<LinOp> result, array<char>& tmp) const;
1087 
1098  std::unique_ptr<Dense> create_submatrix(const span& rows,
1099  const span& columns,
1100  const size_type stride)
1101  {
1102  return this->create_submatrix_impl(rows, columns, stride);
1103  }
1104 
1111  std::unique_ptr<Dense> create_submatrix(const span& rows,
1112  const span& columns)
1113  {
1114  return create_submatrix(rows, columns, this->get_stride());
1115  }
1116 
1124  std::unique_ptr<real_type> create_real_view();
1125 
1129  std::unique_ptr<const real_type> create_real_view() const;
1130 
1143  static std::unique_ptr<Dense> create(std::shared_ptr<const Executor> exec,
1144  const dim<2>& size = {},
1145  size_type stride = 0);
1146 
1163  static std::unique_ptr<Dense> create(std::shared_ptr<const Executor> exec,
1164  const dim<2>& size,
1165  array<value_type> values,
1166  size_type stride);
1167 
1172  template <typename InputValueType>
1173  GKO_DEPRECATED(
1174  "explicitly construct the gko::array argument instead of passing an"
1175  "initializer list")
1176  static std::unique_ptr<Dense> create(
1177  std::shared_ptr<const Executor> exec, const dim<2>& size,
1178  std::initializer_list<InputValueType> values, size_type stride)
1179  {
1180  return create(exec, size, array<value_type>{exec, std::move(values)},
1181  stride);
1182  }
1183 
1195  static std::unique_ptr<const Dense> create_const(
1196  std::shared_ptr<const Executor> exec, const dim<2>& size,
1197  gko::detail::const_array_view<ValueType>&& values, size_type stride);
1198 
1204  Dense& operator=(const Dense&);
1205 
1211  Dense& operator=(Dense&&);
1212 
1217  Dense(const Dense&);
1218 
1223  Dense(Dense&&);
1224 
1225 protected:
1226  Dense(std::shared_ptr<const Executor> exec, const dim<2>& size = {},
1227  size_type stride = 0);
1228 
1229  Dense(std::shared_ptr<const Executor> exec, const dim<2>& size,
1230  array<value_type> values, size_type stride);
1231 
1238  virtual std::unique_ptr<Dense> create_with_same_config() const
1239  {
1240  return Dense::create(this->get_executor(), this->get_size(),
1241  this->get_stride());
1242  }
1243 
1251  virtual std::unique_ptr<Dense> create_with_type_of_impl(
1252  std::shared_ptr<const Executor> exec, const dim<2>& size,
1253  size_type stride) const
1254  {
1255  return Dense::create(exec, size, stride);
1256  }
1257 
1264  virtual std::unique_ptr<Dense> create_view_of_impl()
1265  {
1266  auto exec = this->get_executor();
1267  return Dense::create(
1268  exec, this->get_size(),
1270  this->get_values()),
1271  this->get_stride());
1272  }
1273 
1280  virtual std::unique_ptr<const Dense> create_const_view_of_impl() const
1281  {
1282  auto exec = this->get_executor();
1283  return Dense::create_const(
1284  exec, this->get_size(),
1286  this->get_const_values()),
1287  this->get_stride());
1288  }
1289 
1290  template <typename IndexType>
1291  void convert_impl(Coo<ValueType, IndexType>* result) const;
1292 
1293  template <typename IndexType>
1294  void convert_impl(Csr<ValueType, IndexType>* result) const;
1295 
1296  template <typename IndexType>
1297  void convert_impl(Ell<ValueType, IndexType>* result) const;
1298 
1299  template <typename IndexType>
1300  void convert_impl(Fbcsr<ValueType, IndexType>* result) const;
1301 
1302  template <typename IndexType>
1303  void convert_impl(Hybrid<ValueType, IndexType>* result) const;
1304 
1305  template <typename IndexType>
1306  void convert_impl(Sellp<ValueType, IndexType>* result) const;
1307 
1308  template <typename IndexType>
1309  void convert_impl(SparsityCsr<ValueType, IndexType>* result) const;
1310 
1317  virtual void scale_impl(const LinOp* alpha);
1318 
1325  virtual void inv_scale_impl(const LinOp* alpha);
1326 
1333  virtual void add_scaled_impl(const LinOp* alpha, const LinOp* b);
1334 
1341  virtual void sub_scaled_impl(const LinOp* alpha, const LinOp* b);
1342 
1349  virtual void compute_dot_impl(const LinOp* b, LinOp* result) const;
1350 
1357  virtual void compute_conj_dot_impl(const LinOp* b, LinOp* result) const;
1358 
1365  virtual void compute_norm2_impl(LinOp* result) const;
1366 
1373  virtual void compute_norm1_impl(LinOp* result) const;
1374 
1381  virtual void compute_squared_norm2_impl(LinOp* result) const;
1382 
1386  virtual void compute_mean_impl(LinOp* result) const;
1387 
1396  void resize(gko::dim<2> new_size);
1397 
1405  virtual std::unique_ptr<Dense> create_submatrix_impl(
1406  const span& rows, const span& columns, const size_type stride);
1407 
1408  void apply_impl(const LinOp* b, LinOp* x) const override;
1409 
1410  void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
1411  LinOp* x) const override;
1412 
1413  size_type linearize_index(size_type row, size_type col) const noexcept
1414  {
1415  return row * stride_ + col;
1416  }
1417 
1418  size_type linearize_index(size_type idx) const noexcept
1419  {
1420  return linearize_index(idx / this->get_size()[1],
1421  idx % this->get_size()[1]);
1422  }
1423 
1424  template <typename IndexType>
1425  void permute_impl(const Permutation<IndexType>* permutation,
1426  permute_mode mode, Dense* output) const;
1427 
1428  template <typename IndexType>
1429  void permute_impl(const Permutation<IndexType>* row_permutation,
1430  const Permutation<IndexType>* col_permutation,
1431  bool invert, Dense* output) const;
1432 
1433  template <typename IndexType>
1434  void scale_permute_impl(
1435  const ScaledPermutation<ValueType, IndexType>* permutation,
1436  permute_mode mode, Dense* output) const;
1437 
1438  template <typename IndexType>
1439  void scale_permute_impl(
1440  const ScaledPermutation<ValueType, IndexType>* row_permutation,
1441  const ScaledPermutation<ValueType, IndexType>* column_permutation,
1442  bool invert, Dense* output) const;
1443 
1444  template <typename OutputType, typename IndexType>
1445  void row_gather_impl(const array<IndexType>* row_idxs,
1446  Dense<OutputType>* row_collection) const;
1447 
1448  template <typename OutputType, typename IndexType>
1449  void row_gather_impl(const Dense<ValueType>* alpha,
1450  const array<IndexType>* row_idxs,
1451  const Dense<ValueType>* beta,
1452  Dense<OutputType>* row_collection) const;
1453 
1454 private:
1455  size_type stride_;
1456  array<value_type> values_;
1457 
1458  void add_scaled_identity_impl(const LinOp* a, const LinOp* b) override;
1459 };
1460 
1461 
1462 } // namespace matrix
1463 
1464 
1465 namespace detail {
1466 
1467 
1468 template <typename ValueType>
1469 struct temporary_clone_helper<matrix::Dense<ValueType>> {
1470  static std::unique_ptr<matrix::Dense<ValueType>> create(
1471  std::shared_ptr<const Executor> exec, matrix::Dense<ValueType>* ptr,
1472  bool copy_data)
1473  {
1474  if (copy_data) {
1475  return gko::clone(std::move(exec), ptr);
1476  } else {
1477  return matrix::Dense<ValueType>::create(exec, ptr->get_size());
1478  }
1479  }
1480 };
1481 
1482 
1483 } // namespace detail
1484 
1485 
1493 template <typename VecPtr>
1494 std::unique_ptr<matrix::Dense<typename detail::pointee<VecPtr>::value_type>>
1495 make_dense_view(VecPtr&& vector)
1496 {
1497  using value_type = typename detail::pointee<VecPtr>::value_type;
1499 }
1500 
1501 
1509 template <typename VecPtr>
1510 std::unique_ptr<
1511  const matrix::Dense<typename detail::pointee<VecPtr>::value_type>>
1512 make_const_dense_view(VecPtr&& vector)
1513 {
1514  using value_type = typename detail::pointee<VecPtr>::value_type;
1516 }
1517 
1518 
1539 template <typename Matrix, typename... TArgs>
1540 std::unique_ptr<Matrix> initialize(
1541  size_type stride, std::initializer_list<typename Matrix::value_type> vals,
1542  std::shared_ptr<const Executor> exec, TArgs&&... create_args)
1543 {
1545  size_type num_rows = vals.size();
1546  auto tmp = dense::create(exec->get_master(), dim<2>{num_rows, 1}, stride);
1547  size_type idx = 0;
1548  for (const auto& elem : vals) {
1549  tmp->at(idx) = elem;
1550  ++idx;
1551  }
1552  auto mtx = Matrix::create(exec, std::forward<TArgs>(create_args)...);
1553  tmp->move_to(mtx);
1554  return mtx;
1555 }
1556 
1577 template <typename Matrix, typename... TArgs>
1578 std::unique_ptr<Matrix> initialize(
1579  std::initializer_list<typename Matrix::value_type> vals,
1580  std::shared_ptr<const Executor> exec, TArgs&&... create_args)
1581 {
1582  return initialize<Matrix>(1, vals, std::move(exec),
1583  std::forward<TArgs>(create_args)...);
1584 }
1585 
1586 
1607 template <typename Matrix, typename... TArgs>
1608 std::unique_ptr<Matrix> initialize(
1609  size_type stride,
1610  std::initializer_list<std::initializer_list<typename Matrix::value_type>>
1611  vals,
1612  std::shared_ptr<const Executor> exec, TArgs&&... create_args)
1613 {
1615  size_type num_rows = vals.size();
1616  size_type num_cols = num_rows > 0 ? begin(vals)->size() : 1;
1617  auto tmp =
1618  dense::create(exec->get_master(), dim<2>{num_rows, num_cols}, stride);
1619  size_type ridx = 0;
1620  for (const auto& row : vals) {
1621  size_type cidx = 0;
1622  for (const auto& elem : row) {
1623  tmp->at(ridx, cidx) = elem;
1624  ++cidx;
1625  }
1626  ++ridx;
1627  }
1628  auto mtx = Matrix::create(exec, std::forward<TArgs>(create_args)...);
1629  tmp->move_to(mtx);
1630  return mtx;
1631 }
1632 
1633 
1655 template <typename Matrix, typename... TArgs>
1656 std::unique_ptr<Matrix> initialize(
1657  std::initializer_list<std::initializer_list<typename Matrix::value_type>>
1658  vals,
1659  std::shared_ptr<const Executor> exec, TArgs&&... create_args)
1660 {
1661  return initialize<Matrix>(vals.size() > 0 ? begin(vals)->size() : 0, vals,
1662  std::move(exec),
1663  std::forward<TArgs>(create_args)...);
1664 }
1665 
1666 
1667 } // namespace gko
1668 
1669 
1670 #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::EnablePolymorphicAssignment< ConcreteLinOp >::move_to
void move_to(result_type *result) override
Definition: polymorphic_object.hpp:732
gko::EnablePolymorphicAssignment< ConcreteLinOp >::convert_to
void convert_to(result_type *result) const override
Definition: polymorphic_object.hpp:730
gko::matrix::Csr
CSR is a matrix format which stores only the nonzero coefficients by compressing each row of the matr...
Definition: matrix.hpp:27
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:20
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:902
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:219
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:37
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:108
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: permutation.hpp:112
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:807
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:1098
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:182
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:1540
gko::matrix::Dense::at
value_type & at(size_type row, size_type col) noexcept
Returns a single element of the matrix.
Definition: dense.hpp:867
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:175
gko::matrix::Dense::get_stride
size_type get_stride() const noexcept
Returns the stride of the matrix.
Definition: dense.hpp:845
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:253
gko::range
A range is a multidimensional view of the memory.
Definition: range.hpp:298
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:852
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:826
gko::array
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition: array.hpp:27
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:203
gko::span
A span is a lightweight structure used to create sub-ranges from other ranges.
Definition: range.hpp:47
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:32
gko::next_precision
typename detail::next_precision_impl< T >::type next_precision
Obtains the next type in the singly-linked precision list.
Definition: math.hpp:462
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:875
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:674
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:237
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:835
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:788
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::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::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:265
gko::matrix::Dense::at
ValueType & at(size_type idx) noexcept
Returns a single element of the matrix.
Definition: dense.hpp:894
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:131
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::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:1111
gko::int32
std::int32_t int32
32-bit signed integral type.
Definition: types.hpp:125
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:1495
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:32
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:683
gko::matrix::permute_mode
permute_mode
Specifies how a permutation will be applied to a matrix.
Definition: permutation.hpp:43
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:1512
gko::PolymorphicObject::get_executor
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition: polymorphic_object.hpp:235
gko::array::get_size
size_type get_size() const noexcept
Returns the number of elements in the array.
Definition: array.hpp:657
gko::LinOp::get_size
const dim< 2 > & get_size() const noexcept
Returns the size of the operator.
Definition: lin_op.hpp:211
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:345
gko::matrix::Coo
COO stores a matrix in the coordinate matrix format.
Definition: coo.hpp:50