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
index_set.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_BASE_INDEX_SET_HPP_
34#define GKO_PUBLIC_CORE_BASE_INDEX_SET_HPP_
35
36
37#include <algorithm>
38#include <initializer_list>
39#include <mutex>
40#include <vector>
41
42
43#include <ginkgo/core/base/array.hpp>
44#include <ginkgo/core/base/exception_helpers.hpp>
45#include <ginkgo/core/base/executor.hpp>
46#include <ginkgo/core/base/types.hpp>
47#include <ginkgo/core/base/utils.hpp>
48
49
50namespace gko {
51
52
84template <typename IndexType = int32>
85class index_set {
86public:
90 using index_type = IndexType;
91
97 explicit index_set(std::shared_ptr<const Executor> exec) noexcept
98 : exec_(std::move(exec)),
99 index_space_size_{0},
100 num_stored_indices_{0},
101 subsets_begin_{array<index_type>(exec_)},
102 subsets_end_{array<index_type>(exec_)},
103 superset_cumulative_indices_{array<index_type>(exec_)}
104 {}
105
115 explicit index_set(std::shared_ptr<const gko::Executor> exec,
116 std::initializer_list<IndexType> init_list,
117 const bool is_sorted = false)
118 : exec_(std::move(exec)),
119 index_space_size_(init_list.size() > 0
120 ? *(std::max_element(std::begin(init_list),
121 std::end(init_list))) +
122 1
123 : 0),
124 num_stored_indices_{static_cast<IndexType>(init_list.size())}
125 {
126 GKO_ASSERT(index_space_size_ > 0);
127 this->populate_subsets(
128 array<IndexType>(this->get_executor(), init_list), is_sorted);
129 }
130
141 explicit index_set(std::shared_ptr<const gko::Executor> exec,
142 const index_type size,
144 const bool is_sorted = false)
145 : exec_(std::move(exec)), index_space_size_(size)
146 {
147 GKO_ASSERT(index_space_size_ >= indices.get_num_elems());
148 this->populate_subsets(indices, is_sorted);
149 }
150
157 index_set(std::shared_ptr<const Executor> exec, const index_set& other)
158 : index_set(exec)
159 {
160 *this = other;
161 }
162
170
177 index_set(std::shared_ptr<const Executor> exec, index_set&& other)
178 : index_set(exec)
179 {
180 *this = std::move(other);
181 }
182
191
203 {
204 if (&other == this) {
205 return *this;
206 }
207 this->index_space_size_ = other.index_space_size_;
208 this->num_stored_indices_ = other.num_stored_indices_;
209 this->subsets_begin_ = other.subsets_begin_;
210 this->subsets_end_ = other.subsets_end_;
211 this->superset_cumulative_indices_ = other.superset_cumulative_indices_;
212
213 return *this;
214 }
215
227 {
228 if (&other == this) {
229 return *this;
230 }
231 this->index_space_size_ = std::exchange(other.index_space_size_, 0);
232 this->num_stored_indices_ = std::exchange(other.num_stored_indices_, 0);
233 this->subsets_begin_ = std::move(other.subsets_begin_);
234 this->subsets_end_ = std::move(other.subsets_end_);
235 this->superset_cumulative_indices_ =
236 std::move(other.superset_cumulative_indices_);
237
238 return *this;
239 }
240
249 {
250 this->index_space_size_ = 0;
251 this->num_stored_indices_ = 0;
252 this->subsets_begin_.clear();
253 this->subsets_end_.clear();
254 this->superset_cumulative_indices_.clear();
255 }
256
262 std::shared_ptr<const Executor> get_executor() const { return this->exec_; }
263
269 index_type get_size() const { return this->index_space_size_; }
270
276 bool is_contiguous() const { return (this->get_num_subsets() <= 1); }
277
283 index_type get_num_elems() const { return this->num_stored_indices_; };
284
305
327
344 const bool is_sorted = false) const;
345
360 const bool is_sorted = false) const;
361
369
381 const bool is_sorted = false) const;
382
394
401 {
402 return this->subsets_begin_.get_num_elems();
403 }
404
411 {
412 return this->subsets_begin_.get_const_data();
413 }
414
421 {
422 return this->subsets_end_.get_const_data();
423 }
424
433 {
434 return this->superset_cumulative_indices_.get_const_data();
435 }
436
437private:
438 void populate_subsets(const gko::array<index_type>& indices,
439 const bool is_sorted);
440
441 std::shared_ptr<const Executor> exec_;
442 index_type index_space_size_;
443 index_type num_stored_indices_;
444 gko::array<index_type> subsets_begin_;
445 gko::array<index_type> subsets_end_;
446 gko::array<index_type> superset_cumulative_indices_;
447};
448
449
450} // namespace gko
451
452
453#endif // GKO_PUBLIC_CORE_BASE_INDEX_SET_HPP_
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:187
void clear() noexcept
Deallocates all data used by the array.
Definition array.hpp:585
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
An index set class represents an ordered set of intervals.
Definition index_set.hpp:85
index_set(const index_set &other)
Creates a copy of the input index_set.
Definition index_set.hpp:168
index_set & operator=(index_set &&other)
Moves data from another index_set.
Definition index_set.hpp:226
array< index_type > to_global_indices() const
This function allows the user obtain a decompressed global_indices array from the indices stored in t...
const index_type * get_subsets_begin() const
Returns a pointer to the beginning indices of the subsets.
Definition index_set.hpp:410
index_type get_num_elems() const
Return the actual number of indices stored in the index set.
Definition index_set.hpp:283
index_type get_global_index(index_type local_index) const
Return the global index given a local index.
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.
IndexType index_type
The type of elements stored in the index set.
Definition index_set.hpp:90
const index_type * get_subsets_end() const
Returns a pointer to the end indices of the subsets.
Definition index_set.hpp:420
std::shared_ptr< const Executor > get_executor() const
Returns the executor of the index_set.
Definition index_set.hpp:262
void clear() noexcept
Deallocates all data used by the index_set.
Definition index_set.hpp:248
index_set(std::shared_ptr< const Executor > exec) noexcept
Creates an empty index_set tied to the specified Executor.
Definition index_set.hpp:97
index_type get_local_index(index_type global_index) const
Return the local index given a global index.
index_set(index_set &&other)
Moves the input index_set.
Definition index_set.hpp:188
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.
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:115
index_type get_num_subsets() const
Returns the number of subsets stored in the index set.
Definition index_set.hpp:400
index_set(std::shared_ptr< const Executor > exec, index_set &&other)
Moves the input index_set to a different executor.
Definition index_set.hpp:177
bool contains(const index_type global_index) const
Checks if the global index exists in the index set.
index_set & operator=(const index_set &other)
Copies data from another index_set.
Definition index_set.hpp:202
bool is_contiguous() const
Returns if the index set is contiguous.
Definition index_set.hpp:276
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:141
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.
const index_type * get_superset_indices() const
Returns a pointer to the cumulative indices of the superset of the subsets.
Definition index_set.hpp:432
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:157
index_type get_size() const
Returns the size of the index set space.
Definition index_set.hpp:269
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803