8 #include <ginkgo/core/base/lin_op.hpp>
9 #include <ginkgo/core/matrix/device_views.hpp>
16 template <
typename ValueType>
20 template <
typename ValueType,
typename IndexType>
23 template <
typename ValueType,
typename IndexType>
26 template <
typename ValueType>
29 template <
typename ValueType,
typename IndexType>
32 template <
typename ValueType,
typename IndexType>
35 template <
typename ValueType,
typename IndexType>
38 template <
typename ValueType,
typename IndexType>
41 template <
typename ValueType,
typename IndexType>
63 template <
typename ValueType = default_precision>
64 class Dense :
public LinOp,
65 public EnableCloneable<Dense<ValueType>>,
66 public ConvertibleTo<MultiVector<ValueType>>,
67 public ConvertibleTo<Dense<next_precision<ValueType>>>,
68 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
69 public ConvertibleTo<Dense<next_precision<ValueType, 2>>>,
71 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
72 public ConvertibleTo<Dense<next_precision<ValueType, 3>>>,
74 public ConvertibleTo<Coo<ValueType, int32>>,
75 public ConvertibleTo<Coo<ValueType, int64>>,
76 public ConvertibleTo<Csr<ValueType, int32>>,
77 public ConvertibleTo<Csr<ValueType, int64>>,
78 public ConvertibleTo<Ell<ValueType, int32>>,
79 public ConvertibleTo<Ell<ValueType, int64>>,
80 public ConvertibleTo<Fbcsr<ValueType, int32>>,
81 public ConvertibleTo<Fbcsr<ValueType, int64>>,
82 public ConvertibleTo<Hybrid<ValueType, int32>>,
83 public ConvertibleTo<Hybrid<ValueType, int64>>,
84 public ConvertibleTo<Sellp<ValueType, int32>>,
85 public ConvertibleTo<Sellp<ValueType, int64>>,
86 public ConvertibleTo<SparsityCsr<ValueType, int32>>,
87 public ConvertibleTo<SparsityCsr<ValueType, int64>>,
88 public DiagonalExtractable<ValueType>,
89 public ReadableFromMatrixData<ValueType, int32>,
90 public ReadableFromMatrixData<ValueType, int64>,
91 public WritableToMatrixData<ValueType, int32>,
92 public WritableToMatrixData<ValueType, int64>,
94 public ScaledIdentityAddable {
95 friend class EnableCloneable<Dense>;
98 friend class MultiVector<ValueType>;
99 friend class Coo<ValueType,
int32>;
100 friend class Coo<ValueType,
int64>;
101 friend class Csr<ValueType,
int32>;
102 friend class Csr<ValueType,
int64>;
103 friend class Ell<ValueType,
int32>;
104 friend class Ell<ValueType,
int64>;
105 friend class Fbcsr<ValueType,
int32>;
106 friend class Fbcsr<ValueType,
int64>;
107 friend class Hybrid<ValueType,
int32>;
108 friend class Hybrid<ValueType,
int64>;
109 friend class Sellp<ValueType,
int32>;
110 friend class Sellp<ValueType,
int64>;
111 friend class SparsityCsr<ValueType,
int32>;
112 friend class SparsityCsr<ValueType,
int64>;
113 GKO_ASSERT_SUPPORTED_VALUE_TYPE;
118 using ConvertibleTo<MultiVector<ValueType>>::convert_to;
119 using ConvertibleTo<MultiVector<ValueType>>::move_to;
120 using ConvertibleTo<Coo<ValueType, int32>>::convert_to;
121 using ConvertibleTo<Coo<ValueType, int32>>::move_to;
122 using ConvertibleTo<Coo<ValueType, int64>>::convert_to;
123 using ConvertibleTo<Coo<ValueType, int64>>::move_to;
124 using ConvertibleTo<Csr<ValueType, int32>>::convert_to;
125 using ConvertibleTo<Csr<ValueType, int32>>::move_to;
126 using ConvertibleTo<Csr<ValueType, int64>>::convert_to;
127 using ConvertibleTo<Csr<ValueType, int64>>::move_to;
128 using ConvertibleTo<Ell<ValueType, int32>>::convert_to;
129 using ConvertibleTo<Ell<ValueType, int32>>::move_to;
130 using ConvertibleTo<Ell<ValueType, int64>>::convert_to;
131 using ConvertibleTo<Ell<ValueType, int64>>::move_to;
132 using ConvertibleTo<Fbcsr<ValueType, int32>>::convert_to;
133 using ConvertibleTo<Fbcsr<ValueType, int32>>::move_to;
134 using ConvertibleTo<Fbcsr<ValueType, int64>>::convert_to;
135 using ConvertibleTo<Fbcsr<ValueType, int64>>::move_to;
136 using ConvertibleTo<Hybrid<ValueType, int32>>::convert_to;
137 using ConvertibleTo<Hybrid<ValueType, int32>>::move_to;
138 using ConvertibleTo<Hybrid<ValueType, int64>>::convert_to;
139 using ConvertibleTo<Hybrid<ValueType, int64>>::move_to;
140 using ConvertibleTo<Sellp<ValueType, int32>>::convert_to;
141 using ConvertibleTo<Sellp<ValueType, int32>>::move_to;
142 using ConvertibleTo<Sellp<ValueType, int64>>::convert_to;
143 using ConvertibleTo<Sellp<ValueType, int64>>::move_to;
144 using ConvertibleTo<SparsityCsr<ValueType, int32>>::convert_to;
145 using ConvertibleTo<SparsityCsr<ValueType, int32>>::move_to;
146 using ConvertibleTo<SparsityCsr<ValueType, int64>>::convert_to;
147 using ConvertibleTo<SparsityCsr<ValueType, int64>>::move_to;
149 using value_type = ValueType;
150 using index_type =
int64;
151 using transposed_type = Dense;
152 using mat_data64 = matrix_data<value_type, int64>;
153 using mat_data32 = matrix_data<value_type, int32>;
154 using mat_data [[deprecated(
"Use mat_data64 instead.")]] = mat_data64;
155 using device_mat_data64 = device_matrix_data<value_type, int64>;
156 using device_mat_data32 = device_matrix_data<value_type, int32>;
157 using device_mat_data [[deprecated(
"Use device_mat_data64 instead.")]] =
159 using device_view = view::dense<value_type>;
160 using const_device_view = view::dense<const value_type>;
164 void convert_to(MultiVector<ValueType>* result)
const override;
166 void move_to(MultiVector<ValueType>* result)
override;
168 void convert_to(Dense<next_precision<ValueType>>* result)
const override;
170 void move_to(Dense<next_precision<ValueType>>* result)
override;
172 #if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
174 using ConvertibleTo<Dense<next_precision<ValueType, 2>>>::convert_to;
175 using ConvertibleTo<Dense<next_precision<ValueType, 2>>>::move_to;
177 void convert_to(Dense<next_precision<ValueType, 2>>* result)
const override;
179 void move_to(Dense<next_precision<ValueType, 2>>* result)
override;
182 #if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
184 using ConvertibleTo<Dense<next_precision<ValueType, 3>>>::convert_to;
185 using ConvertibleTo<Dense<next_precision<ValueType, 3>>>::move_to;
187 void convert_to(Dense<next_precision<ValueType, 3>>* result)
const override;
189 void move_to(Dense<next_precision<ValueType, 3>>* result)
override;
192 void convert_to(Coo<ValueType, int32>* result)
const override;
194 void move_to(Coo<ValueType, int32>* result)
override;
196 void convert_to(Coo<ValueType, int64>* result)
const override;
198 void move_to(Coo<ValueType, int64>* result)
override;
200 void convert_to(Csr<ValueType, int32>* result)
const override;
202 void move_to(Csr<ValueType, int32>* result)
override;
204 void convert_to(Csr<ValueType, int64>* result)
const override;
206 void move_to(Csr<ValueType, int64>* result)
override;
208 void convert_to(Ell<ValueType, int32>* result)
const override;
210 void move_to(Ell<ValueType, int32>* result)
override;
212 void convert_to(Ell<ValueType, int64>* result)
const override;
214 void move_to(Ell<ValueType, int64>* result)
override;
216 void convert_to(Fbcsr<ValueType, int32>* result)
const override;
218 void move_to(Fbcsr<ValueType, int32>* result)
override;
220 void convert_to(Fbcsr<ValueType, int64>* result)
const override;
222 void move_to(Fbcsr<ValueType, int64>* result)
override;
224 void convert_to(Hybrid<ValueType, int32>* result)
const override;
226 void move_to(Hybrid<ValueType, int32>* result)
override;
228 void convert_to(Hybrid<ValueType, int64>* result)
const override;
230 void move_to(Hybrid<ValueType, int64>* result)
override;
232 void convert_to(Sellp<ValueType, int32>* result)
const override;
234 void move_to(Sellp<ValueType, int32>* result)
override;
236 void convert_to(Sellp<ValueType, int64>* result)
const override;
238 void move_to(Sellp<ValueType, int64>* result)
override;
240 void convert_to(SparsityCsr<ValueType, int32>* result)
const override;
242 void move_to(SparsityCsr<ValueType, int32>* result)
override;
244 void convert_to(SparsityCsr<ValueType, int64>* result)
const override;
246 void move_to(SparsityCsr<ValueType, int64>* result)
override;
248 void read(
const mat_data32& data)
override;
250 void read(
const mat_data64& data)
override;
252 void read(
const device_mat_data32& data)
override;
254 void read(
const device_mat_data64& data)
override;
256 void read(device_mat_data32&& data)
override;
258 void read(device_mat_data64&& data)
override;
260 void write(mat_data32& data)
const override;
262 void write(mat_data64& data)
const override;
266 void fill(ValueType value);
279 [[nodiscard]] std::unique_ptr<LinOp>
transpose()
const override;
281 [[nodiscard]] std::unique_ptr<LinOp>
conj_transpose()
const override;
289 void transpose(ptr_param<Dense> output)
const;
305 void add_scaled(ptr_param<const AbstractMultiVector> alpha,
306 ptr_param<
const Diagonal<value_type>> diag);
314 void sub_scaled(ptr_param<const AbstractMultiVector> alpha,
315 ptr_param<
const Diagonal<value_type>> diag);
317 [[nodiscard]]
static std::unique_ptr<Dense> create(
318 std::shared_ptr<const Executor> exec,
const dim<2>& size = dim<2>{},
321 [[nodiscard]]
static std::unique_ptr<Dense> create(
322 std::shared_ptr<const Executor> exec,
const dim<2>& size,
323 array<value_type> values,
size_type stride);
325 [[nodiscard]]
static std::unique_ptr<const Dense> create_const(
326 std::shared_ptr<const Executor> exec,
const dim<2>& size,
327 ::gko::detail::const_array_view<ValueType>&& values,
size_type stride);
329 [[nodiscard]] std::unique_ptr<Dense> create_subview(span
rows, span cols);
331 [[nodiscard]] std::unique_ptr<const Dense> create_subview(span
rows,
334 [[nodiscard]] std::unique_ptr<const Dense> create_const_subview(
335 span
rows, span cols)
const;
337 [[nodiscard]] std::unique_ptr<const MultiVector<ValueType>>
338 as_const_multivector_view()
const;
340 [[nodiscard]] std::unique_ptr<MultiVector<ValueType>> as_multivector_view();
342 [[nodiscard]] device_view get_device_view();
344 [[nodiscard]] const_device_view get_const_device_view()
const;
346 ValueType* get_values() noexcept {
return values_.
get_data(); }
348 const ValueType* get_const_values() const noexcept
357 [[nodiscard]]
size_type get_stride() const noexcept;
359 [[nodiscard]]
size_type get_num_stored_elements() const noexcept;
361 Dense(const Dense& other);
363 Dense(Dense&& other);
365 Dense& operator=(const Dense& other);
367 Dense& operator=(Dense&& other);
370 Dense(std::shared_ptr<const Executor> exec, const dim<2>& size = dim<2>{},
373 Dense(std::shared_ptr<const Executor> exec,
const dim<2>& size,
374 array<value_type> values,
size_type stride);
376 void apply_impl(
const AbstractMultiVector* b,
377 AbstractMultiVector* x)
const override;
379 void apply_impl(
const AbstractMultiVector* alpha,
380 const AbstractMultiVector* b,
381 const AbstractMultiVector* beta,
382 AbstractMultiVector* x)
const override;
387 void resize(dim<2> new_size);
389 template <
typename IndexType>
390 void convert_impl(Coo<ValueType, IndexType>* result)
const;
392 template <
typename IndexType>
393 void convert_impl(Csr<ValueType, IndexType>* result)
const;
395 template <
typename IndexType>
396 void convert_impl(Ell<ValueType, IndexType>* result)
const;
398 template <
typename IndexType>
399 void convert_impl(Fbcsr<ValueType, IndexType>* result)
const;
401 template <
typename IndexType>
402 void convert_impl(Hybrid<ValueType, IndexType>* result)
const;
404 template <
typename IndexType>
405 void convert_impl(Sellp<ValueType, IndexType>* result)
const;
407 template <
typename IndexType>
408 void convert_impl(SparsityCsr<ValueType, IndexType>* result)
const;
412 array<value_type> values_;
414 void add_scaled_identity_impl(
const AbstractMultiVector* a,
415 const AbstractMultiVector* b)
override;
425 template <
typename ValueType>
426 struct temporary_clone_helper<matrix::Dense<ValueType>> {
427 static std::unique_ptr<matrix::Dense<ValueType>> create(
428 std::shared_ptr<const Executor> exec, matrix::Dense<ValueType>* ptr,
434 return matrix::Dense<ValueType>::create(exec, ptr->get_size());
464 template <
typename Matrix,
typename... TArgs>
466 size_type stride, std::initializer_list<typename Matrix::value_type> vals,
467 std::shared_ptr<const Executor> exec, TArgs&&... create_args)
471 auto tmp = dense::create(exec->get_master(),
dim<2>{num_rows, 1}, stride);
473 for (
const auto& elem : vals) {
474 tmp->at(idx, 0) = elem;
477 auto mtx = Matrix::create(exec, std::forward<TArgs>(create_args)...);
503 template <
typename Matrix,
typename... TArgs>
505 std::initializer_list<typename Matrix::value_type> vals,
506 std::shared_ptr<const Executor> exec, TArgs&&... create_args)
508 return initialize<Matrix>(1, vals, std::move(exec),
509 std::forward<TArgs>(create_args)...);
534 template <
typename Matrix,
typename... TArgs>
537 std::initializer_list<std::initializer_list<typename Matrix::value_type>>
539 std::shared_ptr<const Executor> exec, TArgs&&... create_args)
543 size_type num_cols = num_rows > 0 ? begin(vals)->size() : 1;
545 dense::create(exec->get_master(),
dim<2>{num_rows, num_cols}, stride);
547 for (
const auto& row : vals) {
549 for (
const auto& elem : row) {
550 tmp->at(ridx, cidx) = elem;
555 auto mtx = Matrix::create(exec, std::forward<TArgs>(create_args)...);
583 template <
typename Matrix,
typename... TArgs>
585 std::initializer_list<std::initializer_list<typename Matrix::value_type>>
587 std::shared_ptr<const Executor> exec, TArgs&&... create_args)
589 return initialize<Matrix>(vals.size() > 0 ? begin(vals)->size() : 0, vals,
591 std::forward<TArgs>(create_args)...);