DASH  0.3.0
ForEach.h
1 #ifndef DASH__ALGORITHM__FOR_EACH_H__
2 #define DASH__ALGORITHM__FOR_EACH_H__
3 
4 #include <dash/algorithm/LocalRange.h>
5 #include <dash/iterator/GlobIter.h>
6 
7 #include <algorithm>
8 
9 
10 namespace dash {
11 
31 template <typename GlobInputIt, class UnaryFunction>
32 void for_each(
34  const GlobInputIt& first,
36  const GlobInputIt& last,
38  UnaryFunction func)
39 {
41  static_assert(
42  iterator_traits::is_global_iterator::value,
43  "must be a global iterator");
45  auto index_range = dash::local_index_range(first, last);
46  auto lbegin_index = index_range.begin;
47  auto lend_index = index_range.end;
48  auto & team = first.pattern().team();
49  if (lbegin_index != lend_index) {
50  // Pattern from global begin iterator:
51  auto & pattern = first.pattern();
52  // Local range to native pointers:
53  auto lrange_begin = (first + pattern.global(lbegin_index)).local();
54  auto lrange_end = lrange_begin + lend_index;
55  std::for_each(lrange_begin, lrange_end, func);
56  }
57  team.barrier();
58 }
59 
79 template <typename GlobInputIt, class UnaryFunctionWithIndex>
82  const GlobInputIt& first,
84  const GlobInputIt& last,
86  UnaryFunctionWithIndex func)
87 {
89  static_assert(
90  iterator_traits::is_global_iterator::value,
91  "must be a global iterator");
92 
94  auto index_range = dash::local_index_range(first, last);
95  auto lbegin_index = index_range.begin;
96  auto lend_index = index_range.end;
97  auto & team = first.pattern().team();
98  if (lbegin_index != lend_index) {
99  // Pattern from global begin iterator:
100  auto & pattern = first.pattern();
101  auto first_offset = first.pos();
102  // Iterate local index range:
103  for (auto lindex = lbegin_index;
104  lindex != lend_index;
105  ++lindex) {
106  auto gindex = pattern.global(lindex);
107  auto element_it = first + (gindex - first_offset);
108  func(*(element_it.local()), gindex);
109  }
110  }
111  team.barrier();
112 }
113 
114 } // namespace dash
115 
116 #endif // DASH__ALGORITHM__FOR_EACH_H__
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
Returns second operand.
Definition: Operation.h:201
void for_each_with_index(const GlobInputIt &first, const GlobInputIt &last, UnaryFunctionWithIndex func)
Invoke a function on every element in a range distributed by a pattern.
Definition: ForEach.h:80
void for_each(const GlobInputIt &first, const GlobInputIt &last, UnaryFunction func)
Invoke a function on every element in a range distributed by a pattern.
Definition: ForEach.h:32
std::enable_if< !GlobInputIter::has_view::value, LocalIndexRange< typename GlobInputIter::pattern_type::index_type >>::type local_index_range(const GlobInputIter &first, const GlobInputIter &last)
Resolves the local index range between global iterators.
Definition: LocalRange.h:101