Reference documentation for deal.II version Git cd5f69ed86 2019-12-07 22:13:35 -0500
\(\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(&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  {
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  {
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
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
191  {
192  std::swap(u.values, v.values);
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:1726
void swap(Vector< Number > &u, Vector< Number > &v)
Definition: vector.h:1376
std::unique_ptr< Number[], decltype(&std::free)> values
Definition: memory_space.h:88
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