Skip to content

Commit

Permalink
Add device_t<> for VectorField too
Browse files Browse the repository at this point in the history
Closes #261.

* Add the option to use `device_t` on `VectorField` and `VectorFieldSpan` too.
* Add a test `device_host_t.cpp` which tests `device_t` and `host_t` on Chunk, ChunkSpan, VectorField and VectorFieldSpan.
* Fix a compilation error due to redeclaration of `deepcopy`  in `vector_field_common.hpp` and `derivative_field.hpp`.

See merge request gysela-developpers/gyselalibxx!542

--------------------------------------------

Co-authored-by: Pauline Vidal <pavi@nb-pavi-1.ipp.mpg.de>
  • Loading branch information
EmilyBourne and PaulineVidal committed Jul 9, 2024
1 parent 0ccdfd3 commit 8339adb
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/data_types/derivative_field_span.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ namespace ddcHelper {
template <
class FieldDst,
class FieldSrc,
class = std::enable_if_t<
is_borrowed_deriv_field_v<FieldDst> && is_borrowed_deriv_field_v<FieldSrc>>>
std::enable_if_t<
is_borrowed_deriv_field_v<FieldDst> && is_borrowed_deriv_field_v<FieldSrc>,
bool> = true>
auto deepcopy(FieldDst&& dst, FieldSrc&& src)
{
assert(dst.get_values_span().domain().extents() == src.get_values_span().domain().extents());
Expand Down
4 changes: 3 additions & 1 deletion src/data_types/vector_field_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ auto get_domain(FieldType const& field) noexcept
template <
class FieldDst,
class FieldSrc,
class = std::enable_if_t<is_borrowed_field_v<FieldDst> && is_borrowed_field_v<FieldSrc>>>
std::enable_if_t<
is_borrowed_field_v<FieldDst> && is_borrowed_field_v<FieldSrc>,
bool> = true>
auto deepcopy(FieldDst&& dst, FieldSrc&& src)
{
static_assert(std::is_same_v<
Expand Down
1 change: 1 addition & 0 deletions src/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ target_include_directories("utils"
target_link_libraries("utils"
INTERFACE
DDC::DDC
gslx::data_types
)

add_library("gslx::utils" ALIAS "utils")
Expand Down
83 changes: 78 additions & 5 deletions src/utils/ddc_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

#include <ddc/ddc.hpp>

#include "directional_tag.hpp"
#include "vector_field.hpp"
#include "vector_field_span.hpp"


namespace ddcHelper {
//TODO: this should be directly handled by ddc::Discretization really,
// in the meantime, we do it ourselves
Expand Down Expand Up @@ -157,19 +162,36 @@ using combine_t = typename detail::Combine<Containers...>::type;
//-----------------------------------------------------------------------------

namespace detail {
/// \cond
template <class, class>
struct OnMemorySpace
{
};

/**
* @brief Set a `ddc::Chunk` on a given NewMemorySpace.
* @tparam NewMemorySpace The new memory space.
* @tparam ElementType Type of the elememts in the ddc::Chunk.
* @tparam SupportType Type of the domain of the ddc::Chunk.
* @tparam Allocator Allocator type (see ddc::KokkosAllocator).
* @see ddc::Chunk
*/
template <class NewMemorySpace, class ElementType, class SupportType, class Allocator>
struct OnMemorySpace<NewMemorySpace, ddc::Chunk<ElementType, SupportType, Allocator>>
{
using type = typename ddc::
Chunk<ElementType, SupportType, ddc::KokkosAllocator<ElementType, NewMemorySpace>>;
};

/**
* @brief Get a new `ddc::ChunkSpan` type with the same parametrisation
* except in the memory space which is set to NewMemorySpace.
* @tparam NewMemorySpace The new memory space.
* @tparam ElementType Type of the elememts in the ddc::Chunk.
* @tparam SupportType Type of the domain of the ddc::Chunk.
* @tparam Layout Layout tag (see Kokkos).
* @tparam MemorySpace The original memory space of the chunk.
* @see ddc::ChunkSpan
*/
template <
class NewMemorySpace,
class ElementType,
Expand All @@ -180,28 +202,79 @@ struct OnMemorySpace<NewMemorySpace, ddc::ChunkSpan<ElementType, SupportType, La
{
using type = typename ddc::ChunkSpan<ElementType, SupportType, Layout, NewMemorySpace>;
};
/// \endcond


/**
* @brief Set a `VectorField` on a given NewMemorySpace.
* @tparam NewMemorySpace The new memory space.
* @tparam ElementType Type of the elememts in the ddc::Chunk of the VectorField.
* @tparam SupportType Type of the domain of the ddc::Chunk in the VectorField.
* @tparam NDTag NDTag object storing the dimensions along which the VectorField is defined.
* The dimensions refer to the dimensions of the arrival domain of the VectorField.
* @tparam Allocator Allocator type (see ddc::KokkosAllocator).
* @see VectorField
*/
template <class NewMemorySpace, class ElementType, class SupportType, class NDTag, class Allocator>
struct OnMemorySpace<NewMemorySpace, VectorField<ElementType, SupportType, NDTag, Allocator>>
{
using type = VectorField<
ElementType,
SupportType,
NDTag,
ddc::KokkosAllocator<ElementType, NewMemorySpace>>;
};

/**
* @brief Get a new `VectorFieldSpan` type with the same parametrisation
* except in the memory space which is set to NewMemorySpace.
* @tparam NewMemorySpace The new memory space.
* @tparam ElementType Type of the elememts in the ddc::Chunk of the VectorField.
* @tparam SupportType Type of the domain of the ddc::Chunk in the VectorField.
* @tparam NDTag NDTag object storing directions of the VectorField as dimensions.
* The dimensions refer to the dimensions of the arrival domain of the VectorField.
* @tparam Layout Layout tag (see Kokkos).
* @tparam MemorySpace The original memory space of the chunk of the VectorField.
* @see VectorFieldSpan
*/
template <
class NewMemorySpace,
class ElementType,
class SupportType,
class NDTag,
class Layout,
class MemorySpace>
struct OnMemorySpace<
NewMemorySpace,
VectorFieldSpan<ElementType, SupportType, NDTag, Layout, MemorySpace>>
{
using type = VectorFieldSpan<ElementType, SupportType, NDTag, Layout, NewMemorySpace>;
};

} // namespace detail

/**
* @brief Alias template helper returning the of a `ddc::Chunk` or a `ddc::Chunkspan` on a MemorySpace
* @brief Alias template helper returning the type of a `ddc::Chunk`, a `ddc::ChunkSpan`, a `VectorField`
* or a `VectorFieldSpan` on a MemorySpace.
*/
template <class MemorySpace, class C>
using on_memory_space_t = typename detail::OnMemorySpace<MemorySpace, C>::type;

/**
* @brief Alias template helper returning the "host" version of a `ddc::Chunk` or a `ddc::Chunkspan`
* @brief Alias template helper returning the "host" version of a `ddc::Chunk`, a `ddc::ChunkSpan`,
* a `VectorField` or a `VectorFieldSpan`.
*/
template <class C>
using host_t = on_memory_space_t<Kokkos::HostSpace, C>;

/**
* @brief Alias template helper returning the "device" version of a `ddc::Chunk` or a `ddc::Chunkspan`
* @brief Alias template helper returning the "device" version of a `ddc::Chunk`, a `ddc::ChunkSpan`,
* a `VectorField` or a `VectorFieldSpan`.
*/
template <class C>
using device_t = on_memory_space_t<Kokkos::DefaultExecutionSpace::memory_space, C>;



namespace ddcHelper {
/// A helper to determine the number of tags in a type sequence.
template <class TypeSeq>
Expand Down
1 change: 1 addition & 0 deletions tests/data_types/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ include(GoogleTest)

add_executable(unit_tests_data_types
deriv_field.cpp
device_host_t.cpp
field.cpp
../main.cpp
)
Expand Down
159 changes: 159 additions & 0 deletions tests/data_types/device_host_t.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// SPDX-License-Identifier: MIT
#include <typeinfo>

#include <ddc/ddc.hpp>

#include <gtest/gtest.h>

#include "ddc_helper.hpp"
#include "directional_tag.hpp"
#include "vector_field.hpp"
#include "vector_field_span.hpp"


namespace {
class Tag1
{
};
class Tag2
{
};

using Direction = NDTag<Tag1, Tag2>;


struct DDimX
{
};
using DElemX = ddc::DiscreteElement<DDimX>;
using DVectX = ddc::DiscreteVector<DDimX>;
using DDomX = ddc::DiscreteDomain<DDimX>;


template <class Datatype>
using ChunkX = ddc::Chunk<Datatype, DDomX>;
template <class Datatype>
using ChunkSpanX = ddc::ChunkSpan<Datatype, DDomX>;


template <class Datatype>
using VectorFieldX = VectorField<Datatype, DDomX, Direction>;
template <class Datatype>
using VectorFieldSpanX = VectorFieldSpan<Datatype, DDomX, Direction>;


static DElemX constexpr lbound_x(50);
static DVectX constexpr nelems_x(3);
static DDomX constexpr dom_x(lbound_x, nelems_x);

} // end namespace


// Tests on DefaultExecutionSpace ----------------------------------------------------------------
TEST(MemorySpace, ChunkOnDeviceT)
{
device_t<ChunkX<double>> chunk_test_alloc(dom_x);
ddc::Chunk<
double,
DDomX,
ddc::KokkosAllocator<double, Kokkos::DefaultExecutionSpace::memory_space>>
chunk_alloc(dom_x);
EXPECT_TRUE(typeid(chunk_test_alloc) == typeid(chunk_alloc));
}


TEST(MemorySpace, ChunkSpanOnDeviceT)
{
device_t<ChunkX<double>> chunk_test_alloc(dom_x);
device_t<ChunkSpanX<double>> chunk_span_test = chunk_test_alloc.span_view();
ddc::Chunk<
double,
DDomX,
ddc::KokkosAllocator<double, Kokkos::DefaultExecutionSpace::memory_space>>
chunk_alloc(dom_x);
ddc::ChunkSpan chunk_span = chunk_alloc.span_view();
EXPECT_TRUE(typeid(chunk_span_test) == typeid(chunk_span));
}


TEST(MemorySpace, VectorFieldOnDeviceT)
{
device_t<VectorFieldX<double>> vector_field_test_alloc(dom_x);
VectorField<
double,
DDomX,
Direction,
ddc::KokkosAllocator<double, Kokkos::DefaultExecutionSpace::memory_space>>
vector_field_alloc(dom_x);
EXPECT_TRUE(typeid(vector_field_test_alloc) == typeid(vector_field_alloc));
}


TEST(MemorySpace, VectorFieldSpanOnDeviceT)
{
device_t<VectorFieldX<double>> vector_field_test_alloc(dom_x);
device_t<VectorFieldSpanX<double>> vector_field_span_test = vector_field_test_alloc.span_view();
VectorField<
double,
DDomX,
Direction,
ddc::KokkosAllocator<double, Kokkos::DefaultExecutionSpace::memory_space>>
vector_field_alloc(dom_x);
VectorFieldSpan vector_field_span = vector_field_alloc.span_view();
EXPECT_TRUE(typeid(vector_field_span_test) == typeid(vector_field_span));
}


// Tests on DefaultHostExecutionSpace ------------------------------------------------------------
TEST(MemorySpace, ChunkOnHostT)
{
host_t<ChunkX<double>> chunk_test_alloc(dom_x);
ddc::Chunk<
double,
DDomX,
ddc::KokkosAllocator<double, Kokkos::DefaultHostExecutionSpace::memory_space>>
chunk_alloc(dom_x);
EXPECT_TRUE(typeid(chunk_test_alloc) == typeid(chunk_alloc));
}


TEST(MemorySpace, ChunkSpanOnHostT)
{
host_t<ChunkX<double>> chunk_test_alloc(dom_x);
host_t<ChunkSpanX<double>> chunk_span_test = chunk_test_alloc.span_view();
ddc::Chunk<
double,
DDomX,
ddc::KokkosAllocator<double, Kokkos::DefaultHostExecutionSpace::memory_space>>
chunk_alloc(dom_x);
ddc::ChunkSpan chunk_span = chunk_alloc.span_view();
EXPECT_TRUE(typeid(chunk_span_test) == typeid(chunk_span));
}


TEST(MemorySpace, VectorFieldOnHostT)
{
host_t<VectorFieldX<double>> vector_field_test_alloc(dom_x);
VectorField<
double,
DDomX,
Direction,
ddc::KokkosAllocator<double, Kokkos::DefaultHostExecutionSpace::memory_space>>
vector_field_alloc(dom_x);
EXPECT_TRUE(typeid(vector_field_test_alloc) == typeid(vector_field_alloc));
}


TEST(MemorySpace, VectorFieldSpanOnHostT)
{
host_t<VectorFieldX<double>> vector_field_test_alloc(dom_x);
host_t<VectorFieldSpanX<double>> vector_field_span_test = vector_field_test_alloc.span_view();
VectorField<
double,
DDomX,
Direction,
ddc::KokkosAllocator<double, Kokkos::DefaultHostExecutionSpace::memory_space>>
vector_field_alloc(dom_x);
VectorFieldSpan vector_field_span = vector_field_alloc.span_view();
EXPECT_TRUE(typeid(vector_field_span_test) == typeid(vector_field_span));
}

0 comments on commit 8339adb

Please sign in to comment.