deal.II version GIT relicensing-6834-g5b78e6bcdf 2026-10-01 11:20:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
memory_space_data.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR 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// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13
14#ifndef dealii_memory_space_data_h
15#define dealii_memory_space_data_h
16
17#include <deal.II/base/config.h>
18
21#include <deal.II/base/kokkos.h>
22
23#include <Kokkos_Core.hpp>
24
25#include <functional>
26#include <memory>
27
29
32namespace MemorySpace
33{
40 template <typename T, typename MemorySpace>
42 {
44
49 void
50 copy_to(T *begin, const std::size_t n_elements);
51
56 void
57 copy_from(const T *begin, const std::size_t n_elements);
58
62 // FIXME Should we move this somewhere else?
63#if DEAL_II_KOKKOS_VERSION_GTE(4, 0, 0)
64 Kokkos::View<T *, Kokkos::SharedHostPinnedSpace> values_host_buffer;
65#else
66 Kokkos::View<T *, Kokkos::HostSpace> values_host_buffer;
67#endif
68
72 Kokkos::View<T *, typename MemorySpace::kokkos_space> values;
73
79 // This a shared pointer so that MemorySpaceData can be copied and
80 // MemorySpaceData::values can be used in Kokkos::parallel_for. This
81 // pointer owns the data when using shared memory with MPI. In this case,
82 // the Kokkos::View @p values is non-owning. When shared memory with MPI is
83 // not used, the @p values_sm_ptr is unused.
84 std::shared_ptr<T> values_sm_ptr;
85
89 std::vector<ArrayView<const T>> values_sm;
90 };
91
92
93
97 template <typename T, typename MemorySpace>
98 inline void
101
102
103#ifndef DOXYGEN
104
105 template <typename T, typename MemorySpace>
107 : values_host_buffer(
108 (::internal::ensure_kokkos_initialized(),
109# if DEAL_II_KOKKOS_VERSION_GTE(4, 0, 0)
110 Kokkos::View<T *, Kokkos::SharedHostPinnedSpace>("host pinned buffer",
111 0)))
112# else
113 Kokkos::View<T *, Kokkos::HostSpace>("host buffer", 0)))
114# endif
115 , values(Kokkos::View<T *, typename MemorySpace::kokkos_space>(
116 "memoryspace data",
117 0))
118 {}
119
120
121
122 template <typename T, typename MemorySpace>
123 void
124 MemorySpaceData<T, MemorySpace>::copy_to(T *begin,
125 const std::size_t n_elements)
126 {
127 Assert(n_elements <= values.extent(0),
129 "n_elements is greater than the size of MemorySpaceData."));
130 using ExecutionSpace = typename MemorySpace::kokkos_space::execution_space;
131 Kokkos::
132 View<T *, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged>>
133 begin_view(begin, n_elements);
134 Kokkos::deep_copy(
135 ExecutionSpace{},
136 begin_view,
137 Kokkos::subview(values, Kokkos::make_pair(std::size_t(0), n_elements)));
138 ExecutionSpace{}.fence();
139 }
140
141
142
143 template <typename T, typename MemorySpace>
144 void
145 MemorySpaceData<T, MemorySpace>::copy_from(const T *begin,
146 const std::size_t n_elements)
147 {
148 Assert(n_elements <= values.extent(0),
150 "n_elements is greater than the size of MemorySpaceData."));
151 using ExecutionSpace = typename MemorySpace::kokkos_space::execution_space;
152 Kokkos::View<const T *,
153 Kokkos::HostSpace,
154 Kokkos::MemoryTraits<Kokkos::Unmanaged>>
155 begin_view(begin, n_elements);
156 Kokkos::deep_copy(
157 ExecutionSpace{},
158 Kokkos::subview(values, Kokkos::make_pair(std::size_t(0), n_elements)),
159 begin_view);
160 ExecutionSpace{}.fence();
161 }
162
163
164
168 template <typename T, typename MemorySpace>
169 inline void
170 swap(MemorySpaceData<T, MemorySpace> &u,
171 MemorySpaceData<T, MemorySpace> &v) noexcept
172 {
173 std::swap(u.values_host_buffer, v.values_host_buffer);
174 std::swap(u.values, v.values);
175 std::swap(u.values_sm_ptr, v.values_sm_ptr);
176 std::swap(u.values_sm, v.values_sm);
177 }
178
179#endif
180
181} // namespace MemorySpace
182
184
185#endif
*  *  iterator begin()
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define DEAL_II_KOKKOS_VERSION_GTE(major, minor, subminor)
Definition config.h:478
#define Assert(cond, exc)
static ::ExceptionBase & ExcMessage(std::string arg1)
constexpr char T
void swap(MemorySpaceData< T, MemorySpace > &u, MemorySpaceData< T, MemorySpace > &v) noexcept
void swap(ObserverPointer< T, P > &t1, ObserverPointer< T, Q > &t2)
Kokkos::View< T *, Kokkos::HostSpace > values_host_buffer
Kokkos::View< T *, typename MemorySpace::kokkos_space > values
std::vector< ArrayView< const T > > values_sm
std::shared_ptr< T > values_sm_ptr
void copy_to(T *begin, const std::size_t n_elements)
void copy_from(const T *begin, const std::size_t n_elements)