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

Add tests for create_mirror[_view][_and_copy] functions #565

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ add_executable(ddc_tests
main.cpp
aligned_allocator.cpp
chunk.cpp
create_mirror.cpp
discrete_domain.cpp
non_uniform_point_sampling.cpp
single_discretization.cpp
Expand Down
189 changes: 189 additions & 0 deletions tests/create_mirror.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
// Copyright (C) The DDC development team, see COPYRIGHT.md file
//
// SPDX-License-Identifier: MIT

#include <ddc/ddc.hpp>

#include <gtest/gtest.h>

namespace DDC_HIP_5_7_ANONYMOUS_NAMESPACE_WORKAROUND(CHUNK_CPP)
{
struct DDimX
{
};
using DElemX = ddc::DiscreteElement<DDimX>;
using DVectX = ddc::DiscreteVector<DDimX>;
using DDomX = ddc::DiscreteDomain<DDimX>;

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

template <class ElementType, class Support, class Layout, class MemorySpace, class T>
[[nodiscard]] bool all_equal_to(
ddc::ChunkSpan<ElementType, Support, Layout, MemorySpace> const& chunk_span,
T const& value)
{
return ddc::parallel_transform_reduce(
"all_equal_to",
typename MemorySpace::execution_space(),
chunk_span.domain(),
true,
ddc::reducer::land<bool>(),
KOKKOS_LAMBDA(DElemX elem_x) { return chunk_span(elem_x) == value; });
}

} // namespace )

TEST(CreateMirror, Host)
{
ddc::Chunk chunk("chunk", dom_x, ddc::HostAllocator<int>());
ddc::parallel_fill(chunk, 3);
auto mirror = ddc::create_mirror(chunk.span_cview());
EXPECT_EQ(chunk.domain(), mirror.domain());
EXPECT_NE(chunk.data_handle(), mirror.data_handle());
EXPECT_TRUE((std::is_same_v<decltype(mirror)::memory_space, Kokkos::HostSpace>));
}

TEST(CreateMirror, Device)
{
ddc::Chunk chunk("chunk", dom_x, ddc::DeviceAllocator<int>());
ddc::parallel_fill(chunk, 3);
auto mirror = ddc::create_mirror(chunk.span_cview());
EXPECT_EQ(chunk.domain(), mirror.domain());
EXPECT_NE(chunk.data_handle(), mirror.data_handle());
EXPECT_TRUE((std::is_same_v<decltype(mirror)::memory_space, Kokkos::HostSpace>));
}

TEST(CreateMirrorWithExecutionSpace, HostToDevice)
{
ddc::Chunk chunk("chunk", dom_x, ddc::HostAllocator<int>());
ddc::parallel_fill(chunk, 3);
auto mirror = ddc::create_mirror(Kokkos::DefaultExecutionSpace(), chunk.span_cview());
EXPECT_EQ(chunk.domain(), mirror.domain());
EXPECT_NE(chunk.data_handle(), mirror.data_handle());
EXPECT_TRUE((std::is_same_v<
decltype(mirror)::memory_space,
Kokkos::DefaultExecutionSpace::memory_space>));
}

TEST(CreateMirrorAndCopy, Host)
{
ddc::Chunk chunk("chunk", dom_x, ddc::HostAllocator<int>());
ddc::parallel_fill(chunk, 3);
auto mirror = ddc::create_mirror_and_copy(chunk.span_cview());
EXPECT_EQ(chunk.domain(), mirror.domain());
EXPECT_NE(chunk.data_handle(), mirror.data_handle());
EXPECT_TRUE((std::is_same_v<decltype(mirror)::memory_space, Kokkos::HostSpace>));
EXPECT_TRUE(all_equal_to(mirror.span_cview(), 3));
}

TEST(CreateMirrorAndCopy, Device)
{
ddc::Chunk chunk("chunk", dom_x, ddc::DeviceAllocator<int>());
ddc::parallel_fill(chunk, 3);
auto mirror = ddc::create_mirror_and_copy(chunk.span_cview());
EXPECT_EQ(chunk.domain(), mirror.domain());
EXPECT_NE(chunk.data_handle(), mirror.data_handle());
EXPECT_TRUE((std::is_same_v<decltype(mirror)::memory_space, Kokkos::HostSpace>));
EXPECT_TRUE(all_equal_to(mirror.span_cview(), 3));
}

TEST(CreateMirrorAndCopyWithExecutionSpace, HostToDevice)
{
ddc::Chunk chunk("chunk", dom_x, ddc::HostAllocator<int>());
ddc::parallel_fill(chunk, 3);
auto mirror = ddc::create_mirror_and_copy(Kokkos::DefaultExecutionSpace(), chunk.span_cview());
EXPECT_EQ(chunk.domain(), mirror.domain());
EXPECT_NE(chunk.data_handle(), mirror.data_handle());
EXPECT_TRUE((std::is_same_v<
decltype(mirror)::memory_space,
Kokkos::DefaultExecutionSpace::memory_space>));
EXPECT_TRUE(all_equal_to(mirror.span_cview(), 3));
}

TEST(CreateMirrorView, Host)
{
ddc::Chunk chunk("chunk", dom_x, ddc::HostAllocator<int>());
ddc::parallel_fill(chunk, 3);
auto mirror = ddc::create_mirror_view(chunk.span_view());
EXPECT_EQ(chunk.domain(), mirror.domain());
EXPECT_EQ(chunk.data_handle(), mirror.data_handle());
EXPECT_TRUE((std::is_same_v<decltype(mirror)::memory_space, Kokkos::HostSpace>));
}

TEST(CreateMirrorView, Device)
{
ddc::Chunk chunk("chunk", dom_x, ddc::DeviceAllocator<int>());
ddc::parallel_fill(chunk, 3);
auto mirror = ddc::create_mirror_view(chunk.span_view());
EXPECT_EQ(chunk.domain(), mirror.domain());
if (Kokkos::SpaceAccessibility<Kokkos::HostSpace, decltype(chunk)::memory_space>::accessible) {
EXPECT_EQ(chunk.data_handle(), mirror.data_handle());
} else {
EXPECT_NE(chunk.data_handle(), mirror.data_handle());
}
EXPECT_TRUE((std::is_same_v<decltype(mirror)::memory_space, Kokkos::HostSpace>));
}

TEST(CreateMirrorViewWithExecutionSpace, HostToDevice)
{
ddc::Chunk chunk("chunk", dom_x, ddc::HostAllocator<int>());
ddc::parallel_fill(chunk, 3);
auto mirror = ddc::create_mirror_view(Kokkos::DefaultExecutionSpace(), chunk.span_view());
EXPECT_EQ(chunk.domain(), mirror.domain());
if (Kokkos::SpaceAccessibility<Kokkos::DefaultExecutionSpace, decltype(chunk)::memory_space>::
accessible) {
EXPECT_EQ(chunk.data_handle(), mirror.data_handle());
} else {
EXPECT_NE(chunk.data_handle(), mirror.data_handle());
}
EXPECT_TRUE((std::is_same_v<
decltype(mirror)::memory_space,
Kokkos::DefaultExecutionSpace::memory_space>));
}

TEST(CreateMirrorViewAndCopy, Host)
{
ddc::Chunk chunk("chunk", dom_x, ddc::HostAllocator<int>());
ddc::parallel_fill(chunk, 3);
auto mirror = ddc::create_mirror_view_and_copy(chunk.span_cview());
EXPECT_EQ(chunk.domain(), mirror.domain());
EXPECT_EQ(chunk.data_handle(), mirror.data_handle());
EXPECT_TRUE((std::is_same_v<decltype(mirror)::memory_space, Kokkos::HostSpace>));
EXPECT_TRUE(all_equal_to(mirror.span_cview(), 3));
}

TEST(CreateMirrorViewAndCopy, Device)
{
ddc::Chunk chunk("chunk", dom_x, ddc::DeviceAllocator<int>());
ddc::parallel_fill(chunk, 3);
auto mirror = ddc::create_mirror_view_and_copy(chunk.span_cview());
EXPECT_EQ(chunk.domain(), mirror.domain());
if (Kokkos::SpaceAccessibility<Kokkos::HostSpace, decltype(chunk)::memory_space>::accessible) {
EXPECT_EQ(chunk.data_handle(), mirror.data_handle());
} else {
EXPECT_NE(chunk.data_handle(), mirror.data_handle());
}
EXPECT_TRUE((std::is_same_v<decltype(mirror)::memory_space, Kokkos::HostSpace>));
EXPECT_TRUE(all_equal_to(mirror.span_cview(), 3));
}

TEST(CreateMirrorViewAndCopyWithExecutionSpace, HostToDevice)
{
ddc::Chunk chunk("chunk", dom_x, ddc::HostAllocator<int>());
ddc::parallel_fill(chunk, 3);
auto mirror
= ddc::create_mirror_view_and_copy(Kokkos::DefaultExecutionSpace(), chunk.span_cview());
EXPECT_EQ(chunk.domain(), mirror.domain());
if (Kokkos::SpaceAccessibility<Kokkos::DefaultExecutionSpace, decltype(chunk)::memory_space>::
accessible) {
EXPECT_EQ(chunk.data_handle(), mirror.data_handle());
} else {
EXPECT_NE(chunk.data_handle(), mirror.data_handle());
}
EXPECT_TRUE((std::is_same_v<
decltype(mirror)::memory_space,
Kokkos::DefaultExecutionSpace::memory_space>));
EXPECT_TRUE(all_equal_to(mirror.span_cview(), 3));
}