From 894829cb05143c908923a831e9f0d13072ef640a Mon Sep 17 00:00:00 2001 From: Matthew Martineau Date: Fri, 6 May 2016 21:02:44 +0100 Subject: [PATCH] Adjusted the Kokkos implementation to fix view initialisation, and store local copies of views for lambda scoping --- KOKKOSStream.cpp | 74 +++++++++++++++++++++++++++++------------------- KOKKOSStream.hpp | 19 +++++++------ main.cpp | 2 ++ 3 files changed, 58 insertions(+), 37 deletions(-) diff --git a/KOKKOSStream.cpp b/KOKKOSStream.cpp index c4d548b..3834081 100644 --- a/KOKKOSStream.cpp +++ b/KOKKOSStream.cpp @@ -7,7 +7,7 @@ #include "KOKKOSStream.hpp" -using Kokkos::parallel_for; +using namespace Kokkos; template KOKKOSStream::KOKKOSStream( @@ -16,21 +16,21 @@ KOKKOSStream::KOKKOSStream( { Kokkos::initialize(); - new(d_a) Kokkos::View("d_a", ARRAY_SIZE); - new(d_b) Kokkos::View("d_b", ARRAY_SIZE); - new(d_c) Kokkos::View("d_c", ARRAY_SIZE); - new(hm_a) Kokkos::View::HostMirror(); - new(hm_b) Kokkos::View::HostMirror(); - new(hm_c) Kokkos::View::HostMirror(); - hm_a = Kokkos::create_mirror_view(d_a); - hm_b = Kokkos::create_mirror_view(d_b); - hm_c = Kokkos::create_mirror_view(d_c); + d_a = new View("d_a", ARRAY_SIZE); + d_b = new View("d_b", ARRAY_SIZE); + d_c = new View("d_c", ARRAY_SIZE); + hm_a = new View::HostMirror(); + hm_b = new View::HostMirror(); + hm_c = new View::HostMirror(); + *hm_a = create_mirror_view(*d_a); + *hm_b = create_mirror_view(*d_b); + *hm_c = create_mirror_view(*d_c); } template KOKKOSStream::~KOKKOSStream() { - Kokkos::finalize(); + finalize(); } template @@ -39,65 +39,81 @@ void KOKKOSStream::write_arrays( { for(int ii = 0; ii < array_size; ++ii) { - hm_a(ii) = a[ii]; - hm_b(ii) = b[ii]; - hm_c(ii) = c[ii]; + (*hm_a)(ii) = a[ii]; + (*hm_b)(ii) = b[ii]; + (*hm_c)(ii) = c[ii]; } - Kokkos::deep_copy(hm_a, d_a); - Kokkos::deep_copy(hm_b, d_b); - Kokkos::deep_copy(hm_c, d_c); + deep_copy(*hm_a, *d_a); + deep_copy(*hm_b, *d_b); + deep_copy(*hm_c, *d_c); } template void KOKKOSStream::read_arrays( std::vector& a, std::vector& b, std::vector& c) { - Kokkos::deep_copy(d_a, hm_a); - Kokkos::deep_copy(d_a, hm_b); - Kokkos::deep_copy(d_a, hm_c); + deep_copy(*d_a, *hm_a); + deep_copy(*d_b, *hm_b); + deep_copy(*d_c, *hm_c); for(int ii = 0; ii < array_size; ++ii) { - a[ii] = hm_a(ii); - b[ii] = hm_b(ii); - c[ii] = hm_c(ii); + a[ii] = (*hm_a)(ii); + b[ii] = (*hm_b)(ii); + c[ii] = (*hm_c)(ii); } } template void KOKKOSStream::copy() { - Kokkos::parallel_for(array_size, KOKKOS_LAMBDA (const int index) + View a(*d_a); + View b(*d_b); + View c(*d_c); + + parallel_for(array_size, KOKKOS_LAMBDA (const int index) { - d_c[index] = d_a[index]; + c[index] = a[index]; }); } template void KOKKOSStream::mul() { + View a(*d_a); + View b(*d_b); + View c(*d_c); + const T scalar = 3.0; parallel_for(array_size, KOKKOS_LAMBDA (const int index) { - d_b[index] = scalar*d_c[index]; + b[index] = scalar*c[index]; }); } template void KOKKOSStream::add() { + View a(*d_a); + View b(*d_b); + View c(*d_c); + parallel_for(array_size, KOKKOS_LAMBDA (const int index) { - d_c[index] = d_a[index] + d_b[index]; + c[index] = a[index] + b[index]; }); } template void KOKKOSStream::triad() { + View a(*d_a); + View b(*d_b); + View c(*d_c); + const T scalar = 3.0; parallel_for(array_size, KOKKOS_LAMBDA (const int index) { - d_a[index] = d_b[index] + scalar*d_c[index]; + a[index] = b[index] + scalar*c[index]; }); } @@ -118,6 +134,6 @@ std::string getDeviceDriver(const int device) return "Kokkos"; } -template class KOKKOSStream; +//template class KOKKOSStream; template class KOKKOSStream; diff --git a/KOKKOSStream.hpp b/KOKKOSStream.hpp index 632ca20..d2b9665 100644 --- a/KOKKOSStream.hpp +++ b/KOKKOSStream.hpp @@ -17,8 +17,11 @@ #define IMPLEMENTATION_STRING "KOKKOS" -#define DEVICE Kokkos::OpenMP - +#ifdef KOKKOS_TARGET_CPU + #define DEVICE Kokkos::OpenMP +#else + #define DEVICE Kokkos::Cuda +#endif template class KOKKOSStream : public Stream @@ -28,12 +31,12 @@ class KOKKOSStream : public Stream unsigned int array_size; // Device side pointers to arrays - Kokkos::View d_a; - Kokkos::View d_b; - Kokkos::View d_c; - Kokkos::View::HostMirror hm_a; - Kokkos::View::HostMirror hm_b; - Kokkos::View::HostMirror hm_c; + Kokkos::View* d_a; + Kokkos::View* d_b; + Kokkos::View* d_c; + Kokkos::View::HostMirror* hm_a; + Kokkos::View::HostMirror* hm_b; + Kokkos::View::HostMirror* hm_c; public: diff --git a/main.cpp b/main.cpp index 4794f9b..007ab7f 100644 --- a/main.cpp +++ b/main.cpp @@ -59,9 +59,11 @@ int main(int argc, char *argv[]) // TODO: Fix SYCL to allow multiple template specializations #ifndef SYCL +#ifndef KOKKOS if (use_float) run(); else +#endif #endif run();