Reference documentation for deal.II version Git 01d13113a1 2020-07-10 09:14:01 +0200
\(\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 
31 namespace MemorySpace
32 {
36  struct Host
37  {};
38 
39 
40 
44  struct CUDA
45  {};
46 
47 
48 
52  template <typename Number, typename MemorySpace>
54  {
56  {
57  static_assert(std::is_same<MemorySpace, Host>::value ||
58  std::is_same<MemorySpace, CUDA>::value,
59  "MemorySpace should be Host or CUDA");
60  }
61 
66  void
67  copy_to(Number *begin, std::size_t n_elements)
68  {
69  (void)begin;
70  (void)n_elements;
71  }
72 
73  void
78  copy_from(Number *begin, std::size_t n_elements)
79  {
80  (void)begin;
81  (void)n_elements;
82  }
83 
87  std::unique_ptr<Number[], decltype(&std::free)> values;
88 
92  std::unique_ptr<Number[]> values_dev;
93  };
94 
95 
96 
100  template <typename Number, typename MemorySpace>
101  inline void
104  {
105  static_assert(std::is_same<MemorySpace, Host>::value ||
106  std::is_same<MemorySpace, CUDA>::value,
107  "MemorySpace should be Host or CUDA");
108  }
109 
110 #ifndef DOXYGEN
111 
112  template <typename Number>
113  struct MemorySpaceData<Number, Host>
114  {
116  : values(nullptr, &std::free)
117  {}
118 
119  void
120  copy_to(Number *begin, std::size_t n_elements)
121  {
122  std::copy(values.get(), values.get() + n_elements, begin);
123  }
124 
125  void
126  copy_from(Number *begin, std::size_t n_elements)
127  {
128  std::copy(begin, begin + n_elements, values.get());
129  }
130 
131  std::unique_ptr<Number[], decltype(&std::free)> values;
132 
133  // This is not used but it allows to simplify the code until we start using
134  // CUDA-aware MPI.
135  std::unique_ptr<Number[]> values_dev;
136  };
137 
138 
139 
140  template <typename Number>
141  inline void
143  {
144  std::swap(u.values, v.values);
145  }
146 
147 
148 
149 # ifdef DEAL_II_COMPILER_CUDA_AWARE
150 
151  template <typename Number>
152  struct MemorySpaceData<Number, CUDA>
153  {
155  : values(nullptr, &std::free)
156  , values_dev(nullptr, Utilities::CUDA::delete_device_data<Number>)
157  {}
158 
159  void
160  copy_to(Number *begin, std::size_t n_elements)
161  {
162  const cudaError_t cuda_error_code =
163  cudaMemcpy(begin,
164  values_dev.get(),
165  n_elements * sizeof(Number),
166  cudaMemcpyDeviceToHost);
167  AssertCuda(cuda_error_code);
168  }
169 
170  void
171  copy_from(Number *begin, std::size_t n_elements)
172  {
173  const cudaError_t cuda_error_code =
174  cudaMemcpy(values_dev.get(),
175  begin,
176  n_elements * sizeof(Number),
177  cudaMemcpyHostToDevice);
178  AssertCuda(cuda_error_code);
179  }
180 
181  std::unique_ptr<Number[], decltype(&std::free)> values;
182  std::unique_ptr<Number[], void (*)(Number *)> values_dev;
183  };
184 
185 
186 
187  template <typename Number>
188  inline void
190  {
191  std::swap(u.values, v.values);
193  }
194 
195 # endif
196 
197 #endif
198 
199 } // namespace MemorySpace
200 
202 
203 #endif
void copy_from(Number *begin, std::size_t n_elements)
Definition: memory_space.h:78
#define AssertCuda(error_code)
Definition: exceptions.h:1710
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:363
std::unique_ptr< Number[], decltype(&std::free)> values
Definition: memory_space.h:87
void swap(MemorySpaceData< Number, MemorySpace > &, MemorySpaceData< Number, MemorySpace > &)
Definition: memory_space.h:102
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:362
VectorType::value_type * begin(VectorType &V)
void free(T *&pointer)
Definition: cuda.h:96
void copy(const T *begin, const T *end, U *dest)
void copy_to(Number *begin, std::size_t n_elements)
Definition: memory_space.h:67
std::unique_ptr< Number[]> values_dev
Definition: memory_space.h:92