DASH  0.3.0
Find.h
1 #ifndef DASH__ALGORITHM__FIND_H__
2 #define DASH__ALGORITHM__FIND_H__
3 
4 #include <dash/Array.h>
5 #include <dash/algorithm/LocalRange.h>
6 #include <dash/algorithm/Operation.h>
8 #include <dash/iterator/GlobIter.h>
9 
10 namespace dash {
11 
19 template<
20  typename GlobIter,
21  typename ElementType>
26  GlobIter last,
28  const ElementType & value)
29 {
30 
31  //use iterator traits
33 
34  using p_index_t = typename iterator_traits::index_type;
35 
36  if(first >= last) {
37  return last;
38  }
39 
40  p_index_t g_index;
41  auto & pattern = first.pattern();
42  auto & team = pattern.team();
43  auto index_range = dash::local_index_range(first, last);
44  auto l_begin_index = index_range.begin;
45  auto l_end_index = index_range.end;
46  auto first_offset = first.pos();
47  if(l_begin_index == l_end_index){
48  g_index = std::numeric_limits<p_index_t>::max();
49  } else {
50  auto g_begin_index = pattern.global(l_begin_index);
51 
52  // Pointer to first element in local memory:
53  auto const* lbegin = dash::local_begin(
54  static_cast<typename GlobIter::pointer>(first), team.myid());
55  // Pointers to first / final element in local range:
56  const auto l_range_begin = lbegin + l_begin_index;
57  const auto l_range_end = lbegin + l_end_index;
58 
59  DASH_LOG_DEBUG("local index range", l_begin_index, l_end_index);
60 
61  auto l_result = std::find(l_range_begin, l_range_end, value);
62  if (l_result == l_range_end) {
63  DASH_LOG_DEBUG("Not found in local range");
64  g_index = std::numeric_limits<p_index_t>::max();
65  } else {
66  auto l_hit_index = l_result - lbegin;
67  g_index = pattern.global(l_hit_index);
68  }
69  }
70  team.barrier();
71 
72  // receive buffer for global maximal index
73  p_index_t g_hit_idx;
74 
75  DASH_ASSERT_RETURNS(
77  &g_index,
78  &g_hit_idx,
79  1,
82  team.dart_id()),
83  DART_OK);
84 
85  if (g_hit_idx == std::numeric_limits<p_index_t>::max()) {
86  DASH_LOG_DEBUG("element not found");
87  } else {
88  return first + g_hit_idx;
89  }
90  return last;
91 }
92 
103 template <typename GlobIter, typename UnaryPredicate>
106  GlobIter first,
108  GlobIter last,
110  UnaryPredicate predicate)
111 {
113 
114  using index_t = typename iterator_traits::index_type;
115 
116  auto& team = first.pattern().team();
117  auto myid = team.myid();
119  auto index_range = dash::local_range(first, last);
120  auto l_first = index_range.begin;
121  auto l_last = index_range.end;
122 
123  auto l_result = std::find_if(l_first, l_last, predicate);
124  auto l_offset = l_result == l_last ? -1 : std::distance(l_first, l_result);
125 
126  dash::Array<index_t> l_results(team.size(), team);
127 
128  l_results.local[0] = l_offset;
129 
130  team.barrier();
131 
132  // All local offsets stored in l_results
133  auto result = last;
134 
135  for (auto u = 0; u < team.size(); u++) {
136  if (l_results[u] >= 0) {
137  auto g_offset = first.pattern().global_index(u, {l_results[u]});
138  result = first + g_offset - first.pos();
139  break;
140  }
141  }
142 
143  team.barrier();
144  return result;
145 }
146 
157 template <
158  typename GlobIter,
159  class UnaryPredicate>
162  GlobIter first,
164  GlobIter last,
166  UnaryPredicate predicate)
167 {
168  return find_if(first, last, std::not1(predicate));
169 }
170 
171 } // namespace dash
172 
173 #endif // DASH__ALGORITHM__FIND_H__
DASH_CONSTEXPR std::enable_if< std::is_same< typename dash::memory_space_traits< MemSpaceT >::memory_space_layout_tag, memory_space_contiguous >::value, T >::type * local_begin(GlobPtr< T, MemSpaceT > global_begin, dash::team_unit_t unit)
Returns the begin of the local memory portion within a global memory segment.
Definition: GlobPtr.h:593
global_unit_t myid()
Shortcut to query the global unit ID of the calling unit.
This class is a simple memory pool which holds allocates elements of size ValueType.
Definition: AllOf.h:8
Signals success.
Definition: dart_types.h:33
RandomAccessIt::difference_type distance(const RandomAccessIt &first, const RandomAccessIt &last)
Resolve the number of elements between two iterators.
Definition: Iterator.h:90
LocalRange< const typename GlobIterType::value_type > local_range(const GlobIterType &first, const GlobIterType &last)
Resolves the local address range between global iterators.
Definition: LocalRange.h:295
GlobIter find_if_not(GlobIter first, GlobIter last, UnaryPredicate predicate)
Returns an iterator to the first element in the range [first,last) that does not satisfy the predicat...
Definition: Find.h:160
GlobIter find_if(GlobIter first, GlobIter last, UnaryPredicate predicate)
Returns an iterator to the first element in the range [first,last) that satisfies the predicate p...
Definition: Find.h:104
Returns second operand.
Definition: Operation.h:201
dart_ret_t dart_allreduce(const void *sendbuf, void *recvbuf, size_t nelem, dart_datatype_t dtype, dart_operation_t op, dart_team_t team)
DART Equivalent to MPI allreduce.
Iterator on Partitioned Global Address Space.
Definition: GlobIter.h:45
DASH_CONSTEXPR index_type pos() const DASH_NOEXCEPT
Position of the iterator in global index space.
Definition: GlobIter.h:336
A distributed array.
Definition: Array.h:89
Type trait for mapping to DART data types.
Definition: Types.h:96
GlobIter find(GlobIter first, GlobIter last, const ElementType &value)
Returns an iterator to the first element in the range [first,last) that compares equal to val...
Definition: Find.h:22
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
Minimum.
Definition: dart_types.h:73
local_type local
Local proxy object, allows use in range-based for loops.
Definition: Array.h:732