DASH  0.3.0
PatternMetrics.h
1 #ifndef DASH__UTIL__PATTERN_METRICS_H__
2 #define DASH__UTIL__PATTERN_METRICS_H__
3 
4 #include <dash/Types.h>
5 
6 #include <algorithm>
7 #include <array>
8 #include <vector>
9 
10 
11 namespace dash {
12 namespace util {
13 
14 template<typename PatternT>
16 {
17 private:
18  typedef typename PatternT::index_type index_t;
19  typedef typename PatternT::size_type extent_t;
20 
21 public:
22 
23  PatternMetrics(const PatternT & pattern)
24  {
25  init_metrics(pattern);
26  }
27 
28  constexpr int num_blocks() const noexcept {
29  return _num_blocks;
30  }
31 
37  constexpr double imbalance_factor() const noexcept {
38  return _imb_factor;
39  }
40 
44  constexpr int min_blocks_per_unit() const noexcept {
45  return _min_blocks;
46  }
47 
51  constexpr int min_elements_per_unit() const noexcept {
52  return _min_blocks * _block_size;
53  }
54 
58  constexpr int max_blocks_per_unit() const noexcept {
59  return _max_blocks;
60  }
61 
65  constexpr int max_elements_per_unit() const noexcept {
66  return _max_blocks * _block_size;
67  }
68 
72  constexpr int num_balanced_units() const noexcept {
73  return _num_bal_units;
74  }
75 
79  constexpr int num_imbalanced_units() const noexcept {
80  return _num_imb_units;
81  }
82 
86  constexpr int unit_local_blocks(dash::team_unit_t unit) const noexcept {
87  return _unit_blocks[unit];
88  }
89 
90 private:
94  void init_metrics(const PatternT & pattern)
95  {
96  _num_blocks = pattern.blockspec().size();
97 
98  size_t nunits = pattern.teamspec().size();
99  _unit_blocks.resize(nunits);
100 
101  for (size_t u = 0; u < nunits; ++u) {
102  _unit_blocks[u] = 0;
103  }
104  for (int bi = 0; bi < _num_blocks; ++bi) {
105  auto block = pattern.block(bi);
106  auto block_unit = pattern.unit_at(std::array<index_t, 2> {{
107  block.offset(0),
108  block.offset(1)
109  }});
110  _unit_blocks[block_unit]++;
111  }
112 
113  _block_size = pattern.blocksize(0) * pattern.blocksize(1);
114  _min_blocks = *std::min_element(_unit_blocks.begin(),
115  _unit_blocks.begin() + nunits);
116  _max_blocks = *std::max_element(_unit_blocks.begin(),
117  _unit_blocks.begin() + nunits);
118  _num_bal_units = std::count(_unit_blocks.begin(),
119  _unit_blocks.begin() + nunits,
120  _min_blocks);
121  _num_imb_units = _min_blocks == _max_blocks
122  ? 0
123  : std::count(_unit_blocks.begin(),
124  _unit_blocks.begin() + nunits,
125  _max_blocks);
126 
127  int min_elements = _min_blocks * _block_size;
128  int max_elements = _max_blocks * _block_size;
129  _imb_factor = static_cast<float>(max_elements) /
130  static_cast<float>(min_elements);
131  }
132 
133 private:
134  std::vector<int> _unit_blocks;
135  int _num_blocks = 0;
136  int _block_size = 0;
137  int _min_blocks = 0;
138  int _max_blocks = 0;
139  int _num_imb_units = 0;
140  int _num_bal_units = 0;
141  double _imb_factor = 0.0;
142 };
143 
144 } // namespace util
145 } // namespace dash
146 
147 #endif // DASH__UTIL__PATTERN_METRICS_H__
This class is a simple memory pool which holds allocates elements of size ValueType.
Definition: AllOf.h:8
constexpr int unit_local_blocks(dash::team_unit_t unit) const noexcept
Number of blocks mapped to given unit.
constexpr double imbalance_factor() const noexcept
Relation of (max.
const ElementType * min_element(const ElementType *l_range_begin, const ElementType *l_range_end, Compare compare=std::less< const ElementType &>())
Finds an iterator pointing to the element with the smallest value in the range [first,last).
Definition: MinMax.h:47
constexpr int num_imbalanced_units() const noexcept
Number of units mapped to maximum number of blocks per unit.
struct dash::unit_id< dash::local_unit, dart_team_unit_t > team_unit_t
Unit ID to use for team-local IDs.
Definition: Types.h:319
constexpr int max_blocks_per_unit() const noexcept
Maximum number of blocks mapped to any unit.
constexpr int max_elements_per_unit() const noexcept
Maximum number of elements mapped to any unit.
GlobIter max_element(const GlobIter &first, const GlobIter &last, Compare compare=Compare())
Finds an iterator pointing to the element with the greatest value in the range [first,last).
Definition: MinMax.h:332
constexpr int min_elements_per_unit() const noexcept
Minimum number of elements mapped to any unit.
constexpr int num_balanced_units() const noexcept
Number of units mapped to minimum number of blocks per unit.
constexpr int min_blocks_per_unit() const noexcept
Minimum number of blocks mapped to any unit.
constexpr auto block(OffsetT block_idx, const ViewType &view) -> typename std::enable_if<(!dash::view_traits< ViewType >::is_local::value), ViewBlockMod< ViewType > >::type
Blocks view from global view.
Definition: Chunked.h:49