-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathkernels.hpp
36 lines (30 loc) · 1.17 KB
/
kernels.hpp
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
#pragma once
//-------------------------------------------------------------------------------------//
// Sample computation: S(N) = 1 + 2 + 3 + ... + N
// Tests: regions, allocation, parallel for and reduction
template <typename data_type>
int run_calculation(const data_type SIZE) {
Kokkos::Profiling::pushRegion("Computation");
Kokkos::View<data_type*> data(Kokkos::ViewAllocateWithoutInitializing("data"),
SIZE);
Kokkos::parallel_for(
"initialize()", SIZE, KOKKOS_LAMBDA(data_type i) { data(i) = i; });
Kokkos::fence();
data_type sum = 0;
Kokkos::parallel_reduce(
"accumulate()", SIZE,
KOKKOS_LAMBDA(data_type i, data_type & lsum) { lsum += 1 + data(i); },
sum);
Kokkos::fence();
Kokkos::Profiling::popRegion();
// check results
const data_type check = (SIZE + 1) * SIZE / 2;
if (sum != check) {
std::cout << "BAD result, got S(" << SIZE << ") = " << sum << " (expected "
<< check << ")" << std::endl;
return 1;
}
std::cout << "Result OK: S(" << SIZE << ") = " << sum << std::endl;
return 0;
}
//-------------------------------------------------------------------------------------//