Initial commit of in progress developments of RAJA and KOKKOS stream

This commit is contained in:
Matthew Martineau 2016-05-06 10:46:35 +01:00
parent 0a738efa54
commit 45381da0b2
4 changed files with 339 additions and 0 deletions

123
KOKKOSStream.cpp Normal file
View File

@ -0,0 +1,123 @@
// Copyright (c) 2015-16 Tom Deakin, Simon McIntosh-Smith,
// University of Bristol HPC
//
// For full license terms please see the LICENSE file distributed with this
// source code
#include "KOKKOSStream.hpp"
using Kokkos::parallel_for;
template <class T>
KOKKOSStream<T>::KOKKOSStream(
const unsigned int ARRAY_SIZE, const int device_index)
: array_size(ARRAY_SIZE)
{
Kokkos::initialize();
new(d_a) Kokkos::View<double*, DEVICE>("d_a", ARRAY_SIZE);
new(d_b) Kokkos::View<double*, DEVICE>("d_b", ARRAY_SIZE);
new(d_c) Kokkos::View<double*, DEVICE>("d_c", ARRAY_SIZE);
new(hm_a) Kokkos::View<double*>::HostMirror();
new(hm_b) Kokkos::View<double*>::HostMirror();
new(hm_c) Kokkos::View<double*>::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);
}
template <class T>
KOKKOSStream<T>::~KOKKOSStream()
{
Kokkos::finalize();
}
template <class T>
void KOKKOSStream<T>::write_arrays(
const std::vector<T>& a, const std::vector<T>& b, const std::vector<T>& c)
{
for(int ii = 0; ii < array_size; ++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);
}
template <class T>
void KOKKOSStream<T>::read_arrays(
std::vector<T>& a, std::vector<T>& b, std::vector<T>& c)
{
Kokkos::deep_copy(d_a, hm_a);
Kokkos::deep_copy(d_a, hm_b);
Kokkos::deep_copy(d_a, 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);
}
}
template <class T>
void KOKKOSStream<T>::copy()
{
Kokkos::parallel_for(array_size, KOKKOS_LAMBDA (const int index)
{
d_c[index] = d_a[index];
});
}
template <class T>
void KOKKOSStream<T>::mul()
{
const T scalar = 3.0;
parallel_for(array_size, KOKKOS_LAMBDA (const int index)
{
d_b[index] = scalar*d_c[index];
});
}
template <class T>
void KOKKOSStream<T>::add()
{
parallel_for(array_size, KOKKOS_LAMBDA (const int index)
{
d_c[index] = d_a[index] + d_b[index];
});
}
template <class T>
void KOKKOSStream<T>::triad()
{
const T scalar = 3.0;
parallel_for(array_size, KOKKOS_LAMBDA (const int index)
{
d_a[index] = d_b[index] + scalar*d_c[index];
});
}
void listDevices(void)
{
std::cout << "This is not the device you are looking for.";
}
std::string getDeviceName(const int device)
{
return "Kokkos";
}
std::string getDeviceDriver(const int device)
{
return "Kokkos";
}
template class KOKKOSStream<float>;
template class KOKKOSStream<double>;

53
KOKKOSStream.hpp Normal file
View File

@ -0,0 +1,53 @@
// Copyright (c) 2015-16 Tom Deakin, Simon McIntosh-Smith,
// University of Bristol HPC
//
// For full license terms please see the LICENSE file distributed with this
// source code
#pragma once
#include <iostream>
#include <stdexcept>
#include <Kokkos_Core.hpp>
#include <Kokkos_Parallel.hpp>
#include <Kokkos_View.hpp>
#include "Stream.h"
#define IMPLEMENTATION_STRING "KOKKOS"
#define DEVICE Kokkos::OpenMP
template <class T>
class KOKKOSStream : public Stream<T>
{
protected:
// Size of arrays
unsigned int array_size;
// Device side pointers to arrays
Kokkos::View<double*, DEVICE> d_a;
Kokkos::View<double*, DEVICE> d_b;
Kokkos::View<double*, DEVICE> d_c;
Kokkos::View<double*>::HostMirror hm_a;
Kokkos::View<double*>::HostMirror hm_b;
Kokkos::View<double*>::HostMirror hm_c;
public:
KOKKOSStream(const unsigned int, const int);
~KOKKOSStream();
virtual void copy() override;
virtual void add() override;
virtual void mul() override;
virtual void triad() override;
virtual void write_arrays(
const std::vector<T>& a, const std::vector<T>& b, const std::vector<T>& c) override;
virtual void read_arrays(
std::vector<T>& a, std::vector<T>& b, std::vector<T>& c) override;
};

