diff --git a/examples/kokkos/CMakeLists.txt b/examples/kokkos/CMakeLists.txt index 05cf82d..b59da42 100644 --- a/examples/kokkos/CMakeLists.txt +++ b/examples/kokkos/CMakeLists.txt @@ -1,5 +1,5 @@ find_package(Kokkos REQUIRED) add_executable(kokkos_simple simple_example.cpp) -#target_link_libraries(kokkos_simple PUBLIC shaman) +target_link_libraries(kokkos_simple PUBLIC shaman) target_link_libraries(kokkos_simple PUBLIC Kokkos::kokkos) diff --git a/examples/kokkos/simple_example.cpp b/examples/kokkos/simple_example.cpp index 2578ccd..ee4aaf0 100644 --- a/examples/kokkos/simple_example.cpp +++ b/examples/kokkos/simple_example.cpp @@ -15,7 +15,8 @@ //@HEADER #include -#include + +#include // // First reduction (parallel_reduce) example: @@ -31,39 +32,31 @@ int main(int argc, char* argv[]) { Kokkos::initialize(argc, argv); - const int n = 10; + const int n = 1<<24; // Compute the sum of squares of integers from 0 to n-1, in // parallel, using Kokkos. This time, use a lambda instead of a // functor. The lambda takes the same arguments as the functor's // operator(). - int sum = 0; -// The KOKKOS_LAMBDA macro replaces the capture-by-value clause [=]. -// It also handles any other syntax needed for CUDA. -// We also need to protect the usage of a lambda against compiling -// with a backend which doesn't support it (i.e. Cuda 6.5/7.0). -#if defined(KOKKOS_ENABLE_CXX11_DISPATCH_LAMBDA) + Sdouble sum = 0; + Kokkos::parallel_reduce( - n, KOKKOS_LAMBDA(const int i, int& lsum) { lsum += i * i; }, sum); -#endif - printf( - "Sum of squares of integers from 0 to %i, " - "computed in parallel, is %i\n", - n - 1, sum); + n, KOKKOS_LAMBDA(const int i, Sdouble& lsum) { lsum += i * i; }, sum); + + std::cout << + "Sum of squares of integers from 0 to " << n-1 << + ", computed in parallel, is " << sum << std::endl; // Compare to a sequential loop. - int seqSum = 0; + Sdouble seqSum = 0; for (int i = 0; i < n; ++i) { seqSum += i * i; } - printf( - "Sum of squares of integers from 0 to %i, " - "computed sequentially, is %i\n", - n - 1, seqSum); + std::cout << + "Sum of squares of integers from 0 to " << n-1 << + ", computed in sequential, is " << seqSum << std::endl; + Kokkos::finalize(); -#if defined(KOKKOS_ENABLE_CXX11_DISPATCH_LAMBDA) - return (sum == seqSum) ? 0 : -1; -#else + return 0; -#endif }