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
permutation.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_PERMUTATION_HPP_
34#define GKO_PUBLIC_CORE_MATRIX_PERMUTATION_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
71enum class permute_mode : unsigned {
73 none = 0b000u,
75 rows = 0b001u,
77 columns = 0b010u,
82 symmetric = 0b011u,
84 inverse = 0b100u,
89 inverse_rows = 0b101u,
94 inverse_columns = 0b110u,
99 inverse_symmetric = 0b111u
100};
101
102
105
106
109
110
113
114
116std::ostream& operator<<(std::ostream& stream, permute_mode mode);
117
118
119using mask_type = gko::uint64;
120
121static constexpr mask_type row_permute = mask_type{1};
122GKO_DEPRECATED("permute mask is no longer supported")
123static constexpr mask_type column_permute = mask_type{1 << 2};
124GKO_DEPRECATED("permute mask is no longer supported")
125static constexpr mask_type inverse_permute = mask_type{1 << 3};
126
139template <typename IndexType = int32>
140class Permutation : public EnableLinOp<Permutation<IndexType>>,
141 public EnableCreateMethod<Permutation<IndexType>>,
142 public WritableToMatrixData<default_precision, IndexType> {
143 friend class EnableCreateMethod<Permutation>;
145
146public:
147 // value_type is only available to enable the usage of gko::write
148 using value_type = default_precision;
149 using index_type = IndexType;
150
156 index_type* get_permutation() noexcept { return permutation_.get_data(); }
157
166 {
167 return permutation_.get_const_data();
168 }
169
177 GKO_DEPRECATED("use get_size()[0] instead")
179
180 GKO_DEPRECATED("permute mask is no longer supported")
181 mask_type get_permute_mask() const;
182
183 GKO_DEPRECATED("permute mask is no longer supported")
184 void set_permute_mask(mask_type permute_mask);
185
193
205 std::unique_ptr<Permutation> compose(
207
208 void write(gko::matrix_data<value_type, index_type>& data) const override;
209
221 GKO_DEPRECATED("use create_const without size and permute mask")
223 std::shared_ptr<const Executor> exec, size_type size,
224 gko::detail::const_array_view<IndexType>&& perm_idxs,
225 mask_type enabled_permute = row_permute);
238 std::shared_ptr<const Executor> exec,
239 gko::detail::const_array_view<IndexType>&& perm_idxs);
240
247 Permutation(std::shared_ptr<const Executor> exec, size_type = 0);
248
262 Permutation(std::shared_ptr<const Executor> exec,
263 array<IndexType> permutation_indices);
264
265 GKO_DEPRECATED(
267 "instead")
268 Permutation(std::shared_ptr<const Executor> exec, const dim<2>& size);
269
270 GKO_DEPRECATED("permute mask is no longer supported")
271 Permutation(std::shared_ptr<const Executor> exec, const dim<2>& size,
272 const mask_type& enabled_permute);
273
275 GKO_DEPRECATED("use the overload without dimensions")
276 Permutation(std::shared_ptr<const Executor> exec, const dim<2>& size,
278 : Permutation{exec, array<IndexType>{exec, std::forward<IndicesArray>(
280 {
281 GKO_ASSERT_EQ(size[0], permutation_.get_num_elems());
282 GKO_ASSERT_IS_SQUARE_MATRIX(size);
283 }
284
285 template <typename IndicesArray>
286 GKO_DEPRECATED("permute mask is no longer supported")
287 Permutation(std::shared_ptr<const Executor> exec, const dim<2>& size,
289 const mask_type& enabled_permute)
290 : Permutation{std::move(exec),
291 array<IndexType>{exec, std::forward<IndicesArray>(
293 {
294 GKO_ASSERT_EQ(enabled_permute, row_permute);
295 GKO_ASSERT_EQ(size[0], permutation_.get_num_elems());
296 GKO_ASSERT_IS_SQUARE_MATRIX(size);
297 }
298
299 void apply_impl(const LinOp* in, LinOp* out) const override;
300
301 void apply_impl(const LinOp*, const LinOp* in, const LinOp*,
302 LinOp* out) const override;
303
304private:
305 array<index_type> permutation_;
306};
307
308
309} // namespace matrix
310} // namespace gko
311
312
313#endif // GKO_PUBLIC_CORE_MATRIX_PERMUTATION_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
The first step in using the Ginkgo library consists of creating an executor.
Definition executor.hpp:644
Definition lin_op.hpp:146
A LinOp implementing this interface can write its data to a matrix_data structure.
Definition lin_op.hpp:689
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
Permutation is a matrix format that represents a permutation matrix, i.e.
Definition permutation.hpp:142
size_type get_permutation_size() const noexcept
Returns the number of elements explicitly stored in the permutation array.
std::unique_ptr< Permutation > compose(ptr_param< const Permutation > other) const
Composes this permutation with another permutation.
index_type * get_permutation() noexcept
Returns a pointer to the array of permutation.
Definition permutation.hpp:156
static std::unique_ptr< const Permutation > create_const(std::shared_ptr< const Executor > exec, size_type size, gko::detail::const_array_view< IndexType > &&perm_idxs, mask_type enabled_permute=row_permute)
Creates a constant (immutable) Permutation matrix from a constant array.
const index_type * get_const_permutation() const noexcept
Returns a pointer to the array of permutation.
Definition permutation.hpp:165
std::unique_ptr< Permutation > compute_inverse() const
Returns the inverse permutation.
This class is used for function parameters in the place of raw pointers.
Definition utils_helper.hpp:71
permute_mode operator|(permute_mode a, permute_mode b)
Combines two permutation modes.
permute_mode operator&(permute_mode a, permute_mode b)
Computes the intersection of two permutation modes.
permute_mode operator^(permute_mode a, permute_mode b)
Computes the symmetric difference of two permutation modes.
std::ostream & operator<<(std::ostream &stream, permute_mode mode)
Prints a permutation mode.
permute_mode
Specifies how a permutation will be applied to a matrix.
Definition permutation.hpp:71
@ none
Neither rows nor columns will be permuted.
@ columns
The columns will be permuted.
@ inverse
The permutation will be inverted before being applied.
@ inverse_columns
The columns will be permuted using the inverse permutation.
@ inverse_symmetric
The rows and columns will be permuted using the inverse permutation.
@ rows
The rows will be permuted.
@ inverse_rows
The rows will be permuted using the inverse permutation.
@ symmetric
The rows and columns will be permuted.
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
std::uint64_t uint64
64-bit unsigned integral type.
Definition types.hpp:166
double default_precision
Precision used if no precision is explicitly specified.
Definition types.hpp:205
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:120
std::decay_t< T > * as(U *obj)
Performs polymorphic type conversion.
Definition utils_helper.hpp:337
A type representing the dimensions of a multidimensional object.
Definition dim.hpp:55
This structure is used as an intermediate data type to store a sparse matrix.
Definition matrix_data.hpp:155