Skip to content

Commit

Permalink
More interesting sum.
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricchevalier19 committed Dec 13, 2023
1 parent 9a56583 commit 3ca2e38
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions examples/kokkos/simple_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,20 @@ int main(int argc, char* argv[]) {
Sdouble sum = 0;

Kokkos::parallel_reduce(
n, KOKKOS_LAMBDA(const int i, Sdouble& lsum) { lsum += i * i; }, sum);
n, KOKKOS_LAMBDA(const int i, Sdouble& lsum) { auto val = Sdouble(i+1); lsum += 1/val * 1/val; }, sum);

std::cout <<
"Sum of squares of integers from 0 to " << n-1 <<
"Sum from 0 to " << n-1 <<
", computed in parallel, is " << sum << std::endl;

// Compare to a sequential loop.
Sdouble seqSum = 0;
for (int i = 0; i < n; ++i) {
seqSum += i * i;
Sdouble val = i + 1;
seqSum += 1/val * 1/val;
}
std::cout <<
"Sum of squares of integers from 0 to " << n-1 <<
"Sum from 0 to " << n-1 <<
", computed in sequential, is " << seqSum << std::endl;

Kokkos::finalize();
Expand Down

0 comments on commit 3ca2e38

Please sign in to comment.