Ginkgo  Generated from pipelines/2837190956 branch based on develop. Ginkgo version 2.0.0
A numerical linear algebra library targeting many-core architectures
partition.hpp
1 // SPDX-FileCopyrightText: 2017 - 2026 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_DISTRIBUTED_PARTITION_HPP_
6 #define GKO_PUBLIC_CORE_DISTRIBUTED_PARTITION_HPP_
7 
8 
9 #include <ginkgo/core/base/array.hpp>
10 #include <ginkgo/core/base/polymorphic_object.hpp>
11 #include <ginkgo/core/base/segmented_array.hpp>
12 #include <ginkgo/core/base/types.hpp>
13 
14 
15 namespace gko {
16 namespace experimental {
22 namespace distributed {
23 
24 
79 template <typename LocalIndexType = int32, typename GlobalIndexType = int64>
80 class Partition
81  : public PolymorphicObject,
82  public EnableCloneable<Partition<LocalIndexType, GlobalIndexType>> {
83  friend class EnableCloneable<Partition>;
84  static_assert(sizeof(GlobalIndexType) >= sizeof(LocalIndexType),
85  "GlobalIndexType must be at least as large as "
86  "LocalIndexType");
87 
88 public:
91 
92  using local_index_type = LocalIndexType;
93  using global_index_type = GlobalIndexType;
94 
100  size_type get_size() const { return size_; }
101 
108  size_type get_num_ranges() const noexcept
109  {
110  return offsets_.get_size() - 1;
111  }
112 
118  comm_index_type get_num_parts() const noexcept { return num_parts_; }
119 
133  bool equals(const Partition& other) const;
134 
140  comm_index_type get_num_empty_parts() const noexcept
141  {
142  return num_empty_parts_;
143  }
144 
152  const global_index_type* get_range_bounds() const noexcept
153  {
154  return offsets_.get_const_data();
155  }
156 
164  const comm_index_type* get_part_ids() const noexcept
165  {
166  return part_ids_.get_const_data();
167  }
168 
182  const local_index_type* get_range_starting_indices() const noexcept
183  {
184  return starting_indices_.get_const_data();
185  }
186 
193  const local_index_type* get_part_sizes() const noexcept
194  {
195  return part_sizes_.get_const_data();
196  }
197 
206  local_index_type get_part_size(comm_index_type part) const;
207 
214  {
215  return ranges_by_part_;
216  }
217 
223  bool has_connected_parts() const;
224 
232  bool has_ordered_parts() const;
233 
243  static std::unique_ptr<Partition> build_from_mapping(
244  std::shared_ptr<const Executor> exec,
245  const array<comm_index_type>& mapping, comm_index_type num_parts);
246 
260  static std::unique_ptr<Partition> build_from_contiguous(
261  std::shared_ptr<const Executor> exec,
262  const array<global_index_type>& ranges,
263  const array<comm_index_type>& part_ids = {});
264 
276  static std::unique_ptr<Partition> build_from_global_size_uniform(
277  std::shared_ptr<const Executor> exec, comm_index_type num_parts,
278  global_index_type global_size);
279 
280 private:
281  Partition(std::shared_ptr<const Executor> exec,
282  comm_index_type num_parts = 0, size_type num_ranges = 0);
283 
284  static std::unique_ptr<Partition> create(
285  std::shared_ptr<const Executor> exec, comm_index_type num_parts = 0,
286  size_type num_ranges = 0);
287 
293  void finalize_construction();
294 
295  comm_index_type num_parts_;
296  comm_index_type num_empty_parts_;
297  global_index_type size_;
298  array<global_index_type> offsets_;
299  array<local_index_type> starting_indices_;
300  array<local_index_type> part_sizes_;
301  array<comm_index_type> part_ids_;
302  segmented_array<size_type> ranges_by_part_;
303 };
304 
305 
306 } // namespace distributed
307 } // namespace experimental
308 } // namespace gko
309 
310 
311 #endif // GKO_PUBLIC_CORE_DISTRIBUTED_PARTITION_HPP_
gko::experimental::distributed::Partition::build_from_global_size_uniform
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.
gko::experimental::distributed::Partition::get_part_size
local_index_type get_part_size(comm_index_type part) const
Returns the size of a part given by its part ID.
gko::experimental::distributed::Partition::get_part_sizes
const local_index_type * get_part_sizes() const noexcept
Returns the part size array.
Definition: partition.hpp:193
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:101
gko::experimental::distributed::Partition::get_range_starting_indices
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:182
gko::experimental::distributed::Partition::build_from_contiguous
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.
gko::experimental::distributed::Partition::get_range_bounds
const global_index_type * get_range_bounds() const noexcept
Returns the ranges boundary array stored by this partition.
Definition: partition.hpp:152
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:19
gko::experimental::distributed::Partition::get_num_parts
comm_index_type get_num_parts() const noexcept
Returns the number of parts represented in this partition.
Definition: partition.hpp:118
gko::array< comm_index_type >
gko::segmented_array< size_type >
gko::experimental::distributed::Partition::has_ordered_parts
bool has_ordered_parts() const
Checks if the ranges are ordered by their part index.
gko::experimental::distributed::Partition::get_part_ids
const comm_index_type * get_part_ids() const noexcept
Returns the part IDs of the ranges in this partition.
Definition: partition.hpp:164
gko::experimental::distributed::Partition::get_size
size_type get_size() const
Returns the total number of elements represented by this partition.
Definition: partition.hpp:100
gko::experimental::distributed::Partition::get_num_empty_parts
comm_index_type get_num_empty_parts() const noexcept
Returns the number of empty parts within this partition.
Definition: partition.hpp:140
gko::experimental::distributed::Partition
Represents a partition of a range of indices [0, size) into a disjoint set of parts.
Definition: assembly.hpp:26
gko::EnableCloneable::convert_to
void convert_to(result_type *result) const override
Converts the implementer to an object of type result_type.
Definition: polymorphic_object.hpp:411
gko::experimental::distributed::Partition::get_ranges_by_part
const segmented_array< size_type > & get_ranges_by_part() const
Returns the range IDs segmented by their part ID.
Definition: partition.hpp:213
gko::experimental::distributed::Partition::has_connected_parts
bool has_connected_parts() const
Checks if each part has no more than one contiguous range.
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::experimental::distributed::Partition::get_num_ranges
size_type get_num_ranges() const noexcept
Returns the number of ranges stored by this partition.
Definition: partition.hpp:108
gko::EnableCloneable::move_to
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:413
gko::array::get_size
size_type get_size() const noexcept
Returns the number of elements in the array.
Definition: array.hpp:669
gko::experimental::distributed::Partition::equals
bool equals(const Partition &other) const
Checks whether this partition describes the same distribution as another one, i.e.
gko::experimental::distributed::Partition::build_from_mapping
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.