tiled_range.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2013 Virginia Tech
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #ifndef TILEDARRAY_TILED_RANGE_H__INCLUDED
21 #define TILEDARRAY_TILED_RANGE_H__INCLUDED
22 
23 #include <TiledArray/range.h>
25 
26 namespace TiledArray {
27 
29 
32 class TiledRange {
33  private:
35  void init() {
36  const std::size_t rank = this->rank();
37 
38  // Indices used to store range start and finish.
40  index_type start;
41  index_type finish;
42  index_type start_element;
43  index_type finish_element;
44 
45  start.reserve(rank);
46  finish.reserve(rank);
47  start_element.reserve(rank);
48  finish_element.reserve(rank);
49 
50  // Find the start and finish of the over all tiles and element ranges.
51  for (unsigned int i = 0; i < rank; ++i) {
52  start.push_back(ranges_[i].tiles_range().first);
53  finish.push_back(ranges_[i].tiles_range().second);
54 
55  start_element.push_back(ranges_[i].elements_range().first);
56  finish_element.push_back(ranges_[i].elements_range().second);
57  }
58  range_type(start, finish).swap(range_);
59  range_type(start_element, finish_element).swap(elements_range_);
60  }
61 
62  public:
63  // typedefs
65  typedef Range range_type; // represents elements/tiles range
69  static_assert(std::is_same_v<TiledRange1::index1_type, index1_type>);
71 
73  TiledRange() : range_(), elements_range_(), ranges_() {}
74 
78  template <typename InIter>
79  explicit TiledRange(InIter first, InIter last)
80  : range_(), elements_range_(), ranges_(first, last) {
81  init();
82  }
83 
86  template <typename TRange1Range,
87  typename = std::enable_if_t<
88  detail::is_range_v<TRange1Range> &&
89  std::is_same_v<detail::value_t<TRange1Range>, TiledRange1>>>
90  explicit TiledRange(const TRange1Range& range_of_trange1s)
91  : range_(),
92  elements_range_(),
93  ranges_(begin(range_of_trange1s), end(range_of_trange1s)) {
94  init();
95  }
96 
98 
102  template <typename Integer,
103  typename = std::enable_if_t<std::is_integral_v<Integer>>>
104  explicit TiledRange(
105  const std::initializer_list<std::initializer_list<Integer>>& list)
106  : range_(), elements_range_() {
107  ranges_.reserve(size(list));
108  for (auto&& l : list) {
109  ranges_.emplace_back(l);
110  }
111  init();
112  }
113 
115  explicit TiledRange(const std::initializer_list<TiledRange1>& list)
116  : range_(), elements_range_(), ranges_(list.begin(), list.end()) {
117  init();
118  }
119 
121  TiledRange(const TiledRange_& other)
122  : range_(other.range_),
123  elements_range_(other.elements_range_),
124  ranges_(other.ranges_) {}
125 
127 
130  if (this != &other) TiledRange_(other).swap(*this);
131  return *this;
132  }
133 
135 
138  TA_ASSERT(p.size() == range_.rank());
139  Ranges temp = p * ranges_;
140  TiledRange(temp.begin(), temp.end()).swap(*this);
141  return *this;
142  }
143 
145 
147  const range_type& tiles_range() const { return range_; }
148 
150 
153  [[deprecated]] const range_type& tiles() const { return tiles_range(); }
154 
156 
158  const range_type& elements_range() const { return elements_range_; }
159 
161 
164  [[deprecated]] const range_type& elements() const { return elements_range(); }
165 
167 
173  range_type tile(const index1_type& i) const { return make_tile_range(i); }
174 
176 
181  TA_ASSERT(tiles_range().includes(i));
182  return make_tile_range(tiles_range().idx(i));
183  }
184 
186 
193  template <typename Index>
194  typename std::enable_if_t<detail::is_integral_range_v<Index>, range_type>
195  tile(const Index& index) const {
196  return make_tile_range(index);
197  }
198 
200 
205  template <typename Index>
206  std::enable_if_t<detail::is_integral_range_v<Index>, range_type>
207  make_tile_range(const Index& index) const {
208  const auto rank = range_.rank();
209  TA_ASSERT(range_.includes(index));
210  typename range_type::index_type lower;
211  typename range_type::index_type upper;
212  lower.reserve(rank);
213  upper.reserve(rank);
214  unsigned d = 0;
215  for (auto&& index_d : index) {
216  lower.push_back(data()[d].tile(index_d).first);
217  upper.push_back(data()[d].tile(index_d).second);
218  ++d;
219  }
220 
221  return range_type(lower, upper);
222  }
223 
225 
232  template <typename Integer,
233  typename = std::enable_if_t<std::is_integral_v<Integer>>>
234  range_type tile(const std::initializer_list<Integer>& index) const {
235  return make_tile_range(index);
236  }
237 
239 
244  template <typename Integer,
245  typename = std::enable_if_t<std::is_integral_v<Integer>>>
247  const std::initializer_list<Integer>& index) const {
248  return make_tile_range<std::initializer_list<Integer>>(index);
249  }
250 
252 
256  template <typename Index>
257  std::enable_if_t<detail::is_integral_range_v<Index>,
258  typename range_type::index>
259  element_to_tile(const Index& index) const {
260  const unsigned int rank = range_.rank();
261  typename range_type::index result;
262  result.reserve(rank);
263  unsigned int d = 0;
264  for (auto&& index_d : index) {
265  TA_ASSERT(d < rank);
266  result.push_back(ranges_[d].element_to_tile(index_d));
267  ++d;
268  }
269  TA_ASSERT(d == rank);
270 
271  return result;
272  }
273 
275 
277  std::size_t rank() const { return ranges_.size(); }
278 
280 
283  const TiledRange1& dim(std::size_t d) const {
284  TA_ASSERT(d < rank());
285  return ranges_[d];
286  }
287 
289 
292  const Ranges& data() const { return ranges_; }
293 
294  void swap(TiledRange_& other) {
295  range_.swap(other.range_);
296  elements_range_.swap(other.elements_range_);
297  std::swap(ranges_, other.ranges_);
298  }
299 
300  template <typename Archive,
301  typename std::enable_if<madness::archive::is_input_archive<
302  Archive>::value>::type* = nullptr>
303  void serialize(const Archive& ar) {
304  ar& range_& elements_range_& ranges_;
305  }
306 
307  template <typename Archive,
308  typename std::enable_if<madness::archive::is_output_archive<
309  Archive>::value>::type* = nullptr>
310  void serialize(const Archive& ar) const {
311  ar& range_& elements_range_& ranges_;
312  }
313 
314  private:
315  range_type range_;
316  range_type elements_range_;
317  Ranges ranges_;
319 };
320 
322 
326 inline TiledRange operator*(const Permutation& p, const TiledRange& r) {
327  TA_ASSERT(r.tiles_range().rank() == p.size());
328  TiledRange::Ranges data = p * r.data();
329 
330  return TiledRange(data.begin(), data.end());
331 }
332 
334 inline void swap(TiledRange& r0, TiledRange& r1) { r0.swap(r1); }
335 
337 inline bool operator==(const TiledRange& r1, const TiledRange& r2) {
338  return (r1.tiles_range().rank() == r2.tiles_range().rank()) &&
339  (r1.tiles_range() == r2.tiles_range()) &&
340  (r1.elements_range() == r2.elements_range()) &&
341  std::equal(r1.data().begin(), r1.data().end(), r2.data().begin());
342 }
343 
344 inline bool operator!=(const TiledRange& r1, const TiledRange& r2) {
345  return !operator==(r1, r2);
346 }
347 
348 inline std::ostream& operator<<(std::ostream& out, const TiledRange& rng) {
349  out << "("
350  << " tiles = " << rng.tiles_range()
351  << ", elements = " << rng.elements_range() << " )";
352  return out;
353 }
354 
355 } // namespace TiledArray
356 
357 #endif // TILEDARRAY_TILED_RANGE_H__INCLUDED
boost::container::small_vector< T, N > svector
Definition: vector.h:43
const range_type & tiles() const
Access the tile range.
Definition: tiled_range.h:153
std::enable_if_t< detail::is_integral_range_v< Index >, range_type > make_tile_range(const Index &index) const
Construct a range for the tile indexed by the given index.
Definition: tiled_range.h:207
index_type size() const
Domain size accessor.
Definition: permutation.h:214
range_type::index1_type index1_type
Definition: tiled_range.h:68
range_type make_tile_range(const std::initializer_list< Integer > &index) const
Construct a range for the tile indexed by the given index.
Definition: tiled_range.h:246
TiledRange(InIter first, InIter last)
Definition: tiled_range.h:79
std::enable_if_t< detail::is_integral_range_v< Index >, range_type > tile(const Index &index) const
Construct a range for the tile indexed by the given index.
Definition: tiled_range.h:195
void swap(TiledRange &r0, TiledRange &r1)
Exchange the content of the two given TiledRange's.
Definition: tiled_range.h:334
TiledRange(const TRange1Range &range_of_trange1s)
Definition: tiled_range.h:90
const range_type & tiles_range() const
Access the tile range.
Definition: tiled_range.h:147
void serialize(const Archive &ar) const
Definition: tiled_range.h:310
Permutation of a sequence of objects indexed by base-0 indices.
Definition: permutation.h:130
bool operator==(const BlockRange &r1, const BlockRange &r2)
BlockRange equality comparison.
Definition: block_range.h:433
range_type tile(const index1_type &i) const
Constructs a range for the tile indexed by the given ordinal index.
Definition: tiled_range.h:173
std::enable_if_t< detail::is_integral_range_v< Index >, typename range_type::index > element_to_tile(const Index &index) const
Convert an element index to a tile index.
Definition: tiled_range.h:259
TiledRange_ & operator*=(const Permutation &p)
In place permutation of this range.
Definition: tiled_range.h:137
container::svector< TiledRange1 > Ranges
Definition: tiled_range.h:69
TiledRange_ & operator=(const TiledRange_ &other)
TiledRange assignment operator.
Definition: tiled_range.h:129
container::svector< index1_type > index_type
Definition: range.h:52
const TiledRange1 & dim(std::size_t d) const
Accessor of the tiled range for one of the dimensions.
Definition: tiled_range.h:283
constexpr bool operator!=(const DenseShape &a, const DenseShape &b)
Definition: dense_shape.h:382
TA_1INDEX_TYPE index1_type
Definition: range.h:49
TiledRange(const TiledRange_ &other)
Copy constructor.
Definition: tiled_range.h:121
TiledRange(const std::initializer_list< TiledRange1 > &list)
Constructed with an initializer_list of TiledRange1's.
Definition: tiled_range.h:115
range_type::index_type index_type
Definition: tiled_range.h:66
range_type make_tile_range(const index1_type &i) const
Construct a range for the tile indexed by the given ordinal index.
Definition: tiled_range.h:180
const range_type & elements_range() const
Access the element range.
Definition: tiled_range.h:158
std::size_t rank() const
The rank accessor.
Definition: tiled_range.h:277
std::array< T, N > operator*(const Permutation &, const std::array< T, N > &)
Permute a std::array.
Definition: permutation.h:492
#define TA_ASSERT(EXPR,...)
Definition: error.h:39
bool includes(const Index &index) const
Check the coordinate to make sure it is within the range.
Definition: range.h:826
Range data of a tiled array.
Definition: tiled_range.h:32
void serialize(const Archive &ar)
Definition: tiled_range.h:303
TiledRange TiledRange_
Definition: tiled_range.h:64
const range_type & elements() const
Access the element range.
Definition: tiled_range.h:164
range_type tile(const std::initializer_list< Integer > &index) const
Construct a range for the tile indexed by the given index.
Definition: tiled_range.h:234
std::ostream & operator<<(std::ostream &os, const DistArray< Tile, Policy > &a)
Add the tensor to an output stream.
Definition: dist_array.h:1602
void swap(TiledRange_ &other)
Definition: tiled_range.h:294
const Ranges & data() const
Tile dimension boundary array accessor.
Definition: tiled_range.h:292
TiledRange()
Default constructor.
Definition: tiled_range.h:73
range_type::ordinal_type ordinal_type
Definition: tiled_range.h:67
void swap(Range_ &other)
Definition: range.h:1124
std::size_t ordinal_type
Ordinal type, to conform to TWG spec.
Definition: range.h:59
index_type index
Coordinate index type (deprecated)
Definition: range.h:54
void swap(Range &r0, Range &r1)
Exchange the values of the give two ranges.
Definition: range.h:1233
TiledRange(const std::initializer_list< std::initializer_list< Integer >> &list)
Constructs using a list of lists convertible to TiledRange1.
Definition: tiled_range.h:104
unsigned int rank() const
Rank accessor.
Definition: range.h:669
A (hyperrectangular) interval on , space of integer -indices.
Definition: range.h:46