Ginkgo  Generated from pipelines/1478841010 branch based on develop. Ginkgo version 1.9.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 #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/permutation.hpp>
20 #include <ginkgo/core/matrix/scaled_permutation.hpp>
21 
22 
23 namespace gko {
24 namespace experimental {
25 namespace distributed {
26 
27 
28 template <typename ValueType>
29 class Vector;
30 
31 
32 }
33 } // namespace experimental
34 
35 
36 namespace matrix {
37 
38 
39 template <typename ValueType, typename IndexType>
40 class Coo;
41 
42 template <typename ValueType, typename IndexType>
43 class Csr;
44 
45 template <typename ValueType>
46 class Diagonal;
47 
48 template <typename ValueType, typename IndexType>
49 class Ell;
50 
51 template <typename ValueType, typename IndexType>
52 class Fbcsr;
53 
54 template <typename ValueType, typename IndexType>
55 class Hybrid;
56 
57 template <typename ValueType, typename IndexType>
58 class Sellp;
59 
60 template <typename ValueType, typename IndexType>
61 class SparsityCsr;
62 
63 
79 template <typename ValueType = default_precision>
80 class Dense
81  : public EnableLinOp<Dense<ValueType>>,
82  public ConvertibleTo<Dense<next_precision<ValueType>>>,
83  public ConvertibleTo<Coo<ValueType, int32>>,
84  public ConvertibleTo<Coo<ValueType, int64>>,
85  public ConvertibleTo<Csr<ValueType, int32>>,
86  public ConvertibleTo<Csr<ValueType, int64>>,
87  public ConvertibleTo<Ell<ValueType, int32>>,
88  public ConvertibleTo<Ell<ValueType, int64>>,
89  public ConvertibleTo<Fbcsr<ValueType, int32>>,
90  public ConvertibleTo<Fbcsr<ValueType, int64>>,
91  public ConvertibleTo<Hybrid<ValueType, int32>>,
92  public ConvertibleTo<Hybrid<ValueType, int64>>,
93  public ConvertibleTo<Sellp<ValueType, int32>>,
94  public ConvertibleTo<Sellp<ValueType, int64>>,
95  public ConvertibleTo<SparsityCsr<ValueType, int32>>,
96  public ConvertibleTo<SparsityCsr<ValueType, int64>>,
97  public DiagonalExtractable<ValueType>,
98  public ReadableFromMatrixData<ValueType, int32>,
99  public ReadableFromMatrixData<ValueType, int64>,
100  public WritableToMatrixData<ValueType, int32>,
101  public WritableToMatrixData<ValueType, int64>,
102  public Transposable,
103  public Permutable<int32>,
104  public Permutable<int64>,
105  public EnableAbsoluteComputation<remove_complex<Dense<ValueType>>>,
106  public ScaledIdentityAddable {
107  friend class EnablePolymorphicObject<Dense, LinOp>;
108  friend class Coo<ValueType, int32>;
109  friend class Coo<ValueType, int64>;
110  friend class Csr<ValueType, int32>;
111  friend class Csr<ValueType, int64>;
112  friend class Diagonal<ValueType>;
113  friend class Ell<ValueType, int32>;
114  friend class Ell<ValueType, int64>;
115  friend class Fbcsr<ValueType, int32>;
116  friend class Fbcsr<ValueType, int64>;
117  friend class Hybrid<ValueType, int32>;
118  friend class Hybrid<ValueType, int64>;
119  friend class Sellp<ValueType, int32>;
120  friend class Sellp<ValueType, int64>;
121  friend class SparsityCsr<ValueType, int32>;
122  friend class SparsityCsr<ValueType, int64>;
123  friend class Dense<to_complex<ValueType>>;
124  friend class experimental::distributed::Vector<ValueType>;
125 
126 public:
129  using ConvertibleTo<Dense<next_precision<ValueType>>>::convert_to;
130  using ConvertibleTo<Dense<next_precision<ValueType>>>::move_to;
131  using ConvertibleTo<Coo<ValueType, int32>>::convert_to;
132  using ConvertibleTo<Coo<ValueType, int32>>::move_to;
133  using ConvertibleTo<Coo<ValueType, int64>>::convert_to;
134  using ConvertibleTo<Coo<ValueType, int64>>::move_to;
135  using ConvertibleTo<Csr<ValueType, int32>>::convert_to;
136  using ConvertibleTo<Csr<ValueType, int32>>::move_to;
137  using ConvertibleTo<Csr<ValueType, int64>>::convert_to;
138  using ConvertibleTo<Csr<ValueType, int64>>::move_to;
139  using ConvertibleTo<Ell<ValueType, int32>>::convert_to;
140  using ConvertibleTo<Ell<ValueType, int32>>::move_to;
141  using ConvertibleTo<Ell<ValueType, int64>>::convert_to;
142  using ConvertibleTo<Ell<ValueType, int64>>::move_to;
143  using ConvertibleTo<Fbcsr<ValueType, int32>>::convert_to;
144  using ConvertibleTo<Fbcsr<ValueType, int32>>::move_to;
145  using ConvertibleTo<Fbcsr<ValueType, int64>>::convert_to;
146  using ConvertibleTo<Fbcsr<ValueType, int64>>::move_to;
147  using ConvertibleTo<Hybrid<ValueType, int32>>::convert_to;
148  using ConvertibleTo<Hybrid<ValueType, int32>>::move_to;
149  using ConvertibleTo<Hybrid<ValueType, int64>>::convert_to;
150  using ConvertibleTo<Hybrid<ValueType, int64>>::move_to;
151  using ConvertibleTo<Sellp<ValueType, int32>>::convert_to;
152  using ConvertibleTo<Sellp<ValueType, int32>>::move_to;
153  using ConvertibleTo<Sellp<ValueType, int64>>::convert_to;
154  using ConvertibleTo<Sellp<ValueType, int64>>::move_to;
155  using ConvertibleTo<SparsityCsr<ValueType, int32>>::convert_to;
156  using ConvertibleTo<SparsityCsr<ValueType, int32>>::move_to;
157  using ConvertibleTo<SparsityCsr<ValueType, int64>>::convert_to;
158  using ConvertibleTo<SparsityCsr<ValueType, int64>>::move_to;
161 
162  using value_type = ValueType;
163  using index_type = int64;
164  using transposed_type = Dense<ValueType>;
165  using mat_data = matrix_data<ValueType, int64>;
166  using mat_data32 = matrix_data<ValueType, int32>;
167  using device_mat_data = device_matrix_data<ValueType, int64>;
168  using device_mat_data32 = device_matrix_data<ValueType, int32>;
169  using absolute_type = remove_complex<Dense>;
170  using real_type = absolute_type;
171  using complex_type = to_complex<Dense>;
172 
173  using row_major_range = gko::range<gko::accessor::row_major<ValueType, 2>>;
174 
181  static std::unique_ptr<Dense> create_with_config_of(
183  {
184  // De-referencing `other` before calling the functions (instead of
185  // using operator `->`) is currently required to be compatible with
186  // CUDA 10.1.
187  // Otherwise, it results in a compile error.
188  return (*other).create_with_same_config();
189  }
190 
202  static std::unique_ptr<Dense> create_with_type_of(
203  ptr_param<const Dense> other, std::shared_ptr<const Executor> exec,
204  const dim<2>& size = dim<2>{})
205  {
206  // See create_with_config_of()
207  return (*other).create_with_type_of_impl(exec, size, size[1]);
208  }
209 
218  static std::unique_ptr<Dense> create_with_type_of(
219  ptr_param<const Dense> other, std::shared_ptr<const Executor> exec,
220  const dim<2>& size, size_type stride)
221  {
222  // See create_with_config_of()
223  return (*other).create_with_type_of_impl(exec, size, stride);
224  }
225 
236  static std::unique_ptr<Dense> create_with_type_of(
237  ptr_param<const Dense> other, std::shared_ptr<const Executor> exec,
238  const dim<2>& size, const dim<2>& local_size, size_type stride)
239  {
240  // See create_with_config_of()
241  return (*other).create_with_type_of_impl(exec, size, stride);
242  }
243 
252  static std::unique_ptr<Dense> create_view_of(ptr_param<Dense> other)
253  {
254  return other->create_view_of_impl();
255  }
256 
264  static std::unique_ptr<const Dense> create_const_view_of(
266  {
267  return other->create_const_view_of_impl();
268  }
269 
270  friend class Dense<next_precision<ValueType>>;
271 
272  void convert_to(Dense<next_precision<ValueType>>* result) const override;
273 
274  void move_to(Dense<next_precision<ValueType>>* result) override;
275 
276  void convert_to(Coo<ValueType, int32>* result) const override;
277 
278  void move_to(Coo<ValueType, int32>* result) override;
279 
280  void convert_to(Coo<ValueType, int64>* result) const override;
281 
282  void move_to(Coo<ValueType, int64>* result) override;
283 
284  void convert_to(Csr<ValueType, int32>* result) const override;
285 
286  void move_to(Csr<ValueType, int32>* result) override;
287 
288  void convert_to(Csr<ValueType, int64>* result) const override;
289 
290  void move_to(Csr<ValueType, int64>* result) override;
291 
292  void convert_to(Ell<ValueType, int32>* result) const override;
293 
294  void move_to(Ell<ValueType, int32>* result) override;
295 
296  void convert_to(Ell<ValueType, int64>* result) const override;
297 
298  void move_to(Ell<ValueType, int64>* result) override;
299 
300  void convert_to(Fbcsr<ValueType, int32>* result) const override;
301 
302  void move_to(Fbcsr<ValueType, int32>* result) override;
303 
304  void convert_to(Fbcsr<ValueType, int64>* result) const override;
305 
306  void move_to(Fbcsr<ValueType, int64>* result) override;
307 
308  void convert_to(Hybrid<ValueType, int32>* result) const override;
309 
310  void move_to(Hybrid<ValueType, int32>* result) override;
311 
312  void convert_to(Hybrid<ValueType, int64>* result) const override;
313 
314  void move_to(Hybrid<ValueType, int64>* result) override;
315 
316  void convert_to(Sellp<ValueType, int32>* result) const override;
317 
318  void move_to(Sellp<ValueType, int32>* result) override;
319 
320  void convert_to(Sellp<ValueType, int64>* result) const override;
321 
322  void move_to(Sellp<ValueType, int64>* result) override;
323 
324  void convert_to(SparsityCsr<ValueType, int32>* result) const override;
325 
326  void move_to(SparsityCsr<ValueType, int32>* result) override;
327 
328  void convert_to(SparsityCsr<ValueType, int64>* result) const override;
329 
330  void move_to(SparsityCsr<ValueType, int64>* result) override;
331 
332  void read(const mat_data& data) override;
333 
334  void read(const mat_data32& data) override;
335 
336  void read(const device_mat_data& data) override;
337 
338  void read(const device_mat_data32& data) override;
339 
340  void read(device_mat_data&& data) override;
341 
342  void read(device_mat_data32&& data) override;
343 
344  void write(mat_data& data) const override;
345 
346  void write(mat_data32& data) const override;
347 
348  std::unique_ptr<LinOp> transpose() const override;
349 
350  std::unique_ptr<LinOp> conj_transpose() const override;
351 
358  void transpose(ptr_param<Dense> output) const;
359 
366  void conj_transpose(ptr_param<Dense> output) const;
367 
373  void fill(const ValueType value);
374 
389  std::unique_ptr<Dense> permute(
390  ptr_param<const Permutation<int32>> permutation,
392 
396  std::unique_ptr<Dense> permute(
397  ptr_param<const Permutation<int64>> permutation,
399 
405  void permute(ptr_param<const Permutation<int32>> permutation,
406  ptr_param<Dense> output, permute_mode mode) const;
407 
412  void permute(ptr_param<const Permutation<int64>> permutation,
413  ptr_param<Dense> output, permute_mode mode) const;
414 
428  std::unique_ptr<Dense> permute(
429  ptr_param<const Permutation<int32>> row_permutation,
430  ptr_param<const Permutation<int32>> column_permutation,
431  bool invert = false) const;
432 
437  std::unique_ptr<Dense> permute(
438  ptr_param<const Permutation<int64>> row_permutation,
439  ptr_param<const Permutation<int64>> column_permutation,
440  bool invert = false) const;
441 
448  void permute(ptr_param<const Permutation<int32>> row_permutation,
449  ptr_param<const Permutation<int32>> column_permutation,
450  ptr_param<Dense> output, bool invert = false) const;
451 
456  void permute(ptr_param<const Permutation<int64>> row_permutation,
457  ptr_param<const Permutation<int64>> column_permutation,
458  ptr_param<Dense> output, bool invert = false) const;
459 
469  std::unique_ptr<Dense> scale_permute(
472 
477  std::unique_ptr<Dense> scale_permute(
480 
487  void scale_permute(
489  ptr_param<Dense> output, permute_mode mode) const;
490 
495  void scale_permute(
497  ptr_param<Dense> output, permute_mode mode) const;
498 
511  std::unique_ptr<Dense> scale_permute(
512  ptr_param<const ScaledPermutation<value_type, int32>> row_permutation,
514  column_permutation,
515  bool invert = false) const;
516 
521  std::unique_ptr<Dense> scale_permute(
522  ptr_param<const ScaledPermutation<value_type, int64>> row_permutation,
524  column_permutation,
525  bool invert = false) const;
526 
533  void scale_permute(
534  ptr_param<const ScaledPermutation<value_type, int32>> row_permutation,
536  column_permutation,
537  ptr_param<Dense> output, bool invert = false) const;
538 
544  void scale_permute(
545  ptr_param<const ScaledPermutation<value_type, int64>> row_permutation,
547  column_permutation,
548  ptr_param<Dense> output, bool invert = false) const;
549 
550  std::unique_ptr<LinOp> permute(
551  const array<int32>* permutation_indices) const override;
552 
553  std::unique_ptr<LinOp> permute(
554  const array<int64>* permutation_indices) const override;
555 
565  void permute(const array<int32>* permutation_indices,
566  ptr_param<Dense> output) const;
567 
571  void permute(const array<int64>* permutation_indices,
572  ptr_param<Dense> output) const;
573 
574  std::unique_ptr<LinOp> inverse_permute(
575  const array<int32>* permutation_indices) const override;
576 
577  std::unique_ptr<LinOp> inverse_permute(
578  const array<int64>* permutation_indices) const override;
579 
590  void inverse_permute(const array<int32>* permutation_indices,
591  ptr_param<Dense> output) const;
592 
596  void inverse_permute(const array<int64>* permutation_indices,
597  ptr_param<Dense> output) const;
598 
599  std::unique_ptr<LinOp> row_permute(
600  const array<int32>* permutation_indices) const override;
601 
602  std::unique_ptr<LinOp> row_permute(
603  const array<int64>* permutation_indices) const override;
604 
614  void row_permute(const array<int32>* permutation_indices,
615  ptr_param<Dense> output) const;
616 
620  void row_permute(const array<int64>* permutation_indices,
621  ptr_param<Dense> output) const;
622 
633  std::unique_ptr<Dense> row_gather(const array<int32>* gather_indices) const;
634 
638  std::unique_ptr<Dense> row_gather(const array<int64>* gather_indices) const;
639 
652  void row_gather(const array<int32>* gather_indices,
653  ptr_param<LinOp> row_collection) const;
654 
658  void row_gather(const array<int64>* gather_indices,
659  ptr_param<LinOp> row_collection) const;
660 
675  const array<int32>* gather_indices,
677  ptr_param<LinOp> row_collection) const;
678 
684  const array<int64>* gather_indices,
686  ptr_param<LinOp> row_collection) const;
687 
688  std::unique_ptr<LinOp> column_permute(
689  const array<int32>* permutation_indices) const override;
690 
691  std::unique_ptr<LinOp> column_permute(
692  const array<int64>* permutation_indices) const override;
693 
703  void column_permute(const array<int32>* permutation_indices,
704  ptr_param<Dense> output) const;
705 
709  void column_permute(const array<int64>* permutation_indices,
710  ptr_param<Dense> output) const;
711 
712  std::unique_ptr<LinOp> inverse_row_permute(
713  const array<int32>* permutation_indices) const override;
714 
715  std::unique_ptr<LinOp> inverse_row_permute(
716  const array<int64>* permutation_indices) const override;
717 
727  void inverse_row_permute(const array<int32>* permutation_indices,
728  ptr_param<Dense> output) const;
729 
733  void inverse_row_permute(const array<int64>* permutation_indices,
734  ptr_param<Dense> output) const;
735 
736  std::unique_ptr<LinOp> inverse_column_permute(
737  const array<int32>* permutation_indices) const override;
738 
739  std::unique_ptr<LinOp> inverse_column_permute(
740  const array<int64>* permutation_indices) const override;
741 
751  void inverse_column_permute(const array<int32>* permutation_indices,
752  ptr_param<Dense> output) const;
753 
757  void inverse_column_permute(const array<int64>* permutation_indices,
758  ptr_param<Dense> output) const;
759 
760  std::unique_ptr<Diagonal<ValueType>> extract_diagonal() const override;
761 
769  void extract_diagonal(ptr_param<Diagonal<ValueType>> output) const;
770 
771  std::unique_ptr<absolute_type> compute_absolute() const override;
772 
780  void compute_absolute(ptr_param<absolute_type> output) const;
781 
782  void compute_absolute_inplace() override;
783 
788  std::unique_ptr<complex_type> make_complex() const;
789 
795  void make_complex(ptr_param<complex_type> result) const;
796 
801  std::unique_ptr<real_type> get_real() const;
802 
806  void get_real(ptr_param<real_type> result) const;
807 
812  std::unique_ptr<real_type> get_imag() const;
813 
818  void get_imag(ptr_param<real_type> result) const;
819 
825  value_type* get_values() noexcept { return values_.get_data(); }
826 
834  const value_type* get_const_values() const noexcept
835  {
836  return values_.get_const_data();
837  }
838 
844  size_type get_stride() const noexcept { return stride_; }
845 
852  {
853  return values_.get_size();
854  }
855 
866  value_type& at(size_type row, size_type col) noexcept
867  {
868  return values_.get_data()[linearize_index(row, col)];
869  }
870 
874  value_type at(size_type row, size_type col) const noexcept
875  {
876  return values_.get_const_data()[linearize_index(row, col)];
877  }
878 
893  ValueType& at(size_type idx) noexcept
894  {
895  return values_.get_data()[linearize_index(idx)];
896  }
897 
901  ValueType at(size_type idx) const noexcept
902  {
903  return values_.get_const_data()[linearize_index(idx)];
904  }
905 
915  void scale(ptr_param<const LinOp> alpha);
916 
926  void inv_scale(ptr_param<const LinOp> alpha);
927 
939 
951 
961 
974  array<char>& tmp) const;
975 
985  ptr_param<LinOp> result) const;
986 
999  array<char>& tmp) const;
1000 
1008  void compute_norm2(ptr_param<LinOp> result) const;
1009 
1020  void compute_norm2(ptr_param<LinOp> result, array<char>& tmp) const;
1021 
1029  void compute_norm1(ptr_param<LinOp> result) const;
1030 
1041  void compute_norm1(ptr_param<LinOp> result, array<char>& tmp) const;
1042 
1051  void compute_squared_norm2(ptr_param<LinOp> result) const;
1052 
1064  void compute_squared_norm2(ptr_param<LinOp> result, array<char>& tmp) const;
1065 
1073  void compute_mean(ptr_param<LinOp> result) const;
1074 
1085  void compute_mean(ptr_param<LinOp> result, array<char>& tmp) const;
1086 
1097  std::unique_ptr<Dense> create_submatrix(const span& rows,
1098  const span& columns,
1099  const size_type stride)
1100  {
1101  return this->create_submatrix_impl(rows, columns, stride);
1102  }
1103 
1110  std::unique_ptr<Dense> create_submatrix(const span& rows,
1111  const span& columns)
1112  {
1113  return create_submatrix(rows, columns, this->get_stride());
1114  }
1115 
1123  std::unique_ptr<real_type> create_real_view();
1124 
1128  std::unique_ptr<const real_type> create_real_view() const;
1129 
1142  static std::unique_ptr<Dense> create(std::shared_ptr<const Executor> exec,
1143  const dim<2>& size = {},
1144  size_type stride = 0);
1145 
1162  static std::unique_ptr<Dense> create(std::shared_ptr<const Executor> exec,
1163  const dim<2>& size,
1164  array<value_type> values,
1165  size_type stride);
1166 
1171  template <typename InputValueType>
1172  GKO_DEPRECATED(
1173  "explicitly construct the gko::array argument instead of passing an"
1174  "initializer list")
1175  static std::unique_ptr<Dense> create(
1176  std::shared_ptr<const Executor> exec, const dim<2>& size,
1177  std::initializer_list<InputValueType> values, size_type stride)
1178  {
1179  return create(exec, size, array<value_type>{exec, std::move(values)},
1180  stride);
1181  }
1182 
1194  static std::unique_ptr<const Dense> create_const(
1195  std::shared_ptr<const Executor> exec, const dim<2>& size,
1196  gko::detail::const_array_view<ValueType>&& values, size_type stride);
1197 
1203  Dense& operator=(const Dense&);
1204 
1210  Dense& operator=(Dense&&);
1211 
1216  Dense(const Dense&);
1217 
1222  Dense(Dense&&);
1223 
1224 protected:
1225  Dense(std::shared_ptr<const Executor> exec, const dim<2>& size = {},
1226  size_type stride = 0);
1227 
1228  Dense(std::shared_ptr<const Executor> exec, const dim<2>& size,
1229  array<value_type> values, size_type stride);
1230 
1237  virtual std::unique_ptr<Dense> create_with_same_config() const
1238  {
1239  return Dense::create(this->get_executor(), this->get_size(),
1240  this->get_stride());
1241  }
1242 
1250  virtual std::unique_ptr<Dense> create_with_type_of_impl(
1251  std::shared_ptr<const Executor> exec, const dim<2>& size,
1252  size_type stride) const
1253  {
1254  return Dense::create(exec, size, stride);
1255  }
1256 
1263  virtual std::unique_ptr<Dense> create_view_of_impl()
1264  {
1265  auto exec = this->get_executor();
1266  return Dense::create(
1267  exec, this->get_size(),
1269  this->get_values()),
1270  this->get_stride());
1271  }
1272 
1279  virtual std::unique_ptr<const Dense> create_const_view_of_impl() const
1280  {
1281  auto exec = this->get_executor();
1282  return Dense::create_const(
1283  exec, this->get_size(),
1285  this->get_const_values()),
1286  this->get_stride());
1287  }
1288 
1289  template <typename IndexType>
1290  void convert_impl(Coo<ValueType, IndexType>* result) const;
1291 
1292  template <typename IndexType>
1293  void convert_impl(Csr<ValueType, IndexType>* result) const;
1294 
1295  template <typename IndexType>
1296  void convert_impl(Ell<ValueType, IndexType>* result) const;
1297 
1298  template <typename IndexType>
1299  void convert_impl(Fbcsr<ValueType, IndexType>* result) const;
1300 
1301  template <typename IndexType>
1302  void convert_impl(Hybrid<ValueType, IndexType>* result) const;
1303 
1304  template <typename IndexType>
1305  void convert_impl(Sellp<ValueType, IndexType>* result) const;
1306 
1307  template <typename IndexType>
1308  void convert_impl(SparsityCsr<ValueType, IndexType>* result) const;
1309 
1316  virtual void scale_impl(const LinOp* alpha);
1317 
1324  virtual void inv_scale_impl(const LinOp* alpha);
1325 
1332  virtual void add_scaled_impl(const LinOp* alpha, const LinOp* b);
1333 
1340  virtual void sub_scaled_impl(const LinOp* alpha, const LinOp* b);
1341 
1348  virtual void compute_dot_impl(const LinOp* b, LinOp* result) const;
1349 
1356  virtual void compute_conj_dot_impl(const LinOp* b, LinOp* result) const;
1357 
1364  virtual void compute_norm2_impl(LinOp* result) const;
1365 
1372  virtual void compute_norm1_impl(LinOp* result) const;
1373 
1380  virtual void compute_squared_norm2_impl(LinOp* result) const;
1381 
1385  virtual void compute_mean_impl(LinOp* result) const;
1386 
1395  void resize(gko::dim<2> new_size);
1396 
1404  virtual std::unique_ptr<Dense> create_submatrix_impl(
1405  const span& rows, const span& columns, const size_type stride);
1406 
1407  void apply_impl(const LinOp* b, LinOp* x) const override;
1408 
1409  void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
1410  LinOp* x) const override;
1411 
1412  size_type linearize_index(size_type row, size_type col) const noexcept
1413  {
1414  return row * stride_ + col;
1415  }
1416 
1417  size_type linearize_index(size_type idx) const noexcept
1418  {
1419  return linearize_index(idx / this->get_size()[1],
1420  idx % this->get_size()[1]);
1421  }
1422 
1423  template <typename IndexType>
1424  void permute_impl(const Permutation<IndexType>* permutation,
1425  permute_mode mode, Dense* output) const;
1426 
1427  template <typename IndexType>
1428  void permute_impl(const Permutation<IndexType>* row_permutation,
1429  const Permutation<IndexType>* col_permutation,
1430  bool invert, Dense* output) const;
1431 
1432  template <typename IndexType>
1433  void scale_permute_impl(
1434  const ScaledPermutation<ValueType, IndexType>* permutation,
1435  permute_mode mode, Dense* output) const;
1436 
1437  template <typename IndexType>
1438  void scale_permute_impl(
1439  const ScaledPermutation<ValueType, IndexType>* row_permutation,
1440  const ScaledPermutation<ValueType, IndexType>* column_permutation,
1441  bool invert, Dense* output) const;
1442 
1443  template <typename OutputType, typename IndexType>
1444  void row_gather_impl(const array<IndexType>* row_idxs,
1445  Dense<OutputType>* row_collection) const;
1446 
1447  template <typename OutputType, typename IndexType>
1448  void row_gather_impl(const Dense<ValueType>* alpha,
1449  const array<IndexType>* row_idxs,
1450  const Dense<ValueType>* beta,
1451  Dense<OutputType>* row_collection) const;
1452 
1453 private:
1454  size_type stride_;
1455  array<value_type> values_;
1456 
1457  void add_scaled_identity_impl(const LinOp* a, const LinOp* b) override;
1458 };
1459 
1460 
1461 } // namespace matrix
1462 
1463 
1464 namespace detail {
1465 
1466 
1467 template <typename ValueType>
1468 struct temporary_clone_helper<matrix::Dense<ValueType>> {
1469  static std::unique_ptr<matrix::Dense<ValueType>> create(
1470  std::shared_ptr<const Executor> exec, matrix::Dense<ValueType>* ptr,
1471  bool copy_data)
1472  {
1473  if (copy_data) {
1474  return gko::clone(std::move(exec), ptr);
1475  } else {
1476  return matrix::Dense<ValueType>::create(exec, ptr->get_size());
1477  }
1478  }
1479 };
1480 
1481 
1482 } // namespace detail
1483 
1484 
1492 template <typename VecPtr>
1493 std::unique_ptr<matrix::Dense<typename detail::pointee<VecPtr>::value_type>>
1494 make_dense_view(VecPtr&& vector)
1495 {
1496  using value_type = typename detail::pointee<VecPtr>::value_type;
1498 }
1499 
1500 
1508 template <typename VecPtr>
1509 std::unique_ptr<
1510  const matrix::Dense<typename detail::pointee<VecPtr>::value_type>>
1511 make_const_dense_view(VecPtr&& vector)
1512 {
1513  using value_type = typename detail::pointee<VecPtr>::value_type;
1515 }
1516 
1517 
1538 template <typename Matrix, typename... TArgs>
1539 std::unique_ptr<Matrix> initialize(
1540  size_type stride, std::initializer_list<typename Matrix::value_type> vals,
1541  std::shared_ptr<const Executor> exec, TArgs&&... create_args)
1542 {
1544  size_type num_rows = vals.size();
1545  auto tmp = dense::create(exec->get_master(), dim<2>{num_rows, 1}, stride);
1546  size_type idx = 0;
1547  for (const auto& elem : vals) {
1548  tmp->at(idx) = elem;
1549  ++idx;
1550  }
1551  auto mtx = Matrix::create(exec, std::forward<TArgs>(create_args)...);
1552  tmp->move_to(mtx);
1553  return mtx;
1554 }
1555 
1576 template <typename Matrix, typename... TArgs>
1577 std::unique_ptr<Matrix> initialize(
1578  std::initializer_list<typename Matrix::value_type> vals,
1579  std::shared_ptr<const Executor> exec, TArgs&&... create_args)
1580 {
1581  return initialize<Matrix>(1, vals, std::move(exec),
1582  std::forward<TArgs>(create_args)...);
1583 }
1584 
1585 
1606 template <typename Matrix, typename... TArgs>
1607 std::unique_ptr<Matrix> initialize(
1608  size_type stride,
1609  std::initializer_list<std::initializer_list<typename Matrix::value_type>>
1610  vals,
1611  std::shared_ptr<const Executor> exec, TArgs&&... create_args)
1612 {
1614  size_type num_rows = vals.size();
1615  size_type num_cols = num_rows > 0 ? begin(vals)->size() : 1;
1616  auto tmp =
1617  dense::create(exec->get_master(), dim<2>{num_rows, num_cols}, stride);
1618  size_type ridx = 0;
1619  for (const auto& row : vals) {
1620  size_type cidx = 0;
1621  for (const auto& elem : row) {
1622  tmp->at(ridx, cidx) = elem;
1623  ++cidx;
1624  }
1625  ++ridx;
1626  }
1627  auto mtx = Matrix::create(exec, std::forward<TArgs>(create_args)...);
1628  tmp->move_to(mtx);
1629  return mtx;
1630 }
1631 
1632 
1654 template <typename Matrix, typename... TArgs>
1655 std::unique_ptr<Matrix> initialize(
1656  std::initializer_list<std::initializer_list<typename Matrix::value_type>>
1657  vals,
1658  std::shared_ptr<const Executor> exec, TArgs&&... create_args)
1659 {
1660  return initialize<Matrix>(vals.size() > 0 ? begin(vals)->size() : 0, vals,
1661  std::move(exec),
1662  std::forward<TArgs>(create_args)...);
1663 }
1664 
1665 
1666 } // namespace gko
1667 
1668 
1669 #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:731
gko::EnablePolymorphicAssignment< ConcreteLinOp >::convert_to
void convert_to(result_type *result) const override
Definition: polymorphic_object.hpp:729
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:19
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:901
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:218
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:86
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:111
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:806
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:1097
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:181
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:1539
gko::matrix::Dense::at
value_type & at(size_type row, size_type col) noexcept
Returns a single element of the matrix.
Definition: dense.hpp:866
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:173
gko::matrix::Dense::get_stride
size_type get_stride() const noexcept
Returns the stride of the matrix.
Definition: dense.hpp:844
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:252
gko::range
A range is a multidimensional view of the memory.
Definition: range.hpp:297
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:851
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:825
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:202
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::next_precision
typename detail::next_precision_impl< T >::type next_precision
Obtains the next type in the singly-linked precision list.
Definition: math.hpp:461
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:41
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:874
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:673
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:236
gko::stop::mode
mode
The mode for the residual norm criterion.
Definition: residual_norm.hpp:36
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:834
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:787
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:264
gko::matrix::Dense::at
ValueType & at(size_type idx) noexcept
Returns a single element of the matrix.
Definition: dense.hpp:893
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:109
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:1110
gko::int32
std::int32_t int32
32-bit signed integral type.
Definition: types.hpp:103
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:1494
gko::Executor
The first step in using the Ginkgo library consists of creating an executor.
Definition: executor.hpp:615
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:682
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:1511
gko::PolymorphicObject::get_executor
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition: polymorphic_object.hpp:234
gko::array::get_size
size_type get_size() const noexcept
Returns the number of elements in the array.
Definition: array.hpp:656
gko::LinOp::get_size
const dim< 2 > & get_size() const noexcept
Returns the size of the operator.
Definition: lin_op.hpp:210
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:344
gko::matrix::Coo
COO stores a matrix in the coordinate matrix format.
Definition: coo.hpp:50