DDC 0.14.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
16namespace detail {
17
18/**
19 * @brief Ensure a layout_right view of a ChunkSpan, copying if necessary.
20 *
21 * If the input `src` already uses `Kokkos::layout_right`, it is returned as-is.
22 * Otherwise, a new chunk with layout_right is allocated and a deep copy of
23 * `src` is performed into it.
24 *
25 * @param[in] src Source ChunkSpan to adapt.
26 * @return Either the original view (if already LayoutRight) or a copied chunk.
27 */
28template <class ElementType, class Support, class Layout, class MemorySpace>
29auto create_layout_right_view_and_copy(
30 ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
31{
32 if constexpr (std::is_same_v<Layout, Kokkos::layout_right>) {
33 return src;
34 } else {
35 Chunk chunk(src.domain(), KokkosAllocator<std::remove_const_t<ElementType>, MemorySpace>());
36 parallel_deepcopy(chunk, src);
37 return chunk;
38 }
39}
40
41} // namespace detail
42
43/// @param[in] space A Kokkos memory space or execution space.
44/// @param[in] src A layout right ChunkSpan.
45/// @return a `Chunk` with the same support and layout as `src` allocated on the `Space::memory_space` memory space.
46template <class Space, class ElementType, class Support, class Layout, class MemorySpace>
47auto create_mirror(
48 [[maybe_unused]] Space const& space,
49 ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
50{
51 // `space` is always unused, but needed for Doxygen
52 static_assert(
53 Kokkos::is_memory_space_v<Space> || Kokkos::is_execution_space_v<Space>,
54 "DDC: parameter \"Space\" must be either a Kokkos execution space or a memory space");
55 static_assert(
56 std::is_same_v<Layout, Kokkos::layout_right>,
57 "DDC: parameter \"Layout\" must be a `layout_right`");
58 return Chunk(
59 src.domain(),
60 KokkosAllocator<std::remove_const_t<ElementType>, typename Space::memory_space>());
61}
62
63/// Equivalent to `create_mirror(Kokkos::HostSpace(), src)`.
64/// @param[in] src A layout right ChunkSpan.
65/// @return a `Chunk` with the same support and layout as `src` allocated on the `Kokkos::HostSpace` memory space.
66template <class ElementType, class Support, class Layout, class MemorySpace>
67auto create_mirror(ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
68{
69 return create_mirror(Kokkos::HostSpace(), src);
70}
71
72/// @param[in] space A Kokkos memory space or execution space.
73/// @param[in] src A layout right ChunkSpan.
74/// @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.
75template <class Space, class ElementType, class Support, class Layout, class MemorySpace>
77 Space const& space,
78 ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
79{
80 static_assert(
81 Kokkos::is_memory_space_v<Space> || Kokkos::is_execution_space_v<Space>,
82 "DDC: parameter \"Space\" must be either a Kokkos execution space or a memory space");
83 static_assert(
84 std::is_same_v<Layout, Kokkos::layout_right>,
85 "DDC: parameter \"Layout\" must be a `layout_right`");
86 Chunk chunk = create_mirror(space, src);
87 parallel_deepcopy(chunk, src);
88 return chunk;
89}
90
91/// Equivalent to `create_mirror_and_copy(Kokkos::HostSpace(), src)`.
92/// @param[in] src A layout right ChunkSpan.
93/// @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.
94template <class ElementType, class Support, class Layout, class MemorySpace>
95auto create_mirror_and_copy(ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
96{
97 return create_mirror_and_copy(Kokkos::HostSpace(), src);
98}
99
100/// @param[in] space A Kokkos memory space or execution space.
101/// @param[in] src A non-const, layout right ChunkSpan.
102/// @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.
103template <class Space, class ElementType, class Support, class Layout, class MemorySpace>
105 [[maybe_unused]] Space const& space,
106 ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
107{
108 static_assert(
109 !std::is_const_v<ElementType>,
110 "DDC: parameter \"ElementType\" must not be `const`");
111 static_assert(
112 Kokkos::is_memory_space_v<Space> || Kokkos::is_execution_space_v<Space>,
113 "DDC: parameter \"Space\" must be either a Kokkos execution space or a memory space");
114 static_assert(
115 std::is_same_v<Layout, Kokkos::layout_right>,
116 "DDC: parameter \"Layout\" must be a `layout_right`");
117 if constexpr (Kokkos::SpaceAccessibility<Space, MemorySpace>::accessible) {
118 return src;
119 } else {
120 return create_mirror(space, src);
121 }
122}
123
124/// Equivalent to `create_mirror_view(Kokkos::HostSpace(), src)`.
125/// @param[in] src A non-const, layout right ChunkSpan.
126/// @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.
127template <class ElementType, class Support, class Layout, class MemorySpace>
128auto create_mirror_view(ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
129{
130 return create_mirror_view(Kokkos::HostSpace(), src);
131}
132
133/// @param[in] space A Kokkos memory space or execution space.
134/// @param[in] src A layout right ChunkSpan.
135/// @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.
136template <class Space, class ElementType, class Support, class Layout, class MemorySpace>
138 [[maybe_unused]] Space const& space,
139 ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
140{
141 static_assert(
142 Kokkos::is_memory_space_v<Space> || Kokkos::is_execution_space_v<Space>,
143 "DDC: parameter \"Space\" must be either a Kokkos execution space or a memory space");
144 static_assert(
145 std::is_same_v<Layout, Kokkos::layout_right>,
146 "DDC: parameter \"Layout\" must be a `layout_right`");
147 if constexpr (Kokkos::SpaceAccessibility<Space, MemorySpace>::accessible) {
148 return src;
149 } else {
150 return create_mirror_and_copy(space, src);
151 }
152}
153
154/// Equivalent to `create_mirror_view_and_copy(Kokkos::HostSpace(), src)`.
155/// @param[in] src A layout right ChunkSpan.
156/// @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.
157template <class ElementType, class Support, class Layout, class MemorySpace>
158auto create_mirror_view_and_copy(ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
159{
160 return create_mirror_view_and_copy(Kokkos::HostSpace(), src);
161}
162
163} // namespace ddc
friend class ChunkSpan
friend class Chunk
Definition chunk.hpp:81
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)