Reference documentation for deal.II version 9.5.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// Copyright (C) 2020 - 2023 by the deal.II authors
4//
5// This file is part of the deal.II library.
6//
7// The deal.II library is free software; you can use it, redistribute
8// it, and/or modify it under the terms of the GNU Lesser General
9// Public License as published by the Free Software Foundation; either
10// version 2.1 of the License, or (at your option) any later version.
11// The full text of the license can be found in the file LICENSE.md at
12// the top level directory of deal.II.
13//
14// ---------------------------------------------------------------------
15
16
17#ifndef dealii_memory_space_data_h
18#define dealii_memory_space_data_h
19
20#include <deal.II/base/config.h>
21
22#include <deal.II/base/cuda.h>
24#include <deal.II/base/kokkos.h>
25
26#include <Kokkos_Core.hpp>
27
28#include <functional>
29#include <memory>
30
32
35namespace MemorySpace
36{
44 template <typename T, typename MemorySpace>
46 {
48
53 void
54 copy_to(T *begin, const std::size_t n_elements);
55
60 void
61 copy_from(const T *begin, const std::size_t n_elements);
62
66 // FIXME Should we move this somewhere else?
67 Kokkos::View<T *, Kokkos::HostSpace> values_host_buffer;
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
100
101
102#ifndef DOXYGEN
103
104 template <typename T, typename MemorySpace>
106 : values_host_buffer(
107 (::internal::ensure_kokkos_initialized(),
108 Kokkos::View<T *, Kokkos::HostSpace>("host buffer", 0)))
109 , values(Kokkos::View<T *, typename MemorySpace::kokkos_space>(
110 "memoryspace data",
111 0))
112 {}
113
114
115
116 template <typename T, typename MemorySpace>
117 void
118 MemorySpaceData<T, MemorySpace>::copy_to(T * begin,
119 const std::size_t n_elements)
120 {
121 Assert(n_elements <= values.extent(0),
123 "n_elements is greater than the size of MemorySpaceData."));
124 using ExecutionSpace = typename MemorySpace::kokkos_space::execution_space;
125 Kokkos::
126 View<T *, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged>>
127 begin_view(begin, n_elements);
128 Kokkos::deep_copy(
129 ExecutionSpace{},
130 begin_view,
131 Kokkos::subview(values, Kokkos::make_pair(std::size_t(0), n_elements)));
132 ExecutionSpace{}.fence();
133 }
134
135
136
137 template <typename T, typename MemorySpace>
138 void
139 MemorySpaceData<T, MemorySpace>::copy_from(const T * begin,
140 const std::size_t n_elements)
141 {
142 Assert(n_elements <= values.extent(0),
144 "n_elements is greater than the size of MemorySpaceData."));
145 using ExecutionSpace = typename MemorySpace::kokkos_space::execution_space;
146 Kokkos::View<const T *,
147 Kokkos::HostSpace,
148 Kokkos::MemoryTraits<Kokkos::Unmanaged>>
149 begin_view(begin, n_elements);
150 Kokkos::deep_copy(
151 ExecutionSpace{},
152 Kokkos::subview(values, Kokkos::make_pair(std::size_t(0), n_elements)),
153 begin_view);
154 ExecutionSpace{}.fence();
155 }
156
157
158
162 template <typename T, typename MemorySpace>
163 inline void
164 swap(MemorySpaceData<T, MemorySpace> &u, MemorySpaceData<T, MemorySpace> &v)
165 {
166 std::swap(u.values_host_buffer, v.values_host_buffer);
167 std::swap(u.values, v.values);
168 std::swap(u.values_sm_ptr, v.values_sm_ptr);
169 }
170
171#endif
172
173} // namespace MemorySpace
174
176
177#endif
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:472
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:473
#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)