deal.II version GIT relicensing-2291-g668cd86249 2024-12-24 11:30:00+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: LGPL-2.1-or-later
4// Copyright (C) 2020 - 2024 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
23#include <deal.II/base/kokkos.h>
24
25#include <Kokkos_Core.hpp>
26
27#include <functional>
28#include <memory>
29
31
34namespace MemorySpace
35{
42 template <typename T, typename MemorySpace>
44 {
46
51 void
52 copy_to(T *begin, const std::size_t n_elements);
53
58 void
59 copy_from(const T *begin, const std::size_t n_elements);
60
64 // FIXME Should we move this somewhere else?
65#if KOKKOS_VERSION < 40000
66 Kokkos::View<T *, Kokkos::HostSpace> values_host_buffer;
67#else
68 Kokkos::View<T *, Kokkos::SharedHostPinnedSpace> values_host_buffer;
69#endif
70
74 Kokkos::View<T *, typename MemorySpace::kokkos_space> values;
75
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
91 std::vector<ArrayView<const T>> values_sm;
92 };
93
94
95
99 template <typename T, typename MemorySpace>
100 inline void
103
104
105#ifndef DOXYGEN
106
107 template <typename T, typename MemorySpace>
109 : values_host_buffer(
110 (::internal::ensure_kokkos_initialized(),
111# if KOKKOS_VERSION < 40000
112 Kokkos::View<T *, Kokkos::HostSpace>("host buffer", 0)))
113# else
114 Kokkos::View<T *, Kokkos::SharedHostPinnedSpace>("host pinned buffer",
115 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),
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),
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
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
186
187#endif
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:498
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:499
#define Assert(cond, exc)
static ::ExceptionBase & ExcMessage(std::string arg1)
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)