Reference documentation for deal.II version 9.6.0
\(\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
21#include <deal.II/base/cuda.h>
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{
43 template <typename T, typename MemorySpace>
45 {
47
52 void
53 copy_to(T *begin, const std::size_t n_elements);
54
59 void
60 copy_from(const T *begin, const std::size_t n_elements);
61
65 // FIXME Should we move this somewhere else?
66#if KOKKOS_VERSION < 40000
67 Kokkos::View<T *, Kokkos::HostSpace> values_host_buffer;
68#else
69 Kokkos::View<T *, Kokkos::SharedHostPinnedSpace> values_host_buffer;
70#endif
71
75 Kokkos::View<T *, typename MemorySpace::kokkos_space> values;
76
82 // This a shared pointer so that MemorySpaceData can be copied and
83 // MemorySpaceData::values can be used in Kokkos::parallel_for. This
84 // pointer owns the data when using shared memory with MPI. In this case,
85 // the Kokkos::View @p values is non-owning. When shared memory with MPI is
86 // not used, the @p values_sm_ptr is unused.
87 std::shared_ptr<T> values_sm_ptr;
88
92 std::vector<ArrayView<const T>> values_sm;
93 };
94
95
96
100 template <typename T, typename MemorySpace>
101 inline void
104
105
106#ifndef DOXYGEN
107
108 template <typename T, typename MemorySpace>
110 : values_host_buffer(
111 (::internal::ensure_kokkos_initialized(),
112# if KOKKOS_VERSION < 40000
113 Kokkos::View<T *, Kokkos::HostSpace>("host buffer", 0)))
114# else
115 Kokkos::View<T *, Kokkos::SharedHostPinnedSpace>("host pinned buffer",
116 0)))
117# endif
118 , values(Kokkos::View<T *, typename MemorySpace::kokkos_space>(
119 "memoryspace data",
120 0))
121 {}
122
123
124
125 template <typename T, typename MemorySpace>
126 void
127 MemorySpaceData<T, MemorySpace>::copy_to(T *begin,
128 const std::size_t n_elements)
129 {
130 Assert(n_elements <= values.extent(0),
132 "n_elements is greater than the size of MemorySpaceData."));
133 using ExecutionSpace = typename MemorySpace::kokkos_space::execution_space;
134 Kokkos::
135 View<T *, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged>>
136 begin_view(begin, n_elements);
137 Kokkos::deep_copy(
138 ExecutionSpace{},
139 begin_view,
140 Kokkos::subview(values, Kokkos::make_pair(std::size_t(0), n_elements)));
141 ExecutionSpace{}.fence();
142 }
143
144
145
146 template <typename T, typename MemorySpace>
147 void
148 MemorySpaceData<T, MemorySpace>::copy_from(const T *begin,
149 const std::size_t n_elements)
150 {
151 Assert(n_elements <= values.extent(0),
153 "n_elements is greater than the size of MemorySpaceData."));
154 using ExecutionSpace = typename MemorySpace::kokkos_space::execution_space;
155 Kokkos::View<const T *,
156 Kokkos::HostSpace,
157 Kokkos::MemoryTraits<Kokkos::Unmanaged>>
158 begin_view(begin, n_elements);
159 Kokkos::deep_copy(
160 ExecutionSpace{},
161 Kokkos::subview(values, Kokkos::make_pair(std::size_t(0), n_elements)),
162 begin_view);
163 ExecutionSpace{}.fence();
164 }
165
166
167
171 template <typename T, typename MemorySpace>
172 inline void
173 swap(MemorySpaceData<T, MemorySpace> &u,
174 MemorySpaceData<T, MemorySpace> &v) noexcept
175 {
176 std::swap(u.values_host_buffer, v.values_host_buffer);
177 std::swap(u.values, v.values);
178 std::swap(u.values_sm_ptr, v.values_sm_ptr);
179 }
180
181#endif
182
183} // namespace MemorySpace
184
186
187#endif
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:503
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:504
#define Assert(cond, exc)
static ::ExceptionBase & ExcMessage(std::string arg1)
void swap(MemorySpaceData< T, MemorySpace > &u, MemorySpaceData< T, MemorySpace > &v) noexcept
void swap(SmartPointer< T, P > &t1, SmartPointer< 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)