-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_reduce.cxx
67 lines (54 loc) · 1.64 KB
/
multi_reduce.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <string>
#include <Kokkos_Core.hpp>
#include "common.hpp"
using namespace std;
static constexpr int DIM = 3;
using view_t = Kokkos::View<int*>;
using pair_t = Kokkos::pair<int, int>;
pair_t& operator+=(pair_t& lhs, const pair_t& rhs)
{
lhs.first += rhs.first;
lhs.second += rhs.second;
return lhs;
}
int main(int argc, char** argv)
{
Kokkos::initialize(argc, argv); {
view_t view1("view1", DIM);
for (int i = 0; i < DIM; ++i) {
view1(i) = i+1;
}
pair_t sum = {0, 0};
// Doesn't work using pairs directly
// Kokkos::parallel_reduce(DIM, KOKKOS_LAMBDA(const int i, pair_t& sum_inner) {
// const int item = view1(i);
// sum_inner.first += item;
// if (item == 2) {
// sum_inner.second = i;
// }
// }, Kokkos::Sum<pair_t>(sum));
Kokkos::parallel_reduce(DIM, KOKKOS_LAMBDA(const int i, int& sum_inner1, int& sum_inner2) {
const int item = view1(i);
sum_inner1 += item;
if (item == 2) {
sum_inner2 = i;
}
}, sum.first, sum.second);
std::cout << "First run: " << std::endl;
std::cout << "Sum is: " << sum.first << std::endl;
std::cout << "Idx is: " << sum.second << std::endl;
Kokkos::parallel_reduce(DIM, KOKKOS_LAMBDA(const int i, int& sum_inner1, int& sum_inner2) {
const int item = view1(i);
sum_inner1 += item;
if (item == 2) {
sum_inner2 = i;
}
}, sum.first, sum.second);
std::cout << "First run: " << std::endl;
std::cout << "Sum is: " << sum.first << std::endl;
std::cout << "Idx is: " << sum.second << std::endl;
}
Kokkos::finalize();
return 0;
}