Ginkgo Generated from branch based on master. Ginkgo version 1.7.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
dense.hpp
1/*******************************<GINKGO LICENSE>******************************
2Copyright (c) 2017-2023, the Ginkgo authors
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
8
91. Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
11
122. Redistributions in binary form must reproduce the above copyright
13notice, this list of conditions and the following disclaimer in the
14documentation and/or other materials provided with the distribution.
15
163. Neither the name of the copyright holder nor the names of its
17contributors may be used to endorse or promote products derived from
18this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31******************************<GINKGO LICENSE>*******************************/
32
33#ifndef GKO_PUBLIC_CORE_MATRIX_DENSE_HPP_
34#define GKO_PUBLIC_CORE_MATRIX_DENSE_HPP_
35
36
37#include <initializer_list>
38#include <type_traits>
39
40
41#include <ginkgo/core/base/array.hpp>
42#include <ginkgo/core/base/exception_helpers.hpp>
43#include <ginkgo/core/base/executor.hpp>
44#include <ginkgo/core/base/lin_op.hpp>
45#include <ginkgo/core/base/range_accessors.hpp>
46#include <ginkgo/core/base/types.hpp>
47#include <ginkgo/core/base/utils.hpp>
48#include <ginkgo/core/matrix/permutation.hpp>
49#include <ginkgo/core/matrix/scaled_permutation.hpp>
50
51
52namespace gko {
53namespace experimental {
54namespace distributed {
55
56
57template <typename ValueType>
58class Vector;
59
60
61}
62} // namespace experimental
63
64
65namespace matrix {
66
67
68template <typename ValueType, typename IndexType>
69class Coo;
70
71template <typename ValueType, typename IndexType>
72class Csr;
73
74template <typename ValueType>
75class Diagonal;
76
77template <typename ValueType, typename IndexType>
78class Ell;
79
80template <typename ValueType, typename IndexType>
81class Fbcsr;
82
83template <typename ValueType, typename IndexType>
84class Hybrid;
85
86template <typename ValueType, typename IndexType>
87class Sellp;
88
89template <typename ValueType, typename IndexType>
90class SparsityCsr;
91
92
108template <typename ValueType = default_precision>
109class Dense
110 : public EnableLinOp<Dense<ValueType>>,
111 public EnableCreateMethod<Dense<ValueType>>,
112 public ConvertibleTo<Dense<next_precision<ValueType>>>,
113 public ConvertibleTo<Coo<ValueType, int32>>,
114 public ConvertibleTo<Coo<ValueType, int64>>,
115 public ConvertibleTo<Csr<ValueType, int32>>,
116 public ConvertibleTo<Csr<ValueType, int64>>,
117 public ConvertibleTo<Ell<ValueType, int32>>,
118 public ConvertibleTo<Ell<ValueType, int64>>,
119 public ConvertibleTo<Fbcsr<ValueType, int32>>,
120 public ConvertibleTo<Fbcsr<ValueType, int64>>,
121 public ConvertibleTo<Hybrid<ValueType, int32>>,
122 public ConvertibleTo<Hybrid<ValueType, int64>>,
123 public ConvertibleTo<Sellp<ValueType, int32>>,
124 public ConvertibleTo<Sellp<ValueType, int64>>,
125 public ConvertibleTo<SparsityCsr<ValueType, int32>>,
126 public ConvertibleTo<SparsityCsr<ValueType, int64>>,
127 public DiagonalExtractable<ValueType>,
128 public ReadableFromMatrixData<ValueType, int32>,
129 public ReadableFromMatrixData<ValueType, int64>,
130 public WritableToMatrixData<ValueType, int32>,
131 public WritableToMatrixData<ValueType, int64>,
132 public Transposable,
133 public Permutable<int32>,
134 public Permutable<int64>,
135 public EnableAbsoluteComputation<remove_complex<Dense<ValueType>>>,
136 public ScaledIdentityAddable {
137 friend class EnableCreateMethod<Dense>;
138 friend class EnablePolymorphicObject<Dense, LinOp>;
139 friend class Coo<ValueType, int32>;
140 friend class Coo<ValueType, int64>;
141 friend class Csr<ValueType, int32>;
142 friend class Csr<ValueType, int64>;
143 friend class Diagonal<ValueType>;
144 friend class Ell<ValueType, int32>;
145 friend class Ell<ValueType, int64>;
146 friend class Fbcsr<ValueType, int32>;
147 friend class Fbcsr<ValueType, int64>;
148 friend class Hybrid<ValueType, int32>;
149 friend class Hybrid<ValueType, int64>;
150 friend class Sellp<ValueType, int32>;
151 friend class Sellp<ValueType, int64>;
152 friend class SparsityCsr<ValueType, int32>;
153 friend class SparsityCsr<ValueType, int64>;
154 friend class Dense<to_complex<ValueType>>;
155 friend class experimental::distributed::Vector<ValueType>;
156
157public:
158 using EnableLinOp<Dense>::convert_to;
159 using EnableLinOp<Dense>::move_to;
162 using ConvertibleTo<Coo<ValueType, int32>>::convert_to;
163 using ConvertibleTo<Coo<ValueType, int32>>::move_to;
164 using ConvertibleTo<Coo<ValueType, int64>>::convert_to;
165 using ConvertibleTo<Coo<ValueType, int64>>::move_to;
166 using ConvertibleTo<Csr<ValueType, int32>>::convert_to;
167 using ConvertibleTo<Csr<ValueType, int32>>::move_to;
168 using ConvertibleTo<Csr<ValueType, int64>>::convert_to;
169 using ConvertibleTo<Csr<ValueType, int64>>::move_to;
170 using ConvertibleTo<Ell<ValueType, int32>>::convert_to;
171 using ConvertibleTo<Ell<ValueType, int32>>::move_to;
172 using ConvertibleTo<Ell<ValueType, int64>>::convert_to;
173 using ConvertibleTo<Ell<ValueType, int64>>::move_to;
174 using ConvertibleTo<Fbcsr<ValueType, int32>>::convert_to;
176 using ConvertibleTo<Fbcsr<ValueType, int64>>::convert_to;
178 using ConvertibleTo<Hybrid<ValueType, int32>>::convert_to;
180 using ConvertibleTo<Hybrid<ValueType, int64>>::convert_to;
182 using ConvertibleTo<Sellp<ValueType, int32>>::convert_to;
184 using ConvertibleTo<Sellp<ValueType, int64>>::convert_to;
190 using ReadableFromMatrixData<ValueType, int32>::read;
191 using ReadableFromMatrixData<ValueType, int64>::read;
192
193 using value_type = ValueType;
194 using index_type = int64;
200 using absolute_type = remove_complex<Dense>;
201 using real_type = absolute_type;
202 using complex_type = to_complex<Dense>;
203
205
212 static std::unique_ptr<Dense> create_with_config_of(
214 {
215 // De-referencing `other` before calling the functions (instead of
216 // using operator `->`) is currently required to be compatible with
217 // CUDA 10.1.
218 // Otherwise, it results in a compile error.
219 return (*other).create_with_same_config();
220 }
221
233 static std::unique_ptr<Dense> create_with_type_of(
234 ptr_param<const Dense> other, std::shared_ptr<const Executor> exec,
235 const dim<2>& size = dim<2>{})
236 {
237 // See create_with_config_of()
238 return (*other).create_with_type_of_impl(exec, size, size[1]);
239 }
240
249 static std::unique_ptr<Dense> create_with_type_of(
250 ptr_param<const Dense> other, std::shared_ptr<const Executor> exec,
251 const dim<2>& size, size_type stride)
252 {
253 // See create_with_config_of()
254 return (*other).create_with_type_of_impl(exec, size, stride);
255 }
256
267 static std::unique_ptr<Dense> create_with_type_of(
268 ptr_param<const Dense> other, std::shared_ptr<const Executor> exec,
269 const dim<2>& size, const dim<2>& local_size, size_type stride)
270 {
271 // See create_with_config_of()
272 return (*other).create_with_type_of_impl(exec, size, stride);
273 }
274
283 static std::unique_ptr<Dense> create_view_of(ptr_param<Dense> other)
284 {
285 return other->create_view_of_impl();
286 }
287
295 static std::unique_ptr<const Dense> create_const_view_of(
297 {
298 return other->create_const_view_of_impl();
299 }
300
301 friend class Dense<next_precision<ValueType>>;
302
303 void convert_to(Dense<next_precision<ValueType>>* result) const override;
304
305 void move_to(Dense<next_precision<ValueType>>* result) override;
306
307 void convert_to(Coo<ValueType, int32>* result) const override;
308
309 void move_to(Coo<ValueType, int32>* result) override;
310
311 void convert_to(Coo<ValueType, int64>* result) const override;
312
313 void move_to(Coo<ValueType, int64>* result) override;
314
315 void convert_to(Csr<ValueType, int32>* result) const override;
316
317 void move_to(Csr<ValueType, int32>* result) override;
318
319 void convert_to(Csr<ValueType, int64>* result) const override;
320
321 void move_to(Csr<ValueType, int64>* result) override;
322
323 void convert_to(Ell<ValueType, int32>* result) const override;
324
325 void move_to(Ell<ValueType, int32>* result) override;
326
327 void convert_to(Ell<ValueType, int64>* result) const override;
328
329 void move_to(Ell<ValueType, int64>* result) override;
330
331 void convert_to(Fbcsr<ValueType, int32>* result) const override;
332
333 void move_to(Fbcsr<ValueType, int32>* result) override;
334
335 void convert_to(Fbcsr<ValueType, int64>* result) const override;
336
337 void move_to(Fbcsr<ValueType, int64>* result) override;
338
339 void convert_to(Hybrid<ValueType, int32>* result) const override;
340
341 void move_to(Hybrid<ValueType, int32>* result) override;
342
343 void convert_to(Hybrid<ValueType, int64>* result) const override;
344
345 void move_to(Hybrid<ValueType, int64>* result) override;
346
347 void convert_to(Sellp<ValueType, int32>* result) const override;
348
349 void move_to(Sellp<ValueType, int32>* result) override;
350
351 void convert_to(Sellp<ValueType, int64>* result) const override;
352
353 void move_to(Sellp<ValueType, int64>* result) override;
354
355 void convert_to(SparsityCsr<ValueType, int32>* result) const override;
356
357 void move_to(SparsityCsr<ValueType, int32>* result) override;
358
359 void convert_to(SparsityCsr<ValueType, int64>* result) const override;
360
361 void move_to(SparsityCsr<ValueType, int64>* result) override;
362
363 void read(const mat_data& data) override;
364
365 void read(const mat_data32& data) override;
366
367 void read(const device_mat_data& data) override;
368
369 void read(const device_mat_data32& data) override;
370
371 void read(device_mat_data&& data) override;
372
373 void read(device_mat_data32&& data) override;
374
375 void write(mat_data& data) const override;
376
377 void write(mat_data32& data) const override;
378
379 std::unique_ptr<LinOp> transpose() const override;
380
381 std::unique_ptr<LinOp> conj_transpose() const override;
382
389 void transpose(ptr_param<Dense> output) const;
390
398
404 void fill(const ValueType value);
405
420 std::unique_ptr<Dense> permute(
423
427 std::unique_ptr<Dense> permute(
430
437 ptr_param<Dense> output, permute_mode mode) const;
438
444 ptr_param<Dense> output, permute_mode mode) const;
445
459 std::unique_ptr<Dense> permute(
462 bool invert = false) const;
463
468 std::unique_ptr<Dense> permute(
471 bool invert = false) const;
472
481 ptr_param<Dense> output, bool invert = false) const;
482
489 ptr_param<Dense> output, bool invert = false) const;
490
500 std::unique_ptr<Dense> scale_permute(
503
508 std::unique_ptr<Dense> scale_permute(
511
520 ptr_param<Dense> output, permute_mode mode) const;
521
528 ptr_param<Dense> output, permute_mode mode) const;
529
542 std::unique_ptr<Dense> scale_permute(
546 bool invert = false) const;
547
552 std::unique_ptr<Dense> scale_permute(
556 bool invert = false) const;
557
568 ptr_param<Dense> output, bool invert = false) const;
569
579 ptr_param<Dense> output, bool invert = false) const;
580
581 std::unique_ptr<LinOp> permute(
582 const array<int32>* permutation_indices) const override;
583
584 std::unique_ptr<LinOp> permute(
585 const array<int64>* permutation_indices) const override;
586
597 ptr_param<Dense> output) const;
598
603 ptr_param<Dense> output) const;
604
605 std::unique_ptr<LinOp> inverse_permute(
606 const array<int32>* permutation_indices) const override;
607
608 std::unique_ptr<LinOp> inverse_permute(
609 const array<int64>* permutation_indices) const override;
610
622 ptr_param<Dense> output) const;
623
628 ptr_param<Dense> output) const;
629
630 std::unique_ptr<LinOp> row_permute(
631 const array<int32>* permutation_indices) const override;
632
633 std::unique_ptr<LinOp> row_permute(
634 const array<int64>* permutation_indices) const override;
635
646 ptr_param<Dense> output) const;
647
652 ptr_param<Dense> output) const;
653
664 std::unique_ptr<Dense> row_gather(const array<int32>* gather_indices) const;
665
669 std::unique_ptr<Dense> row_gather(const array<int64>* gather_indices) const;
670
685
691
709
718
719 std::unique_ptr<LinOp> column_permute(
720 const array<int32>* permutation_indices) const override;
721
722 std::unique_ptr<LinOp> column_permute(
723 const array<int64>* permutation_indices) const override;
724
735 ptr_param<Dense> output) const;
736
741 ptr_param<Dense> output) const;
742
743 std::unique_ptr<LinOp> inverse_row_permute(
744 const array<int32>* permutation_indices) const override;
745
746 std::unique_ptr<LinOp> inverse_row_permute(
747 const array<int64>* permutation_indices) const override;
748
759 ptr_param<Dense> output) const;
760
765 ptr_param<Dense> output) const;
766
767 std::unique_ptr<LinOp> inverse_column_permute(
768 const array<int32>* permutation_indices) const override;
769
770 std::unique_ptr<LinOp> inverse_column_permute(
771 const array<int64>* permutation_indices) const override;
772
783 ptr_param<Dense> output) const;
784
789 ptr_param<Dense> output) const;
790
791 std::unique_ptr<Diagonal<ValueType>> extract_diagonal() const override;
792
801
802 std::unique_ptr<absolute_type> compute_absolute() const override;
803
812
814
819 std::unique_ptr<complex_type> make_complex() const;
820
827
832 std::unique_ptr<real_type> get_real() const;
833
838
843 std::unique_ptr<real_type> get_imag() const;
844
850
856 value_type* get_values() noexcept { return values_.get_data(); }
857
865 const value_type* get_const_values() const noexcept
866 {
867 return values_.get_const_data();
868 }
869
875 size_type get_stride() const noexcept { return stride_; }
876
883 {
884 return values_.get_num_elems();
885 }
886
897 value_type& at(size_type row, size_type col) noexcept
898 {
899 return values_.get_data()[linearize_index(row, col)];
900 }
901
905 value_type at(size_type row, size_type col) const noexcept
906 {
907 return values_.get_const_data()[linearize_index(row, col)];
908 }
909
924 ValueType& at(size_type idx) noexcept
925 {
926 return values_.get_data()[linearize_index(idx)];
927 }
928
932 ValueType at(size_type idx) const noexcept
933 {
934 return values_.get_const_data()[linearize_index(idx)];
935 }
936
947
958
970
982
992
1005 array<char>& tmp) const;
1006
1016 ptr_param<LinOp> result) const;
1017
1030 array<char>& tmp) const;
1031
1040
1052
1061
1073
1083
1096
1105
1117
1128 std::unique_ptr<Dense> create_submatrix(const span& rows,
1129 const span& columns,
1130 const size_type stride)
1131 {
1132 return this->create_submatrix_impl(rows, columns, stride);
1133 }
1134
1141 std::unique_ptr<Dense> create_submatrix(const span& rows,
1142 const span& columns)
1143 {
1144 return create_submatrix(rows, columns, this->get_stride());
1145 }
1146
1154 std::unique_ptr<real_type> create_real_view();
1155
1159 std::unique_ptr<const real_type> create_real_view() const;
1160
1172 static std::unique_ptr<const Dense> create_const(
1173 std::shared_ptr<const Executor> exec, const dim<2>& size,
1174 gko::detail::const_array_view<ValueType>&& values, size_type stride)
1175 {
1176 // cast const-ness away, but return a const object afterwards,
1177 // so we can ensure that no modifications take place.
1178 return std::unique_ptr<const Dense>(new Dense{
1179 exec, size, gko::detail::array_const_cast(std::move(values)),
1180 stride});
1181 }
1182
1189
1196
1201 Dense(const Dense&);
1202
1208
1209protected:
1216 Dense(std::shared_ptr<const Executor> exec, const dim<2>& size = dim<2>{})
1217 : Dense(std::move(exec), size, size[1])
1218 {}
1219
1229 Dense(std::shared_ptr<const Executor> exec, const dim<2>& size,
1230 size_type stride)
1231 : EnableLinOp<Dense>(exec, size),
1232 values_(exec, size[0] * stride),
1233 stride_(stride)
1234 {}
1235
1252 template <typename ValuesArray>
1253 Dense(std::shared_ptr<const Executor> exec, const dim<2>& size,
1254 ValuesArray&& values, size_type stride)
1255 : EnableLinOp<Dense>(exec, size),
1256 values_{exec, std::forward<ValuesArray>(values)},
1257 stride_{stride}
1258 {
1259 if (size[0] > 0 && size[1] > 0) {
1260 GKO_ENSURE_IN_BOUNDS((size[0] - 1) * stride + size[1] - 1,
1261 values_.get_num_elems());
1262 }
1263 }
1264
1271 virtual std::unique_ptr<Dense> create_with_same_config() const
1272 {
1273 return Dense::create(this->get_executor(), this->get_size(),
1274 this->get_stride());
1275 }
1276
1284 virtual std::unique_ptr<Dense> create_with_type_of_impl(
1285 std::shared_ptr<const Executor> exec, const dim<2>& size,
1286 size_type stride) const
1287 {
1288 return Dense::create(exec, size, stride);
1289 }
1290
1297 virtual std::unique_ptr<Dense> create_view_of_impl()
1298 {
1299 auto exec = this->get_executor();
1300 return Dense::create(
1301 exec, this->get_size(),
1303 this->get_values()),
1304 this->get_stride());
1305 }
1306
1313 virtual std::unique_ptr<const Dense> create_const_view_of_impl() const
1314 {
1315 auto exec = this->get_executor();
1316 return Dense::create_const(
1317 exec, this->get_size(),
1319 this->get_const_values()),
1320 this->get_stride());
1321 }
1322
1323 template <typename IndexType>
1324 void convert_impl(Coo<ValueType, IndexType>* result) const;
1325
1326 template <typename IndexType>
1327 void convert_impl(Csr<ValueType, IndexType>* result) const;
1328
1329 template <typename IndexType>
1330 void convert_impl(Ell<ValueType, IndexType>* result) const;
1331
1332 template <typename IndexType>
1333 void convert_impl(Fbcsr<ValueType, IndexType>* result) const;
1334
1335 template <typename IndexType>
1336 void convert_impl(Hybrid<ValueType, IndexType>* result) const;
1337
1338 template <typename IndexType>
1339 void convert_impl(Sellp<ValueType, IndexType>* result) const;
1340
1341 template <typename IndexType>
1342 void convert_impl(SparsityCsr<ValueType, IndexType>* result) const;
1343
1350 virtual void scale_impl(const LinOp* alpha);
1351
1358 virtual void inv_scale_impl(const LinOp* alpha);
1359
1366 virtual void add_scaled_impl(const LinOp* alpha, const LinOp* b);
1367
1374 virtual void sub_scaled_impl(const LinOp* alpha, const LinOp* b);
1375
1382 virtual void compute_dot_impl(const LinOp* b, LinOp* result) const;
1383
1390 virtual void compute_conj_dot_impl(const LinOp* b, LinOp* result) const;
1391
1398 virtual void compute_norm2_impl(LinOp* result) const;
1399
1406 virtual void compute_norm1_impl(LinOp* result) const;
1407
1414 virtual void compute_squared_norm2_impl(LinOp* result) const;
1415
1419 virtual void compute_mean_impl(LinOp* result) const;
1420
1429 void resize(gko::dim<2> new_size);
1430
1438 virtual std::unique_ptr<Dense> create_submatrix_impl(
1439 const span& rows, const span& columns, const size_type stride);
1440
1441 void apply_impl(const LinOp* b, LinOp* x) const override;
1442
1443 void apply_impl(const LinOp* alpha, const LinOp* b, const LinOp* beta,
1444 LinOp* x) const override;
1445
1446 size_type linearize_index(size_type row, size_type col) const noexcept
1447 {
1448 return row * stride_ + col;
1449 }
1450
1451 size_type linearize_index(size_type idx) const noexcept
1452 {
1453 return linearize_index(idx / this->get_size()[1],
1454 idx % this->get_size()[1]);
1455 }
1456
1457 template <typename IndexType>
1458 void permute_impl(const Permutation<IndexType>* permutation,
1459 permute_mode mode, Dense* output) const;
1460
1461 template <typename IndexType>
1462 void permute_impl(const Permutation<IndexType>* row_permutation,
1464 bool invert, Dense* output) const;
1465
1466 template <typename IndexType>
1467 void scale_permute_impl(
1469 permute_mode mode, Dense* output) const;
1470
1471 template <typename IndexType>
1472 void scale_permute_impl(
1475 bool invert, Dense* output) const;
1476
1477 template <typename OutputType, typename IndexType>
1478 void row_gather_impl(const array<IndexType>* row_idxs,
1480
1481 template <typename OutputType, typename IndexType>
1482 void row_gather_impl(const Dense<ValueType>* alpha,
1483 const array<IndexType>* row_idxs,
1484 const Dense<ValueType>* beta,
1486
1487private:
1488 array<value_type> values_;
1489 size_type stride_;
1490
1491 void add_scaled_identity_impl(const LinOp* a, const LinOp* b) override;
1492};
1493
1494
1495} // namespace matrix
1496
1497
1498namespace detail {
1499
1500
1501template <typename ValueType>
1502struct temporary_clone_helper<matrix::Dense<ValueType>> {
1503 static std::unique_ptr<matrix::Dense<ValueType>> create(
1504 std::shared_ptr<const Executor> exec, matrix::Dense<ValueType>* ptr,
1505 bool copy_data)
1506 {
1507 if (copy_data) {
1508 return gko::clone(std::move(exec), ptr);
1509 } else {
1510 return matrix::Dense<ValueType>::create(exec, ptr->get_size());
1511 }
1512 }
1513};
1514
1515
1516} // namespace detail
1517
1518
1526template <typename VecPtr>
1527std::unique_ptr<matrix::Dense<typename detail::pointee<VecPtr>::value_type>>
1529{
1530 using value_type = typename detail::pointee<VecPtr>::value_type;
1532}
1533
1534
1542template <typename VecPtr>
1543std::unique_ptr<
1544 const matrix::Dense<typename detail::pointee<VecPtr>::value_type>>
1546{
1547 using value_type = typename detail::pointee<VecPtr>::value_type;
1549}
1550
1551
1572template <typename Matrix, typename... TArgs>
1573std::unique_ptr<Matrix> initialize(
1574 size_type stride, std::initializer_list<typename Matrix::value_type> vals,
1575 std::shared_ptr<const Executor> exec, TArgs&&... create_args)
1576{
1578 size_type num_rows = vals.size();
1579 auto tmp = dense::create(exec->get_master(), dim<2>{num_rows, 1}, stride);
1580 size_type idx = 0;
1581 for (const auto& elem : vals) {
1582 tmp->at(idx) = elem;
1583 ++idx;
1584 }
1585 auto mtx = Matrix::create(exec, std::forward<TArgs>(create_args)...);
1586 tmp->move_to(mtx);
1587 return mtx;
1588}
1589
1610template <typename Matrix, typename... TArgs>
1611std::unique_ptr<Matrix> initialize(
1612 std::initializer_list<typename Matrix::value_type> vals,
1613 std::shared_ptr<const Executor> exec, TArgs&&... create_args)
1614{
1615 return initialize<Matrix>(1, vals, std::move(exec),
1616 std::forward<TArgs>(create_args)...);
1617}
1618
1619
1640template <typename Matrix, typename... TArgs>
1641std::unique_ptr<Matrix> initialize(
1642 size_type stride,
1643 std::initializer_list<std::initializer_list<typename Matrix::value_type>>
1644 vals,
1645 std::shared_ptr<const Executor> exec, TArgs&&... create_args)
1646{
1648 size_type num_rows = vals.size();
1649 size_type num_cols = num_rows > 0 ? begin(vals)->size() : 1;
1650 auto tmp =
1651 dense::create(exec->get_master(), dim<2>{num_rows, num_cols}, stride);
1652 size_type ridx = 0;
1653 for (const auto& row : vals) {
1654 size_type cidx = 0;
1655 for (const auto& elem : row) {
1656 tmp->at(ridx, cidx) = elem;
1657 ++cidx;
1658 }
1659 ++ridx;
1660 }
1661 auto mtx = Matrix::create(exec, std::forward<TArgs>(create_args)...);
1662 tmp->move_to(mtx);
1663 return mtx;
1664}
1665
1666
1688template <typename Matrix, typename... TArgs>
1689std::unique_ptr<Matrix> initialize(
1690 std::initializer_list<std::initializer_list<typename Matrix::value_type>>
1691 vals,
1692 std::shared_ptr<const Executor> exec, TArgs&&... create_args)
1693{
1694 return initialize<Matrix>(vals.size() > 0 ? begin(vals)->size() : 0, vals,
1695 std::move(exec),
1696 std::forward<TArgs>(create_args)...);
1697}
1698
1699
1700} // namespace gko
1701
1702
1703#endif // GKO_PUBLIC_CORE_MATRIX_DENSE_HPP_
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition polymorphic_object.hpp:499
The diagonal of a LinOp implementing this interface can be extracted.
Definition lin_op.hpp:772
The EnableAbsoluteComputation mixin provides the default implementations of compute_absolute_linop an...
Definition lin_op.hpp:823
This mixin implements a static create() method on ConcreteType that dynamically allocates the memory,...
Definition polymorphic_object.hpp:776
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition lin_op.hpp:908
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:691
Definition lin_op.hpp:146
Linear operators which support permutation should implement the Permutable interface.
Definition lin_op.hpp:513
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition polymorphic_object.hpp:263
A LinOp implementing this interface can read its data from a matrix_data structure.
Definition lin_op.hpp:634
Adds the operation M <- a I + b M for matrix M, identity operator I and scalars a and b,...
Definition lin_op.hpp:847
Linear operators which support transposition should implement the Transposable interface.
Definition lin_op.hpp:462
A LinOp implementing this interface can write its data to a matrix_data structure.
Definition lin_op.hpp:689
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:187
value_type * get_data() noexcept
Returns a pointer to the block of memory used to store the elements of the array.
Definition array.hpp:646
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:655
size_type get_num_elems() const noexcept
Returns the number of elements in the array.
Definition array.hpp:637
This type is a device-side equivalent to matrix_data.
Definition device_matrix_data.hpp:63
Vector is a format which explicitly stores (multiple) distributed column vectors in a dense storage f...
Definition vector.hpp:92
COO stores a matrix in the coordinate matrix format.
Definition coo.hpp:87
CSR is a matrix format which stores only the nonzero coefficients by compressing each row of the matr...
Definition csr.hpp:146
Dense is a matrix format which explicitly stores all values of the matrix.
Definition dense.hpp:136
std::unique_ptr< Dense > scale_permute(ptr_param< const ScaledPermutation< value_type, int64 > > row_permutation, ptr_param< const ScaledPermutation< value_type, int64 > > column_permutation, bool invert=false) const
value_type & at(size_type row, size_type col) noexcept
Returns a single element of the matrix.
Definition dense.hpp:897
void compute_norm1(ptr_param< LinOp > result) const
Computes the column-wise (L^1) norm of this matrix.
void compute_absolute(ptr_param< absolute_type > output) const
Writes the absolute values of this matrix into an existing matrix.
void compute_norm2(ptr_param< LinOp > result, array< char > &tmp) const
Computes the column-wise Euclidean (L^2) norm of this matrix.
std::unique_ptr< LinOp > row_permute(const array< int64 > *permutation_indices) const override
Returns a LinOp representing the row permutation of the Permutable object.
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:212
std::unique_ptr< Dense > permute(ptr_param< const Permutation< int64 > > row_permutation, ptr_param< const Permutation< int64 > > column_permutation, bool invert=false) const
Dense & operator=(const Dense &)
Copy-assigns a Dense matrix.
void get_imag(ptr_param< real_type > result) const
Extracts the imaginary part of the original matrix into a given real matrix.
void row_permute(const array< int32 > *permutation_indices, ptr_param< Dense > output) const
Writes the row-permuted matrix into the given output matrix.
void get_real(ptr_param< real_type > result) const
Extracts the real part of the original matrix into a given real matrix.
std::unique_ptr< LinOp > column_permute(const array< int64 > *permutation_indices) const override
Returns a LinOp representing the column permutation of the Permutable object.
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:267
void compute_dot(ptr_param< const LinOp > b, ptr_param< LinOp > result) const
Computes the column-wise dot product of this matrix and b.
void sub_scaled(ptr_param< const LinOp > alpha, ptr_param< const LinOp > b)
Subtracts b scaled by alpha from the matrix (aka: BLAS axpy).
std::unique_ptr< Dense > permute(ptr_param< const Permutation< int64 > > permutation, permute_mode mode=permute_mode::symmetric) const
std::unique_ptr< LinOp > permute(const array< int64 > *permutation_indices) const override
Returns a LinOp representing the symmetric row and column permutation of the Permutable object.
ValueType at(size_type idx) const noexcept
Returns a single element of the matrix.
Definition dense.hpp:932
std::unique_ptr< Dense > row_gather(const array< int64 > *gather_indices) const
Create a Dense matrix consisting of the given rows from this matrix.
std::unique_ptr< LinOp > inverse_permute(const array< int64 > *permutation_indices) const override
Returns a LinOp representing the symmetric inverse row and column permutation of the Permutable objec...
std::unique_ptr< Dense > permute(ptr_param< const Permutation< int32 > > row_permutation, ptr_param< const Permutation< int32 > > column_permutation, bool invert=false) const
Creates a non-symmetrically permuted copy of this matrix with the given row and column permutations...
void row_gather(const array< int32 > *gather_indices, ptr_param< LinOp > row_collection) const
Copies the given rows from this matrix into row_collection
void column_permute(const array< int32 > *permutation_indices, ptr_param< Dense > output) const
Writes the column-permuted matrix into the given output matrix.
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.
Dense(const Dense &)
Copy-constructs a Dense matrix.
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 .
void inverse_permute(const array< int64 > *permutation_indices, ptr_param< Dense > output) const
void compute_squared_norm2(ptr_param< LinOp > result, array< char > &tmp) const
Computes the square of the column-wise Euclidean (L^2) norm of this matrix.
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:233
void permute(const array< int64 > *permutation_indices, ptr_param< Dense > output) const
void scale(ptr_param< const LinOp > alpha)
Scales the matrix with a scalar (aka: BLAS scal).
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.
void conj_transpose(ptr_param< Dense > output) const
Writes the conjugate-transposed matrix into the given output matrix.
std::unique_ptr< Dense > create_submatrix(const span &rows, const span &columns)
Create a submatrix from the original matrix.
Definition dense.hpp:1141
void row_permute(const array< int64 > *permutation_indices, ptr_param< Dense > output) const
std::unique_ptr< LinOp > permute(const array< int32 > *permutation_indices) const override
Returns a LinOp representing the symmetric row and column permutation of the Permutable object.
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:1128
void compute_squared_norm2(ptr_param< LinOp > result) const
Computes the square of the column-wise Euclidean (L^2) norm of this matrix.
std::unique_ptr< LinOp > row_permute(const array< int32 > *permutation_indices) const override
Returns a LinOp representing the row permutation of the Permutable object.
void permute(ptr_param< const Permutation< int64 > > permutation, ptr_param< Dense > output, permute_mode mode) const
void inverse_permute(const array< int32 > *permutation_indices, ptr_param< Dense > output) const
Writes the inverse symmetrically permuted matrix into the given output matrix.
std::unique_ptr< Dense > row_gather(const array< int32 > *gather_indices) const
Create a Dense matrix consisting of the given rows from this matrix.
void extract_diagonal(ptr_param< Diagonal< ValueType > > output) const
Writes the diagonal of this matrix into an existing diagonal matrix.
void make_complex(ptr_param< complex_type > result) const
Writes a complex copy of the original matrix to a given complex matrix.
std::unique_ptr< absolute_type > compute_absolute() const override
Gets the AbsoluteLinOp.
void scale_permute(ptr_param< const ScaledPermutation< value_type, int32 > > row_permutation, ptr_param< const ScaledPermutation< value_type, int32 > > column_permutation, ptr_param< Dense > output, bool invert=false) const
Overload of scale_permute(ptr_param<const ScaledPermutation<value_type, int32>>, ptr_param<const Scal...
void permute(ptr_param< const Permutation< int32 > > permutation, ptr_param< Dense > output, permute_mode mode) const
Overload of permute(ptr_param<const Permutation<int32>>, permute_mode) that writes the permuted copy ...
void row_gather(ptr_param< const LinOp > alpha, const array< int32 > *gather_indices, ptr_param< const LinOp > beta, ptr_param< LinOp > row_collection) const
Copies the given rows from this matrix into row_collection with scaling.
const value_type * get_const_values() const noexcept
Returns a pointer to the array of values of the matrix.
Definition dense.hpp:865
size_type get_num_stored_elements() const noexcept
Returns the number of elements explicitly stored in the matrix.
Definition dense.hpp:882
void compute_mean(ptr_param< LinOp > result, array< char > &tmp) const
Computes the column-wise arithmetic mean of this matrix.
ValueType & at(size_type idx) noexcept
Returns a single element of the matrix.
Definition dense.hpp:924
std::unique_ptr< Dense > scale_permute(ptr_param< const ScaledPermutation< value_type, int32 > > row_permutation, ptr_param< const ScaledPermutation< value_type, int32 > > column_permutation, bool invert=false) const
Creates a scaled and permuted copy of this matrix.
void scale_permute(ptr_param< const ScaledPermutation< value_type, int64 > > row_permutation, ptr_param< const ScaledPermutation< value_type, int64 > > column_permutation, ptr_param< Dense > output, bool invert=false) const
void compute_conj_dot(ptr_param< const LinOp > b, ptr_param< LinOp > result, array< char > &tmp) const
Computes the column-wise dot product of conj(this matrix) and b.
void compute_mean(ptr_param< LinOp > result) const
Computes the column-wise arithmetic mean of this matrix.
size_type get_stride() const noexcept
Returns the stride of the matrix.
Definition dense.hpp:875
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...
void permute(const array< int32 > *permutation_indices, ptr_param< Dense > output) const
Writes the symmetrically permuted matrix into the given output matrix.
void scale_permute(ptr_param< const ScaledPermutation< value_type, int64 > > permutation, ptr_param< Dense > output, permute_mode mode) const
void inverse_row_permute(const array< int32 > *permutation_indices, ptr_param< Dense > output) const
Writes the inverse row-permuted matrix into the given output matrix.
void inverse_column_permute(const array< int32 > *permutation_indices, ptr_param< Dense > output) const
Writes the inverse column-permuted matrix into the given output matrix.
void add_scaled(ptr_param< const LinOp > alpha, ptr_param< const LinOp > b)
Adds b scaled by alpha to the matrix (aka: BLAS axpy).
std::unique_ptr< LinOp > inverse_column_permute(const array< int64 > *permutation_indices) const override
Returns a LinOp representing the row permutation of the inverse permuted object.
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:283
void permute(ptr_param< const Permutation< int32 > > row_permutation, ptr_param< const Permutation< int32 > > column_permutation, ptr_param< Dense > output, bool invert=false) const
Overload of permute(ptr_param<const Permutation<int32>>, ptr_param<const Permutation<int32>>,...
void compute_norm1(ptr_param< LinOp > result, array< char > &tmp) const
Computes the column-wise (L^1) norm of this matrix.
std::unique_ptr< LinOp > inverse_row_permute(const array< int64 > *permutation_indices) const override
Returns a LinOp representing the row permutation of the inverse permuted object.
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.
void transpose(ptr_param< Dense > output) const
Writes the transposed matrix into the given output matrix.
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.
std::unique_ptr< real_type > get_real() const
Creates a new real matrix and extracts the real part of the original matrix into that.
std::unique_ptr< Dense > scale_permute(ptr_param< const ScaledPermutation< value_type, int64 > > permutation, permute_mode mode=permute_mode::symmetric) const
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:295
std::unique_ptr< LinOp > conj_transpose() const override
Returns a LinOp representing the conjugate transpose of the Transposable object.
std::unique_ptr< LinOp > column_permute(const array< int32 > *permutation_indices) const override
Returns a LinOp representing the column permutation of the Permutable object.
std::unique_ptr< LinOp > transpose() const override
Returns a LinOp representing the transpose of the Transposable object.
void scale_permute(ptr_param< const ScaledPermutation< value_type, int32 > > permutation, ptr_param< Dense > output, permute_mode mode) const
Overload of scale_permute(ptr_param<const ScaledPermutation<value_type, int32>>, permute_mode) that w...
void compute_absolute_inplace() override
Compute absolute inplace on each element.
std::unique_ptr< complex_type > make_complex() const
Creates a complex copy of the original matrix.
std::unique_ptr< const real_type > create_real_view() const
Create a real view of the (potentially) complex original matrix.
void column_permute(const array< int64 > *permutation_indices, ptr_param< Dense > output) const
Dense(Dense &&)
Move-constructs a Dense matrix.
void inverse_row_permute(const array< int64 > *permutation_indices, ptr_param< Dense > output) const
value_type at(size_type row, size_type col) const noexcept
Returns a single element of the matrix.
Definition dense.hpp:905
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.
Definition dense.hpp:1172
void permute(ptr_param< const Permutation< int64 > > row_permutation, ptr_param< const Permutation< int64 > > column_permutation, ptr_param< Dense > output, bool invert=false) const
void inv_scale(ptr_param< const LinOp > alpha)
Scales the matrix with the inverse of a scalar.
Dense & operator=(Dense &&)
Move-assigns a Dense matrix.
value_type * get_values() noexcept
Returns a pointer to the array of values of the matrix.
Definition dense.hpp:856
void inverse_column_permute(const array< int64 > *permutation_indices, ptr_param< Dense > output) const
void compute_norm2(ptr_param< LinOp > result) const
Computes the column-wise Euclidean (L^2) norm of this matrix.
void row_gather(const array< int64 > *gather_indices, ptr_param< LinOp > row_collection) const
std::unique_ptr< real_type > get_imag() const
Creates a new real matrix and extracts the imaginary part of the original matrix into that.
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:249
std::unique_ptr< real_type > create_real_view()
Create a real view of the (potentially) complex original matrix.
std::unique_ptr< Diagonal< ValueType > > extract_diagonal() const override
Extracts the diagonal entries of the matrix into a vector.
void fill(const ValueType value)
Fill the dense matrix with a given value.
void compute_dot(ptr_param< const LinOp > b, ptr_param< LinOp > result, array< char > &tmp) const
Computes the column-wise dot product of this matrix and b.
void row_gather(ptr_param< const LinOp > alpha, const array< int64 > *gather_indices, ptr_param< const LinOp > beta, ptr_param< LinOp > row_collection) const
This class is a utility which efficiently implements the diagonal matrix (a linear operator which sca...
Definition diagonal.hpp:79
ELL is a matrix format where stride with explicit zeros is used such that all rows have the same numb...
Definition ell.hpp:89
Fixed-block compressed sparse row storage matrix format.
Definition fbcsr.hpp:138
HYBRID is a matrix format which splits the matrix into ELLPACK and COO format.
Definition hybrid.hpp:81
Permutation is a matrix format that represents a permutation matrix, i.e.
Definition permutation.hpp:142
ScaledPermutation is a matrix combining a permutation with scaling factors.
Definition scaled_permutation.hpp:67
SELL-P is a matrix format similar to ELL format.
Definition sellp.hpp:80
SparsityCsr is a matrix format which stores only the sparsity pattern of a sparse matrix by compressi...
Definition sparsity_csr.hpp:87
This class is used for function parameters in the place of raw pointers.
Definition utils_helper.hpp:71
A range is a multidimensional view of the memory.
Definition range.hpp:326
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:1573
permute_mode
Specifies how a permutation will be applied to a matrix.
Definition permutation.hpp:71
@ columns
The columns will be permuted.
@ rows
The rows will be permuted.
@ symmetric
The rows and columns will be permuted.
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
typename detail::remove_complex_s< T >::type remove_complex
Obtain the type which removed the complex of complex/scalar type or the template parameter of class b...
Definition math.hpp:354
std::int32_t int32
32-bit signed integral type.
Definition types.hpp:137
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:1545
typename detail::next_precision_impl< T >::type next_precision
Obtains the next type in the singly-linked precision list.
Definition math.hpp:490
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:373
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:779
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:1528
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:760
std::int64_t int64
64-bit signed integral type.
Definition types.hpp:143
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:120
detail::cloned_type< Pointer > clone(const Pointer &p)
Creates a unique clone of the object pointed to by p.
Definition utils_helper.hpp:203
A type representing the dimensions of a multidimensional object.
Definition dim.hpp:55
This structure is used as an intermediate data type to store a sparse matrix.
Definition matrix_data.hpp:155
A span is a lightweight structure used to create sub-ranges from other ranges.
Definition range.hpp:75