Adjusted the Kokkos implementation to fix view initialisation, and store local copies of views for lambda scoping

This commit is contained in:
Matthew Martineau 2016-05-06 21:02:44 +01:00
parent 1a60f130eb
commit 894829cb05
3 changed files with 58 additions and 37 deletions

View File

@ -7,7 +7,7 @@
#include "KOKKOSStream.hpp" #include "KOKKOSStream.hpp"
using Kokkos::parallel_for; using namespace Kokkos;
template <class T> template <class T>
KOKKOSStream<T>::KOKKOSStream( KOKKOSStream<T>::KOKKOSStream(
@ -16,21 +16,21 @@ KOKKOSStream<T>::KOKKOSStream(
{ {
Kokkos::initialize(); Kokkos::initialize();
new(d_a) Kokkos::View<double*, DEVICE>("d_a", ARRAY_SIZE); d_a = new View<double*, DEVICE>("d_a", ARRAY_SIZE);
new(d_b) Kokkos::View<double*, DEVICE>("d_b", ARRAY_SIZE); d_b = new View<double*, DEVICE>("d_b", ARRAY_SIZE);
new(d_c) Kokkos::View<double*, DEVICE>("d_c", ARRAY_SIZE); d_c = new View<double*, DEVICE>("d_c", ARRAY_SIZE);
new(hm_a) Kokkos::View<double*>::HostMirror(); hm_a = new View<double*, DEVICE>::HostMirror();
new(hm_b) Kokkos::View<double*>::HostMirror(); hm_b = new View<double*, DEVICE>::HostMirror();
new(hm_c) Kokkos::View<double*>::HostMirror(); hm_c = new View<double*, DEVICE>::HostMirror();
hm_a = Kokkos::create_mirror_view(d_a); *hm_a = create_mirror_view(*d_a);
hm_b = Kokkos::create_mirror_view(d_b); *hm_b = create_mirror_view(*d_b);
hm_c = Kokkos::create_mirror_view(d_c); *hm_c = create_mirror_view(*d_c);
} }
template <class T> template <class T>
KOKKOSStream<T>::~KOKKOSStream() KOKKOSStream<T>::~KOKKOSStream()
{ {
Kokkos::finalize(); finalize();
} }
template <class T> template <class T>
@ -39,65 +39,81 @@ void KOKKOSStream<T>::write_arrays(
{ {
for(int ii = 0; ii < array_size; ++ii) for(int ii = 0; ii < array_size; ++ii)
{ {
hm_a(ii) = a[ii]; (*hm_a)(ii) = a[ii];
hm_b(ii) = b[ii]; (*hm_b)(ii) = b[ii];
hm_c(ii) = c[ii]; (*hm_c)(ii) = c[ii];
} }
Kokkos::deep_copy(hm_a, d_a); deep_copy(*hm_a, *d_a);
Kokkos::deep_copy(hm_b, d_b); deep_copy(*hm_b, *d_b);
Kokkos::deep_copy(hm_c, d_c); deep_copy(*hm_c, *d_c);
} }
template <class T> template <class T>
void KOKKOSStream<T>::read_arrays( void KOKKOSStream<T>::read_arrays(
std::vector<T>& a, std::vector<T>& b, std::vector<T>& c) std::vector<T>& a, std::vector<T>& b, std::vector<T>& c)
{ {
Kokkos::deep_copy(d_a, hm_a); deep_copy(*d_a, *hm_a);
Kokkos::deep_copy(d_a, hm_b); deep_copy(*d_b, *hm_b);
Kokkos::deep_copy(d_a, hm_c); deep_copy(*d_c, *hm_c);
for(int ii = 0; ii < array_size; ++ii) for(int ii = 0; ii < array_size; ++ii)
{ {
a[ii] = hm_a(ii); a[ii] = (*hm_a)(ii);
b[ii] = hm_b(ii); b[ii] = (*hm_b)(ii);
c[ii] = hm_c(ii); c[ii] = (*hm_c)(ii);
} }
} }
template <class T> template <class T>
void KOKKOSStream<T>::copy() void KOKKOSStream<T>::copy()
{ {
Kokkos::parallel_for(array_size, KOKKOS_LAMBDA (const int index) View<double*, DEVICE> a(*d_a);
View<double*, DEVICE> b(*d_b);
View<double*, DEVICE> c(*d_c);
parallel_for(array_size, KOKKOS_LAMBDA (const int index)
{ {
d_c[index] = d_a[index]; c[index] = a[index];
}); });
} }
template <class T> template <class T>
void KOKKOSStream<T>::mul() void KOKKOSStream<T>::mul()
{ {
View<double*, DEVICE> a(*d_a);
View<double*, DEVICE> b(*d_b);
View<double*, DEVICE> c(*d_c);
const T scalar = 3.0; const T scalar = 3.0;
parallel_for(array_size, KOKKOS_LAMBDA (const int index) parallel_for(array_size, KOKKOS_LAMBDA (const int index)
{ {
d_b[index] = scalar*d_c[index]; b[index] = scalar*c[index];
}); });
} }
template <class T> template <class T>
void KOKKOSStream<T>::add() void KOKKOSStream<T>::add()
{ {
View<double*, DEVICE> a(*d_a);
View<double*, DEVICE> b(*d_b);
View<double*, DEVICE> c(*d_c);
parallel_for(array_size, KOKKOS_LAMBDA (const int index) 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 <class T> template <class T>
void KOKKOSStream<T>::triad() void KOKKOSStream<T>::triad()
{ {
View<double*, DEVICE> a(*d_a);
View<double*, DEVICE> b(*d_b);
View<double*, DEVICE> c(*d_c);
const T scalar = 3.0; const T scalar = 3.0;
parallel_for(array_size, KOKKOS_LAMBDA (const int index) 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"; return "Kokkos";
} }
template class KOKKOSStream<float>; //template class KOKKOSStream<float>;
template class KOKKOSStream<double>; template class KOKKOSStream<double>;

View File

@ -17,8 +17,11 @@
#define IMPLEMENTATION_STRING "KOKKOS" #define IMPLEMENTATION_STRING "KOKKOS"
#define DEVICE Kokkos::OpenMP #ifdef KOKKOS_TARGET_CPU
#define DEVICE Kokkos::OpenMP
#else
#define DEVICE Kokkos::Cuda
#endif
template <class T> template <class T>
class KOKKOSStream : public Stream<T> class KOKKOSStream : public Stream<T>
@ -28,12 +31,12 @@ class KOKKOSStream : public Stream<T>
unsigned int array_size; unsigned int array_size;
// Device side pointers to arrays // Device side pointers to arrays
Kokkos::View<double*, DEVICE> d_a; Kokkos::View<double*, DEVICE>* d_a;
Kokkos::View<double*, DEVICE> d_b; Kokkos::View<double*, DEVICE>* d_b;
Kokkos::View<double*, DEVICE> d_c; Kokkos::View<double*, DEVICE>* d_c;
Kokkos::View<double*>::HostMirror hm_a; Kokkos::View<double*>::HostMirror* hm_a;
Kokkos::View<double*>::HostMirror hm_b; Kokkos::View<double*>::HostMirror* hm_b;
Kokkos::View<double*>::HostMirror hm_c; Kokkos::View<double*>::HostMirror* hm_c;
public: public:

View File

@ -59,9 +59,11 @@ int main(int argc, char *argv[])
// TODO: Fix SYCL to allow multiple template specializations // TODO: Fix SYCL to allow multiple template specializations
#ifndef SYCL #ifndef SYCL
#ifndef KOKKOS
if (use_float) if (use_float)
run<float>(); run<float>();
else else
#endif
#endif #endif
run<double>(); run<double>();