Reference documentation for deal.II version GIT b206511199 2023-01-31 13:40: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\}}\)
memory_space_data.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2020 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 
27 #include <Kokkos_Core.hpp>
29 
30 #include <functional>
31 #include <memory>
32 
34 
37 namespace MemorySpace
38 {
46  template <typename T, typename MemorySpace>
48  {
50 
55  void
56  copy_to(T *begin, const std::size_t n_elements);
57 
62  void
63  copy_from(const T *begin, const std::size_t n_elements);
64 
68  // FIXME Should we move this somewhere else?
69  Kokkos::View<T *, Kokkos::HostSpace> values_host_buffer;
70 
74  Kokkos::View<T *, typename MemorySpace::kokkos_space> values;
75 
81  // This a shared pointer 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
102 
103 
104 #ifndef DOXYGEN
105 
106  template <typename T, typename MemorySpace>
108  : values_host_buffer(
110  Kokkos::View<T *, Kokkos::HostSpace>("host buffer", 0)))
111  , values(Kokkos::View<T *, typename MemorySpace::kokkos_space>(
112  "memoryspace data",
113  0))
114  {}
115 
116 
117 
118  template <typename T, typename MemorySpace>
119  void
120  MemorySpaceData<T, MemorySpace>::copy_to(T * begin,
121  const std::size_t n_elements)
122  {
123  Assert(n_elements <= values.extent(0),
124  ExcMessage(
125  "n_elements is greater than the size of MemorySpaceData."));
126  using ExecutionSpace = typename MemorySpace::kokkos_space::execution_space;
127  Kokkos::
128  View<T *, Kokkos::HostSpace, Kokkos::MemoryTraits<Kokkos::Unmanaged>>
129  begin_view(begin, n_elements);
130  Kokkos::deep_copy(
131  ExecutionSpace{},
132  begin_view,
133  Kokkos::subview(values, Kokkos::make_pair(std::size_t(0), n_elements)));
134  ExecutionSpace{}.fence();
135  }
136 
137 
138 
139  template <typename T, typename MemorySpace>
140  void
141  MemorySpaceData<T, MemorySpace>::copy_from(const T * begin,
142  const std::size_t n_elements)
143  {
144  Assert(n_elements <= values.extent(0),
145  ExcMessage(
146  "n_elements is greater than the size of MemorySpaceData."));
147  using ExecutionSpace = typename MemorySpace::kokkos_space::execution_space;
148  Kokkos::View<const T *,
149  Kokkos::HostSpace,
150  Kokkos::MemoryTraits<Kokkos::Unmanaged>>
151  begin_view(begin, n_elements);
152  Kokkos::deep_copy(
153  ExecutionSpace{},
154  Kokkos::subview(values, Kokkos::make_pair(std::size_t(0), n_elements)),
155  begin_view);
156  ExecutionSpace{}.fence();
157  }
158 
159 
160 
164  template <typename T, typename MemorySpace>
165  inline void
166  swap(MemorySpaceData<T, MemorySpace> &u, MemorySpaceData<T, MemorySpace> &v)
167  {
168  std::swap(u.values_host_buffer, v.values_host_buffer);
169  std::swap(u.values, v.values);
170  std::swap(u.values_sm_ptr, v.values_sm_ptr);
171  }
172 
173 #endif
174 
175 } // namespace MemorySpace
176 
178 
179 #endif
void swap(BlockIndices &u, BlockIndices &v)
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:461
#define DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
Definition: config.h:475
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:462
#define DEAL_II_ENABLE_EXTRA_DIAGNOSTICS
Definition: config.h:516
#define Assert(cond, exc)
Definition: exceptions.h:1509
static ::ExceptionBase & ExcMessage(std::string arg1)
typename ::internal::FEInterfaceViews::ViewType< dim, spacedim, Extractor >::type View
static const char T
void swap(MemorySpaceData< T, MemorySpace > &u, MemorySpaceData< T, MemorySpace > &v)
VectorType::value_type * begin(VectorType &V)
void ensure_kokkos_initialized()
Definition: kokkos.cc:30
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)