From 9c713009982683bc0847d201f570eb5d5d78a195 Mon Sep 17 00:00:00 2001 From: Thomas Padioleau Date: Fri, 26 Jul 2024 10:38:07 +0200 Subject: [PATCH] Add tests for create_mirror[_view][_and_copy] functions --- tests/CMakeLists.txt | 1 + tests/create_mirror.cpp | 188 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 189 insertions(+) create mode 100644 tests/create_mirror.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 44249de9c..56119d4b2 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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 diff --git a/tests/create_mirror.cpp b/tests/create_mirror.cpp new file mode 100644 index 000000000..cbbc2a675 --- /dev/null +++ b/tests/create_mirror.cpp @@ -0,0 +1,188 @@ +// Copyright (C) The DDC development team, see COPYRIGHT.md file +// +// SPDX-License-Identifier: MIT + +#include + +#include + +namespace DDC_HIP_5_7_ANONYMOUS_NAMESPACE_WORKAROUND(CHUNK_CPP) +{ + struct DDimX + { + }; + using DElemX = ddc::DiscreteElement; + using DVectX = ddc::DiscreteVector; + using DDomX = ddc::DiscreteDomain; + + static DElemX constexpr lbound_x(50); + static DVectX constexpr nelems_x(3); + static DDomX constexpr dom_x(lbound_x, nelems_x); + + template + [[nodiscard]] bool all_equal_to(ChunkSpan const chunk_span, T const t) + { + static_assert(ddc::is_borrowed_chunk_v); + return ddc::parallel_transform_reduce( + "all_equal_to", + typename ChunkSpan::memory_space::execution_space(), + chunk_span.domain(), + true, + ddc::reducer::land(), + KOKKOS_LAMBDA(DElemX elem_x) { return chunk_span(elem_x) == t; }); + } + +} // namespace ) + +TEST(CreateMirror, Host) +{ + ddc::Chunk chunk("chunk", dom_x, ddc::HostAllocator()); + 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)); +} + +TEST(CreateMirror, Device) +{ + ddc::Chunk chunk("chunk", dom_x, ddc::DeviceAllocator()); + 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)); +} + +TEST(CreateMirrorWithExecutionSpace, HostToDevice) +{ + ddc::Chunk chunk("chunk", dom_x, ddc::HostAllocator()); + 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()); + 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)); + EXPECT_TRUE(all_equal_to(mirror.span_cview(), 3)); +} + +TEST(CreateMirrorAndCopy, Device) +{ + ddc::Chunk chunk("chunk", dom_x, ddc::DeviceAllocator()); + 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)); + EXPECT_TRUE(all_equal_to(mirror.span_cview(), 3)); +} + +TEST(CreateMirrorAndCopyWithExecutionSpace, HostToDevice) +{ + ddc::Chunk chunk("chunk", dom_x, ddc::HostAllocator()); + 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()); + 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)); +} + +TEST(CreateMirrorView, Device) +{ + ddc::Chunk chunk("chunk", dom_x, ddc::DeviceAllocator()); + ddc::parallel_fill(chunk, 3); + auto mirror = ddc::create_mirror_view(chunk.span_view()); + EXPECT_EQ(chunk.domain(), mirror.domain()); + if (Kokkos::SpaceAccessibility::accessible) { + EXPECT_EQ(chunk.data_handle(), mirror.data_handle()); + } else { + EXPECT_NE(chunk.data_handle(), mirror.data_handle()); + } + EXPECT_TRUE((std::is_same_v)); +} + +TEST(CreateMirrorViewWithExecutionSpace, HostToDevice) +{ + ddc::Chunk chunk("chunk", dom_x, ddc::HostAllocator()); + 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:: + 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()); + 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)); + EXPECT_TRUE(all_equal_to(mirror.span_cview(), 3)); +} + +TEST(CreateMirrorViewAndCopy, Device) +{ + ddc::Chunk chunk("chunk", dom_x, ddc::DeviceAllocator()); + 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::accessible) { + EXPECT_EQ(chunk.data_handle(), mirror.data_handle()); + } else { + EXPECT_NE(chunk.data_handle(), mirror.data_handle()); + } + EXPECT_TRUE((std::is_same_v)); + EXPECT_TRUE(all_equal_to(mirror.span_cview(), 3)); +} + +TEST(CreateMirrorViewAndCopyWithExecutionSpace, HostToDevice) +{ + ddc::Chunk chunk("chunk", dom_x, ddc::HostAllocator()); + 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:: + 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)); +}