Ginkgo  Generated from pipelines/1330831941 branch based on master. Ginkgo version 1.8.0
A numerical linear algebra library targeting many-core architectures
index_set.hpp
1 // SPDX-FileCopyrightText: 2017 - 2024 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 
15 #include <ginkgo/core/base/array.hpp>
16 #include <ginkgo/core/base/exception_helpers.hpp>
17 #include <ginkgo/core/base/executor.hpp>
18 #include <ginkgo/core/base/types.hpp>
19 #include <ginkgo/core/base/utils.hpp>
20 
21 
22 namespace gko {
23 
24 
56 template <typename IndexType = int32>
57 class index_set {
58 public:
62  using index_type = IndexType;
63 
69  explicit index_set(std::shared_ptr<const Executor> exec) noexcept
70  : exec_(std::move(exec)),
71  index_space_size_{0},
72  num_stored_indices_{0},
73  subsets_begin_{array<index_type>(exec_)},
74  subsets_end_{array<index_type>(exec_)},
75  superset_cumulative_indices_{array<index_type>(exec_)}
76  {}
77 
87  explicit index_set(std::shared_ptr<const gko::Executor> exec,
88  std::initializer_list<IndexType> init_list,
89  const bool is_sorted = false)
90  : exec_(std::move(exec)),
91  index_space_size_(init_list.size() > 0
92  ? *(std::max_element(std::begin(init_list),
93  std::end(init_list))) +
94  1
95  : 0),
96  num_stored_indices_{static_cast<IndexType>(init_list.size())}
97  {
98  GKO_ASSERT(index_space_size_ > 0);
99  this->populate_subsets(
100  array<IndexType>(this->get_executor(), init_list), is_sorted);
101  }
102 
113  explicit index_set(std::shared_ptr<const gko::Executor> exec,
114  const index_type size,
115  const gko::array<index_type>& indices,
116  const bool is_sorted = false)
117  : exec_(std::move(exec)), index_space_size_(size)
118  {
119  GKO_ASSERT(index_space_size_ >= indices.get_size());
120  this->populate_subsets(indices, is_sorted);
121  }
122 
129  index_set(std::shared_ptr<const Executor> exec, const index_set& other)
130  : index_set(exec)
131  {
132  *this = other;
133  }
134 
140  index_set(const index_set& other) : index_set(other.get_executor(), other)
141  {}
142 
149  index_set(std::shared_ptr<const Executor> exec, index_set&& other)
150  : index_set(exec)
151  {
152  *this = std::move(other);
153  }
154 
161  : index_set(other.get_executor(), std::move(other))
162  {}
163 
175  {
176  if (&other == this) {
177  return *this;
178  }
179  this->index_space_size_ = other.index_space_size_;
180  this->num_stored_indices_ = other.num_stored_indices_;
181  this->subsets_begin_ = other.subsets_begin_;
182  this->subsets_end_ = other.subsets_end_;
183  this->superset_cumulative_indices_ = other.superset_cumulative_indices_;
184 
185  return *this;
186  }
187 
199  {
200  if (&other == this) {
201  return *this;
202  }
203  this->index_space_size_ = std::exchange(other.index_space_size_, 0);
204  this->num_stored_indices_ = std::exchange(other.num_stored_indices_, 0);
205  this->subsets_begin_ = std::move(other.subsets_begin_);
206  this->subsets_end_ = std::move(other.subsets_end_);
207  this->superset_cumulative_indices_ =
208  std::move(other.superset_cumulative_indices_);
209 
210  return *this;
211  }
212 
220  void clear() noexcept
221  {
222  this->index_space_size_ = 0;
223  this->num_stored_indices_ = 0;
224  this->subsets_begin_.clear();
225  this->subsets_end_.clear();
226  this->superset_cumulative_indices_.clear();
227  }
228 
234  std::shared_ptr<const Executor> get_executor() const { return this->exec_; }
235 
241  index_type get_size() const { return this->index_space_size_; }
242 
248  bool is_contiguous() const { return (this->get_num_subsets() <= 1); }
249 
255  index_type get_num_elems() const { return this->num_stored_indices_; };
256 
276  index_type get_global_index(index_type local_index) const;
277 
298  index_type get_local_index(index_type global_index) const;
299 
315  const array<index_type>& local_indices,
316  const bool is_sorted = false) const;
317 
331  const array<index_type>& global_indices,
332  const bool is_sorted = false) const;
333 
341 
352  array<bool> contains(const array<index_type>& global_indices,
353  const bool is_sorted = false) const;
354 
365  bool contains(const index_type global_index) const;
366 
373  {
374  return this->subsets_begin_.get_size();
375  }
376 
383  {
384  return this->subsets_begin_.get_const_data();
385  }
386 
393  {
394  return this->subsets_end_.get_const_data();
395  }
396 
405  {
406  return this->superset_cumulative_indices_.get_const_data();
407  }
408 
409 private:
410  void populate_subsets(const gko::array<index_type>& indices,
411  const bool is_sorted);
412 
413  std::shared_ptr<const Executor> exec_;
414  index_type index_space_size_;
415  index_type num_stored_indices_;
416  gko::array<index_type> subsets_begin_;
417  gko::array<index_type> subsets_end_;
418  gko::array<index_type> superset_cumulative_indices_;
419 };
420 
421 
422 } // namespace gko
423 
424 
425 #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:140
gko::index_set::is_contiguous
bool is_contiguous() const
Returns if the index set is contiguous.
Definition: index_set.hpp:248
gko::index_set::operator=
index_set & operator=(index_set &&other)
Moves data from another index_set.
Definition: index_set.hpp:198
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:87
gko::index_set::index_type
IndexType index_type
The type of elements stored in the index set.
Definition: index_set.hpp:62
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:149
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:392
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:382
gko::index_set::index_set
index_set(index_set &&other)
Moves the input index_set.
Definition: index_set.hpp:160
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:404
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
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:113
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:372
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:129
gko::index_set
An index set class represents an ordered set of intervals.
Definition: index_set.hpp:57
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:255
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:220
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:174
gko::array::clear
void clear() noexcept
Deallocates all data used by the array.
Definition: array.hpp:605
gko::index_set::get_executor
std::shared_ptr< const Executor > get_executor() const
Returns the executor of the index_set.
Definition: index_set.hpp:234
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::array::get_size
size_type get_size() const noexcept
Returns the number of elements in the array.
Definition: array.hpp:657
gko::index_set::get_size
index_type get_size() const
Returns the size of the index set space.
Definition: index_set.hpp:241
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:69