Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kokkos #3271

Closed
wants to merge 10 commits into from
1 change: 1 addition & 0 deletions bindings/CXX11/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ install(
adios2/cxx11/Operator.h
adios2/cxx11/Query.h
adios2/cxx11/Types.h
adios2/cxx11/kokkos_interop.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/adios2/cxx11
COMPONENT adios2_cxx11-development
)
Expand Down
81 changes: 81 additions & 0 deletions bindings/CXX11/adios2/cxx11/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,38 @@
namespace adios2
{

namespace detail
{

template <class...>
using void_t = void;

} // end namespace detail

template <typename C, typename = void>
struct ndarray_traits : std::false_type
{
};

template <typename C>
struct ndarray_traits<
C, detail::void_t<typename C::value_type, typename C::pointer,
typename C::size_type, decltype(std::declval<C>().data()),
decltype(std::declval<C>().size()),
// don't match std::vector since it's handled separately
typename std::enable_if<!std::is_same<
C, std::vector<typename C::value_type>>::type>>>
: std::true_type
{
using value_type = typename C::value_type;
using pointer = typename C::pointer;
using size_type = typename C::size_type;
static constexpr auto memory_space = adios2::MemorySpace::Detect;

static pointer data(C &c) { return c.data(); }
static size_type size(C &c) { return c.size(); }
};

/// \cond EXCLUDE_FROM_DOXYGEN
// forward declare
class IO; // friend
Expand Down Expand Up @@ -146,6 +178,31 @@ class Engine
void Put(VariableNT &variable, const void *data,
const Mode launch = Mode::Deferred);

/**
* Put data associated with a container-like thing, e.g., a Kokkos::View
* @param variable contains variable metadata information
* @param ndarray user data that is multi-dimensional array-like
* @param launch mode policy
*/
template <class T, class C,
typename = typename std::enable_if<
ndarray_traits<const C>::value &&
std::is_same<typename std::remove_cv<typename ndarray_traits<
const C>::value_type>::type,
T>::value>::type>
void Put(Variable<T> variable, const C &ndarray,
const Mode launch = Mode::Deferred)
{
auto mem_space = ndarray_traits<const C>::memory_space;
if (mem_space != adios2::MemorySpace::Detect)
{
variable.SetMemorySpace(mem_space);
}
Put(variable,
const_cast<const T *>(ndarray_traits<const C>::data(ndarray)),
launch);
}

/**
* Put data associated with a Variable in the Engine
* Overloaded version that accepts a variable name string.
Expand Down Expand Up @@ -232,6 +289,30 @@ class Engine
void Get(VariableNT &variable, void *data,
const Mode launch = Mode::Deferred);

/**
* Get data associated with a Variable from the Engine
* @param variable contains variable metadata information
* @param ndarray user data that is multi-dimensional array-like
* @param launch mode policy
* @exception std::invalid_argument for invalid variable or nullptr data
*/
template <class T, class C,
typename = typename std::enable_if<
ndarray_traits<const C>::value &&
std::is_same<typename std::remove_cv<typename ndarray_traits<
const C>::value_type>::type,
T>::value>::type>
void Get(Variable<T> variable, C &ndarray,
const Mode launch = Mode::Deferred)
{
auto mem_space = ndarray_traits<const C>::memory_space;
if (mem_space != adios2::MemorySpace::Detect)
{
variable.SetMemorySpace(mem_space);
}
Get(variable, ndarray_traits<const C>::data(ndarray), launch);
}

/**
* Get data associated with a Variable from the Engine. Overloaded version
* to get variable by name.
Expand Down
52 changes: 52 additions & 0 deletions bindings/CXX11/adios2/cxx11/kokkos_interop.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

#ifndef ADIOS2_BINDINGS_CXX11_CXX11_KOKKOS_INTEROP_H_
#define ADIOS2_BINDINGS_CXX11_CXX11_KOKKOS_INTEROP_H_

#include <Kokkos_Core.hpp>
#include <adios2.h>

namespace adios2
{

namespace detail
{

template <typename T>
struct memspace_kokkos_to_adios2;

template <>
struct memspace_kokkos_to_adios2<Kokkos::HostSpace>
{
static constexpr adios2::MemorySpace value = adios2::MemorySpace::Host;
};

#ifdef KOKKOS_ENABLE_CUDA

template <>
struct memspace_kokkos_to_adios2<Kokkos::CudaSpace>
{
static constexpr adios2::MemorySpace space = adios2::MemorySpace::CUDA;
};

#endif

} // namespace detail

template <typename T>
struct ndarray_traits<T,
typename std::enable_if<Kokkos::is_view<T>::value>::type>
: std::true_type
{
using value_type = typename T::value_type;
using size_type = typename T::size_type;
using pointer = typename T::pointer_type;
static constexpr auto memory_space =
detail::memspace_kokkos_to_adios2<typename T::memory_space>::value;

static pointer data(const T &c) { return c.data(); }
static size_type size(const T &c) { return c.size(); }
};

} // namespace adios2

#endif