Ginkgo  Generated from pipelines/1330831941 branch based on master. Ginkgo version 1.8.0
A numerical linear algebra library targeting many-core architectures
permutation.hpp
1 // SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_MATRIX_PERMUTATION_HPP_
6 #define GKO_PUBLIC_CORE_MATRIX_PERMUTATION_HPP_
7 
8 
9 #include <algorithm>
10 #include <memory>
11 #include <numeric>
12 #include <vector>
13 
14 
15 #include <ginkgo/core/base/array.hpp>
16 #include <ginkgo/core/base/exception.hpp>
17 #include <ginkgo/core/base/exception_helpers.hpp>
18 #include <ginkgo/core/base/executor.hpp>
19 #include <ginkgo/core/base/lin_op.hpp>
20 #include <ginkgo/core/base/types.hpp>
21 #include <ginkgo/core/base/utils.hpp>
22 
23 
24 namespace gko {
25 namespace matrix {
26 
27 
43 enum class permute_mode : unsigned {
45  none = 0b000u,
47  rows = 0b001u,
49  columns = 0b010u,
54  symmetric = 0b011u,
56  inverse = 0b100u,
61  inverse_rows = 0b101u,
66  inverse_columns = 0b110u,
71  inverse_symmetric = 0b111u
72 };
73 
74 
77 
78 
81 
82 
85 
86 
88 std::ostream& operator<<(std::ostream& stream, permute_mode mode);
89 
90 
91 using mask_type = gko::uint64;
92 
93 static constexpr mask_type row_permute = mask_type{1};
94 GKO_DEPRECATED("permute mask is no longer supported")
95 static constexpr mask_type column_permute = mask_type{1 << 2};
96 GKO_DEPRECATED("permute mask is no longer supported")
97 static constexpr mask_type inverse_permute = mask_type{1 << 3};
98 
111 template <typename IndexType = int32>
112 class Permutation : public EnableLinOp<Permutation<IndexType>>,
113  public WritableToMatrixData<default_precision, IndexType> {
115 
116 public:
117  // value_type is only available to enable the usage of gko::write
118  using value_type = default_precision;
119  using index_type = IndexType;
120 
126  index_type* get_permutation() noexcept { return permutation_.get_data(); }
127 
135  const index_type* get_const_permutation() const noexcept
136  {
137  return permutation_.get_const_data();
138  }
139 
147  GKO_DEPRECATED("use get_size()[0] instead")
148  size_type get_permutation_size() const noexcept;
149 
150  GKO_DEPRECATED("permute mask is no longer supported")
151  mask_type get_permute_mask() const;
152 
153  GKO_DEPRECATED("permute mask is no longer supported")
154  void set_permute_mask(mask_type permute_mask);
155 
162  std::unique_ptr<Permutation> compute_inverse() const;
163 
175  std::unique_ptr<Permutation> compose(
176  ptr_param<const Permutation> other) const;
177 
178  void write(gko::matrix_data<value_type, index_type>& data) const override;
179 
187  static std::unique_ptr<Permutation> create(
188  std::shared_ptr<const Executor> exec, size_type size = 0);
189 
205  static std::unique_ptr<Permutation> create(
206  std::shared_ptr<const Executor> exec,
207  array<IndexType> permutation_indices);
208 
209  GKO_DEPRECATED(
210  "dim<2> is no longer supported as a dimension parameter, use size_type "
211  "instead")
212  static std::unique_ptr<Permutation> create(
213  std::shared_ptr<const Executor> exec, const dim<2>& size);
214 
215  GKO_DEPRECATED("permute mask is no longer supported")
216  static std::unique_ptr<Permutation> create(
217  std::shared_ptr<const Executor> exec, const dim<2>& size,
218  const mask_type& enabled_permute);
219 
220  GKO_DEPRECATED("use the overload without dimensions")
221  static std::unique_ptr<Permutation> create(
222  std::shared_ptr<const Executor> exec, const dim<2>& size,
223  array<IndexType> permutation_indices);
224 
225  GKO_DEPRECATED("permute mask is no longer supported")
226  static std::unique_ptr<Permutation> create(
227  std::shared_ptr<const Executor> exec, const dim<2>& size,
228  array<index_type> permutation_indices,
229  const mask_type& enabled_permute);
230 
242  GKO_DEPRECATED("use create_const without size and permute mask")
243  static std::unique_ptr<const Permutation> create_const(
244  std::shared_ptr<const Executor> exec, size_type size,
245  gko::detail::const_array_view<IndexType>&& perm_idxs,
246  mask_type enabled_permute = row_permute);
258  static std::unique_ptr<const Permutation> create_const(
259  std::shared_ptr<const Executor> exec,
260  gko::detail::const_array_view<IndexType>&& perm_idxs);
261 
262 protected:
263  Permutation(std::shared_ptr<const Executor> exec, size_type = 0);
264 
265  Permutation(std::shared_ptr<const Executor> exec,
266  array<IndexType> permutation_indices);
267 
268  void apply_impl(const LinOp* in, LinOp* out) const override;
269 
270  void apply_impl(const LinOp*, const LinOp* in, const LinOp*,
271  LinOp* out) const override;
272 
273 private:
274  array<index_type> permutation_;
275 };
276 
277 
278 } // namespace matrix
279 } // namespace gko
280 
281 
282 #endif // GKO_PUBLIC_CORE_MATRIX_PERMUTATION_HPP_
gko::matrix::Permutation::write
void write(gko::matrix_data< value_type, index_type > &data) const override
Writes a matrix to a matrix_data structure.
gko::matrix::Permutation::get_permutation
index_type * get_permutation() noexcept
Returns a pointer to the array of permutation.
Definition: permutation.hpp:126
gko::matrix::permute_mode::rows
The rows will be permuted.
gko::LinOp
Definition: lin_op.hpp:118
gko::matrix::permute_mode::columns
The columns will be permuted.
gko::matrix::operator<<
std::ostream & operator<<(std::ostream &stream, permute_mode mode)
Prints a permutation mode.
gko::matrix::operator^
permute_mode operator^(permute_mode a, permute_mode b)
Computes the symmetric difference of two permutation modes.
gko::matrix::permute_mode::inverse_columns
The columns will be permuted using the inverse permutation.
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:108
gko::matrix::Permutation
Permutation is a matrix format that represents a permutation matrix, i.e.
Definition: permutation.hpp:112
gko::matrix::permute_mode::none
Neither rows nor columns will be permuted.
gko::matrix::Permutation::get_const_permutation
const index_type * get_const_permutation() const noexcept
Returns a pointer to the array of permutation.
Definition: permutation.hpp:135
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::matrix::operator|
permute_mode operator|(permute_mode a, permute_mode b)
Combines two permutation modes.
gko::array
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition: array.hpp:27
gko::dim
A type representing the dimensions of a multidimensional object.
Definition: dim.hpp:27
gko::matrix_data
This structure is used as an intermediate data type to store a sparse matrix.
Definition: matrix_data.hpp:127
gko::as
std::decay_t< T > * as(U *obj)
Performs polymorphic type conversion.
Definition: utils_helper.hpp:309
gko::matrix::permute_mode::inverse_symmetric
The rows and columns will be permuted using the inverse permutation.
gko::ptr_param
This class is used for function parameters in the place of raw pointers.
Definition: utils_helper.hpp:43
gko::matrix::operator&
permute_mode operator&(permute_mode a, permute_mode b)
Computes the intersection of two permutation modes.
gko::matrix::permute_mode::inverse
The permutation will be inverted before being applied.
gko::array::get_data
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
gko::matrix::Permutation::create_const
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.
gko::WritableToMatrixData
A LinOp implementing this interface can write its data to a matrix_data structure.
Definition: lin_op.hpp:661
gko::matrix::permute_mode::symmetric
The rows and columns will be permuted.
gko::matrix::Permutation::compute_inverse
std::unique_ptr< Permutation > compute_inverse() const
Returns the inverse permutation.
gko::matrix::Permutation::create
static std::unique_ptr< Permutation > create(std::shared_ptr< const Executor > exec, size_type size=0)
Creates an uninitialized Permutation arrays on the specified executor.
gko::stop::mode
mode
The mode for the residual norm criterion.
Definition: residual_norm.hpp:37
gko::matrix::Permutation::get_permutation_size
size_type get_permutation_size() const noexcept
Returns the number of elements explicitly stored in the permutation array.
gko::default_precision
double default_precision
Precision used if no precision is explicitly specified.
Definition: types.hpp:193
gko::matrix::Permutation::compose
std::unique_ptr< Permutation > compose(ptr_param< const Permutation > other) const
Composes this permutation with another permutation.
gko::Executor
The first step in using the Ginkgo library consists of creating an executor.
Definition: executor.hpp:616
gko::array::get_const_data
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
gko::matrix::permute_mode
permute_mode
Specifies how a permutation will be applied to a matrix.
Definition: permutation.hpp:43
gko::uint64
std::uint64_t uint64
64-bit unsigned integral type.
Definition: types.hpp:154
gko::EnableLinOp
The EnableLinOp mixin can be used to provide sensible default implementations of the majority of the ...
Definition: lin_op.hpp:878
gko::EnablePolymorphicObject
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition: polymorphic_object.hpp:662
gko::matrix::permute_mode::inverse_rows
The rows will be permuted using the inverse permutation.