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_csr.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_CSR_HPP_
6#define GKO_PUBLIC_CORE_MATRIX_BATCH_CSR_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/types.hpp>
19#include <ginkgo/core/base/utils.hpp>
20#include <ginkgo/core/matrix/csr.hpp>
21
22
23namespace gko {
24namespace batch {
25namespace matrix {
26
27
47template <typename ValueType = default_precision, typename IndexType = int32>
48class Csr final
49 : public EnableBatchLinOp<Csr<ValueType, IndexType>>,
50 public ConvertibleTo<Csr<next_precision<ValueType>, IndexType>> {
52 friend class Csr<to_complex<ValueType>, IndexType>;
53 friend class Csr<next_precision<ValueType>, IndexType>;
54 static_assert(std::is_same<IndexType, int32>::value,
55 "IndexType must be a 32 bit integer");
56
57public:
58 using EnableBatchLinOp<Csr>::convert_to;
59 using EnableBatchLinOp<Csr>::move_to;
60
61 using value_type = ValueType;
62 using index_type = IndexType;
64 using absolute_type = remove_complex<Csr>;
65 using complex_type = to_complex<Csr>;
66
67 void convert_to(
68 Csr<next_precision<ValueType>, IndexType>* result) const override;
69
70 void move_to(Csr<next_precision<ValueType>, IndexType>* result) override;
71
82 std::unique_ptr<unbatch_type> create_view_for_item(size_type item_id);
83
87 std::unique_ptr<const unbatch_type> create_const_view_for_item(
88 size_type item_id) const;
89
95 value_type* get_values() noexcept { return values_.get_data(); }
96
104 const value_type* get_const_values() const noexcept
105 {
106 return values_.get_const_data();
107 }
108
114 index_type* get_col_idxs() noexcept { return col_idxs_.get_data(); }
115
123 const index_type* get_const_col_idxs() const noexcept
124 {
125 return col_idxs_.get_const_data();
126 }
127
133 index_type* get_row_ptrs() noexcept { return row_ptrs_.get_data(); }
134
142 const index_type* get_const_row_ptrs() const noexcept
143 {
144 return row_ptrs_.get_const_data();
145 }
146
155 {
156 return values_.get_size();
157 }
158
165 {
166 return this->get_num_stored_elements() / this->get_num_batch_items();
167 }
168
177 value_type* get_values_for_item(size_type batch_id) noexcept
178 {
179 GKO_ASSERT(batch_id < this->get_num_batch_items());
180 GKO_ASSERT(values_.get_size() >=
181 batch_id * this->get_num_elements_per_item());
182 return values_.get_data() +
183 batch_id * this->get_num_elements_per_item();
184 }
185
193 const value_type* get_const_values_for_item(
194 size_type batch_id) const noexcept
195 {
196 GKO_ASSERT(batch_id < this->get_num_batch_items());
197 GKO_ASSERT(values_.get_size() >=
198 batch_id * this->get_num_elements_per_item());
199 return values_.get_const_data() +
200 batch_id * this->get_num_elements_per_item();
201 }
202
218 static std::unique_ptr<Csr> create(
219 std::shared_ptr<const Executor> exec,
220 const batch_dim<2>& size = batch_dim<2>{},
221 size_type num_nonzeros_per_item = {});
222
239 static std::unique_ptr<Csr> create(std::shared_ptr<const Executor> exec,
240 const batch_dim<2>& size,
241 array<value_type> values,
242 array<index_type> col_idxs,
243 array<index_type> row_ptrs);
244
250 template <typename InputValueType, typename ColIndexType,
251 typename RowPtrType>
252 GKO_DEPRECATED(
253 "explicitly construct the gko::array arguments instead of passing "
254 "initializer lists")
255 static std::unique_ptr<Csr> create(
256 std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
257 std::initializer_list<InputValueType> values,
258 std::initializer_list<ColIndexType> col_idxs,
259 std::initializer_list<RowPtrType> row_ptrs)
260 {
261 return create(exec, size, array<value_type>{exec, std::move(values)},
262 array<index_type>{exec, std::move(col_idxs)},
263 array<index_type>{exec, std::move(row_ptrs)});
264 }
265
284 static std::unique_ptr<const Csr> create_const(
285 std::shared_ptr<const Executor> exec, const batch_dim<2>& sizes,
286 gko::detail::const_array_view<value_type>&& values,
287 gko::detail::const_array_view<index_type>&& col_idxs,
288 gko::detail::const_array_view<index_type>&& row_ptrs);
289
299
314
320
330
337 void scale(const array<value_type>& row_scale,
338 const array<value_type>& col_scale);
339
352
353private:
354 Csr(std::shared_ptr<const Executor> exec,
355 const batch_dim<2>& size = batch_dim<2>{},
356 size_type num_nonzeros_per_item = {});
357
358 Csr(std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
359 array<value_type> values, array<index_type> col_idxs,
360 array<index_type> row_ptrs)
361 : EnableBatchLinOp<Csr>(exec, size),
362 values_{exec, std::move(values)},
363 col_idxs_{exec, std::move(col_idxs)},
364 row_ptrs_{exec, std::move(row_ptrs)}
365 {
366 // Ensure that the value and col_idxs arrays have the correct size
367 auto max_num_elems = this->get_common_size()[0] *
368 this->get_common_size()[1] *
369 this->get_num_batch_items();
370 GKO_ASSERT(values_.get_size() <= max_num_elems);
371 GKO_ASSERT_EQ(row_ptrs_.get_size(), this->get_common_size()[0] + 1);
372 GKO_ASSERT_EQ(this->get_num_elements_per_item(), col_idxs_.get_size());
373 }
374
375 void apply_impl(const MultiVector<value_type>* b,
376 MultiVector<value_type>* x) const;
377
378 void apply_impl(const MultiVector<value_type>* alpha,
379 const MultiVector<value_type>* b,
380 const MultiVector<value_type>* beta,
381 MultiVector<value_type>* x) const;
382
383 array<value_type> values_;
384 array<index_type> col_idxs_;
385 array<index_type> row_ptrs_;
386};
387
388
389} // namespace matrix
390} // namespace batch
391} // namespace gko
392
393
394#endif // GKO_PUBLIC_CORE_MATRIX_BATCH_CSR_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
Csr is a general sparse matrix format that stores the column indices for each nonzero entry and a cum...
Definition batch_csr.hpp:50
void scale(const array< value_type > &row_scale, const array< value_type > &col_scale)
Performs in-place row and column scaling for this matrix.
const Csr * apply(ptr_param< const MultiVector< value_type > > b, ptr_param< MultiVector< value_type > > x) const
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_csr.hpp:177
std::unique_ptr< const unbatch_type > create_const_view_for_item(size_type item_id) const
Creates a mutable view (of matrix::Csr type) of one item of the batch::matrix::Csr<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_csr.hpp:154
Csr * apply(ptr_param< const MultiVector< value_type > > b, ptr_param< MultiVector< value_type > > x)
Apply the matrix to a multi-vector.
value_type * get_values() noexcept
Returns a pointer to the array of values of the matrix.
Definition batch_csr.hpp:95
const Csr * 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
static std::unique_ptr< Csr > create(std::shared_ptr< const Executor > exec, const batch_dim< 2 > &size, array< value_type > values, array< index_type > col_idxs, array< index_type > row_ptrs)
Creates a Csr matrix from an already allocated (and initialized) array.
const index_type * get_const_row_ptrs() const noexcept
Returns a pointer to the array of row pointers of the matrix.
Definition batch_csr.hpp:142
static std::unique_ptr< const Csr > create_const(std::shared_ptr< const Executor > exec, const batch_dim< 2 > &sizes, gko::detail::const_array_view< value_type > &&values, gko::detail::const_array_view< index_type > &&col_idxs, gko::detail::const_array_view< index_type > &&row_ptrs)
Creates a constant (immutable) batch csr matrix from a constant array.
Csr * 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.
index_type * get_col_idxs() noexcept
Returns a pointer to the array of column indices of the matrix.
Definition batch_csr.hpp:114
const index_type * get_const_col_idxs() const noexcept
Returns a pointer to the array of column indices of the matrix.
Definition batch_csr.hpp:123
size_type get_num_elements_per_item() const noexcept
Returns the number of stored elements in each batch item.
Definition batch_csr.hpp:164
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_csr.hpp:193
static std::unique_ptr< Csr > create(std::shared_ptr< const Executor > exec, const batch_dim< 2 > &size=batch_dim< 2 >{}, size_type num_nonzeros_per_item={})
Creates an uninitialized Csr matrix of the specified size.
const value_type * get_const_values() const noexcept
Returns a pointer to the array of values of the matrix.
Definition batch_csr.hpp:104
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.
index_type * get_row_ptrs() noexcept
Returns a pointer to the array of row pointers of the matrix.
Definition batch_csr.hpp:133
std::unique_ptr< unbatch_type > create_view_for_item(size_type item_id)
Creates a mutable view (of matrix::Csr type) of one item of the batch::matrix::Csr<value_type> object...
CSR is a matrix format which stores only the nonzero coefficients by compressing each row of the matr...
Definition sparsity_csr.hpp:22
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