| 1 |
//@HEADER |
| 2 |
// ************************************************************************ |
| 3 |
// |
| 4 |
// Kokkos v. 4.0 |
| 5 |
// Copyright (2022) National Technology & Engineering |
| 6 |
// Solutions of Sandia, LLC (NTESS). |
| 7 |
// |
| 8 |
// Under the terms of Contract DE-NA0003525 with NTESS, |
| 9 |
// the U.S. Government retains certain rights in this software. |
| 10 |
// |
| 11 |
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions. |
| 12 |
// See https://kokkos.org/LICENSE for license information. |
| 13 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 14 |
// |
| 15 |
//@HEADER |
| 16 |
|
| 17 |
#include <Kokkos_Core.hpp> |
| 18 |
|
| 19 |
#include <cstdio> |
| 20 |
#include <iostream> |
| 21 |
|
| 22 |
extern "C" void print_fortran_(); |
| 23 |
void print_cxx(); |
| 24 |
|
| 25 |
struct CountEvenIntegers { |
| 26 |
KOKKOS_FUNCTION void operator()(const long i, long& lcount) const { |
| 27 |
lcount += (i % 2) == 0; |
| 28 |
} |
| 29 |
}; |
| 30 |
|
| 31 |
int main(int argc, char* argv[]) { |
| 32 |
Kokkos::ScopeGuard guard(argc, argv); |
| 33 |
Kokkos::DefaultExecutionSpace().print_configuration(std::cout); |
| 34 |
|
| 35 |
const long n = argc > 1 ? atoi(argv[1]) : 10; |
| 36 |
|
| 37 |
printf("Number of even integers from 0 to %ld\n", n - 1); |
| 38 |
|
| 39 |
Kokkos::Timer timer; |
| 40 |
timer.reset(); |
| 41 |
|
| 42 |
// Compute the number of even integers from 0 to n-1, in parallel. |
| 43 |
long count = 0; |
| 44 |
CountEvenIntegers functor; |
| 45 |
Kokkos::parallel_reduce(n, functor, count); |
| 46 |
|
| 47 |
double count_time = timer.seconds(); |
| 48 |
printf(" Parallel: %ld %10.6f\n", count, count_time); |
| 49 |
|
| 50 |
timer.reset(); |
| 51 |
|
| 52 |
// Compare to a sequential loop. |
| 53 |
long seq_count = 0; |
| 54 |
for (long i = 0; i < n; ++i) { |
| 55 |
seq_count += (i % 2) == 0; |
| 56 |
} |
| 57 |
|
| 58 |
count_time = timer.seconds(); |
| 59 |
printf("Sequential: %ld %10.6f\n", seq_count, count_time); |
| 60 |
|
| 61 |
print_fortran_(); |
| 62 |
|
| 63 |
return (count == seq_count) ? 0 : -1; |
| 64 |
} |