From 1505522a4b8ce45e0102a5e7b0604085d1d13f06 Mon Sep 17 00:00:00 2001 From: Baptiste Legouix Date: Wed, 15 Nov 2023 06:21:21 +0100 Subject: [PATCH] Mirroring (#203) * init * wip * mirroring for chunk * chunkspan * work on chunk but not working * kokkos_view move-returned * Update mirroring * Update include/ddc/chunk.hpp --------- Co-authored-by: Thomas Padioleau --- include/ddc/ddc.hpp | 1 + include/ddc/mirror.hpp | 92 ++++++++++++++++++++++++++++++++++++++++++ tests/chunk.cpp | 13 ++++++ 3 files changed, 106 insertions(+) create mode 100644 include/ddc/mirror.hpp diff --git a/include/ddc/ddc.hpp b/include/ddc/ddc.hpp index e19ffb8b7..dca7eebbe 100644 --- a/include/ddc/ddc.hpp +++ b/include/ddc/ddc.hpp @@ -28,6 +28,7 @@ #include "ddc/deepcopy.hpp" #include "ddc/fill.hpp" #include "ddc/for_each.hpp" +#include "ddc/mirror.hpp" #include "ddc/reducer.hpp" #include "ddc/transform_reduce.hpp" diff --git a/include/ddc/mirror.hpp b/include/ddc/mirror.hpp new file mode 100644 index 000000000..e3eed9971 --- /dev/null +++ b/include/ddc/mirror.hpp @@ -0,0 +1,92 @@ +// SPDX-License-Identifier: MIT + +#pragma once + +#include + +#include "ddc/chunk_span.hpp" +#include "ddc/kokkos_allocator.hpp" + +namespace ddc { + +/// Returns a new `Chunk` with the same layout as `src` allocated on the memory space `Space::memory_space`. +template +auto create_mirror( + Space const& space, + ChunkSpan const& src) +{ + return Chunk>( + src.domain()); +} + +/// Returns a new host `Chunk` with the same layout as `src`. +template +auto create_mirror(ChunkSpan const& src) +{ + return create_mirror(Kokkos::DefaultHostExecutionSpace(), src); +} + +/// Returns a new `Chunk` with the same layout as `src` allocated on the memory space `Space::memory_space` and operates a deep copy between the two. +template +auto create_mirror_and_copy( + Space const& space, + ChunkSpan const& src) +{ + Chunk> chunk( + src.domain()); + deepcopy(chunk, src); + return chunk; +} + +/// Returns a new host `Chunk` with the same layout as `src` and operates a deep copy between the two. +template +auto create_mirror_and_copy(ChunkSpan const& src) +{ + return create_mirror_and_copy(Kokkos::DefaultHostExecutionSpace(), src); +} + +/// If `src` is accessible from `space` then returns a copy of `src`, +/// otherwise returns a new `Chunk` with the same layout as `src` allocated on the memory space `Space::memory_space`. +template +auto create_mirror_view( + Space const& space, + ChunkSpan const& src) +{ + if constexpr (Kokkos::SpaceAccessibility::accessible) { + return src; + } else { + return create_mirror(space, src); + } +} + +/// If `src` is host accessible then returns a copy of `src`, +/// otherwise returns a new host `Chunk` with the same layout. +template +auto create_mirror_view(ChunkSpan const& src) +{ + return create_mirror_view(Kokkos::DefaultHostExecutionSpace(), src); +} + +/// If `src` is accessible from `space` then returns a copy of `src`, +/// otherwise returns a new `Chunk` with the same layout as `src` allocated on the memory space `Space::memory_space` and operates a deep copy between the two. +template +auto create_mirror_view_and_copy( + Space const& space, + ChunkSpan const& src) +{ + if constexpr (Kokkos::SpaceAccessibility::accessible) { + return src; + } else { + return create_mirror_and_copy(space, src); + } +} + +/// If `src` is host accessible then returns a copy of `src`, +/// otherwise returns a new host `Chunk` with the same layout as `src` and operates a deep copy between the two. +template +auto create_mirror_view_and_copy(ChunkSpan const& src) +{ + return create_mirror_view_and_copy(Kokkos::DefaultHostExecutionSpace(), src); +} + +} // namespace ddc diff --git a/tests/chunk.cpp b/tests/chunk.cpp index 0ad45fe4f..94d9a4af1 100644 --- a/tests/chunk.cpp +++ b/tests/chunk.cpp @@ -629,3 +629,16 @@ TEST(Chunk3DTest, AccessFromDiscreteElements) } } } + +TEST(Chunk2DTest, Mirror) +{ + ChunkXY chunk(dom_x_y); + ddc::fill(chunk, 1.4); + auto const chunk2 = ddc::create_mirror_and_copy(chunk.span_view()); + for (auto&& ix : chunk.domain()) { + for (auto&& iy : chunk.domain()) { + // we expect complete equality, not EXPECT_DOUBLE_EQ: these are copy + EXPECT_EQ(chunk2(ix, iy), chunk(ix, iy)); + } + } +}