Debian Codegraph @

include/deal.II/base/memory_space_data.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2020 - 2025 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // Part of the source code is dual licensed under Apache-2.0 WITH
9 // LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10 // governing the source code and code contributions can be found in
11 // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12 //
13 // ------------------------------------------------------------------------
14
15
16 #ifndef dealii_memory_space_data_h
17 #define dealii_memory_space_data_h
18
19 #include <deal.II/base/config.h>
20
21 #include <deal.II/base/array_view.h>
22 #include <deal.II/base/exceptions.h>
23 #include <deal.II/base/kokkos.h>
24
25 #include <Kokkos_Core.hpp>
26
27 #include <functional>
28 #include <memory>
29
30 DEAL_II_NAMESPACE_OPEN
31
32 /**
33 */
34 namespace MemorySpace
35 {
36 /**
37 * Structure which stores data on the host or the @ref GlossDevice "device" depending on the
38 * template parameter @p MemorySpace. Valid choices are MemorySpace::Host and MemorySpace::Default.
39 * The data is copied into the structure which then owns the data
40 * and will release the memory when the destructor is called.
41 */
42 template <typename T, typename MemorySpace>
43 struct MemorySpaceData
44 {
45 MemorySpaceData();
46
47 /**
48 * Copy the class member values to @p begin.
49 * If the data is on the @ref GlossDevice "device" it is moved to the host.
50 */
51 void
52 copy_to(T *begin, const std::size_t n_elements);
53
54 /**
55 * Copy the data in @p begin to the class member values.
56 * The pointer @p begin must be on the host.
57 */
58 void
59 copy_from(const T *begin, const std::size_t n_elements);
60
61 /**
62 * Kokkos View owning a host buffer used for MPI communication.
63 */
64 // FIXME Should we move this somewhere else?
65 #if DEAL_II_KOKKOS_VERSION_GTE(4, 0, 0)
66 Kokkos::View<T *, Kokkos::SharedHostPinnedSpace> values_host_buffer;
67 #else
68 Kokkos::View<T *, Kokkos::HostSpace> values_host_buffer;
69 #endif
70
71 /**
72 * Kokkos View owning the data on the @ref GlossDevice "device" (unless @p values_sm_ptr is used).
73 */
74 Kokkos::View<T *, typename MemorySpace::kokkos_space> values;
75
76 /**
77 * Pointer to data on the host. The pointer points to the same data as
78 * @p values when using shared memory and the memory space is
79 * MemorySpace::Host. Otherwise it is not set.
80 */
81 // This a shared pointer so that MemorySpaceData can be copied and
82 // MemorySpaceData::values can be used in Kokkos::parallel_for. This
83 // pointer owns the data when using shared memory with MPI. In this case,
84 // the Kokkos::View @p values is non-owning. When shared memory with MPI is
85 // not used, the @p values_sm_ptr is unused.
86 std::shared_ptr<T> values_sm_ptr;
87
88 /**
89 * Pointers to the data of the processes sharing the same memory.
90 */
91 std::vector<ArrayView<const T>> values_sm;
92 };
93
94
95
96 /**
97 * Swap function similar to std::swap.
98 */
99 template <typename T, typename MemorySpace>
100 inline void
101 swap(MemorySpaceData<T, MemorySpace> &u,
102 MemorySpaceData<T, MemorySpace> &v) noexcept;
103
104
105 #ifndef DOXYGEN
106
107 template <typename T, typename MemorySpace>
108 MemorySpaceData<T, MemorySpace>::MemorySpaceData()
109 : values_host_buffer(
110 (dealii::internal::ensure_kokkos_initialized(),
111 # if DEAL_II_KOKKOS_VERSION_GTE(4, 0, 0)
112 Kokkos::View<T *, Kokkos::SharedHostPinnedSpace>("host pinned buffer",
113 0)))
114 # else
115 Kokkos::View<T *, Kokkos::HostSpace>("host buffer", 0)))
116 # endif
117 , values(Kokkos::View<T *, typename MemorySpace::kokkos_space>(
118 "memoryspace data",
119 0))
120 {}
121
122
123
124 template <typename T, typename MemorySpace>
125 void
126 MemorySpaceData<T, MemorySpace>::copy_to(T *begin,
127 const std::size_t n_elements)
128 {
129 Assert(n_elements <= values.extent(0),
130 ExcMessage(
131 "n_elements is greater than the size of MemorySpaceData."));
132 using ExecutionSpace = typename MemorySpace::kokkos_space::execution_space;
133 Kokkos::
134 View<T *, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged>>
135 begin_view(begin, n_elements);
136 Kokkos::deep_copy(
137 ExecutionSpace{},
138 begin_view,
139 Kokkos::subview(values, Kokkos::make_pair(std::size_t(0), n_elements)));
140 ExecutionSpace{}.fence();
141 }
142
143
144
145 template <typename T, typename MemorySpace>
146 void
147 MemorySpaceData<T, MemorySpace>::copy_from(const T *begin,
148 const std::size_t n_elements)
149 {
150 Assert(n_elements <= values.extent(0),
151 ExcMessage(
152 "n_elements is greater than the size of MemorySpaceData."));
153 using ExecutionSpace = typename MemorySpace::kokkos_space::execution_space;
154 Kokkos::View<const T *,
155 Kokkos::HostSpace,
156 Kokkos::MemoryTraits<Kokkos::Unmanaged>>
157 begin_view(begin, n_elements);
158 Kokkos::deep_copy(
159 ExecutionSpace{},
160 Kokkos::subview(values, Kokkos::make_pair(std::size_t(0), n_elements)),
161 begin_view);
162 ExecutionSpace{}.fence();
163 }
164
165
166
167 /**
168 * Swap function similar to std::swap.
169 */
170 template <typename T, typename MemorySpace>
171 inline void
172 swap(MemorySpaceData<T, MemorySpace> &u,
173 MemorySpaceData<T, MemorySpace> &v) noexcept
174 {
175 std::swap(u.values_host_buffer, v.values_host_buffer);
176 std::swap(u.values, v.values);
177 std::swap(u.values_sm_ptr, v.values_sm_ptr);
178 std::swap(u.values_sm, v.values_sm);
179 }
180
181 #endif
182
183 } // namespace MemorySpace
184
185 DEAL_II_NAMESPACE_CLOSE
186
187 #endif