Ginkgo: ginkgo/core/matrix/batch_csr.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_csr.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_CSR_HPP_
6#define GKO_PUBLIC_CORE_MATRIX_BATCH_CSR_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/types.hpp>
18#include <ginkgo/core/base/utils.hpp>
19#include <ginkgo/core/matrix/csr.hpp>
20
21
22namespace gko {
23namespace batch {
24namespace matrix {
25
26
46template <typename ValueType = default_precision, typename IndexType = int32>
47 class Csr final
48 : public EnableBatchLinOp<Csr<ValueType, IndexType>>,
49#if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
50 public ConvertibleTo<Csr<next_precision<ValueType, 2>, IndexType>>,
51#endif
52#if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
53 public ConvertibleTo<Csr<next_precision<ValueType, 3>, IndexType>>,
54#endif
55 public ConvertibleTo<Csr<next_precision<ValueType>, IndexType>> {
56 friend class EnablePolymorphicObject<Csr, BatchLinOp>;
57 friend class Csr<to_complex<ValueType>, IndexType>;
58 friend class Csr<previous_precision<ValueType>, IndexType>;
59 static_assert(std::is_same<IndexType, int32>::value,
60 "IndexType must be a 32 bit integer");
61 GKO_ASSERT_SUPPORTED_VALUE_TYPE;
62
63public:
64 using EnableBatchLinOp<Csr>::convert_to;
65 using EnableBatchLinOp<Csr>::move_to;
66
67 using value_type = ValueType;
68 using index_type = IndexType;
69 using unbatch_type = gko::matrix::Csr<value_type, index_type>;
70 using absolute_type = remove_complex<Csr>;
71 using complex_type = to_complex<Csr>;
72
73 void convert_to(
74 Csr<next_precision<ValueType>, IndexType>* result) const override;
75
76 void move_to(Csr<next_precision<ValueType>, IndexType>* result) override;
77
78#if GINKGO_ENABLE_HALF || GINKGO_ENABLE_BFLOAT16
79 friend class Csr<previous_precision<ValueType, 2>, IndexType>;
80 using ConvertibleTo<
81 Csr<next_precision<ValueType, 2>, IndexType>>::convert_to;
82 using ConvertibleTo<Csr<next_precision<ValueType, 2>, IndexType>>::move_to;
83
84 void convert_to(
85 Csr<next_precision<ValueType, 2>, IndexType>* result) const override;
86
87 void move_to(Csr<next_precision<ValueType, 2>, IndexType>* result) override;
88#endif
89
90#if GINKGO_ENABLE_HALF && GINKGO_ENABLE_BFLOAT16
91 friend class Csr<previous_precision<ValueType, 3>, IndexType>;
92 using ConvertibleTo<
93 Csr<next_precision<ValueType, 3>, IndexType>>::convert_to;
94 using ConvertibleTo<Csr<next_precision<ValueType, 3>, IndexType>>::move_to;
95
96 void convert_to(
97 Csr<next_precision<ValueType, 3>, IndexType>* result) const override;
98
99 void move_to(Csr<next_precision<ValueType, 3>, IndexType>* result) override;
100#endif
101
112 std::unique_ptr<unbatch_type> create_view_for_item(size_type item_id);
113
117 std::unique_ptr<const unbatch_type> create_const_view_for_item(
118 size_type item_id) const;
119
125 value_type* get_values() noexcept { return values_.get_data(); }
126
134 const value_type* get_const_values() const noexcept
135 {
136 return values_.get_const_data();
137 }
138
144 index_type* get_col_idxs() noexcept { return col_idxs_.get_data(); }
145
153 const index_type* get_const_col_idxs() const noexcept
154 {
155 return col_idxs_.get_const_data();
156 }
157
163 index_type* get_row_ptrs() noexcept { return row_ptrs_.get_data(); }
164
172 const index_type* get_const_row_ptrs() const noexcept
173 {
174 return row_ptrs_.get_const_data();
175 }
176
184 size_type get_num_stored_elements() const noexcept
185 {
186 return values_.get_size();
187 }
188
194 size_type get_num_elements_per_item() const noexcept
195 {
196 return this->get_num_stored_elements() / this->get_num_batch_items();
197 }
198
207 value_type* get_values_for_item(size_type batch_id) noexcept
208 {
209 GKO_ASSERT(batch_id < this->get_num_batch_items());
210 GKO_ASSERT(values_.get_size() >=
211 batch_id * this->get_num_elements_per_item());
212 return values_.get_data() +
213 batch_id * this->get_num_elements_per_item();
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 GKO_ASSERT(values_.get_size() >=
228 batch_id * this->get_num_elements_per_item());
229 return values_.get_const_data() +
230 batch_id * this->get_num_elements_per_item();
231 }
232
248 static std::unique_ptr<Csr> create(
249 std::shared_ptr<const Executor> exec,
250 const batch_dim<2>& size = batch_dim<2>{},
251 size_type num_nonzeros_per_item = {});
252
269 static std::unique_ptr<Csr> create(std::shared_ptr<const Executor> exec,
270 const batch_dim<2>& size,
271 array<value_type> values,
272 array<index_type> col_idxs,
273 array<index_type> row_ptrs);
274
280 template <typename InputValueType, typename ColIndexType,
281 typename RowPtrType>
282 GKO_DEPRECATED(
283 "explicitly construct the gko::array arguments instead of passing "
284 "initializer lists")
285 static std::unique_ptr<Csr> create(
286 std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
287 std::initializer_list<InputValueType> values,
288 std::initializer_list<ColIndexType> col_idxs,
289 std::initializer_list<RowPtrType> row_ptrs)
290 {
291 return create(exec, size, array<value_type>{exec, std::move(values)},
292 array<index_type>{exec, std::move(col_idxs)},
293 array<index_type>{exec, std::move(row_ptrs)});
294 }
295
314 static std::unique_ptr<const Csr> create_const(
315 std::shared_ptr<const Executor> exec, const batch_dim<2>& sizes,
316 gko::detail::const_array_view<value_type>&& values,
317 gko::detail::const_array_view<index_type>&& col_idxs,
318 gko::detail::const_array_view<index_type>&& row_ptrs);
319
327 Csr* apply(ptr_param<const MultiVector<value_type>> b,
328 ptr_param<MultiVector<value_type>> x);
329
340 Csr* apply(ptr_param<const MultiVector<value_type>> alpha,
341 ptr_param<const MultiVector<value_type>> b,
342 ptr_param<const MultiVector<value_type>> beta,
343 ptr_param<MultiVector<value_type>> x);
344
348 const Csr* apply(ptr_param<const MultiVector<value_type>> b,
349 ptr_param<MultiVector<value_type>> x) const;
350
356 const Csr* apply(ptr_param<const MultiVector<value_type>> alpha,
357 ptr_param<const MultiVector<value_type>> b,
358 ptr_param<const MultiVector<value_type>> beta,
359 ptr_param<MultiVector<value_type>> x) const;
360
367 void scale(const array<value_type>& row_scale,
368 const array<value_type>& col_scale);
369
380 void add_scaled_identity(ptr_param<const MultiVector<value_type>> alpha,
381 ptr_param<const MultiVector<value_type>> beta);
382
383private:
384 Csr(std::shared_ptr<const Executor> exec,
385 const batch_dim<2>& size = batch_dim<2>{},
386 size_type num_nonzeros_per_item = {});
387
388 Csr(std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
389 array<value_type> values, array<index_type> col_idxs,
390 array<index_type> row_ptrs)
391 : EnableBatchLinOp<Csr>(exec, size),
392 values_{exec, std::move(values)},
393 col_idxs_{exec, std::move(col_idxs)},
394 row_ptrs_{exec, std::move(row_ptrs)}
395 {
396 // Ensure that the value and col_idxs arrays have the correct size
397 auto max_num_elems = this->get_common_size()[0] *
398 this->get_common_size()[1] *
399 this->get_num_batch_items();
400 GKO_ASSERT(values_.get_size() <= max_num_elems);
401 GKO_ASSERT_EQ(row_ptrs_.get_size(), this->get_common_size()[0] + 1);
402 GKO_ASSERT_EQ(this->get_num_elements_per_item(), col_idxs_.get_size());
403 }
404
405 void apply_impl(const MultiVector<value_type>* b,
406 MultiVector<value_type>* x) const;
407
408 void apply_impl(const MultiVector<value_type>* alpha,
409 const MultiVector<value_type>* b,
410 const MultiVector<value_type>* beta,
411 MultiVector<value_type>* x) const;
412
413 array<value_type> values_;
414 array<index_type> col_idxs_;
415 array<index_type> row_ptrs_;
416};
417
418
419} // namespace matrix
420} // namespace batch
421} // namespace gko
422
423
424#endif // GKO_PUBLIC_CORE_MATRIX_BATCH_CSR_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::array::get_size
size_type get_size() const noexcept
Returns the number of elements in the array.
Definition array.hpp:670
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::Csr::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::Csr::apply
const Csr * apply(ptr_param< const MultiVector< value_type > > b, ptr_param< MultiVector< value_type > > x) const
gko::batch::matrix::Csr::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_csr.hpp:207
gko::batch::matrix::Csr::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 matrix::Csr type) of one item of the batch::matrix::Csr<value_type> object...
gko::batch::matrix::Csr::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_csr.hpp:184
gko::batch::matrix::Csr::apply
Csr * apply(ptr_param< const MultiVector< value_type > > b, ptr_param< MultiVector< value_type > > x)
Apply the matrix to a multi-vector.
gko::batch::matrix::Csr::get_values
value_type * get_values() noexcept
Returns a pointer to the array of values of the matrix.
Definition batch_csr.hpp:125
gko::batch::matrix::Csr::apply
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
gko::batch::matrix::Csr::create
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.
gko::batch::matrix::Csr::get_const_row_ptrs
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:172
gko::batch::matrix::Csr::create_const
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.
gko::batch::matrix::Csr::apply
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.
gko::batch::matrix::Csr::get_col_idxs
index_type * get_col_idxs() noexcept
Returns a pointer to the array of column indices of the matrix.
Definition batch_csr.hpp:144
gko::batch::matrix::Csr::get_const_col_idxs
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:153
gko::batch::matrix::Csr::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_csr.hpp:194
gko::batch::matrix::Csr::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_csr.hpp:223
gko::batch::matrix::Csr::create
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.
gko::batch::matrix::Csr::get_const_values
const value_type * get_const_values() const noexcept
Returns a pointer to the array of values of the matrix.
Definition batch_csr.hpp:134
gko::batch::matrix::Csr::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::Csr::get_row_ptrs
index_type * get_row_ptrs() noexcept
Returns a pointer to the array of row pointers of the matrix.
Definition batch_csr.hpp:163
gko::batch::matrix::Csr::create_view_for_item
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...
gko::matrix::Csr
CSR is a matrix format which stores only the nonzero coefficients by compressing each row of the matr...
Definition csr.hpp:126
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::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