Reference documentation for deal.II version 9.1.1
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
memory_space.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2018 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>
23 #include <deal.II/base/exceptions.h>
24 
25 #include <memory>
26 
27 DEAL_II_NAMESPACE_OPEN
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  {
58  static_assert(std::is_same<MemorySpace, Host>::value ||
59  std::is_same<MemorySpace, CUDA>::value,
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(&free)> values;
89 
93  std::unique_ptr<Number[]> values_dev;
94  };
95 
96 
97 
101  template <typename Number, typename MemorySpace>
102  inline void
105  {
106  static_assert(std::is_same<MemorySpace, Host>::value ||
107  std::is_same<MemorySpace, CUDA>::value,
108  "MemorySpace should be Host or CUDA");
109  }
110 
111 #ifndef DOXYGEN
112 
113  template <typename Number>
114  struct MemorySpaceData<Number, Host>
115  {
116  MemorySpaceData()
117  : values(nullptr, &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(&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  {
155  MemorySpaceData()
156  : values(nullptr, &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(&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 
202 DEAL_II_NAMESPACE_CLOSE
203 
204 #endif
void copy_from(Number *begin, std::size_t n_elements)
Definition: memory_space.h:79
#define AssertCuda(error_code)
Definition: exceptions.h:1722
std::unique_ptr< Number[], decltype(&free)> values
Definition: memory_space.h:88
void swap(Vector< Number > &u, Vector< Number > &v)
Definition: vector.h:1376
Definition: cuda.h:31
void delete_device_data(Number *device_ptr) noexcept
Definition: cuda.h:120
void free(T *&pointer)
Definition: cuda.h:96
void copy_to(Number *begin, std::size_t n_elements)
Definition: memory_space.h:68
std::unique_ptr< Number[]> values_dev
Definition: memory_space.h:93