Ginkgo: ginkgo/core/matrix/batch_dense.hpp Source File

| Ginkgo Generated from branch based on main. Ginkgo version 1.11.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>
48 class 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 GKO_ASSERT_SUPPORTED_VALUE_TYPE;
60
61public:
62 using EnableBatchLinOp<Dense>::convert_to;
63 using EnableBatchLinOp<Dense>::move_to;
64
65 using value_type = ValueType;
66 using index_type = int32;
67 using transposed_type = Dense<ValueType>;
68 using unbatch_type = gko::matrix::Dense<ValueType>;
69 using absolute_type = remove_complex<Dense>;
70 using complex_type = to_complex<Dense>;
71
72 void convert_to(Dense<next_precision<ValueType>>* result) const override;
73
74 void move_to(Dense<next_precision<ValueType>>* result) override;
75
76#if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
77 friend class Dense<previous_precision<ValueType, 2>>;
78 using ConvertibleTo<Dense<next_precision<ValueType, 2>>>::convert_to;
79 using ConvertibleTo<Dense<next_precision<ValueType, 2>>>::move_to;
80
81 void convert_to(Dense<next_precision<ValueType, 2>>* result) const override;
82
83 void move_to(Dense<next_precision<ValueType, 2>>* result) override;
84#endif
85
86#if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
87 friend class Dense<previous_precision<ValueType, 3>>;
88 using ConvertibleTo<Dense<next_precision<ValueType, 3>>>::convert_to;
89 using ConvertibleTo<Dense<next_precision<ValueType, 3>>>::move_to;
90
91 void convert_to(Dense<next_precision<ValueType, 3>>* result) const override;
92
93 void move_to(Dense<next_precision<ValueType, 3>>* result) override;
94#endif
95
106 std::unique_ptr<unbatch_type> create_view_for_item(size_type item_id);
107
111 std::unique_ptr<const unbatch_type> create_const_view_for_item(
112 size_type item_id) const;
113
121 size_type get_cumulative_offset(size_type batch_id) const
122 {
123 GKO_ASSERT(batch_id < this->get_num_batch_items());
124 return batch_id * this->get_common_size()[0] *
125 this->get_common_size()[1];
126 }
127
133 value_type* get_values() noexcept { return values_.get_data(); }
134
142 const value_type* get_const_values() const noexcept
143 {
144 return values_.get_const_data();
145 }
146
158 value_type& at(size_type batch_id, size_type row, size_type col)
159 {
160 GKO_ASSERT(batch_id < this->get_num_batch_items());
161 return values_.get_data()[linearize_index(batch_id, row, col)];
162 }
163
167 value_type at(size_type batch_id, size_type row, size_type col) const
168 {
169 GKO_ASSERT(batch_id < this->get_num_batch_items());
170 return values_.get_const_data()[linearize_index(batch_id, row, col)];
171 }
172
187 ValueType& at(size_type batch_id, size_type idx) noexcept
188 {
189 GKO_ASSERT(batch_id < this->get_num_batch_items());
190 return values_.get_data()[linearize_index(batch_id, idx)];
191 }
192
196 ValueType at(size_type batch_id, size_type idx) const noexcept
197 {
198 GKO_ASSERT(batch_id < this->get_num_batch_items());
199 return values_.get_const_data()[linearize_index(batch_id, idx)];
200 }
201
210 value_type* get_values_for_item(size_type batch_id) noexcept
211 {
212 GKO_ASSERT(batch_id < this->get_num_batch_items());
213 return values_.get_data() + this->get_cumulative_offset(batch_id);
214 }
215
223 const value_type* get_const_values_for_item(
224 size_type batch_id) const noexcept
225 {
226 GKO_ASSERT(batch_id < this->get_num_batch_items());
227 return values_.get_const_data() + this->get_cumulative_offset(batch_id);
228 }
229
237 size_type get_num_stored_elements() const noexcept
238 {
239 return values_.get_size();
240 }
241
247 size_type get_num_elements_per_item() const noexcept
248 {
249 return this->get_num_stored_elements() / this->get_num_batch_items();
250 }
251
260 static std::unique_ptr<Dense> create(
261 std::shared_ptr<const Executor> exec,
262 const batch_dim<2>& size = batch_dim<2>{});
263
278 static std::unique_ptr<Dense> create(std::shared_ptr<const Executor> exec,
279 const batch_dim<2>& size,
280 array<value_type> values);
281
286 template <typename InputValueType>
287 GKO_DEPRECATED(
288 "explicitly construct the gko::array argument instead of passing an"
289 "initializer list")
290 static std::unique_ptr<Dense> create(
291 std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
292 std::initializer_list<InputValueType> values)
293 {
294 return create(exec, size, array<value_type>{exec, std::move(values)});
295 }
296
311 static std::unique_ptr<const Dense> create_const(
312 std::shared_ptr<const Executor> exec, const batch_dim<2>& sizes,
313 gko::detail::const_array_view<ValueType>&& values);
314
322 Dense* apply(ptr_param<const MultiVector<value_type>> b,
323 ptr_param<MultiVector<value_type>> x);
324
335 Dense* apply(ptr_param<const MultiVector<value_type>> alpha,
336 ptr_param<const MultiVector<value_type>> b,
337 ptr_param<const MultiVector<value_type>> beta,
338 ptr_param<MultiVector<value_type>> x);
339
343 const Dense* apply(ptr_param<const MultiVector<value_type>> b,
344 ptr_param<MultiVector<value_type>> x) const;
345
351 const Dense* apply(ptr_param<const MultiVector<value_type>> alpha,
352 ptr_param<const MultiVector<value_type>> b,
353 ptr_param<const MultiVector<value_type>> beta,
354 ptr_param<MultiVector<value_type>> x) const;
355
362 void scale(const array<value_type>& row_scale,
363 const array<value_type>& col_scale);
364
373 void scale_add(ptr_param<const MultiVector<value_type>> alpha,
374 ptr_param<const batch::matrix::Dense<value_type>> b);
375
384 void add_scaled_identity(ptr_param<const MultiVector<value_type>> alpha,
385 ptr_param<const MultiVector<value_type>> beta);
386
387private:
388 inline size_type compute_num_elems(const batch_dim<2>& size)
389 {
390 return size.get_num_batch_items() * size.get_common_size()[0] *
391 size.get_common_size()[1];
392 }
393
394 Dense(std::shared_ptr<const Executor> exec,
395 const batch_dim<2>& size = batch_dim<2>{});
396
397 Dense(std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
398 array<value_type> values);
399
400 void apply_impl(const MultiVector<value_type>* b,
401 MultiVector<value_type>* x) const;
402
403 void apply_impl(const MultiVector<value_type>* alpha,
404 const MultiVector<value_type>* b,
405 const MultiVector<value_type>* beta,
406 MultiVector<value_type>* x) const;
407
408 size_type linearize_index(size_type batch, size_type row,
409 size_type col) const noexcept
410 {
411 return this->get_cumulative_offset(batch) +
412 row * this->get_size().get_common_size()[1] + col;
413 }
414
415 size_type linearize_index(size_type batch, size_type idx) const noexcept
416 {
417 return linearize_index(batch, idx / this->get_common_size()[1],
418 idx % this->get_common_size()[1]);
419 }
420
421 array<value_type> values_;
422};
423
424
425} // namespace matrix
426} // namespace batch
427} // namespace gko
428
429
430#endif // GKO_PUBLIC_CORE_MATRIX_BATCH_DENSE_HPP_
gko::EnablePolymorphicObject
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:668
gko::Executor
The first step in using the Ginkgo library consists of creating an executor.
Definition executor.hpp:615
gko::array
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:166
gko::batch::BatchLinOp
Definition batch_lin_op.hpp:59
gko::batch::EnableBatchLinOp
The EnableBatchLinOp mixin can be used to provide sensible default implementations of the majority of...
Definition batch_lin_op.hpp:252
gko::batch::MultiVector
MultiVector stores multiple vectors in a batched fashion and is useful for batched operations.
Definition batch_multi_vector.hpp:61
gko::batch::matrix::Dense
Dense is a batch matrix format which explicitly stores all values of the matrix in each of the batche...
Definition batch_dense.hpp:55
gko::batch::matrix::Dense::create
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.
gko::batch::matrix::Dense::get_num_elements_per_item
size_type get_num_elements_per_item() const noexcept
Returns the number of stored elements in each batch item.
Definition batch_dense.hpp:247
gko::batch::matrix::Dense::create_view_for_item
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...
gko::batch::matrix::Dense::apply
const Dense * apply(ptr_param< const MultiVector< value_type > > b, ptr_param< MultiVector< value_type > > x) const
gko::batch::matrix::Dense::get_values
value_type * get_values() noexcept
Returns a pointer to the array of values of the multi-vector.
Definition batch_dense.hpp:133
gko::batch::matrix::Dense::apply
Dense * apply(ptr_param< const MultiVector< value_type > > b, ptr_param< MultiVector< value_type > > x)
Apply the matrix to a multi-vector.
gko::batch::matrix::Dense::at
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:167
gko::batch::matrix::Dense::scale
void scale(const array< value_type > &row_scale, const array< value_type > &col_scale)
Performs in-place row and column scaling for this matrix.
gko::batch::matrix::Dense::get_cumulative_offset
size_type get_cumulative_offset(size_type batch_id) const
Get the cumulative storage size offset.
Definition batch_dense.hpp:121
gko::batch::matrix::Dense::at
ValueType & at(size_type batch_id, size_type idx) noexcept
Returns a single element for a particular batch item.
Definition batch_dense.hpp:187
gko::batch::matrix::Dense::create
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.
gko::batch::matrix::Dense::create_const_view_for_item
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...
gko::batch::matrix::Dense::create_const
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.
gko::batch::matrix::Dense::get_values_for_item
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:210
gko::batch::matrix::Dense::at
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:158
gko::batch::matrix::Dense::add_scaled_identity
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.
gko::batch::matrix::Dense::scale_add
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.
gko::batch::matrix::Dense::get_num_stored_elements
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:237
gko::batch::matrix::Dense::apply
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
gko::batch::matrix::Dense::apply
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.
gko::batch::matrix::Dense::at
ValueType at(size_type batch_id, size_type idx) const noexcept
Returns a single element for a particular batch item.
Definition batch_dense.hpp:196
gko::batch::matrix::Dense::get_const_values
const value_type * get_const_values() const noexcept
Returns a pointer to the array of values of the multi-vector.
Definition batch_dense.hpp:142
gko::batch::matrix::Dense::get_const_values_for_item
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:223
gko::matrix::Dense
Dense is a matrix format which explicitly stores all values of the matrix.
Definition dense.hpp:120
gko::ptr_param
This class is used for function parameters in the place of raw pointers.
Definition utils_helper.hpp:41
gko
The Ginkgo namespace.
Definition abstract_factory.hpp:20
gko::remove_complex
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
gko::int32
std::int32_t int32
32-bit signed integral type.
Definition types.hpp:107
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:283
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:90
gko::previous_precision
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
gko::next_precision
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
std
STL namespace.
gko::batch_dim
A type representing the dimensions of a multidimensional batch object.
Definition batch_dim.hpp:27
gko::batch_dim::get_common_size
dim< dimensionality, dimension_type > get_common_size() const
Get the common size of the batch items.
Definition batch_dim.hpp:43
gko::batch_dim::get_num_batch_items
size_type get_num_batch_items() const
Get the number of batch items stored.
Definition batch_dim.hpp:36