DASH  0.3.0
Distribution.h
1 #ifndef DASH__DISTRIBUTION_H_
2 #define DASH__DISTRIBUTION_H_
3 
4 #include <dash/Types.h>
5 #include <dash/Exception.h>
6 #include <dash/internal/Math.h>
7 #include <dash/internal/Logging.h>
8 
9 namespace dash {
10 
25 {
26 private:
27  typedef Distribution self_t;
28 
29  friend std::ostream & operator<<(
30  std::ostream & os,
31  const self_t & distribution);
32 
33 public:
34  dash::internal::DistributionType type{dash::internal::DIST_NONE};
35  dash::default_size_t blocksz{0};
36 
41  Distribution() = default;
42 
48  dash::internal::DistributionType distType,
49  int blockSize)
50  : type(distType),
51  blocksz(blockSize) {
52  }
53 
54  Distribution(const self_t& other) = default;
55 
56  self_t& operator=(const self_t& other) = default;
57 
62  template <typename IndexType, typename SizeType>
63  inline IndexType local_index_to_block_coord(
64  // The unit's offset in the distribution's dimension
65  // within the global team specification
66  IndexType unit_teamspec_coord,
67  // Local index of the element
68  IndexType local_index,
69  // Number of units in the distribution's dimension
70  SizeType num_units_in_dim) const {
71  // NOTE: blocksize should be this->blocksz
72  SizeType local_block_offset = 0;
73  switch (type) {
74  case dash::internal::DIST_NONE:
75  // There is only one block in this dimension, so
76  // block coordinate is 0:
77  return 0;
78  case dash::internal::DIST_BLOCKED:
79  // Same as blockcyclic, but local block offset is
80  // always 0:
81  return unit_teamspec_coord;
82  case dash::internal::DIST_TILE:
83  // Same as blockcyclic
84  case dash::internal::DIST_BLOCKCYCLIC:
85  // Number of blocks local to the unit that are
86  // in front of the given local index:
87  local_block_offset = local_index / blocksz;
88  // Number of blocks of any unit that are in front
89  // of the given local index. Unit's coordinate in team
90  // spec is equivalent to the number of units in front of
91  // the unit.
92  return (local_block_offset * num_units_in_dim) +
93  unit_teamspec_coord;
94  case dash::internal::DIST_CYCLIC:
95  // Like blockcyclic, but with blocksize 1:
96  DASH_LOG_TRACE("Distribution.local_index_to_block_coord",
97  "unit_teamspec_coord", unit_teamspec_coord,
98  "local_index", local_index,
99  "num_units_in_dim", num_units_in_dim);
100  return unit_teamspec_coord + local_index * num_units_in_dim;
101  default:
102  DASH_THROW(
104  "Distribution type undefined in " <<
105  "local_index_to_block_coord");
106  }
107  }
108 
113  template <typename IndexType, typename SizeType>
114  inline IndexType max_blocksize_in_range(
116  IndexType range,
118  SizeType num_units) const
119  {
120  DASH_LOG_TRACE("Distribution.max_blocksize_in_range()",
121  "range:", range, "nunits:", num_units);
122  switch (type) {
123  case dash::internal::DIST_NONE:
124  return range;
125  case dash::internal::DIST_BLOCKED:
126  return num_units == 0 ? 0 : dash::math::div_ceil(range, num_units);
127  case dash::internal::DIST_CYCLIC:
128  return 1;
129  case dash::internal::DIST_BLOCKCYCLIC:
130  case dash::internal::DIST_TILE:
131  DASH_LOG_TRACE("Distribution.max_blocksize_in_range",
132  "TILE", "blocksz:", blocksz);
133  // Shrink block size in dimension if it exceeds the number
134  // of elements in dimension:
135  return std::min<SizeType>(range, blocksz);
136  default:
137  DASH_THROW(
139  "Distribution type undefined in max_blocksize_in_range");
140  }
141  }
142 
146  bool operator==(const Distribution & other) const {
147  return (this->type == other.type &&
148  this->blocksz == other.blocksz);
149  }
153  bool operator!=(const Distribution & other) const {
154  return !(*this == other);
155  }
156 };
157 
165 extern const Distribution BLOCKED;
174 extern const Distribution CYCLIC;
181 extern const Distribution NONE;
182 
190 Distribution TILE(int blockSize);
191 
199 Distribution BLOCKCYCLIC(int blockSize);
200 
201 } // namespace dash
202 
203 #endif // DASH__DISTRIBUTION_H_
internal::default_unsigned_index default_size_t
Unsigned integer type used as default for size values.
Definition: Types.h:69
This class is a simple memory pool which holds allocates elements of size ValueType.
Definition: AllOf.h:8
bool operator==(const Distribution &other) const
Equality comparison operator.
Definition: Distribution.h:146
Distribution TILE(int blockSize)
Distribution specifying that elements in a Pattern&#39;s dimension shall be distributed to units in a til...
const Distribution CYCLIC
Distribution specifying that elements in a Pattern&#39;s dimension shall be distributed by cycling among ...
Definition: Distribution.h:174
Distribution BLOCKCYCLIC(int blockSize)
Distribution specifying that elements in a Pattern&#39;s dimension shall be distributed to units in block...
bool operator!=(const Distribution &other) const
Inequality comparison operator.
Definition: Distribution.h:153
Distribution()=default
Constructor, initializes Distribution with distribution type NONE.
const Distribution NONE
Distribution specifying that elements in a Pattern&#39;s dimension shall not be distributed.
Definition: Distribution.h:181
IndexType local_index_to_block_coord(IndexType unit_teamspec_coord, IndexType local_index, SizeType num_units_in_dim) const
Resolve the block coordinate for a given local index in the distribution&#39;s dimension.
Definition: Distribution.h:63
Specifies how a Pattern distributes elements to units in a specific dimension.
Definition: Distribution.h:24
IndexType max_blocksize_in_range(IndexType range, SizeType num_units) const
The maximum size of a single block within an extent for a given total number of units.
Definition: Distribution.h:114
const Distribution BLOCKED
Distribution specifying that elements in a Pattern&#39;s dimension shall be distributed to units in even-...
Definition: Distribution.h:165
Distribution(dash::internal::DistributionType distType, int blockSize)
Constructor, initializes Distribution with a distribution type and a block size.
Definition: Distribution.h:47