DASH  0.3.0
ViewMod.h
1 #ifndef DASH__VIEW__VIEW_MOD_H__INCLUDED
2 #define DASH__VIEW__VIEW_MOD_H__INCLUDED
3 
4 #include <dash/Types.h>
5 #include <dash/Range.h>
6 #include <dash/Iterator.h>
7 
8 #include <dash/util/UniversalMember.h>
9 #include <dash/util/ArrayExpr.h>
10 
11 #include <dash/view/IndexSet.h>
12 #include <dash/view/ViewTraits.h>
13 #include <dash/view/ViewIterator.h>
14 #include <dash/view/ViewOrigin.h>
15 
16 #include <dash/view/Local.h>
17 #include <dash/view/Global.h>
18 #include <dash/view/Origin.h>
19 #include <dash/view/Domain.h>
20 #include <dash/view/Apply.h>
21 
22 
23 namespace dash {
24 
115 #ifndef DOXYGEN
116 
117 // ------------------------------------------------------------------------
118 // Forward-declarations
119 // ------------------------------------------------------------------------
120 
121 template <
122  class ViewModType,
123  class DomainType,
124  dim_t NDim = dash::view_traits<
125  typename std::decay<DomainType>::type>::rank::value >
126 class ViewModBase;
127 
128 template <
129  class DomainType,
130  dim_t NDim = dash::view_traits<
131  typename std::decay<DomainType>::type>::rank::value >
132 class ViewLocalMod;
133 
134 template <
135  class DomainType,
136  dim_t SubDim = 0,
137  dim_t NDim = dash::view_traits<
138  typename std::decay<DomainType>::type>::rank::value >
139 class ViewSubMod;
140 
141 template <
142  class DomainType,
143  dim_t NDim = dash::view_traits<
144  typename std::decay<DomainType>::type>::rank::value >
145 class ViewGlobalMod;
146 
147 #endif // DOXYGEN
148 
149 
150 
151 // ------------------------------------------------------------------------
152 // ViewModBase
153 // ------------------------------------------------------------------------
154 
155 template <
156  class ViewModType,
157  class DomainType,
158  dim_t NDim >
160 {
162 public:
163  typedef DomainType domain_type;
164 
165  typedef typename std::conditional<
167  const domain_type &,
168  domain_type
169  >::type
170  domain_member_type;
171 
172  // TODO: BUG!
173  // For example, assume
174  // domain = sub(local(array))
175  // then view_traits<domain>::is_local resolves to `true`
176  // and domain_type is defined as ViewSub<ViewLocal<Array>>
177  // instead of ViewLocal<Array> or Array::local_type.
178  //
179  // Note that the origin of ViewLocalMod is the global origin
180  // while the origin of and view on ViewLocalMod is the local
181  // origin.
182  typedef typename std::conditional<
184  // domain_type,
185  typename view_traits<
186  typename view_traits<domain_type>::origin_type
187  >::local_type,
188  typename view_traits<domain_type>::origin_type
189  >::type
190  origin_type;
191 
192  typedef decltype(
193  dash::begin(
194  std::declval<
195  typename std::add_lvalue_reference<origin_type>::type
196  >() ))
197  origin_iterator;
198 
199  typedef decltype(
200  dash::begin(
201  std::declval<
202  typename std::add_lvalue_reference<const origin_type>::type
203  >() ))
204  const_origin_iterator;
205 
206  typedef
207  decltype(*dash::begin(
208  std::declval<
209  typename std::add_lvalue_reference<origin_type>::type
210  >() ))
211  reference;
212 
213  typedef
214  decltype(*dash::begin(
215  std::declval<
216  typename std::add_lvalue_reference<const origin_type>::type
217  >() ))
218  const_reference;
219 
220  typedef typename view_traits<DomainType>::index_type index_type;
221  typedef typename view_traits<DomainType>::size_type size_type;
222  typedef typename origin_type::value_type value_type;
223 
224  typedef std::integral_constant<dim_t, NDim> rank;
225 
226  static constexpr dim_t ndim() { return NDim; }
227 
228 protected:
229  domain_member_type _domain;
230 
231  ViewModType & derived() {
232  return static_cast<ViewModType &>(*this);
233  }
234  constexpr const ViewModType & derived() const {
235  return static_cast<const ViewModType &>(*this);
236  }
237 
241  constexpr explicit ViewModBase(domain_type && domain)
242  : _domain(std::forward<domain_type>(domain))
243  { }
244 
248  constexpr explicit ViewModBase(const domain_type & domain)
249  : _domain(domain)
250  { }
251 
252  constexpr ViewModBase() = delete;
253  ~ViewModBase() = default;
254 
255 public:
256  constexpr ViewModBase(const self_t &) = default;
257  constexpr ViewModBase(self_t &&) = default;
258  self_t & operator=(const self_t &) = default;
259  self_t & operator=(self_t &&) = default;
260 
261  constexpr const domain_type & domain() const & {
262  return _domain;
263  }
264 
265  constexpr domain_type domain() const && {
266  return _domain;
267  }
268 
269  constexpr bool operator==(const ViewModType & rhs) const {
270  return &derived() == &rhs;
271  }
272 
273  constexpr bool operator!=(const ViewModType & rhs) const {
274  return !(derived() == rhs);
275  }
276 
277  constexpr bool is_local() const {
279  }
280 
281  // ---- extents ---------------------------------------------------------
282 
283  constexpr const std::array<size_type, NDim> extents() const {
284  return domain().extents();
285  }
286 
287  template <dim_t ShapeDim>
288  constexpr size_type extent() const {
289  return domain().template extent<ShapeDim>();
290  }
291 
292  constexpr size_type extent(dim_t shape_dim) const {
293  return domain().extent(shape_dim);
294  }
295 
296  // ---- offsets ---------------------------------------------------------
297 
298  constexpr const std::array<index_type, NDim> offsets() const {
299  return domain().offsets();
300  }
301 
302  template <dim_t ShapeDim>
303  constexpr index_type offset() const {
304  return domain().template offset<ShapeDim>();
305  }
306 
307  constexpr index_type offset(dim_t shape_dim) const {
308  return domain().offset(shape_dim);
309  }
310 
311  // ---- size ------------------------------------------------------------
312 
313  constexpr index_type size() const {
314  return dash::index(derived()).size();
315  }
316 };
317 
318 
319 // ------------------------------------------------------------------------
320 // ViewLocalMod
321 // ------------------------------------------------------------------------
322 
323 template <
324  class DomainType,
325  dim_t NDim >
326 struct view_traits<ViewLocalMod<DomainType, NDim> > {
327  typedef DomainType domain_type;
328  typedef typename view_traits<domain_type>::origin_type origin_type;
329  typedef typename view_traits<domain_type>::pattern_type pattern_type;
330  typedef typename domain_type::local_type image_type;
332  typedef domain_type global_type;
333 
334  typedef typename view_traits<domain_type>::index_type index_type;
335  typedef typename view_traits<domain_type>::size_type size_type;
336  typedef dash::IndexSetLocal<DomainType> index_set_type;
337 
338  typedef std::integral_constant<bool, false> is_projection;
339  typedef std::integral_constant<bool, true> is_view;
340  typedef std::integral_constant<bool, false> is_origin;
341  typedef std::integral_constant<bool, true> is_local;
342 
343  typedef std::integral_constant<dim_t, DomainType::rank::value> rank;
344 };
345 
346 template <
347  class DomainType,
348  dim_t NDim >
350 : public ViewModBase<
351  ViewLocalMod<DomainType, NDim>,
352  DomainType,
353  NDim > {
354 public:
355  typedef DomainType domain_type;
356  typedef typename view_traits<DomainType>::origin_type origin_type;
357  typedef typename domain_type::local_type image_type;
358  typedef typename view_traits<DomainType>::index_type index_type;
359  typedef typename view_traits<DomainType>::size_type size_type;
360 private:
362  typedef ViewModBase<
363  ViewLocalMod<DomainType, NDim>, DomainType, NDim > base_t;
364 public:
365  typedef dash::IndexSetLocal<DomainType> index_set_type;
366  typedef self_t local_type;
367  typedef typename domain_type::global_type global_type;
368 
369  typedef std::integral_constant<bool, true> is_local;
370 
371  typedef
372  decltype(
373  dash::begin(
375  std::declval<
376  typename std::add_lvalue_reference<domain_type>::type >()
377  ))))
378  iterator;
379 
380  typedef
381  decltype(
382  dash::begin(
384  std::declval<
385  typename std::add_lvalue_reference<const domain_type>::type >()
386  ))))
387  const_iterator;
388 
389  typedef
390  decltype(
391  *(dash::begin(
393  std::declval<
394  typename std::add_lvalue_reference<domain_type>::type >()
395  )))))
396  reference;
397 
398  typedef
399  decltype(
400  *(dash::begin(
402  std::declval<
403  typename std::add_lvalue_reference<const domain_type>::type >()
404  )))))
405  const_reference;
406 
407 private:
408  index_set_type _index_set;
409 public:
410  constexpr ViewLocalMod() = delete;
411  constexpr ViewLocalMod(self_t &&) = default;
412  constexpr ViewLocalMod(const self_t &) = default;
413  ~ViewLocalMod() = default;
414  self_t & operator=(self_t &&) = default;
415  self_t & operator=(const self_t &) = default;
416 
420  constexpr explicit ViewLocalMod(
421  domain_type && domain)
422  : base_t(std::forward<domain_type>(domain))
423  , _index_set(this->domain())
424  { }
425 
429  constexpr explicit ViewLocalMod(
430  const DomainType & domain)
431  : base_t(domain)
432  , _index_set(domain)
433  { }
434 
435  constexpr bool operator==(const self_t & rhs) const {
436  return (this == &rhs ||
437  ( base_t::operator==(rhs) &&
438  _index_set == rhs._index_set ) );
439  }
440 
441  constexpr bool operator!=(const self_t & rhs) const {
442  return not (*this == rhs);
443  }
444 
445  // ---- extents ---------------------------------------------------------
446 
447  constexpr const std::array<size_type, NDim> extents() const {
448  return _index_set.extents();
449  }
450 
451  template <dim_t ShapeDim>
452  constexpr size_type extent() const {
453  return _index_set.template extent<ShapeDim>();
454  }
455 
456  constexpr size_type extent(dim_t shape_dim) const {
457  return _index_set.extent(shape_dim);
458  }
459 
460  // ---- offsets ---------------------------------------------------------
461 
462  constexpr const std::array<index_type, NDim> offsets() const {
463  return _index_set.offsets();
464  }
465 
466  // ---- size ------------------------------------------------------------
467 
468  constexpr size_type size(dim_t sub_dim = 0) const {
469  return index_set().size(sub_dim);
470  }
471 
472  // ---- access ----------------------------------------------------------
473 
474  constexpr const_iterator begin() const {
475  return dash::begin(
476  dash::local(
477  dash::origin(*this) ))
478  + _index_set[0];
479  }
480 
481  iterator begin() {
482  return dash::begin(
483  dash::local(
484  const_cast<origin_type &>(dash::origin(*this))
485  ))
486  + _index_set[0];
487  }
488 
489  constexpr const_iterator end() const {
490  return dash::begin(
491  dash::local(
492  dash::origin(*this) ))
493  + _index_set[_index_set.size() - 1] + 1;
494  }
495 
496  iterator end() {
497  return dash::begin(
498  dash::local(
499  const_cast<origin_type &>(dash::origin(*this))
500  ))
501  + _index_set[_index_set.size() - 1] + 1;
502  }
503 
504  constexpr const_reference operator[](int offset) const {
505  return *(dash::begin(
506  dash::local(
507  dash::origin(*this) ))
508  + _index_set[offset]);
509  }
510 
511  reference operator[](int offset) {
512  return *(dash::begin(
513  dash::local(
514  const_cast<origin_type &>(dash::origin(*this))
515  ))
516  + _index_set[offset]);
517  }
518 
519  constexpr const local_type & local() const {
520  return *this;
521  }
522 
523  local_type & local() {
524  return *this;
525  }
526 
527  constexpr const global_type & global() const {
528  return dash::global(dash::domain(*this));
529  }
530 
531  constexpr const index_set_type & index_set() const {
532  return _index_set;
533  }
534 };
535 
536 
537 // ------------------------------------------------------------------------
538 // ViewSubMod
539 // ------------------------------------------------------------------------
540 
541 template <
542  class DomainType,
543  dim_t SubDim,
544  dim_t NDim >
545 struct view_traits<ViewSubMod<DomainType, SubDim, NDim> > {
546  typedef DomainType domain_type;
547  typedef typename view_traits<domain_type>::origin_type origin_type;
548  typedef typename view_traits<domain_type>::pattern_type pattern_type;
550 //typedef ViewSubMod<DomainType, SubDim, NDim> local_type;
551  typedef ViewLocalMod<
553  typedef ViewSubMod<DomainType, SubDim, NDim> global_type;
554 
555  typedef typename DomainType::index_type index_type;
556  typedef typename DomainType::size_type size_type;
557  typedef dash::IndexSetSub<DomainType, SubDim> index_set_type;
558 
559  typedef std::integral_constant<bool, false> is_projection;
560  typedef std::integral_constant<bool, true> is_view;
561  typedef std::integral_constant<bool, false> is_origin;
562  typedef std::integral_constant<bool,
564 
565  typedef std::integral_constant<dim_t, DomainType::rank::value> rank;
566 };
567 
568 
569 template <
570  class DomainType,
571  dim_t SubDim,
572  dim_t NDim >
574 : public ViewModBase<
575  ViewSubMod<DomainType, SubDim, NDim>,
576  DomainType,
577  NDim >
578 {
579  public:
580  typedef DomainType domain_type;
581  private:
583  typedef ViewModBase<
585  domain_type, NDim > base_t;
586  public:
587  typedef typename base_t::origin_type origin_type;
588 
589  typedef typename view_traits<domain_type>::index_type index_type;
590  typedef typename view_traits<domain_type>::size_type size_type;
591  public:
593  typedef self_t global_type;
594 
595  typedef std::integral_constant<bool, false> is_local;
596 
597  typedef dash::IndexSetSub<domain_type, SubDim> index_set_type;
598 
599  typedef ViewIterator<
600  typename base_t::origin_iterator, index_set_type >
601  iterator;
602  typedef ViewIterator<
603  typename base_t::const_origin_iterator, index_set_type >
605 
606  using reference = typename base_t::reference;
607  using const_reference = typename base_t::const_reference;
608 
609  private:
610  index_set_type _index_set;
611 
612  public:
613  constexpr ViewSubMod() = delete;
614  constexpr ViewSubMod(self_t &&) = default;
615  constexpr ViewSubMod(const self_t &) = default;
616  ~ViewSubMod() = default;
617  self_t & operator=(self_t &&) = default;
618  self_t & operator=(const self_t &) = default;
619 
620  constexpr ViewSubMod(
621  domain_type && domain,
622  index_type begin,
623  index_type end)
624  : base_t(std::forward<domain_type>(domain))
625  , _index_set(this->domain(), begin, end)
626  { }
627 
628  constexpr ViewSubMod(
629  const domain_type & domain,
630  index_type begin,
631  index_type end)
632  : base_t(domain)
633  , _index_set(domain, begin, end)
634  { }
635 
636  // ---- extents ---------------------------------------------------------
637 
638  constexpr std::array<size_type, NDim> extents() const {
639  return _index_set.extents();
640  }
641 
642  template <dim_t ExtDim>
643  constexpr size_type extent() const {
644  return _index_set.template extent<ExtDim>();
645  }
646 
647  constexpr size_type extent(dim_t shape_dim) const {
648  return _index_set.extent(shape_dim);
649  }
650 
651  // ---- offsets ---------------------------------------------------------
652 
653  template <dim_t ExtDim>
654  constexpr index_type offset() const {
655  return _index_set.template offset<ExtDim>();
656  }
657 
658  constexpr std::array<index_type, NDim> offsets() const {
659  return _index_set.offsets();
660  }
661 
662  constexpr index_type offset(dim_t shape_dim) const {
663  return _index_set.offset(shape_dim);
664  }
665 
666  // ---- size ------------------------------------------------------------
667 
668  constexpr size_type size(dim_t sub_dim = 0) const {
669  return _index_set.size(sub_dim);
670  }
671 
672  // ---- access ----------------------------------------------------------
673 
674  constexpr const_iterator begin() const {
675  return const_iterator(
676  dash::origin(*this).begin(),
677  _index_set, 0);
678  }
679 
680  iterator begin() {
681  return iterator(
682  const_cast<origin_type &>(
683  dash::origin(*this)
684  ).begin(),
685  _index_set, 0);
686  }
687 
688  constexpr const_iterator end() const {
689  return const_iterator(
690  dash::origin(*this).begin(),
691  _index_set, _index_set.size());
692  }
693 
694  iterator end() {
695  return iterator(
696  const_cast<origin_type &>(
697  dash::origin(*this)
698  ).begin(),
699  _index_set, _index_set.size());
700  }
701 
702  constexpr const_reference operator[](int offset) const {
703  return *(const_iterator(dash::origin(*this).begin(),
704  _index_set, offset));
705  }
706 
707  reference operator[](int offset) {
708  return *(iterator(const_cast<origin_type &>(
709  dash::origin(*this)
710  ).begin(),
711  _index_set, offset));
712  }
713 
714  constexpr const index_set_type & index_set() const {
715  return _index_set;
716  }
717 
718  constexpr local_type local() const {
719  return local_type(*this);
720  }
721 };
722 
723 
724 } // namespace dash
725 
726 #include <dash/view/ViewMod1D.h>
727 
728 #endif // DASH__VIEW__VIEW_MOD_H__INCLUDED
constexpr std::enable_if< std::is_integral< IndexType >::value, IndexType >::type index(IndexType idx)
Definition: Iterator.h:60
size_t size()
Return the number of units in the global team.
constexpr auto end(RangeType &&range) -> decltype(std::forward< RangeType >(range).end())
Definition: Range.h:98
constexpr auto local(ViewType &v) -> typename std::enable_if<(std::is_pointer< typename ViewType::iterator >::value||(dash::view_traits< ViewValueT >::is_local::value)), ViewType &>::type
Definition: Local.h:28
This class is a simple memory pool which holds allocates elements of size ValueType.
Definition: AllOf.h:8
constexpr ViewLocalMod(const DomainType &domain)
Constructor, creates a view on a given domain.
Definition: ViewMod.h:429
constexpr auto begin(RangeType &&range) -> decltype(std::forward< RangeType >(range).begin())
Definition: Range.h:89
int dim_t
Scalar type for a dimension value, with 0 indicating the first dimension.
Definition: Types.h:39
dash::view_traits< ViewT >::origin_type origin(ViewT &view)
constexpr std::enable_if< dash::view_traits< ViewType >::is_view::value &&dash::view_traits< ViewType >::is_local::value, const typename ViewType::global_type & >::type global(const ViewType &v)
Definition: Global.h:19
View type traits.
Definition: ViewTraits.h:31
constexpr DimensionalType::extent_type extent(const DimensionalType &d)
Definition: Dimensional.h:73
constexpr ViewLocalMod(domain_type &&domain)
Constructor, creates a view on a given domain.
Definition: ViewMod.h:420
see https://en.cppreference.com/w/cpp/feature_test for recommended feature tests
Definition: cstddef.h:8
constexpr auto domain(ViewT &&view) -> typename std::enable_if< dash::detail::has_type_domain_type< ViewValueT >::value, decltype(std::forward< ViewT >(view).domain()) >::type
Definition: Domain.h:23