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_ell.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_ELL_HPP_
6#define GKO_PUBLIC_CORE_MATRIX_BATCH_ELL_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/base/utils_helper.hpp>
21#include <ginkgo/core/matrix/ell.hpp>
22
23
24namespace gko {
25namespace batch {
26namespace matrix {
27
28
51template <typename ValueType = default_precision, typename IndexType = int32>
52class Ell final
53 : public EnableBatchLinOp<Ell<ValueType, IndexType>>,
54#if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
55 public ConvertibleTo<Ell<next_precision<ValueType, 2>, IndexType>>,
56#endif
57#if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
58 public ConvertibleTo<Ell<next_precision<ValueType, 3>, IndexType>>,
59#endif
60 public ConvertibleTo<Ell<next_precision<ValueType>, IndexType>> {
61 friend class EnablePolymorphicObject<Ell, BatchLinOp>;
62 friend class Ell<to_complex<ValueType>, IndexType>;
63 friend class Ell<previous_precision<ValueType>, IndexType>;
64 GKO_ASSERT_SUPPORTED_VALUE_TYPE;
65 static_assert(std::is_same<IndexType, int32>::value,
66 "IndexType must be a 32 bit integer");
67
68public:
69 using EnableBatchLinOp<Ell>::convert_to;
70 using EnableBatchLinOp<Ell>::move_to;
71
72 using value_type = ValueType;
73 using index_type = IndexType;
75 using absolute_type = remove_complex<Ell>;
76 using complex_type = to_complex<Ell>;
77
78 void convert_to(
79 Ell<next_precision<ValueType>, IndexType>* result) const override;
80
81 void move_to(Ell<next_precision<ValueType>, IndexType>* result) override;
82
83#if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
84 friend class Ell<previous_precision<ValueType, 2>, IndexType>;
85 using ConvertibleTo<
86 Ell<next_precision<ValueType, 2>, IndexType>>::convert_to;
87 using ConvertibleTo<Ell<next_precision<ValueType, 2>, IndexType>>::move_to;
88
89 void convert_to(
90 Ell<next_precision<ValueType, 2>, IndexType>* result) const override;
91
92 void move_to(Ell<next_precision<ValueType, 2>, IndexType>* result) override;
93#endif
94
95#if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
96 friend class Ell<previous_precision<ValueType, 3>, IndexType>;
97 using ConvertibleTo<
98 Ell<next_precision<ValueType, 3>, IndexType>>::convert_to;
99 using ConvertibleTo<Ell<next_precision<ValueType, 3>, IndexType>>::move_to;
100
101 void convert_to(
102 Ell<next_precision<ValueType, 3>, IndexType>* result) const override;
103
104 void move_to(Ell<next_precision<ValueType, 3>, IndexType>* result) override;
105#endif
106
117 std::unique_ptr<unbatch_type> create_view_for_item(size_type item_id);
118
122 std::unique_ptr<const unbatch_type> create_const_view_for_item(
123 size_type item_id) const;
124
130 value_type* get_values() noexcept { return values_.get_data(); }
131
139 const value_type* get_const_values() const noexcept
140 {
141 return values_.get_const_data();
142 }
143
149 index_type* get_col_idxs() noexcept { return col_idxs_.get_data(); }
150
158 const index_type* get_const_col_idxs() const noexcept
159 {
160 return col_idxs_.get_const_data();
161 }
162
169 index_type get_num_stored_elements_per_row() const noexcept
170 {
171 return num_elems_per_row_;
172 }
173
182 {
183 return values_.get_size();
184 }
185
192 {
193 return this->get_num_stored_elements() / this->get_num_batch_items();
194 }
195
204 index_type* get_col_idxs_for_item(size_type batch_id) noexcept
205 {
206 GKO_ASSERT(batch_id < this->get_num_batch_items());
207 return col_idxs_.get_data();
208 }
209
218 size_type batch_id) const noexcept
219 {
220 GKO_ASSERT(batch_id < this->get_num_batch_items());
221 return col_idxs_.get_const_data();
222 }
223
232 value_type* get_values_for_item(size_type batch_id) noexcept
233 {
234 GKO_ASSERT(batch_id < this->get_num_batch_items());
235 return values_.get_data() +
236 batch_id * this->get_num_elements_per_item();
237 }
238
246 const value_type* get_const_values_for_item(
247 size_type batch_id) const noexcept
248 {
249 GKO_ASSERT(batch_id < this->get_num_batch_items());
250 return values_.get_const_data() +
251 batch_id * this->get_num_elements_per_item();
252 }
253
263 static std::unique_ptr<Ell> create(
264 std::shared_ptr<const Executor> exec,
265 const batch_dim<2>& size = batch_dim<2>{},
266 const IndexType num_elems_per_row = 0);
267
284 static std::unique_ptr<Ell> create(std::shared_ptr<const Executor> exec,
285 const batch_dim<2>& size,
286 const IndexType num_elems_per_row,
287 array<value_type> values,
288 array<index_type> col_idxs);
289
295 template <typename InputValueType, typename ColIndexType>
296 GKO_DEPRECATED(
297 "explicitly construct the gko::array arguments instead of passing "
298 "initializer lists")
299 static std::unique_ptr<Ell> create(
300 std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
301 const IndexType num_elems_per_row,
302 std::initializer_list<InputValueType> values,
303 std::initializer_list<ColIndexType> col_idxs)
304 {
305 return create(exec, size, num_elems_per_row,
306 array<value_type>{exec, std::move(values)},
307 array<index_type>{exec, std::move(col_idxs)});
308 }
309
326 static std::unique_ptr<const Ell> create_const(
327 std::shared_ptr<const Executor> exec, const batch_dim<2>& sizes,
328 const index_type num_elems_per_row,
329 gko::detail::const_array_view<value_type>&& values,
330 gko::detail::const_array_view<index_type>&& col_idxs);
331
341
356
362
368 const Ell* apply(ptr_param<const MultiVector<value_type>> alpha,
372
379 void scale(const array<value_type>& row_scale,
380 const array<value_type>& col_scale);
381
394
395private:
396 size_type compute_num_elems(const batch_dim<2>& size,
397 IndexType num_elems_per_row)
398 {
399 return size.get_num_batch_items() * size.get_common_size()[0] *
400 num_elems_per_row;
401 }
402
403 Ell(std::shared_ptr<const Executor> exec,
404 const batch_dim<2>& size = batch_dim<2>{},
405 const IndexType num_elems_per_row = 0);
406
407 Ell(std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
408 const IndexType num_elems_per_row, array<value_type> values,
409 array<index_type> col_idxs);
410
411 void apply_impl(const MultiVector<value_type>* b,
412 MultiVector<value_type>* x) const;
413
414 void apply_impl(const MultiVector<value_type>* alpha,
416 const MultiVector<value_type>* beta,
417 MultiVector<value_type>* x) const;
418
419 index_type num_elems_per_row_;
420 array<value_type> values_;
421 array<index_type> col_idxs_;
422};
423
424
425} // namespace matrix
426} // namespace batch
427} // namespace gko
428
429
430#endif // GKO_PUBLIC_CORE_MATRIX_BATCH_ELL_HPP_
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
Ell is a sparse matrix format that stores the same number of nonzeros in each row,...
Definition batch_ell.hpp:60
const Ell * apply(ptr_param< const MultiVector< value_type > > b, ptr_param< MultiVector< value_type > > x) const
const index_type * get_const_col_idxs_for_item(size_type batch_id) const noexcept
Returns a pointer to the array of col_idxs of the matrix.
Definition batch_ell.hpp:217
static std::unique_ptr< const Ell > create_const(std::shared_ptr< const Executor > exec, const batch_dim< 2 > &sizes, const index_type num_elems_per_row, gko::detail::const_array_view< value_type > &&values, gko::detail::const_array_view< index_type > &&col_idxs)
Creates a constant (immutable) batch ell matrix from a constant array.
index_type get_num_stored_elements_per_row() const noexcept
Returns the number of elements per row explicitly stored.
Definition batch_ell.hpp:169
value_type * get_values() noexcept
Returns a pointer to the array of values of the matrix.
Definition batch_ell.hpp:130
Ell * apply(ptr_param< const MultiVector< value_type > > b, ptr_param< MultiVector< value_type > > x)
Apply the matrix to a multi-vector.
const index_type * get_const_col_idxs() const noexcept
Returns a pointer to the array of column indices of the matrix.
Definition batch_ell.hpp:158
static std::unique_ptr< Ell > create(std::shared_ptr< const Executor > exec, const batch_dim< 2 > &size, const IndexType num_elems_per_row, array< value_type > values, array< index_type > col_idxs)
Creates a Ell 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_ell.hpp:191
const Ell * 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
const value_type * get_const_values() const noexcept
Returns a pointer to the array of values of the matrix.
Definition batch_ell.hpp:139
std::unique_ptr< const unbatch_type > create_const_view_for_item(size_type item_id) const
Creates a mutable view (of matrix::Ell type) of one item of the batch::matrix::Ell<value_type> object...
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_ell.hpp:181
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.
Ell * 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.
std::unique_ptr< unbatch_type > create_view_for_item(size_type item_id)
Creates a mutable view (of matrix::Ell type) of one item of the batch::matrix::Ell<value_type> object...
static std::unique_ptr< Ell > create(std::shared_ptr< const Executor > exec, const batch_dim< 2 > &size=batch_dim< 2 >{}, const IndexType num_elems_per_row=0)
Creates an uninitialized Ell matrix of the specified size.
index_type * get_col_idxs() noexcept
Returns a pointer to the array of column indices of the matrix.
Definition batch_ell.hpp:149
index_type * get_col_idxs_for_item(size_type batch_id) noexcept
Returns a pointer to the array of col_idxs of the matrix.
Definition batch_ell.hpp:204
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_ell.hpp:246
void scale(const array< value_type > &row_scale, const array< value_type > &col_scale)
Performs in-place row and column scaling for this matrix.
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_ell.hpp:232
ELL is a matrix format where stride with explicit zeros is used such that all rows have the same numb...
Definition ell.hpp:66
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
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