Reference documentation for deal.II version GIT relicensing-822-gf1642d4ea5 2024-06-12 14:10:02+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 - 2023 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
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(
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(
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
173 {
174 std::swap(u.values_host_buffer, v.values_host_buffer);
175 std::swap(u.values, v.values);
176 std::swap(u.values_sm_ptr, v.values_sm_ptr);
177 }
178
179#endif
180
181} // namespace MemorySpace
182
184
185#endif
friend class Tensor
Definition tensor.h:882
std::conditional_t< rank_==1, std::array< Number, dim >, std::array< Tensor< rank_ - 1, dim, Number >, dim > > values
Definition tensor.h:868
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:502
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:503
#define Assert(cond, exc)
static ::ExceptionBase & ExcMessage(std::string arg1)
void swap(MemorySpaceData< T, MemorySpace > &u, MemorySpaceData< T, MemorySpace > &v)
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)