Ginkgo  Generated from pipelines/2837190956 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
index_set.hpp
1 // SPDX-FileCopyrightText: 2017 - 2026 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_BASE_INDEX_SET_HPP_
6 #define GKO_PUBLIC_CORE_BASE_INDEX_SET_HPP_
7 
8 
9 #include <algorithm>
10 #include <initializer_list>
11 #include <mutex>
12 #include <vector>
13 
14 #include <ginkgo/core/base/array.hpp>
15 #include <ginkgo/core/base/exception_helpers.hpp>
16 #include <ginkgo/core/base/executor.hpp>
17 #include <ginkgo/core/base/types.hpp>
18 #include <ginkgo/core/base/utils.hpp>
19 
20 
21 namespace gko {
22 
23 
54 template <typename IndexType = int32>
55 class index_set {
56 public:
60  using index_type = IndexType;
61 
67  explicit index_set(std::shared_ptr<const Executor> exec) noexcept
68  : exec_(std::move(exec)),
69  index_space_size_{0},
70  num_stored_indices_{0},
71  subsets_begin_{array<index_type>(exec_)},
72  subsets_end_{array<index_type>(exec_)},
73  superset_cumulative_indices_{array<index_type>(exec_)}
74  {}
75 
85  explicit index_set(std::shared_ptr<const gko::Executor> exec,
86  std::initializer_list<IndexType> init_list,
87  const bool is_sorted = false)
88  : exec_(std::move(exec)),
89  index_space_size_(init_list.size() > 0
90  ? *(std::max_element(std::begin(init_list),
91  std::end(init_list))) +
92  1
93  : 0),
94  num_stored_indices_{static_cast<IndexType>(init_list.size())}
95  {
96  GKO_ASSERT(index_space_size_ > 0);
97  this->populate_subsets(
98  array<IndexType>(this->get_executor(), init_list), is_sorted);
99  }
100 
111  explicit index_set(std::shared_ptr<const gko::Executor> exec,
112  const index_type size,
113  const gko::array<index_type>& indices,
114  const bool is_sorted = false)
115  : exec_(std::move(exec)), index_space_size_(size)
116  {
117  GKO_ASSERT(index_space_size_ >= indices.get_size());
118  this->populate_subsets(indices, is_sorted);
119  }
120 
127  index_set(std::shared_ptr<const Executor> exec, const index_set& other)
128  : index_set(exec)
129  {
130  *this = other;
131  }
132 
138  index_set(const index_set& other) : index_set(other.get_executor(), other)
139  {}
140 
147  index_set(std::shared_ptr<const Executor> exec, index_set&& other)
148  : index_set(exec)
149  {
150  *this = std::move(other);
151  }
152 
159  : index_set(other.get_executor(), std::move(other))
160  {}
161 
173  {
174  if (&other == this) {
175  return *this;
176  }
177  this->index_space_size_ = other.index_space_size_;
178  this->num_stored_indices_ = other.num_stored_indices_;
179  this->subsets_begin_ = other.subsets_begin_;
180  this->subsets_end_ = other.subsets_end_;
181  this->superset_cumulative_indices_ = other.superset_cumulative_indices_;
182 
183  return *this;
184  }
185 
197  {
198  if (&other == this) {
199  return *this;
200  }
201  this->index_space_size_ = std::exchange(other.index_space_size_, 0);
202  this->num_stored_indices_ = std::exchange(other.num_stored_indices_, 0);
203  this->subsets_begin_ = std::move(other.subsets_begin_);
204  this->subsets_end_ = std::move(other.subsets_end_);
205  this->superset_cumulative_indices_ =
206  std::move(other.superset_cumulative_indices_);
207 
208  return *this;
209  }
210 
218  void clear() noexcept
219  {
220  this->index_space_size_ = 0;
221  this->num_stored_indices_ = 0;
222  this->subsets_begin_.clear();
223  this->subsets_end_.clear();
224  this->superset_cumulative_indices_.clear();
225  }
226 
232  std::shared_ptr<const Executor> get_executor() const { return this->exec_; }
233 
239  index_type get_size() const { return this->index_space_size_; }
240 
246  bool is_contiguous() const { return (this->get_num_subsets() <= 1); }
247 
253  index_type get_num_elems() const { return this->num_stored_indices_; };
254 
274  index_type get_global_index(index_type local_index) const;
275 
296  index_type get_local_index(index_type global_index) const;
297 
313  const array<index_type>& local_indices,
314  const bool is_sorted = false) const;
315 
329  const array<index_type>& global_indices,
330  const bool is_sorted = false) const;
331 
339 
350  array<bool> contains(const array<index_type>& global_indices,
351  const bool is_sorted = false) const;
352 
363  bool contains(const index_type global_index) const;
364 
371  {
372  return this->subsets_begin_.get_size();
373  }
374 
381  {
382  return this->subsets_begin_.get_const_data();
383  }
384 
391  {
392  return this->subsets_end_.get_const_data();
393  }
394 
403  {
404  return this->superset_cumulative_indices_.get_const_data();
405  }
406 
407 private:
408  void populate_subsets(const gko::array<index_type>& indices,
409  const bool is_sorted);
410 
411  std::shared_ptr<const Executor> exec_;
412  index_type index_space_size_;
413  index_type num_stored_indices_;
414  gko::array<index_type> subsets_begin_;
415  gko::array<index_type> subsets_end_;
416  gko::array<index_type> superset_cumulative_indices_;
417 };
418 
419 
420 } // namespace gko
421 
422 
423 #endif // GKO_PUBLIC_CORE_BASE_INDEX_SET_HPP_
gko::index_set::index_set
index_set(const index_set &other)
Creates a copy of the input index_set.
Definition: index_set.hpp:138
gko::index_set::is_contiguous
bool is_contiguous() const
Returns if the index set is contiguous.
Definition: index_set.hpp:246
gko::index_set::operator=
index_set & operator=(index_set &&other)
Moves data from another index_set.
Definition: index_set.hpp:196
gko::index_set::index_set
index_set(std::shared_ptr< const gko::Executor > exec, std::initializer_list< IndexType > init_list, const bool is_sorted=false)
Creates an index set on the specified executor from the initializer list.
Definition: index_set.hpp:85
gko::index_set::index_type
IndexType index_type
The type of elements stored in the index set.
Definition: index_set.hpp:60
gko::index_set::index_set
index_set(std::shared_ptr< const Executor > exec, index_set &&other)
Moves the input index_set to a different executor.
Definition: index_set.hpp:147
gko::index_set::get_local_index
index_type get_local_index(index_type global_index) const
Return the local index given a global index.
gko::index_set::contains
array< bool > contains(const array< index_type > &global_indices, const bool is_sorted=false) const
Checks if the individual global indeices exist in the index set.
gko::index_set::get_subsets_end
const index_type * get_subsets_end() const
Returns a pointer to the end indices of the subsets.
Definition: index_set.hpp:390
gko::index_set::map_global_to_local
array< index_type > map_global_to_local(const array< index_type > &global_indices, const bool is_sorted=false) const
This is an array version of the scalar function above.
gko::index_set::get_subsets_begin
const index_type * get_subsets_begin() const
Returns a pointer to the beginning indices of the subsets.
Definition: index_set.hpp:380
gko::index_set::index_set
index_set(index_set &&other)
Moves the input index_set.
Definition: index_set.hpp:158
gko::index_set::get_superset_indices
const index_type * get_superset_indices() const
Returns a pointer to the cumulative indices of the superset of the subsets.
Definition: index_set.hpp:402
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:19
gko::array< IndexType >
gko::index_set::index_set
index_set(std::shared_ptr< const gko::Executor > exec, const index_type size, const gko::array< index_type > &indices, const bool is_sorted=false)
Creates an index set on the specified executor and the given size.
Definition: index_set.hpp:111
gko::index_set::get_num_subsets
index_type get_num_subsets() const
Returns the number of subsets stored in the index set.
Definition: index_set.hpp:370
gko::index_set::index_set
index_set(std::shared_ptr< const Executor > exec, const index_set &other)
Creates a copy of the input index_set on a different executor.
Definition: index_set.hpp:127
gko::index_set
An index set class represents an ordered set of intervals.
Definition: index_set.hpp:55
gko::index_set::get_num_elems
index_type get_num_elems() const
Return the actual number of indices stored in the index set.
Definition: index_set.hpp:253
gko::index_set::map_local_to_global
array< index_type > map_local_to_global(const array< index_type > &local_indices, const bool is_sorted=false) const
This is an array version of the scalar function above.
gko::index_set::clear
void clear() noexcept
Deallocates all data used by the index_set.
Definition: index_set.hpp:218
gko::index_set::to_global_indices
array< index_type > to_global_indices() const
This function allows the user obtain a decompressed global_indices array from the indices stored in t...
gko::index_set::operator=
index_set & operator=(const index_set &other)
Copies data from another index_set.
Definition: index_set.hpp:172
gko::array::clear
void clear() noexcept
Deallocates all data used by the array.
Definition: array.hpp:603
gko::index_set::get_executor
std::shared_ptr< const Executor > get_executor() const
Returns the executor of the index_set.
Definition: index_set.hpp:232
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:695
gko::array::get_size
size_type get_size() const noexcept
Returns the number of elements in the array.
Definition: array.hpp:669
gko::index_set::get_size
index_type get_size() const
Returns the size of the index set space.
Definition: index_set.hpp:239
gko::index_set::get_global_index
index_type get_global_index(index_type local_index) const
Return the global index given a local index.
gko::index_set::index_set
index_set(std::shared_ptr< const Executor > exec) noexcept
Creates an empty index_set tied to the specified Executor.
Definition: index_set.hpp:67