105
RAJAStream.cpp Normal file
View File

@ -0,0 +1,105 @@
// Copyright (c) 2015-16 Tom Deakin, Simon McIntosh-Smith,
// University of Bristol HPC
//
// For full license terms please see the LICENSE file distributed with this
// source code
#include "RAJAStream.hpp"
using RAJA::forall;
using RAJA::RangeSegment;
template <class T>
RAJAStream<T>::RAJAStream(const unsigned int ARRAY_SIZE, const int device_index)
: array_size(ARRAY_SIZE)
{
RangeSegment seg(0, ARRAY_SIZE);
index_set.push_back(seg);
d_a = new T[ARRAY_SIZE];
d_b = new T[ARRAY_SIZE];
d_c = new T[ARRAY_SIZE];
}
template <class T>
RAJAStream<T>::~RAJAStream()
{
delete[] d_a;
delete[] d_b;
delete[] d_c;
}
template <class T>
void RAJAStream<T>::write_arrays(const std::vector<T>& a, const std::vector<T>& b, const std::vector<T>& c)
{
std::copy(a.begin(), a.end(), d_a);
std::copy(b.begin(), b.end(), d_b);
std::copy(c.begin(), c.end(), d_c);
}
template <class T>
void RAJAStream<T>::read_arrays(std::vector<T>& a, std::vector<T>& b, std::vector<T>& c)
{
std::copy(d_a, d_a + array_size - 1, a.data());
std::copy(d_b, d_b + array_size - 1, b.data());
std::copy(d_c, d_c + array_size - 1, c.data());
}
template <class T>
void RAJAStream<T>::copy()
{
forall<policy>(index_set, [=] RAJA_DEVICE (int index)
{
d_c[index] = d_a[index];
});
}
template <class T>
void RAJAStream<T>::mul()
{
const T scalar = 3.0;
forall<policy>(index_set, [=] RAJA_DEVICE (int index)
{
d_b[index] = scalar*d_c[index];
});
}
template <class T>
void RAJAStream<T>::add()
{
forall<policy>(index_set, [=] RAJA_DEVICE (int index)
{
d_c[index] = d_a[index] + d_b[index];
});
}
template <class T>
void RAJAStream<T>::triad()
{
const T scalar = 3.0;
forall<policy>(index_set, [=] RAJA_DEVICE (int index)
{
d_a[index] = d_b[index] + scalar*d_c[index];
});
}
void listDevices(void)
{
std::cout << "This is not the device you are looking for.";
}
std::string getDeviceName(const int device)
{
return "RAJA";
}
std::string getDeviceDriver(const int device)
{
return "RAJA";
}
template class RAJAStream<float>;
template class RAJAStream<double>;

58
RAJAStream.hpp Normal file
View File

@ -0,0 +1,58 @@
// Copyright (c) 2015-16 Tom Deakin, Simon McIntosh-Smith,
// University of Bristol HPC
//
// For full license terms please see the LICENSE file distributed with this
// source code
#pragma once
#include <iostream>
#include <stdexcept>
#include "RAJA/RAJA.hxx"
#include "Stream.h"
#define IMPLEMENTATION_STRING "RAJA"
#ifdef RAJA_USE_CUDA
const size_t block_size = 128;
typedef RAJA::IndexSet::ExecPolicy<
RAJA::seq_segit,
RAJA::cuda_exec_async<block_size>> policy;
#else
typedef RAJA::IndexSet::ExecPolicy<
RAJA::seq_segit,
RAJA::omp_parallel_for_exec> policy;
#endif
template <class T>
class RAJAStream : public Stream<T>
{
protected:
// Size of arrays
unsigned int array_size;
// Contains iteration space
RAJA::IndexSet index_set;
// Device side pointers to arrays
T* d_a;
T* d_b;
T* d_c;
public:
RAJAStream(const unsigned int, const int);
~RAJAStream();
virtual void copy() override;
virtual void add() override;
virtual void mul() override;
virtual void triad() override;
virtual void write_arrays(
const std::vector<T>& a, const std::vector<T>& b, const std::vector<T>& c) override;
virtual void read_arrays(
std::vector<T>& a, std::vector<T>& b, std::vector<T>& c) override;
};