DASH  0.3.0
Bcast.h
1 #ifndef DASH__ALGORITHM__BCAST_H__
2 #define DASH__ALGORITHM__BCAST_H__
3 
4 #include <dash/iterator/GlobIter.h>
5 #include <dash/iterator/IteratorTraits.h>
6 
7 #include <dash/Team.h>
8 #include <dash/Shared.h>
9 #include <dash/algorithm/LocalRange.h>
10 #include <dash/Coarray.h>
11 
12 #include <iterator>
13 #include <vector>
14 #include <algorithm>
15 
16 
17 namespace dash {
18 
19 namespace internal {
20 
21 template<class LocalInputIter>
22 void
23 broadcast(
24  LocalInputIter in_first,
25  LocalInputIter in_last,
26  dash::team_unit_t root,
27  dash::Team & team,
28  std::random_access_iterator_tag)
29 {
30  using value_t = typename std::iterator_traits<LocalInputIter>::value_type;
31  auto myid = team.myid();
32  auto nelem = std::distance(in_first, in_last);
33  auto ds = dash::dart_storage<value_t>(nelem);
34 
35  DASH_ASSERT_RETURNS(
36  dart_bcast(&(*in_first), ds.nelem, ds.dtype, root, team.dart_id()),
37  DART_OK);
38 }
39 
40 template<class LocalInputIter, class IterTag>
41 void
42 broadcast(
43  LocalInputIter in_first,
44  LocalInputIter in_last,
45  dash::team_unit_t root,
46  dash::Team & team,
47  IterTag tag = {})
48 {
49  using value_t = typename std::iterator_traits<LocalInputIter>::value_type;
50 
51  // create a contiguous buffer
52  std::vector<value_t> tmp(in_first, in_last);
53 
54  // broadcast
55  dash::internal::broadcast(tmp.begin(), tmp.end(), root, team,
56  std::random_access_iterator_tag{});
57 
58  // copy back into non-contiguous memory
59  if (team.myid() != root) {
60  std::copy(tmp.begin(), tmp.end(), in_first);
61  }
62 }
63 
64 } // namespace internal
65 
89 template <
90  class LocalInputIter,
91  typename = typename std::enable_if<
92  !dash::detail::is_global_iterator<LocalInputIter>::value
93  >::type>
94 void
96  LocalInputIter in_first,
97  LocalInputIter in_last,
98  dash::team_unit_t root,
99  dash::Team & team = dash::Team::All())
100 {
102  in_first, in_last, root, team,
103  typename std::iterator_traits<LocalInputIter>::iterator_category());
104 }
105 
123 template <class ValueType>
125 {
126  ValueType res{};
127  auto& team = shared.team();
128  auto owner = shared.owner();
129  ValueType *ptr = (team.myid() == owner) ? shared.local() : std::addressof(res);
130  dash::broadcast(ptr,
131  std::next(ptr),
132  owner,
133  shared.team());
134 
135  return *ptr;
136 }
137 
151 template<typename T>
152 void broadcast(Coarray<T> & coarr, const team_unit_t & root){
153  dash::coarray::cobroadcast(coarr, root);
154 }
155 
156 } // namespace dash
157 
158 #endif // DASH__ALGORITHM__BCAST_H__
global_unit_t myid()
Shortcut to query the global unit ID of the calling unit.
dash::Team & team() const noexcept
The dash::Team that created this shared object.
Definition: Shared.h:289
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
dart_ret_t dart_bcast(void *buf, size_t nelem, dart_datatype_t dtype, dart_team_unit_t root, dart_team_t team)
DART Equivalent to MPI broadcast.
A Team instance specifies a subset of all available units.
Definition: Team.h:41
void broadcast(LocalInputIter in_first, LocalInputIter in_last, dash::team_unit_t root, dash::Team &team=dash::Team::All())
Broadcast the local range of elements [in_first, in_last) from unit root to all other units in team...
Definition: Bcast.h:95
void cobroadcast(Coarray< T > &coarr, const team_unit_t &master)
Broadcasts the value on master to all other members of this co_array.
Definition: Utils.h:142
dash::team_unit_t owner() const noexcept
The unit owning the memory in the global address space.
Definition: Shared.h:281
struct dash::unit_id< dash::local_unit, dart_team_unit_t > team_unit_t
Unit ID to use for team-local IDs.
Definition: Types.h:319
OutputIt copy(InputIt in_first, InputIt in_last, OutputIt out_first)
Copies the elements in the range, defined by [in_first, in_last), to another range beginning at out_f...
void broadcast(Coarray< T > &coarr, const team_unit_t &root)
Broadcasts the value on root to all other members of this co_array.
Definition: Bcast.h:152
struct dash::dart_operation ValueType
Reduce operands to their minimum value.
Shared access to a value in global memory across a team.
Definition: Shared.h:23
constexpr value_type const * local() const noexcept
Native pointer to the starting address of the local memory of the unit that initialized this dash::Sh...
Definition: Shared.h:264
A fortran style coarray.
Definition: Coarray.h:184
Convencience wrapper to determine the DART type and number of elements required for the given templat...
Definition: Types.h:295
static Team & All()
The invariant Team instance containing all available units.
Definition: Team.h:213
dart_team_t dart_id() const
Index of this team relative to global team dash::Team::All().
Definition: Team.h:522