Skip to content

Commit

Permalink
Sync DDC
Browse files Browse the repository at this point in the history
  • Loading branch information
tpadioleau committed Nov 15, 2023
1 parent 75aa653 commit b75b033
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions vendor/ddc/include/ddc/ddc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
92 changes: 92 additions & 0 deletions vendor/ddc/include/ddc/mirror.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// SPDX-License-Identifier: MIT

#pragma once

#include <Kokkos_Core.hpp>

#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 <class Space, class ElementType, class Support, class Layout, class MemorySpace>
auto create_mirror(
Space const& space,
ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
{
return Chunk<ElementType, Support, KokkosAllocator<ElementType, typename Space::memory_space>>(
src.domain());
}

/// Returns a new host `Chunk` with the same layout as `src`.
template <class ElementType, class Support, class Layout, class MemorySpace>
auto create_mirror(ChunkSpan<ElementType, Support, Layout, MemorySpace> 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 <class Space, class ElementType, class Support, class Layout, class MemorySpace>
auto create_mirror_and_copy(
Space const& space,
ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
{
Chunk<ElementType, Support, KokkosAllocator<ElementType, typename Space::memory_space>> 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 <class ElementType, class Support, class Layout, class MemorySpace>
auto create_mirror_and_copy(ChunkSpan<ElementType, Support, Layout, MemorySpace> 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 <class Space, class ElementType, class Support, class Layout, class MemorySpace>
auto create_mirror_view(
Space const& space,
ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
{
if constexpr (Kokkos::SpaceAccessibility<Space, MemorySpace>::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 <class ElementType, class Support, class Layout, class MemorySpace>
auto create_mirror_view(ChunkSpan<ElementType, Support, Layout, MemorySpace> 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 <class Space, class ElementType, class Support, class Layout, class MemorySpace>
auto create_mirror_view_and_copy(
Space const& space,
ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
{
if constexpr (Kokkos::SpaceAccessibility<Space, MemorySpace>::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 <class ElementType, class Support, class Layout, class MemorySpace>
auto create_mirror_view_and_copy(ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
{
return create_mirror_view_and_copy(Kokkos::DefaultHostExecutionSpace(), src);
}

} // namespace ddc
13 changes: 13 additions & 0 deletions vendor/ddc/tests/chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,3 +629,16 @@ TEST(Chunk3DTest, AccessFromDiscreteElements)
}
}
}

TEST(Chunk2DTest, Mirror)
{
ChunkXY<double> 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<DDimX>()) {
for (auto&& iy : chunk.domain<DDimY>()) {
// we expect complete equality, not EXPECT_DOUBLE_EQ: these are copy
EXPECT_EQ(chunk2(ix, iy), chunk(ix, iy));
}
}
}

0 comments on commit b75b033

Please sign in to comment.