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
partition.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_DISTRIBUTED_PARTITION_HPP_
34#define GKO_PUBLIC_CORE_DISTRIBUTED_PARTITION_HPP_
35
36
37#include <ginkgo/core/base/array.hpp>
38#include <ginkgo/core/base/polymorphic_object.hpp>
39#include <ginkgo/core/base/types.hpp>
40
41
42namespace gko {
43namespace experimental {
49namespace distributed {
50
51
106template <typename LocalIndexType = int32, typename GlobalIndexType = int64>
109 Partition<LocalIndexType, GlobalIndexType>>,
111 Partition<LocalIndexType, GlobalIndexType>>,
112 public EnableCreateMethod<Partition<LocalIndexType, GlobalIndexType>> {
113 friend class EnableCreateMethod<Partition>;
115 static_assert(sizeof(GlobalIndexType) >= sizeof(LocalIndexType),
116 "GlobalIndexType must be at least as large as "
117 "LocalIndexType");
118
119public:
120 using EnableCreateMethod<Partition>::create;
123
124 using local_index_type = LocalIndexType;
125 using global_index_type = GlobalIndexType;
126
132 size_type get_size() const { return size_; }
133
141 {
142 return offsets_.get_num_elems() - 1;
143 }
144
151
158 {
159 return num_empty_parts_;
160 }
161
169 const global_index_type* get_range_bounds() const noexcept
170 {
171 return offsets_.get_const_data();
172 }
173
182 {
183 return part_ids_.get_const_data();
184 }
185
199 const local_index_type* get_range_starting_indices() const noexcept
200 {
201 return starting_indices_.get_const_data();
202 }
203
210 const local_index_type* get_part_sizes() const noexcept
211 {
212 return part_sizes_.get_const_data();
213 }
214
223 local_index_type get_part_size(comm_index_type part) const
224 {
225 return this->get_executor()->copy_val_to_host(
226 part_sizes_.get_const_data() + part);
227 }
228
235
243 bool has_ordered_parts() const;
244
254 static std::unique_ptr<Partition> build_from_mapping(
255 std::shared_ptr<const Executor> exec,
257
271 static std::unique_ptr<Partition> build_from_contiguous(
272 std::shared_ptr<const Executor> exec,
274 const array<comm_index_type>& part_ids = {});
275
287 static std::unique_ptr<Partition> build_from_global_size_uniform(
288 std::shared_ptr<const Executor> exec, comm_index_type num_parts,
289 global_index_type global_size);
290
291private:
296 Partition(std::shared_ptr<const Executor> exec,
299 num_parts_{num_parts},
300 num_empty_parts_{0},
301 size_{0},
302 offsets_{exec, num_ranges + 1},
303 starting_indices_{exec, num_ranges},
304 part_sizes_{exec, static_cast<size_type>(num_parts)},
305 part_ids_{exec, num_ranges}
306 {
307 offsets_.fill(0);
308 starting_indices_.fill(0);
309 part_sizes_.fill(0);
310 part_ids_.fill(0);
311 }
312
318 void finalize_construction();
319
320 comm_index_type num_parts_;
321 comm_index_type num_empty_parts_;
322 global_index_type size_;
323 array<global_index_type> offsets_;
324 array<local_index_type> starting_indices_;
325 array<local_index_type> part_sizes_;
326 array<comm_index_type> part_ids_;
327};
328
329
330} // namespace distributed
331} // namespace experimental
332} // namespace gko
333
334
335#endif // GKO_PUBLIC_CORE_DISTRIBUTED_PARTITION_HPP_
This mixin implements a static create() method on ConcreteType that dynamically allocates the memory,...
Definition polymorphic_object.hpp:776
This mixin is used to enable a default PolymorphicObject::copy_from() implementation for objects that...
Definition polymorphic_object.hpp:752
void move_to(result_type *result) override
Converts the implementer to an object of type result_type by moving data from this object.
Definition polymorphic_object.hpp:760
void convert_to(result_type *result) const override
Converts the implementer to an object of type result_type.
Definition polymorphic_object.hpp:758
This mixin inherits from (a subclass of) PolymorphicObject and provides a base implementation of a ne...
Definition polymorphic_object.hpp:691
std::shared_ptr< const Executor > get_executor() const noexcept
Returns the Executor of the object.
Definition polymorphic_object.hpp:263
An array is a container which encapsulates fixed-sized arrays, stored on the Executor tied to the arr...
Definition array.hpp:187
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
void fill(const value_type value)
Fill the array with the given value.
size_type get_num_elems() const noexcept
Returns the number of elements in the array.
Definition array.hpp:637
Represents a partition of a range of indices [0, size) into a disjoint set of parts.
Definition partition.hpp:112
comm_index_type get_num_empty_parts() const noexcept
Returns the number of empty parts within this partition.
Definition partition.hpp:157
size_type get_num_ranges() const noexcept
Returns the number of ranges stored by this partition.
Definition partition.hpp:140
const local_index_type * get_part_sizes() const noexcept
Returns the part size array.
Definition partition.hpp:210
const local_index_type * get_range_starting_indices() const noexcept
Returns the part-local starting index for each range in this partition.
Definition partition.hpp:199
static std::unique_ptr< Partition > build_from_global_size_uniform(std::shared_ptr< const Executor > exec, comm_index_type num_parts, global_index_type global_size)
Builds a partition by evenly distributing the global range.
bool has_connected_parts() const
Checks if each part has no more than one contiguous range.
const comm_index_type * get_part_ids() const noexcept
Returns the part IDs of the ranges in this partition.
Definition partition.hpp:181
local_index_type get_part_size(comm_index_type part) const
Returns the size of a part given by its part ID.
Definition partition.hpp:223
size_type get_size() const
Returns the total number of elements represented by this partition.
Definition partition.hpp:132
comm_index_type get_num_parts() const noexcept
Returns the number of parts represented in this partition.
Definition partition.hpp:150
static std::unique_ptr< Partition > build_from_contiguous(std::shared_ptr< const Executor > exec, const array< global_index_type > &ranges, const array< comm_index_type > &part_ids={})
Builds a partition consisting of contiguous ranges, one for each part.
static std::unique_ptr< Partition > build_from_mapping(std::shared_ptr< const Executor > exec, const array< comm_index_type > &mapping, comm_index_type num_parts)
Builds a partition from a given mapping global_index -> part_id.
bool has_ordered_parts() const
Checks if the ranges are ordered by their part index.
const global_index_type * get_range_bounds() const noexcept
Returns the ranges boundary array stored by this partition.
Definition partition.hpp:169
int comm_index_type
Index type for enumerating processes in a distributed application.
Definition types.hpp:807
The Ginkgo namespace.
Definition abstract_factory.hpp:48
constexpr T one()
Returns the multiplicative identity for T.
Definition math.hpp:803
std::size_t size_type
Integral type used for allocation quantities.
Definition types.hpp:120