Ginkgo Generated from branch based on master. Ginkgo version 1.7.0
A numerical linear algebra library targeting many-core architectures
Loading...
Searching...
No Matches
batch_ell.hpp
1/*******************************<GINKGO LICENSE>******************************
2Copyright (c) 2017-2023, the Ginkgo authors
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions
7are met:
8
91. Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
11
122. Redistributions in binary form must reproduce the above copyright
13notice, this list of conditions and the following disclaimer in the
14documentation and/or other materials provided with the distribution.
15
163. Neither the name of the copyright holder nor the names of its
17contributors may be used to endorse or promote products derived from
18this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31******************************<GINKGO LICENSE>*******************************/
32
33#ifndef GKO_PUBLIC_CORE_MATRIX_BATCH_ELL_HPP_
34#define GKO_PUBLIC_CORE_MATRIX_BATCH_ELL_HPP_
35
36
37#include <initializer_list>
38#include <vector>
39
40
41#include <ginkgo/core/base/array.hpp>
42#include <ginkgo/core/base/batch_lin_op.hpp>
43#include <ginkgo/core/base/batch_multi_vector.hpp>
44#include <ginkgo/core/base/executor.hpp>
45#include <ginkgo/core/base/mtx_io.hpp>
46#include <ginkgo/core/base/range_accessors.hpp>
47#include <ginkgo/core/base/types.hpp>
48#include <ginkgo/core/base/utils.hpp>
49#include <ginkgo/core/matrix/ell.hpp>
50
51
52namespace gko {
53namespace batch {
54namespace matrix {
55
56
79template <typename ValueType = default_precision, typename IndexType = int32>
81 : public EnableBatchLinOp<Ell<ValueType, IndexType>>,
82 public EnableCreateMethod<Ell<ValueType, IndexType>>,
83 public ConvertibleTo<Ell<next_precision<ValueType>, IndexType>> {
84 friend class EnableCreateMethod<Ell>;
86 friend class Ell<to_complex<ValueType>, IndexType>;
87 friend class Ell<next_precision<ValueType>, IndexType>;
88 static_assert(std::is_same<IndexType, int32>::value,
89 "IndexType must be a 32 bit integer");
90
91public:
92 using EnableBatchLinOp<Ell>::convert_to;
93 using EnableBatchLinOp<Ell>::move_to;
94
95 using value_type = ValueType;
96 using index_type = IndexType;
98 using absolute_type = remove_complex<Ell>;
99 using complex_type = to_complex<Ell>;
100
101 void convert_to(
102 Ell<next_precision<ValueType>, IndexType>* result) const override;
103
104 void move_to(Ell<next_precision<ValueType>, IndexType>* result) override;
105
116 std::unique_ptr<unbatch_type> create_view_for_item(size_type item_id);
117
121 std::unique_ptr<const unbatch_type> create_const_view_for_item(
122 size_type item_id) const;
123
129 value_type* get_values() noexcept { return values_.get_data(); }
130
138 const value_type* get_const_values() const noexcept
139 {
140 return values_.get_const_data();
141 }
142
148 index_type* get_col_idxs() noexcept { return col_idxs_.get_data(); }
149
158 {
159 return col_idxs_.get_const_data();
160 }
161
169 {
170 return num_elems_per_row_;
171 }
172
181 {
182 return values_.get_num_elems();
183 }
184
191 {
192 return this->get_num_stored_elements() / this->get_num_batch_items();
193 }
194
204 {
205 GKO_ASSERT(batch_id < this->get_num_batch_items());
206 return col_idxs_.get_data();
207 }
208
217 size_type batch_id) const noexcept
218 {
219 GKO_ASSERT(batch_id < this->get_num_batch_items());
220 return col_idxs_.get_const_data();
221 }
222
232 {
233 GKO_ASSERT(batch_id < this->get_num_batch_items());
234 return values_.get_data() +
236 }
237
245 const value_type* get_const_values_for_item(
246 size_type batch_id) const noexcept
247 {
248 GKO_ASSERT(batch_id < this->get_num_batch_items());
249 return values_.get_const_data() +
251 }
252
267 static std::unique_ptr<const Ell> create_const(
268 std::shared_ptr<const Executor> exec, const batch_dim<2>& sizes,
269 const index_type num_elems_per_row,
270 gko::detail::const_array_view<value_type>&& values,
271 gko::detail::const_array_view<index_type>&& col_idxs);
272
282
297
303
313
314private:
315 size_type compute_num_elems(const batch_dim<2>& size,
316 IndexType num_elems_per_row)
317 {
318 return size.get_num_batch_items() * size.get_common_size()[0] *
320 }
321
329 Ell(std::shared_ptr<const Executor> exec,
330 const batch_dim<2>& size = batch_dim<2>{},
331 const IndexType num_elems_per_row = 0);
332
349 template <typename ValuesArray, typename IndicesArray>
350 Ell(std::shared_ptr<const Executor> exec, const batch_dim<2>& size,
351 const IndexType num_elems_per_row, ValuesArray&& values,
352 IndicesArray&& col_idxs)
353 : EnableBatchLinOp<Ell>(exec, size),
354 num_elems_per_row_{num_elems_per_row},
355 values_{exec, std::forward<ValuesArray>(values)},
356 col_idxs_{exec, std::forward<IndicesArray>(col_idxs)}
357 {
358 // Ensure that the value and col_idxs arrays have the correct size
359 auto num_elems = this->get_common_size()[0] * num_elems_per_row *
360 this->get_num_batch_items();
361 GKO_ASSERT_EQ(num_elems, values_.get_num_elems());
362 GKO_ASSERT_EQ(this->get_num_elements_per_item(),
363 col_idxs_.get_num_elems());
364 }
365
366 void apply_impl(const MultiVector<value_type>* b,
367 MultiVector<value_type>* x) const;
368
369 void apply_impl(const MultiVector<value_type>* alpha,
371 const MultiVector<value_type>* beta,
372 MultiVector<value_type>* x) const;
373
374 index_type num_elems_per_row_;
375 array<value_type> values_;
376 array<index_type> col_idxs_;
377};
378
379
380} // namespace matrix
381} // namespace batch
382} // namespace gko
383
384
385#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:499
This mixin implements a static create() method on ConcreteType that dynamically allocates the memory,...
Definition polymorphic_object.hpp:776
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:691
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:187
value_type * get_data() noexcept
Returns a pointer to the block of memory used to store the elements of the array.
Definition array.hpp:646
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:655
size_type get_num_elems() const noexcept
Returns the number of elements in the array.
Definition array.hpp:637
Definition batch_lin_op.hpp:88
The EnableBatchLinOp mixin can be used to provide sensible default implementations of the majority of...
Definition batch_lin_op.hpp:281
MultiVector stores multiple vectors in a batched fashion and is useful for batched operations.
Definition batch_multi_vector.hpp:85
Ell is a sparse matrix format that stores the same number of nonzeros in each row,...
Definition batch_ell.hpp:83
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:216
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:168
value_type * get_values() noexcept
Returns a pointer to the array of values of the matrix.
Definition batch_ell.hpp:129
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:157
size_type get_num_elements_per_item() const noexcept
Returns the number of stored elements in each batch item.
Definition batch_ell.hpp:190
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:138
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:180
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...
index_type * get_col_idxs() noexcept
Returns a pointer to the array of column indices of the matrix.
Definition batch_ell.hpp:148
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:203
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:245
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:231
ELL is a matrix format where stride with explicit zeros is used such that all rows have the same numb...
Definition ell.hpp:89
This class is used for function parameters in the place of raw pointers.
Definition utils_helper.hpp:71
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
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:354
typename detail::next_precision_impl< T >::type next_precision
Obtains the next type in the singly-linked precision list.
Definition math.hpp:490
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:373
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:120
A type representing the dimensions of a multidimensional batch object.
Definition batch_dim.hpp:56
dim< dimensionality, dimension_type > get_common_size() const
Get the common size of the batch items.
Definition batch_dim.hpp:72
size_type get_num_batch_items() const
Get the number of batch items stored.
Definition batch_dim.hpp:65