DASH  0.3.0
main.cpp
1 
7 #include <unistd.h>
8 #include <iostream>
9 #include <sstream>
10 #include <libdash.h>
11 
12 using namespace std;
13 
14 typedef struct test_t_s {
15  int a;
16  double b;
17 } test_t;
18 
19 std::ostream & operator<<(std::ostream & os, const test_t & test)
20 {
21  std::ostringstream ss;
22  ss << "test_t(a:" << test.a << " b:" << test.b << ")";
23  return operator<<(os, ss.str());
24 }
25 
26 int main(int argc, char * argv[])
27 {
28  dash::init(&argc, &argv);
29 
30  dash::Array<int> arr(100);
31 
32  cout << "Unit " << dash::myid() << " PID: " << getpid()
33  << endl;
34  arr.barrier();
35 
36  if (dash::myid() == 0) {
37  for (auto i = 0; i < arr.size(); i++ ) {
38  arr[i] = i;
39  }
40  }
41  arr.barrier();
42 
43  if (dash::myid() == 0) {
44  cout << "dash::min_element on dash::Array<int>" << endl;
45  }
46  // progressively restrict the global range from the front until
47  // reaching arr.end(); call min_element() for each sub-range
48  auto it = arr.begin();
49  while (it != arr.end()) {
50  auto min = dash::min_element(it, arr.end());
51  if (dash::myid() == 0) {
52  std::stringstream ss;
53  ss << "Min: " << (int)(*min) << endl;
54  cout << ss.str();
55  }
56  it++;
57  }
58 
59  dash::Array<test_t> arr2(100);
60  for (auto & el : arr2.local) {
61  el = {rand() % 100, 23.3};
62  }
63  arr2.barrier();
64 
65  if (dash::myid() == 0) {
66  cout << "dash::min_element on dash::Array<test_t>" << endl;
67  }
68  arr2.barrier();
69 
70  // the following shows the usage of min_element with a composite
71  // datatype (test_t). We're passing a comparator function as a
72  // lambda expression
73  auto min = dash::min_element(
74  arr2.begin(),
75  arr2.end(),
76  [](const test_t & t1, const test_t & t2) -> bool {
77  return t1.a < t2.a;
78  }
79  );
80 
81  if (dash::myid() == 0) {
82  cout << "Min. test_t: " << ((test_t)*min).a
83  << " " << ((test_t)*min).b
84  << endl;
85  }
86 
88 }
global_unit_t myid()
Shortcut to query the global unit ID of the calling unit.
iterator end() noexcept
Global pointer to the end of the array.
Definition: Array.h:1057
void finalize()
Finalize the DASH library and the underlying runtime system.
const ElementType * min_element(const ElementType *l_range_begin, const ElementType *l_range_end, Compare compare=std::less< const ElementType &>())
Finds an iterator pointing to the element with the smallest value in the range [first,last).
Definition: MinMax.h:47
A distributed array.
Definition: Array.h:89
constexpr size_type size() const noexcept
The size of the array.
Definition: Array.h:1173
void barrier() const
Establish a barrier for all units operating on the array, publishing all changes to all units...
Definition: Array.h:1254
see https://en.cppreference.com/w/cpp/feature_test for recommended feature tests
Definition: cstddef.h:8
void init(int *argc, char ***argv)
Initialize the DASH library and the underlying runtime system.
iterator begin() noexcept
Global pointer to the beginning of the array.
Definition: Array.h:1040
local_type local
Local proxy object, allows use in range-based for loops.
Definition: Array.h:732