Reference documentation for deal.II version 9.2.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\}}\)
memory_space.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2018 - 2019 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_h
18 #define dealii_memory_space_h
19 
20 #include <deal.II/base/config.h>
21 
22 #include <deal.II/base/cuda.h>
24 
25 #include <memory>
26 
28 
32 namespace MemorySpace
33 {
37  struct Host
38  {};
39 
40 
41 
45  struct CUDA
46  {};
47 
48 
49 
53  template <typename Number, typename MemorySpace>
55  {
57  {
60  "MemorySpace should be Host or CUDA");
61  }
62 
67  void
68  copy_to(Number *begin, std::size_t n_elements)
69  {
70  (void)begin;
71  (void)n_elements;
72  }
73 
74  void
79  copy_from(Number *begin, std::size_t n_elements)
80  {
81  (void)begin;
82  (void)n_elements;
83  }
84 
88  std::unique_ptr<Number[], decltype(&std::free)> values;
89 
93  std::unique_ptr<Number[]> values_dev;
94  };
95 
96 
97 
101  template <typename Number, typename MemorySpace>
102  inline void
105  {
108  "MemorySpace should be Host or CUDA");
109  }
110 
111 #ifndef DOXYGEN
112 
113  template <typename Number>
114  struct MemorySpaceData<Number, Host>
115  {
117  : values(nullptr, &std::free)
118  {}
119 
120  void
121  copy_to(Number *begin, std::size_t n_elements)
122  {
123  std::copy(values.get(), values.get() + n_elements, begin);
124  }
125 
126  void
127  copy_from(Number *begin, std::size_t n_elements)
128  {
129  std::copy(begin, begin + n_elements, values.get());
130  }
131 
132  std::unique_ptr<Number[], decltype(&std::free)> values;
133 
134  // This is not used but it allows to simplify the code until we start using
135  // CUDA-aware MPI.
136  std::unique_ptr<Number[]> values_dev;
137  };
138 
139 
140 
141  template <typename Number>
142  inline void
143  swap(MemorySpaceData<Number, Host> &u, MemorySpaceData<Number, Host> &v)
144  {
145  std::swap(u.values, v.values);
146  }
147 
148 
149 
150 # ifdef DEAL_II_COMPILER_CUDA_AWARE
151 
152  template <typename Number>
153  struct MemorySpaceData<Number, CUDA>
154  {
156  : values(nullptr, &std::free)
157  , values_dev(nullptr, Utilities::CUDA::delete_device_data<Number>)
158  {}
159 
160  void
161  copy_to(Number *begin, std::size_t n_elements)
162  {
163  const cudaError_t cuda_error_code =
164  cudaMemcpy(begin,
165  values_dev.get(),
166  n_elements * sizeof(Number),
167  cudaMemcpyDeviceToHost);
168  AssertCuda(cuda_error_code);
169  }
170 
171  void
172  copy_from(Number *begin, std::size_t n_elements)
173  {
174  const cudaError_t cuda_error_code =
175  cudaMemcpy(values_dev.get(),
176  begin,
177  n_elements * sizeof(Number),
178  cudaMemcpyHostToDevice);
179  AssertCuda(cuda_error_code);
180  }
181 
182  std::unique_ptr<Number[], decltype(&std::free)> values;
183  std::unique_ptr<Number[], void (*)(Number *)> values_dev;
184  };
185 
186 
187 
188  template <typename Number>
189  inline void
190  swap(MemorySpaceData<Number, CUDA> &u, MemorySpaceData<Number, CUDA> &v)
191  {
192  std::swap(u.values, v.values);
193  std::swap(u.values_dev, v.values_dev);
194  }
195 
196 # endif
197 
198 #endif
199 
200 } // namespace MemorySpace
201 
203 
204 #endif
MemorySpace::Host
Definition: memory_space.h:37
cuda.h
MemorySpace
Definition: memory_space.h:32
MemorySpace::CUDA
Definition: memory_space.h:45
MemorySpace::MemorySpaceData::copy_to
void copy_to(Number *begin, std::size_t n_elements)
Definition: memory_space.h:68
Utilities::CUDA::delete_device_data
void delete_device_data(Number *device_ptr) noexcept
Definition: cuda.h:120
Utilities::CUDA::free
void free(T *&pointer)
Definition: cuda.h:96
AssertCuda
#define AssertCuda(error_code)
Definition: exceptions.h:1734
MemorySpace::MemorySpaceData::MemorySpaceData
MemorySpaceData()
Definition: memory_space.h:56
TrilinosWrappers::internal::begin
VectorType::value_type * begin(VectorType &V)
Definition: trilinos_sparse_matrix.cc:51
MemorySpace::MemorySpaceData::values
std::unique_ptr< Number[], decltype(&std::free)> values
Definition: memory_space.h:88
DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:358
MemorySpace::swap
void swap(MemorySpaceData< Number, MemorySpace > &, MemorySpaceData< Number, MemorySpace > &)
Definition: memory_space.h:103
MemorySpace::MemorySpaceData
Definition: memory_space.h:54
MemorySpace::MemorySpaceData::copy_from
void copy_from(Number *begin, std::size_t n_elements)
Definition: memory_space.h:79
exceptions.h
value
static const bool value
Definition: dof_tools_constraints.cc:433
MemorySpace::MemorySpaceData::values_dev
std::unique_ptr< Number[]> values_dev
Definition: memory_space.h:93
config.h
DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:359
Utilities
Definition: cuda.h:31
internal::VectorOperations::copy
void copy(const T *begin, const T *end, U *dest)
Definition: vector_operations_internal.h:67