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
row_gatherer.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_ROW_GATHERER_HPP_
34#define GKO_PUBLIC_CORE_MATRIX_ROW_GATHERER_HPP_
35
36
37#include <algorithm>
38#include <memory>
39#include <numeric>
40#include <vector>
41
42
43#include <ginkgo/core/base/array.hpp>
44#include <ginkgo/core/base/exception.hpp>
45#include <ginkgo/core/base/exception_helpers.hpp>
46#include <ginkgo/core/base/executor.hpp>
47#include <ginkgo/core/base/lin_op.hpp>
48#include <ginkgo/core/base/types.hpp>
49#include <ginkgo/core/base/utils.hpp>
50
51
52namespace gko {
53namespace matrix {
54
55
71template <typename IndexType = int32>
72class RowGatherer : public EnableLinOp<RowGatherer<IndexType>>,
73 public EnableCreateMethod<RowGatherer<IndexType>> {
74 friend class EnableCreateMethod<RowGatherer>;
76
77public:
78 using index_type = IndexType;
79
85 index_type* get_row_idxs() noexcept { return row_idxs_.get_data(); }
86
94 const index_type* get_const_row_idxs() const noexcept
95 {
96 return row_idxs_.get_const_data();
97 }
98
109 static std::unique_ptr<const RowGatherer> create_const(
110 std::shared_ptr<const Executor> exec, const dim<2>& size,
111 gko::detail::const_array_view<IndexType>&& row_idxs)
112 {
113 // cast const-ness away, but return a const object afterwards,
114 // so we can ensure that no modifications take place.
115 return std::unique_ptr<const RowGatherer>(new RowGatherer{
116 exec, size, gko::detail::array_const_cast(std::move(row_idxs))});
117 }
118
119protected:
125 RowGatherer(std::shared_ptr<const Executor> exec)
126 : RowGatherer(std::move(exec), dim<2>{})
127 {}
128
135 RowGatherer(std::shared_ptr<const Executor> exec, const dim<2>& size)
136 : EnableLinOp<RowGatherer>(exec, size), row_idxs_(exec, size[0])
137 {}
138
153 template <typename IndicesArray>
154 RowGatherer(std::shared_ptr<const Executor> exec, const dim<2>& size,
155 IndicesArray&& row_idxs)
156 : EnableLinOp<RowGatherer>(exec, size),
157 row_idxs_{exec, std::forward<IndicesArray>(row_idxs)}
158 {
159 GKO_ASSERT_EQ(size[0], row_idxs_.get_num_elems());
160 }
161
162 void apply_impl(const LinOp* in, LinOp* out) const override;
163
164 void apply_impl(const LinOp* alpha, const LinOp* in, const LinOp* beta,
165 LinOp* out) const override;
166
167private:
168 gko::array<index_type> row_idxs_;
169};
170
171
172} // namespace matrix
173} // namespace gko
174
175
176#endif // GKO_PUBLIC_CORE_MATRIX_ROW_GATHERER_HPP_
This mixin implements a static create() method on ConcreteType that dynamically allocates the memory,...
Definition polymorphic_object.hpp:776
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition lin_op.hpp:908
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:691
Definition lin_op.hpp:146
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
RowGatherer is a matrix "format" which stores the gather indices arrays which can be used to gather r...
Definition row_gatherer.hpp:73
static std::unique_ptr< const RowGatherer > create_const(std::shared_ptr< const Executor > exec, const dim< 2 > &size, gko::detail::const_array_view< IndexType > &&row_idxs)
Creates a constant (immutable) RowGatherer matrix from a constant array.
Definition row_gatherer.hpp:109
index_type * get_row_idxs() noexcept
Returns a pointer to the row index array for gathering.
Definition row_gatherer.hpp:85
const index_type * get_const_row_idxs() const noexcept
Returns a pointer to the row index array for gathering.
Definition row_gatherer.hpp:94
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
A type representing the dimensions of a multidimensional object.
Definition dim.hpp:55