Derecho  0.9
Distributed systems toolkit for RDMA
subgroup_functions.hpp
Go to the documentation of this file.
1 
7 #pragma once
8 
9 #include <memory>
10 #include <variant>
11 
12 #include "derecho_modes.hpp"
14 #include "subgroup_info.hpp"
15 
16 namespace derecho {
17 
25 template <typename T>
26 std::unique_ptr<T> deep_pointer_copy(const std::unique_ptr<T>& to_copy) {
27  if(to_copy) {
28  return std::make_unique<T>(*to_copy);
29  } else {
30  return nullptr;
31  }
32 }
33 
34 /*
35  * String constants for the names of configuration profile fields that
36  * the default subgroup allocator will look up
37  */
38 constexpr char num_shards_profile_field[] = "num_shards";
39 constexpr char min_nodes_profile_field[] = "min_nodes";
40 constexpr char max_nodes_profile_field[] = "max_nodes";
41 /* It would be really nice if we could group these together in an enumerated class
42  * called ProfileFields or something, but there's no way to do that with strings. */
43 
49 subgroup_allocation_map_t one_subgroup_entire_view(const std::vector<std::type_index>& subgroup_type_order,
50  const std::unique_ptr<View>& prev_view, View& curr_view);
57 subgroup_allocation_map_t one_subgroup_entire_view_raw(const std::vector<std::type_index>& subgroup_type_order,
58  const std::unique_ptr<View>& prev_view, View& curr_view);
59 
80  std::string shards_profile;
84  std::vector<int> min_num_nodes_by_shard;
87  std::vector<int> max_num_nodes_by_shard;
91  std::vector<Mode> modes_by_shard;
95  std::vector<std::string> profiles_by_shard;
96 };
97 
110  std::vector<ShardAllocationPolicy> shard_policy_by_subgroup;
111 };
112 
128  std::pair<std::type_index, uint32_t> source_subgroup;
132  std::pair<std::type_index, uint32_t> target_subgroup;
133 };
134 
135 /* Helper functions that construct ShardAllocationPolicy values for common cases. */
136 
150  const std::string& profile = "default");
151 
162 ShardAllocationPolicy flexible_even_shards(const std::string& profile);
163 
174 ShardAllocationPolicy fixed_even_shards(int num_shards, int nodes_per_shard,
175  const std::string& profile = "default");
176 
188  const std::string& profile = "default");
203 ShardAllocationPolicy custom_shards_policy(const std::vector<int>& min_nodes_by_shard,
204  const std::vector<int>& max_nodes_by_shard,
205  const std::vector<Mode>& delivery_modes_by_shard,
206  const std::vector<std::string>& profiles_by_shard);
207 
221 ShardAllocationPolicy custom_shard_policy(const std::vector<Mode>& delivery_modes_by_shard,
222  const std::vector<std::string>& profiles_by_shard);
223 
232 
241 SubgroupAllocationPolicy identical_subgroups_policy(int num_subgroups, const ShardAllocationPolicy& subgroup_policy);
242 
251 protected:
258  const std::map<std::type_index, std::variant<SubgroupAllocationPolicy, CrossProductPolicy>> policies;
259 
279  std::map<std::type_index, std::vector<std::vector<uint32_t>>> compute_standard_shard_sizes(
280  const std::vector<std::type_index>& subgroup_type_order,
281  const std::unique_ptr<View>& prev_view,
282  const View& curr_view) const;
283 
292  subgroup_shard_layout_t allocate_standard_subgroup_type(
293  const std::type_index subgroup_type,
294  View& curr_view,
295  const std::map<std::type_index, std::vector<std::vector<uint32_t>>>& shard_sizes) const;
296 
308  subgroup_shard_layout_t update_standard_subgroup_type(
309  const std::type_index subgroup_type,
310  const subgroup_type_id_t subgroup_type_id,
311  const std::unique_ptr<View>& prev_view,
312  View& curr_view,
313  const std::map<std::type_index, std::vector<std::vector<uint32_t>>>& shard_sizes) const;
314 
325  void compute_standard_memberships(const std::vector<std::type_index>& subgroup_type_order,
326  const std::unique_ptr<View>& prev_view,
327  View& curr_view,
328  subgroup_allocation_map_t& subgroup_layouts) const;
329 
341  void compute_cross_product_memberships(const std::vector<std::type_index>& subgroup_type_order,
342  const std::unique_ptr<View>& prev_view,
343  View& curr_view,
344  subgroup_allocation_map_t& subgroup_layouts) const;
345 
346 public:
347  DefaultSubgroupAllocator(const std::map<std::type_index,
348  std::variant<SubgroupAllocationPolicy, CrossProductPolicy>>&
349  policies_by_subgroup_type)
350  : policies(policies_by_subgroup_type) {}
352  : policies(to_copy.policies) {}
354 
355  subgroup_allocation_map_t operator()(const std::vector<std::type_index>& subgroup_type_order,
356  const std::unique_ptr<View>& prev_view,
357  View& curr_view) const;
358 };
359 
360 } // namespace derecho
const std::map< std::type_index, std::variant< SubgroupAllocationPolicy, CrossProductPolicy > > policies
The entry for each type of subgroup is either a SubgroupAllocationPolicy if that type should use the ...
A data structure defining the parameters of the default subgroup allocation function for a single sub...
ShardAllocationPolicy fixed_even_shards(int num_shards, int nodes_per_shard, const std::string &profile="default")
Returns a ShardAllocationPolicy that specifies num_shards shards with the same fixed number of nodes ...
uint32_t subgroup_type_id_t
Type of the numeric ID used to refer to subgroup types within a Group; this is currently computed as ...
ShardAllocationPolicy raw_fixed_even_shards(int num_shards, int nodes_per_shard, const std::string &profile="default")
Returns a ShardAllocationPolicy that specifies num_shards shards with the same fixed number of nodes ...
constexpr char max_nodes_profile_field[]
DefaultSubgroupAllocator(const std::map< std::type_index, std::variant< SubgroupAllocationPolicy, CrossProductPolicy >> &policies_by_subgroup_type)
ShardAllocationPolicy custom_shards_policy(const std::vector< int > &min_nodes_by_shard, const std::vector< int > &max_nodes_by_shard, const std::vector< Mode > &delivery_modes_by_shard, const std::vector< std::string > &profiles_by_shard)
Returns a ShardAllocationPolicy for a subgroup that has a different number of members in each shard...
std::vector< std::vector< SubView > > subgroup_shard_layout_t
The data structure used to store a subgroups-and-shards layout for a single subgroup type (i...
std::vector< Mode > modes_by_shard
If even_shards is false, this will contain an entry for each shard indicating which delivery mode it ...
std::pair< std::type_index, uint32_t > source_subgroup
The (type, index) pair identifying the "source" subgroup of the cross-product.
subgroup_allocation_map_t one_subgroup_entire_view(const std::vector< std::type_index > &subgroup_type_order, const std::unique_ptr< View > &prev_view, View &curr_view)
A simple implementation of shard_view_generator_t that creates a single, un-sharded subgroup containi...
std::vector< int > min_num_nodes_by_shard
If even_shards is false, this will contain an entry for each shard indicating the minimum number of m...
int num_subgroups
The number of subgroups of the same Replicated type to create.
std::pair< std::type_index, uint32_t > target_subgroup
The (type, index) pair identifying the "target" subgroup of the cross-product.
SubgroupAllocationPolicy one_subgroup_policy(const ShardAllocationPolicy &policy)
Returns a SubgroupAllocationPolicy for a replicated type that only has a single subgroup.
std::unique_ptr< T > deep_pointer_copy(const std::unique_ptr< T > &to_copy)
A copy constructor for objects owned by unique_ptr.
int num_shards
The number of shards; set to 1 for a non-sharded subgroup.
constexpr char num_shards_profile_field[]
std::map< std::type_index, subgroup_shard_layout_t > subgroup_allocation_map_t
The data structure used to store the subgroups-and-shards layouts for all subgroup types in a Group (...
Functor of type shard_view_generator_t that implements the default subgroup allocation algorithm...
subgroup_allocation_map_t one_subgroup_entire_view_raw(const std::vector< std::type_index > &subgroup_type_order, const std::unique_ptr< View > &prev_view, View &curr_view)
A simple implementation of shard_view_generator_t that returns a single, un-sharded subgroup in Unord...
std::vector< std::string > profiles_by_shard
If even_shards is false, this will contain an entry for each shard indicating which profile it should...
bool even_shards
Whether all shards should contain the same number of members.
std::string shards_profile
If even_shards is true, this is the profile that will be used for every shard.
DefaultSubgroupAllocator(const DefaultSubgroupAllocator &to_copy)
An alternate type of subgroup allocation policy for subgroup types whose membership will be defined a...
ShardAllocationPolicy flexible_even_shards(int num_shards, int min_nodes_per_shard, int max_nodes_per_shard, const std::string &profile="default")
Returns a ShardAllocationPolicy that specifies num_shards "flexible" or fault-tolerant shards...
std::vector< ShardAllocationPolicy > shard_policy_by_subgroup
If identical_subgroups is true, contains a single entry with the allocation policy for all subgroups ...
A data structure defining the parameters of the default subgroup allocation function for a single sub...
SubgroupAllocationPolicy identical_subgroups_policy(int num_subgroups, const ShardAllocationPolicy &subgroup_policy)
Returns a SubgroupAllocationPolicy for a replicated type that needs n subgroups with identical shardi...
std::vector< int > max_num_nodes_by_shard
If even_shards is false, this will contain an entry for each shard indicating the maximum number of m...
int max_nodes_per_shard
If even_shards is true, this is the maximum number of nodes per shard.
ShardAllocationPolicy custom_shard_policy(const std::vector< Mode > &delivery_modes_by_shard, const std::vector< std::string > &profiles_by_shard)
Returns a ShardAllocationPolicy for a subgroup that has a different number of members in each shard...
constexpr char min_nodes_profile_field[]
bool identical_subgroups
Whether all subgroups of this type will have an identical shard layout.
int min_nodes_per_shard
If even_shards is true, this is the minimum number of nodes per shard.
Mode shards_mode
If even_shards is true, this is the delivery mode that will be used for every shard.