DDC 0.1.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 "ddc/chunk_span.hpp"
12#include "ddc/kokkos_allocator.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 static_assert(
25 Kokkos::is_memory_space_v<Space> || Kokkos::is_execution_space_v<Space>,
26 "DDC: parameter \"Space\" must be either a Kokkos execution space or a memory space");
27 static_assert(
28 std::is_same_v<Layout, Kokkos::layout_right>,
29 "DDC: parameter \"Layout\" must be a `layout_right`");
30 return Chunk(
31 src.domain(),
32 KokkosAllocator<std::remove_const_t<ElementType>, typename Space::memory_space>());
33}
34
35/// Equivalent to `create_mirror(Kokkos::HostSpace(), src)`.
36/// @param[in] src A layout right ChunkSpan.
37/// @return a `Chunk` with the same support and layout as `src` allocated on the `Kokkos::HostSpace` memory space.
38template <class ElementType, class Support, class Layout, class MemorySpace>
39auto create_mirror(ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
40{
41 return create_mirror(Kokkos::HostSpace(), src);
42}
43
44/// @param[in] space A Kokkos memory space or execution space.
45/// @param[in] src A layout right ChunkSpan.
46/// @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.
47template <class Space, class ElementType, class Support, class Layout, class MemorySpace>
49 Space const& space,
50 ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
51{
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 Chunk chunk = create_mirror(space, src);
59 parallel_deepcopy(chunk, src);
60 return chunk;
61}
62
63/// Equivalent to `create_mirror_and_copy(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 and operates a deep copy between the two.
66template <class ElementType, class Support, class Layout, class MemorySpace>
67auto create_mirror_and_copy(ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
68{
69 return create_mirror_and_copy(Kokkos::HostSpace(), src);
70}
71
72/// @param[in] space A Kokkos memory space or execution space.
73/// @param[in] src A non-const, layout right ChunkSpan.
74/// @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.
75template <class Space, class ElementType, class Support, class Layout, class MemorySpace>
77 [[maybe_unused]] Space const& space,
78 ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
79{
80 static_assert(
81 !std::is_const_v<ElementType>,
82 "DDC: parameter \"ElementType\" must not be `const`");
83 static_assert(
84 Kokkos::is_memory_space_v<Space> || Kokkos::is_execution_space_v<Space>,
85 "DDC: parameter \"Space\" must be either a Kokkos execution space or a memory space");
86 static_assert(
87 std::is_same_v<Layout, Kokkos::layout_right>,
88 "DDC: parameter \"Layout\" must be a `layout_right`");
89 if constexpr (Kokkos::SpaceAccessibility<Space, MemorySpace>::accessible) {
90 return src;
91 } else {
92 return create_mirror(space, src);
93 }
94}
95
96/// Equivalent to `create_mirror_view(Kokkos::HostSpace(), src)`.
97/// @param[in] src A non-const, layout right ChunkSpan.
98/// @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.
99template <class ElementType, class Support, class Layout, class MemorySpace>
100auto create_mirror_view(ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
101{
102 return create_mirror_view(Kokkos::HostSpace(), src);
103}
104
105/// @param[in] space A Kokkos memory space or execution space.
106/// @param[in] src A layout right ChunkSpan.
107/// @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.
108template <class Space, class ElementType, class Support, class Layout, class MemorySpace>
110 [[maybe_unused]] Space const& space,
111 ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
112{
113 static_assert(
114 Kokkos::is_memory_space_v<Space> || Kokkos::is_execution_space_v<Space>,
115 "DDC: parameter \"Space\" must be either a Kokkos execution space or a memory space");
116 static_assert(
117 std::is_same_v<Layout, Kokkos::layout_right>,
118 "DDC: parameter \"Layout\" must be a `layout_right`");
119 if constexpr (Kokkos::SpaceAccessibility<Space, MemorySpace>::accessible) {
120 return src;
121 } else {
122 return create_mirror_and_copy(space, src);
123 }
124}
125
126/// Equivalent to `create_mirror_view_and_copy(Kokkos::HostSpace(), src)`.
127/// @param[in] src A layout right ChunkSpan.
128/// @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.
129template <class ElementType, class Support, class Layout, class MemorySpace>
130auto create_mirror_view_and_copy(ChunkSpan<ElementType, Support, Layout, MemorySpace> const& src)
131{
132 return create_mirror_view_and_copy(Kokkos::HostSpace(), src);
133}
134
135} // namespace ddc
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)