DDC 0.10.0
Loading...
Searching...
No Matches
create_mirror.hpp
1// Copyright (C) The DDC development team, see COPYRIGHT.md file
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <type_traits>
8
9#include <Kokkos_Core.hpp>
10
11#include "chunk_span.hpp"
13
14namespace ddc {
15
16/// @param[in] space A Kokkos memory space or execution space.
17/// @param[in] src A layout right ChunkSpan.
18/// @return a `Chunk` with the same support and layout as `src` allocated on the `Space::memory_space` memory space.
19template <class Space, class ElementType, class Support, class Layout, class MemorySpace>
20auto create_mirror(
21 [[maybe_unused]] Space const& space,
22 ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
23{
24 // `space` is always unused, but needed for Doxygen
25 static_assert(
26 Kokkos::is_memory_space_v<Space> || Kokkos::is_execution_space_v<Space>,
27 "DDC: parameter \"Space\" must be either a Kokkos execution space or a memory space");
28 static_assert(
29 std::is_same_v<Layout, Kokkos::layout_right>,
30 "DDC: parameter \"Layout\" must be a `layout_right`");
31 return Chunk(
32 src.domain(),
33 KokkosAllocator<std::remove_const_t<ElementType>, typename Space::memory_space>());
34}
35
36/// Equivalent to `create_mirror(Kokkos::HostSpace(), src)`.
37/// @param[in] src A layout right ChunkSpan.
38/// @return a `Chunk` with the same support and layout as `src` allocated on the `Kokkos::HostSpace` memory space.
39template <class ElementType, class Support, class Layout, class MemorySpace>
40auto create_mirror(ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
41{
42 return create_mirror(Kokkos::HostSpace(), src);
43}
44
45/// @param[in] space A Kokkos memory space or execution space.
46/// @param[in] src A layout right ChunkSpan.
47/// @return a `Chunk` with the same support and layout as `src` allocated on the `Space::memory_space` memory space and operates a deep copy between the two.
48template <class Space, class ElementType, class Support, class Layout, class MemorySpace>
50 Space const& space,
51 ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
52{
53 static_assert(
54 Kokkos::is_memory_space_v<Space> || Kokkos::is_execution_space_v<Space>,
55 "DDC: parameter \"Space\" must be either a Kokkos execution space or a memory space");
56 static_assert(
57 std::is_same_v<Layout, Kokkos::layout_right>,
58 "DDC: parameter \"Layout\" must be a `layout_right`");
59 Chunk chunk = create_mirror(space, src);
60 parallel_deepcopy(chunk, src);
61 return chunk;
62}
63
64/// Equivalent to `create_mirror_and_copy(Kokkos::HostSpace(), src)`.
65/// @param[in] src A layout right ChunkSpan.
66/// @return a `Chunk` with the same support and layout as `src` allocated on the `Kokkos::HostSpace` memory space and operates a deep copy between the two.
67template <class ElementType, class Support, class Layout, class MemorySpace>
68auto create_mirror_and_copy(ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
69{
70 return create_mirror_and_copy(Kokkos::HostSpace(), src);
71}
72
73/// @param[in] space A Kokkos memory space or execution space.
74/// @param[in] src A non-const, layout right ChunkSpan.
75/// @return If `MemorySpace` is accessible from `Space` then returns a copy of `src`, otherwise returns a `Chunk` with the same support and layout as `src` allocated on the `Space::memory_space` memory space.
76template <class Space, class ElementType, class Support, class Layout, class MemorySpace>
78 [[maybe_unused]] Space const& space,
79 ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
80{
81 static_assert(
82 !std::is_const_v<ElementType>,
83 "DDC: parameter \"ElementType\" must not be `const`");
84 static_assert(
85 Kokkos::is_memory_space_v<Space> || Kokkos::is_execution_space_v<Space>,
86 "DDC: parameter \"Space\" must be either a Kokkos execution space or a memory space");
87 static_assert(
88 std::is_same_v<Layout, Kokkos::layout_right>,
89 "DDC: parameter \"Layout\" must be a `layout_right`");
90 if constexpr (Kokkos::SpaceAccessibility<Space, MemorySpace>::accessible) {
91 return src;
92 } else {
93 return create_mirror(space, src);
94 }
95}
96
97/// Equivalent to `create_mirror_view(Kokkos::HostSpace(), src)`.
98/// @param[in] src A non-const, layout right ChunkSpan.
99/// @return If `Kokkos::HostSpace` is accessible from `Space` then returns a copy of `src`, otherwise returns a `Chunk` with the same support and layout as `src` allocated on the `Kokkos::HostSpace` memory space.
100template <class ElementType, class Support, class Layout, class MemorySpace>
101auto create_mirror_view(ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
102{
103 return create_mirror_view(Kokkos::HostSpace(), src);
104}
105
106/// @param[in] space A Kokkos memory space or execution space.
107/// @param[in] src A layout right ChunkSpan.
108/// @return If `MemorySpace` is accessible from `Space` then returns a copy of `src`, otherwise returns a `Chunk` with the same support and layout as `src` allocated on the `Space::memory_space` memory space and operates a deep copy between the two.
109template <class Space, class ElementType, class Support, class Layout, class MemorySpace>
111 [[maybe_unused]] Space const& space,
112 ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
113{
114 static_assert(
115 Kokkos::is_memory_space_v<Space> || Kokkos::is_execution_space_v<Space>,
116 "DDC: parameter \"Space\" must be either a Kokkos execution space or a memory space");
117 static_assert(
118 std::is_same_v<Layout, Kokkos::layout_right>,
119 "DDC: parameter \"Layout\" must be a `layout_right`");
120 if constexpr (Kokkos::SpaceAccessibility<Space, MemorySpace>::accessible) {
121 return src;
122 } else {
123 return create_mirror_and_copy(space, src);
124 }
125}
126
127/// Equivalent to `create_mirror_view_and_copy(Kokkos::HostSpace(), src)`.
128/// @param[in] src A layout right ChunkSpan.
129/// @return If `Kokkos::HostSpace` is accessible from `Space` then returns a copy of `src`, otherwise returns a `Chunk` with the same support and layout as `src` allocated on the `Kokkos::HostSpace` memory space and operates a deep copy between the two.
130template <class ElementType, class Support, class Layout, class MemorySpace>
131auto create_mirror_view_and_copy(ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
132{
133 return create_mirror_view_and_copy(Kokkos::HostSpace(), src);
134}
135
136} // namespace ddc
friend class ChunkSpan
friend class Chunk
Definition chunk.hpp:83
The top-level namespace of DDC.
auto create_mirror_view_and_copy(ChunkSpan< ElementType, Support, Layout, MemorySpace > const &src)
Equivalent to create_mirror_view_and_copy(Kokkos::HostSpace(), src).
auto create_mirror_view(ChunkSpan< ElementType, Support, Layout, MemorySpace > const &src)
Equivalent to create_mirror_view(Kokkos::HostSpace(), src).
auto create_mirror_view_and_copy(Space const &space, ChunkSpan< ElementType, Support, Layout, MemorySpace > const &src)
auto create_mirror_and_copy(Space const &space, ChunkSpan< ElementType, Support, Layout, MemorySpace > const &src)
auto create_mirror(ChunkSpan< ElementType, Support, Layout, MemorySpace > const &src)
Equivalent to create_mirror(Kokkos::HostSpace(), src).
auto create_mirror_and_copy(ChunkSpan< ElementType, Support, Layout, MemorySpace > const &src)
Equivalent to create_mirror_and_copy(Kokkos::HostSpace(), src).
auto create_mirror(Space const &space, ChunkSpan< ElementType, Support, Layout, MemorySpace > const &src)
auto create_mirror_view(Space const &space, ChunkSpan< ElementType, Support, Layout, MemorySpace > const &src)