TiledArray  0.7.0
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 RANGE_H__INCLUDED
21 #define RANGE_H__INCLUDED
22 
24 #include <TiledArray/range.h>
25 
26 namespace TiledArray {
27 
29 
31  class TiledRange {
32  private:
33 
35  void init() {
36  const std::size_t rank = this->rank();
37 
38  // Indices used to store range start and finish.
39  std::vector<size_type> start;
40  std::vector<size_type> finish;
41  std::vector<size_type> start_element;
42  std::vector<size_type> finish_element;
43 
44  start.reserve(rank);
45  finish.reserve(rank);
46  start_element.reserve(rank);
47  finish_element.reserve(rank);
48 
49  // Find the start and finish of the over all tiles and element ranges.
50  for(unsigned int i = 0; i < rank; ++i) {
51  start.push_back(ranges_[i].tiles_range().first);
52  finish.push_back(ranges_[i].tiles_range().second);
53 
54  start_element.push_back(ranges_[i].elements_range().first);
55  finish_element.push_back(ranges_[i].elements_range().second);
56  }
57  range_type(start, finish).swap(range_);
58  range_type(start_element, finish_element).swap(elements_range_);
59  }
60 
61  public:
62  // typedefs
64  typedef Range range_type; // represents elements/tiles range
65  typedef std::size_t size_type;
68  typedef std::vector<TiledRange1> Ranges;
69 
71  TiledRange() : range_(), elements_range_(), ranges_() { }
72 
74  template <typename InIter>
75  TiledRange(InIter first, InIter last) :
76  range_(), elements_range_(), ranges_(first, last)
77  {
78  init();
79  }
80 
82  TiledRange(const std::initializer_list<std::initializer_list<size_type> >& list) :
83  range_(), elements_range_(), ranges_(list.begin(), list.end())
84  {
85  init();
86  }
87 
89  TiledRange(const std::initializer_list<TiledRange1>& list) :
90  range_(), elements_range_(), ranges_(list.begin(), list.end())
91  {
92  init();
93  }
94 
96  TiledRange(const TiledRange_& other) :
97  range_(other.range_), elements_range_(other.elements_range_), ranges_(other.ranges_)
98  { }
99 
101 
104  if(this != &other)
105  TiledRange_(other).swap(*this);
106  return *this;
107  }
108 
110 
113  TA_ASSERT(p.dim() == range_.rank());
114  Ranges temp = p * ranges_;
115  TiledRange(temp.begin(), temp.end()).swap(*this);
116  return *this;
117  }
118 
120 
122  const range_type& tiles_range() const {
123  return range_;
124  }
125 
127 
130  DEPRECATED const range_type& tiles() const {
131  return tiles_range();
132  }
133 
135 
137  const range_type& elements_range() const {
138  return elements_range_;
139  }
140 
142 
145  DEPRECATED const range_type& elements() const {
146  return elements_range();
147  }
148 
150 
156  range_type tile(const size_type& i) const {
157  return make_tile_range(i);
158  }
159 
161 
166  TA_ASSERT(tiles_range().includes(i));
167  return make_tile_range(tiles_range().idx(i));
168  }
169 
171 
177  template <typename Index>
178  typename std::enable_if<!std::is_integral<Index>::value,
179  range_type>::type
180  tile(const Index& index) const {
181  return make_tile_range(index);
182  }
183 
185 
189  template <typename Index>
190  typename std::enable_if<! std::is_integral<Index>::value, range_type>::type
191  make_tile_range(const Index& index) const {
192  const auto rank = range_.rank();
193  TA_ASSERT(index.size() == rank);
194  TA_ASSERT(range_.includes(index));
195  typename range_type::index lower;
196  typename range_type::index upper;
197  lower.reserve(rank);
198  upper.reserve(rank);
199  unsigned d = 0;
200  for(const auto& index_d: index) {
201  lower.push_back(data()[d].tile(index_d).first);
202  upper.push_back(data()[d].tile(index_d).second);
203  ++d;
204  }
205 
206  return range_type(lower, upper);
207  }
208 
210 
216  template <typename Integer>
217  range_type
218  tile(const std::initializer_list<Integer>& index) const {
219  return make_tile_range(index);
220  }
221 
223 
227  template <typename Integer>
228  range_type
229  make_tile_range(const std::initializer_list<Integer>& index) const {
230  return make_tile_range<std::initializer_list<Integer>>(index);
231  }
232 
234 
238  template <typename Index>
239  typename std::enable_if<! std::is_integral<Index>::value, typename range_type::index>::type
240  element_to_tile(const Index& index) const {
241  const unsigned int rank = range_.rank();
242  typename range_type::index result;
243  result.reserve(rank);
244  for(size_type i = 0; i < rank; ++i)
245  result.push_back(ranges_[i].element_to_tile(index[i]));
246 
247  return result;
248  }
249 
251 
253  std::size_t rank() const { return ranges_.size(); }
254 
256 
259  const TiledRange1& dim(std::size_t d) const {
260  TA_ASSERT(d < rank());
261  return ranges_[d];
262  }
263 
265 
268  const Ranges& data() const { return ranges_; }
269 
270 
271  void swap(TiledRange_& other) {
272  range_.swap(other.range_);
273  elements_range_.swap(other.elements_range_);
274  std::swap(ranges_, other.ranges_);
275  }
276 
277  private:
278  range_type range_;
279  range_type elements_range_;
280  Ranges ranges_;
281  };
282 
284 
288  inline TiledRange operator *(const Permutation& p, const TiledRange& r) {
289  TA_ASSERT(r.tiles_range().rank() == p.dim());
290  TiledRange::Ranges data = p * r.data();
291 
292  return TiledRange(data.begin(), data.end());
293  }
294 
296  inline void swap(TiledRange& r0, TiledRange& r1) { r0.swap(r1); }
297 
299  inline bool operator ==(const TiledRange& r1, const TiledRange& r2) {
300  return (r1.tiles_range().rank() == r2.tiles_range().rank()) &&
301  (r1.tiles_range() == r2.tiles_range()) && (r1.elements_range() == r2.elements_range()) &&
302  std::equal(r1.data().begin(), r1.data().end(), r2.data().begin());
303  }
304 
305  inline bool operator !=(const TiledRange& r1, const TiledRange& r2) {
306  return ! operator ==(r1, r2);
307  }
308 
309  inline std::ostream& operator<<(std::ostream& out, const TiledRange& rng) {
310  out << "(" << " tiles = " << rng.tiles_range()
311  << ", elements = " << rng.elements_range() << " )";
312  return out;
313  }
314 
315 } // namespace TiledArray
316 
317 
318 #endif // RANGE_H__INCLUDED
std::vector< size_type > index
Coordinate index type.
Definition: range.h:43
void swap(TiledRange_ &other)
Definition: tiled_range.h:271
constexpr bool operator==(const DenseShape &a, const DenseShape &b)
Definition: dense_shape.h:178
A (hyperrectangular) interval on , space of integer n-indices.
Definition: range.h:39
void swap(TiledRange &r0, TiledRange &r1)
Exchange the content of the two given TiledRange&#39;s.
Definition: tiled_range.h:296
void swap(Range &r0, Range &r1)
Exchange the values of the give two ranges.
Definition: range.h:1048
TiledRange TiledRange_
Definition: tiled_range.h:63
#define DEPRECATED
Definition: error.h:148
auto data(T &t)
Container data pointer accessor.
Definition: utility.h:89
TiledRange(const std::initializer_list< std::initializer_list< size_type > > &list)
Constructed with a set of ranges pointed to by [ first, last ).
Definition: tiled_range.h:82
std::size_t rank() const
The rank accessor.
Definition: tiled_range.h:253
void swap(Range_ &other)
Definition: range.h:943
constexpr bool operator!=(const DenseShape &a, const DenseShape &b)
Definition: dense_shape.h:179
std::array< T, N > operator*(const Permutation &, const std::array< T, N > &)
Permute a std::array.
Definition: permutation.h:484
unsigned int rank() const
Rank accessor.
Definition: range.h:542
const Ranges & data() const
Tile dimension boundary array accessor.
Definition: tiled_range.h:268
range_type::size_array size_array
Definition: tiled_range.h:67
index_type dim() const
Domain size accessor.
Definition: permutation.h:206
TiledRange_ & operator*=(const Permutation &p)
In place permutation of this range.
Definition: tiled_range.h:112
range_type make_tile_range(const size_type &i) const
Construct a range for the tile indexed by the given ordinal index.
Definition: tiled_range.h:165
const range_type & tiles_range() const
Access the tile range.
Definition: tiled_range.h:122
TiledRange(InIter first, InIter last)
Constructed with a set of ranges pointed to by [ first, last ).
Definition: tiled_range.h:75
#define TA_ASSERT(a)
Definition: error.h:107
TiledRange()
Default constructor.
Definition: tiled_range.h:71
std::size_t size_type
Definition: tiled_range.h:65
std::vector< TiledRange1 > Ranges
Definition: tiled_range.h:68
range_type tile(const size_type &i) const
Constructs a range for the tile indexed by the given ordinal index.
Definition: tiled_range.h:156
std::enable_if<! std::is_integral< Index >::value, range_type >::type make_tile_range(const Index &index) const
Construct a range for the tile indexed by the given index.
Definition: tiled_range.h:191
Range data of a tiled array.
Definition: tiled_range.h:31
TiledRange_ & operator=(const TiledRange_ &other)
TiledRange assignment operator.
Definition: tiled_range.h:103
std::ostream & operator<<(std::ostream &os, const DistArray< Tile, Policy > &a)
Add the tensor to an output stream.
Definition: dist_array.h:853
bool includes(const Index &index) const
Check the coordinate to make sure it is within the range.
Definition: range.h:682
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:229
Permutation of a sequence of objects indexed by base-0 indices.
Definition: permutation.h:119
DEPRECATED const range_type & tiles() const
Access the tile range.
Definition: tiled_range.h:130
TiledRange(const TiledRange_ &other)
Copy constructor.
Definition: tiled_range.h:96
range_type::index index
Definition: tiled_range.h:66
const TiledRange1 & dim(std::size_t d) const
Accessor of the tiled range for one of the dimensions.
Definition: tiled_range.h:259
TiledRange(const std::initializer_list< TiledRange1 > &list)
Constructed with an initializer_list of TiledRange1&#39;s.
Definition: tiled_range.h:89
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:218
std::enable_if<!std::is_integral< Index >::value, range_type >::type tile(const Index &index) const
Construct a range for the tile indexed by the given index.
Definition: tiled_range.h:180
DEPRECATED const range_type & elements() const
Access the element range.
Definition: tiled_range.h:145
std::enable_if<! std::is_integral< Index >::value, typename range_type::index >::type element_to_tile(const Index &index) const
Convert an element index to a tile index.
Definition: tiled_range.h:240
const range_type & elements_range() const
Access the element range.
Definition: tiled_range.h:137