Ginkgo Generated from branch based on main. Ginkgo version 1.10.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
batch_dense.hpp
1// SPDX-FileCopyrightText: 2017 - 2025 The Ginkgo authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#ifndef GKO_PUBLIC_CORE_MATRIX_BATCH_DENSE_HPP_
6#define GKO_PUBLIC_CORE_MATRIX_BATCH_DENSE_HPP_
7
8
9#include <initializer_list>
10#include <vector>
11
12#include <ginkgo/core/base/array.hpp>
13#include <ginkgo/core/base/batch_lin_op.hpp>
14#include <ginkgo/core/base/batch_multi_vector.hpp>
15#include <ginkgo/core/base/executor.hpp>
16#include <ginkgo/core/base/mtx_io.hpp>
17#include <ginkgo/core/base/range_accessors.hpp>
18#include <ginkgo/core/base/types.hpp>
19#include <ginkgo/core/base/utils.hpp>
20#include <ginkgo/core/matrix/dense.hpp>
21
22
23namespace gko {
24namespace batch {
25namespace matrix {
26
27
47template <typename ValueType = default_precision>
48class Dense final : public EnableBatchLinOp<Dense<ValueType>>,
49#if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
50 public ConvertibleTo<Dense<next_precision<ValueType, 2>>>,
51#endif
52#if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
53 public ConvertibleTo<Dense<next_precision<ValueType, 3>>>,
54#endif
55 public ConvertibleTo<Dense<next_precision<ValueType>>> {
56 friend class EnablePolymorphicObject<Dense, BatchLinOp>;
57 friend class Dense<to_complex<ValueType>>;
58 friend class Dense<previous_precision<ValueType>>;
59
60public:
61 using EnableBatchLinOp<Dense>::convert_to;
62 using EnableBatchLinOp<Dense>::move_to;
63
64 using value_type = ValueType;
65 using index_type = int32;
66 using transposed_type = Dense<ValueType>;
67 using unbatch_type = gko::matrix::Dense<ValueType>;
68 using absolute_type = remove_complex<Dense>;
69 using complex_type = to_complex<Dense>;
70
71 void convert_to(Dense<next_precision<ValueType>>* result) const override;
72
73 void move_to(Dense<next_precision<ValueType>>* result) override;
74
75#if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
76 friend class Dense<previous_precision<ValueType, 2>>;
77 using ConvertibleTo<Dense<next_precision<ValueType, 2>>>::convert_to;
78 using ConvertibleTo<Dense<next_precision<ValueType, 2>>>::move_to;
79
80 void convert_to(Dense<next_precision<ValueType, 2>>* result) const override;
81
82 void move_to(Dense<next_precision<ValueType, 2>>* result) override;
83#endif
84
85#if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
86 friend class Dense<previous_precision<ValueType, 3>>;
87 using ConvertibleTo<Dense<next_precision<ValueType, 3>>>::convert_to;
88 using ConvertibleTo<Dense<next_precision<ValueType, 3>>>::move_to;
89
90 void convert_to(Dense<next_precision<ValueType, 3>>* result) const override;
91
92 void move_to(Dense<next_precision<ValueType, 3>>* result) override;
93#endif
94
105 std::unique_ptr<unbatch_type> create_view_for_item(size_type item_id);
106
110 std::unique_ptr<const unbatch_type> create_const_view_for_item(
111 size_type item_id) const;
112
121 {
122 GKO_ASSERT(batch_id < this->get_num_batch_items());
123 return batch_id * this->get_common_size()[0] *
124 this->get_common_size()[1];
125 }
126
132 value_type* get_values() noexcept { return values_.get_data(); }
133
141 const value_type* get_const_values() const noexcept
142 {
143 return values_.get_const_data();
144 }
145
157 value_type& at(size_type batch_id, size_type row, size_type col)
158 {
159 GKO_ASSERT(batch_id < this->get_num_batch_items());
160 return values_.get_data()[linearize_index(batch_id, row, col)];
161 }
162
166 value_type at(size_type batch_id, size_type row, size_type col) const
167 {
168 GKO_ASSERT(batch_id < this->get_num_batch_items());
169 return values_.get_const_data()[linearize_index(batch_id, row, col)];
170 }
171
186 ValueType& at(size_type batch_id, size_type idx) noexcept
187 {
188 GKO_ASSERT(batch_id < this->get_num_batch_items());
189 return values_.get_data()[linearize_index(batch_id, idx)];
190 }
191
195 ValueType at(size_type batch_id, size_type idx) const noexcept
196 {
197 GKO_ASSERT(batch_id < this->get_num_batch_items());
198 return values_.get_const_data()[linearize_index(batch_id, idx)];
199 }
200
209 value_type* get_values_for_item(size_type batch_id) noexcept
210 {
211 GKO_ASSERT(batch_id < this->get_num_batch_items());
212 return values_.get_data() + this->get_cumulative_offset(batch_id);
213 }
214
222 const value_type* get_const_values_for_item(
223 size_type batch_id) const noexcept
224 {
225 GKO_ASSERT(batch_id < this->get_num_batch_items());
226 return values_.get_const_data() + this->get_cumulative_offset(batch_id);
227 }
228
237 {
238 return values_.get_size();
239 }
240
247 {
248 return this->get_num_stored_elements() / this->get_num_batch_items();
249 }
250
259 static std::unique_ptr<Dense> create(
260 std::shared_ptr<const Executor> exec,
261 const batch_dim<2>& size = batch_dim<2>{});
262
277 static std::unique_ptr<Dense> create(std::shared_ptr<const Executor> exec,
278 const batch_dim<2>& size,
279 array<value_type> values);
280
285 template <typename InputValueType>
286 GKO_DEPRECATED(
287 "explicitly construct the gko::array argument instead of passing an"
288 "initializer list")
289 static std::unique_ptr<Dense> create(
290 std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
291 std::initializer_list<InputValueType> values)
292 {
293 return create(exec, size, array<value_type>{exec, std::move(values)});
294 }
295
310 static std::unique_ptr<const Dense> create_const(
311 std::shared_ptr<const Executor> exec, const batch_dim<2>& sizes,
312 gko::detail::const_array_view<ValueType>&& values);
313
323
338
344
350 const Dense* apply(ptr_param<const MultiVector<value_type>> alpha,
354
361 void scale(const array<value_type>& row_scale,
362 const array<value_type>& col_scale);
363
374
385
386private:
387 inline size_type compute_num_elems(const batch_dim<2>& size)
388 {
389 return size.get_num_batch_items() * size.get_common_size()[0] *
390 size.get_common_size()[1];
391 }
392
393 Dense(std::shared_ptr<const Executor> exec,
394 const batch_dim<2>& size = batch_dim<2>{});
395
396 Dense(std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
397 array<value_type> values);
398
399 void apply_impl(const MultiVector<value_type>* b,
400 MultiVector<value_type>* x) const;
401
402 void apply_impl(const MultiVector<value_type>* alpha,
404 const MultiVector<value_type>* beta,
405 MultiVector<value_type>* x) const;
406
407 size_type linearize_index(size_type batch, size_type row,
408 size_type col) const noexcept
409 {
410 return this->get_cumulative_offset(batch) +
411 row * this->get_size().get_common_size()[1] + col;
412 }
413
414 size_type linearize_index(size_type batch, size_type idx) const noexcept
415 {
416 return linearize_index(batch, idx / this->get_common_size()[1],
417 idx % this->get_common_size()[1]);
418 }
419
420 array<value_type> values_;
421};
422
423
424} // namespace matrix
425} // namespace batch
426} // namespace gko
427
428
429#endif // GKO_PUBLIC_CORE_MATRIX_BATCH_DENSE_HPP_
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition polymorphic_object.hpp:479
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:668
The first step in using the Ginkgo library consists of creating an executor.
Definition executor.hpp:615
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:166
Definition batch_lin_op.hpp:59
The EnableBatchLinOp mixin can be used to provide sensible default implementations of the majority of...
Definition batch_lin_op.hpp:252
MultiVector stores multiple vectors in a batched fashion and is useful for batched operations.
Definition batch_multi_vector.hpp:61
Dense is a batch matrix format which explicitly stores all values of the matrix in each of the batche...
Definition batch_dense.hpp:55
static std::unique_ptr< Dense > create(std::shared_ptr< const Executor > exec, const batch_dim< 2 > &size, array< value_type > values)
Creates a Dense matrix from an already allocated (and initialized) array.
size_type get_num_elements_per_item() const noexcept
Returns the number of stored elements in each batch item.
Definition batch_dense.hpp:246
std::unique_ptr< unbatch_type > create_view_for_item(size_type item_id)
Creates a mutable view (of gko::matrix::Dense type) of one item of the batch::matrix::Dense<value_typ...
const Dense * apply(ptr_param< const MultiVector< value_type > > b, ptr_param< MultiVector< value_type > > x) const
value_type * get_values() noexcept
Returns a pointer to the array of values of the multi-vector.
Definition batch_dense.hpp:132
Dense * apply(ptr_param< const MultiVector< value_type > > b, ptr_param< MultiVector< value_type > > x)
Apply the matrix to a multi-vector.
value_type at(size_type batch_id, size_type row, size_type col) const
Returns a single element for a particular batch item.
Definition batch_dense.hpp:166
void scale(const array< value_type > &row_scale, const array< value_type > &col_scale)
Performs in-place row and column scaling for this matrix.
size_type get_cumulative_offset(size_type batch_id) const
Get the cumulative storage size offset.
Definition batch_dense.hpp:120
ValueType & at(size_type batch_id, size_type idx) noexcept
Returns a single element for a particular batch item.
Definition batch_dense.hpp:186
static std::unique_ptr< Dense > create(std::shared_ptr< const Executor > exec, const batch_dim< 2 > &size=batch_dim< 2 >{})
Creates an uninitialized Dense matrix of the specified size.
std::unique_ptr< const unbatch_type > create_const_view_for_item(size_type item_id) const
Creates a mutable view (of gko::matrix::Dense type) of one item of the batch::matrix::Dense<value_typ...
static std::unique_ptr< const Dense > create_const(std::shared_ptr< const Executor > exec, const batch_dim< 2 > &sizes, gko::detail::const_array_view< ValueType > &&values)
Creates a constant (immutable) batch dense matrix from a constant array.
value_type * get_values_for_item(size_type batch_id) noexcept
Returns a pointer to the array of values of the matrix for a specific batch item.
Definition batch_dense.hpp:209
value_type & at(size_type batch_id, size_type row, size_type col)
Returns a single element for a particular batch item.
Definition batch_dense.hpp:157
void add_scaled_identity(ptr_param< const MultiVector< value_type > > alpha, ptr_param< const MultiVector< value_type > > beta)
Performs the operation this = alpha*I + beta*this.
void scale_add(ptr_param< const MultiVector< value_type > > alpha, ptr_param< const batch::matrix::Dense< value_type > > b)
Performs the operation this = alpha*this + b.
size_type get_num_stored_elements() const noexcept
Returns the number of elements explicitly stored in the batch matrix, cumulative across all the batch...
Definition batch_dense.hpp:236
const Dense * apply(ptr_param< const MultiVector< value_type > > alpha, ptr_param< const MultiVector< value_type > > b, ptr_param< const MultiVector< value_type > > beta, ptr_param< MultiVector< value_type > > x) const
Dense * apply(ptr_param< const MultiVector< value_type > > alpha, ptr_param< const MultiVector< value_type > > b, ptr_param< const MultiVector< value_type > > beta, ptr_param< MultiVector< value_type > > x)
Apply the matrix to a multi-vector with a linear combination of the given input vector.
ValueType at(size_type batch_id, size_type idx) const noexcept
Returns a single element for a particular batch item.
Definition batch_dense.hpp:195
const value_type * get_const_values() const noexcept
Returns a pointer to the array of values of the multi-vector.
Definition batch_dense.hpp:141
const value_type * get_const_values_for_item(size_type batch_id) const noexcept
Returns a pointer to the array of values of the matrix for a specific batch item.
Definition batch_dense.hpp:222
Dense is a matrix format which explicitly stores all values of the matrix.
Definition dense.hpp:120
This class is used for function parameters in the place of raw pointers.
Definition utils_helper.hpp:41
The Ginkgo namespace.
Definition abstract_factory.hpp:20
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:264
std::int32_t int32
32-bit signed integral type.
Definition types.hpp:107
typename detail::to_complex_s< T >::type to_complex
Obtain the type which adds the complex of complex/scalar type or the template parameter of class by a...
Definition math.hpp:283
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:90
typename detail::find_precision_impl< T, -step >::type previous_precision
Obtains the previous move type of T in the singly-linked precision corresponding bfloat16/half.
Definition math.hpp:473
typename detail::find_precision_impl< T, step >::type next_precision
Obtains the next move type of T in the singly-linked precision corresponding bfloat16/half.
Definition math.hpp:466
STL namespace.
A type representing the dimensions of a multidimensional batch object.
Definition batch_dim.hpp:27
dim< dimensionality, dimension_type > get_common_size() const
Get the common size of the batch items.
Definition batch_dim.hpp:43
size_type get_num_batch_items() const
Get the number of batch items stored.
Definition batch_dim.hpp:36