Ginkgo Generated from branch based on master. Ginkgo version 1.8.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
batch_ell.hpp
1// SPDX-FileCopyrightText: 2017 - 2024 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
13#include <ginkgo/core/base/array.hpp>
14#include <ginkgo/core/base/batch_lin_op.hpp>
15#include <ginkgo/core/base/batch_multi_vector.hpp>
16#include <ginkgo/core/base/executor.hpp>
17#include <ginkgo/core/base/mtx_io.hpp>
18#include <ginkgo/core/base/range_accessors.hpp>
19#include <ginkgo/core/base/types.hpp>
20#include <ginkgo/core/base/utils.hpp>
21#include <ginkgo/core/base/utils_helper.hpp>
22#include <ginkgo/core/matrix/ell.hpp>
23
24
25namespace gko {
26namespace batch {
27namespace matrix {
28
29
52template <typename ValueType = default_precision, typename IndexType = int32>
53class Ell final
54 : public EnableBatchLinOp<Ell<ValueType, IndexType>>,
55 public ConvertibleTo<Ell<next_precision<ValueType>, IndexType>> {
57 friend class Ell<to_complex<ValueType>, IndexType>;
58 friend class Ell<next_precision<ValueType>, IndexType>;
59 static_assert(std::is_same<IndexType, int32>::value,
60 "IndexType must be a 32 bit integer");
61
62public:
63 using EnableBatchLinOp<Ell>::convert_to;
64 using EnableBatchLinOp<Ell>::move_to;
65
66 using value_type = ValueType;
67 using index_type = IndexType;
69 using absolute_type = remove_complex<Ell>;
70 using complex_type = to_complex<Ell>;
71
72 void convert_to(
73 Ell<next_precision<ValueType>, IndexType>* result) const override;
74
75 void move_to(Ell<next_precision<ValueType>, IndexType>* result) override;
76
87 std::unique_ptr<unbatch_type> create_view_for_item(size_type item_id);
88
92 std::unique_ptr<const unbatch_type> create_const_view_for_item(
93 size_type item_id) const;
94
100 value_type* get_values() noexcept { return values_.get_data(); }
101
109 const value_type* get_const_values() const noexcept
110 {
111 return values_.get_const_data();
112 }
113
119 index_type* get_col_idxs() noexcept { return col_idxs_.get_data(); }
120
128 const index_type* get_const_col_idxs() const noexcept
129 {
130 return col_idxs_.get_const_data();
131 }
132
139 index_type get_num_stored_elements_per_row() const noexcept
140 {
141 return num_elems_per_row_;
142 }
143
152 {
153 return values_.get_size();
154 }
155
162 {
163 return this->get_num_stored_elements() / this->get_num_batch_items();
164 }
165
174 index_type* get_col_idxs_for_item(size_type batch_id) noexcept
175 {
176 GKO_ASSERT(batch_id < this->get_num_batch_items());
177 return col_idxs_.get_data();
178 }
179
188 size_type batch_id) const noexcept
189 {
190 GKO_ASSERT(batch_id < this->get_num_batch_items());
191 return col_idxs_.get_const_data();
192 }
193
202 value_type* get_values_for_item(size_type batch_id) noexcept
203 {
204 GKO_ASSERT(batch_id < this->get_num_batch_items());
205 return values_.get_data() +
206 batch_id * this->get_num_elements_per_item();
207 }
208
216 const value_type* get_const_values_for_item(
217 size_type batch_id) const noexcept
218 {
219 GKO_ASSERT(batch_id < this->get_num_batch_items());
220 return values_.get_const_data() +
221 batch_id * this->get_num_elements_per_item();
222 }
223
233 static std::unique_ptr<Ell> create(
234 std::shared_ptr<const Executor> exec,
235 const batch_dim<2>& size = batch_dim<2>{},
236 const IndexType num_elems_per_row = 0);
237
254 static std::unique_ptr<Ell> create(std::shared_ptr<const Executor> exec,
255 const batch_dim<2>& size,
256 const IndexType num_elems_per_row,
257 array<value_type> values,
258 array<index_type> col_idxs);
259
265 template <typename InputValueType, typename ColIndexType>
266 GKO_DEPRECATED(
267 "explicitly construct the gko::array arguments instead of passing "
268 "initializer lists")
269 static std::unique_ptr<Ell> create(
270 std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
271 const IndexType num_elems_per_row,
272 std::initializer_list<InputValueType> values,
273 std::initializer_list<ColIndexType> col_idxs)
274 {
275 return create(exec, size, num_elems_per_row,
276 array<value_type>{exec, std::move(values)},
277 array<index_type>{exec, std::move(col_idxs)});
278 }
279
296 static std::unique_ptr<const Ell> create_const(
297 std::shared_ptr<const Executor> exec, const batch_dim<2>& sizes,
298 const index_type num_elems_per_row,
299 gko::detail::const_array_view<value_type>&& values,
300 gko::detail::const_array_view<index_type>&& col_idxs);
301
311
326
332
342
349 void scale(const array<value_type>& row_scale,
350 const array<value_type>& col_scale);
351
364
365private:
366 size_type compute_num_elems(const batch_dim<2>& size,
367 IndexType num_elems_per_row)
368 {
369 return size.get_num_batch_items() * size.get_common_size()[0] *
370 num_elems_per_row;
371 }
372
373 Ell(std::shared_ptr<const Executor> exec,
374 const batch_dim<2>& size = batch_dim<2>{},
375 const IndexType num_elems_per_row = 0);
376
377 Ell(std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
378 const IndexType num_elems_per_row, array<value_type> values,
379 array<index_type> col_idxs);
380
381 void apply_impl(const MultiVector<value_type>* b,
382 MultiVector<value_type>* x) const;
383
384 void apply_impl(const MultiVector<value_type>* alpha,
386 const MultiVector<value_type>* beta,
387 MultiVector<value_type>* x) const;
388
389 index_type num_elems_per_row_;
390 array<value_type> values_;
391 array<index_type> col_idxs_;
392};
393
394
395} // namespace matrix
396} // namespace batch
397} // namespace gko
398
399
400#endif // GKO_PUBLIC_CORE_MATRIX_BATCH_ELL_HPP_
ConvertibleTo interface is used to mark that the implementer can be converted to the object of Result...
Definition polymorphic_object.hpp:471
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:663
The first step in using the Ginkgo library consists of creating an executor.
Definition executor.hpp:616
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition logger.hpp:25
value_type * get_data() noexcept
Returns a pointer to the block of memory used to store the elements of the array.
Definition array.hpp:674
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:683
size_type get_size() const noexcept
Returns the number of elements in the array.
Definition array.hpp:657
Definition batch_lin_op.hpp:60
The EnableBatchLinOp mixin can be used to provide sensible default implementations of the majority of...
Definition batch_lin_op.hpp:253
MultiVector stores multiple vectors in a batched fashion and is useful for batched operations.
Definition logger.hpp:41
Ell is a sparse matrix format that stores the same number of nonzeros in each row,...
Definition batch_ell.hpp:55
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:187
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:139
value_type * get_values() noexcept
Returns a pointer to the array of values of the matrix.
Definition batch_ell.hpp:100
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:128
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:161
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:109
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:151
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:119
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:174
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:216
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:202
ELL is a matrix format where stride with explicit zeros is used such that all rows have the same numb...
Definition ell.hpp:60
This class is used for function parameters in the place of raw pointers.
Definition utils_helper.hpp:43
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:326
typename detail::next_precision_impl< T >::type next_precision
Obtains the next type in the singly-linked precision list.
Definition math.hpp:462
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:345
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:86
A type representing the dimensions of a multidimensional batch object.
Definition batch_dim.hpp:28
dim< dimensionality, dimension_type > get_common_size() const
Get the common size of the batch items.
Definition batch_dim.hpp:44
size_type get_num_batch_items() const
Get the number of batch items stored.
Definition batch_dim.hpp:37