Reference documentation for deal.II version Git 8596a7cd07 2020-12-04 07:30:43 +0100
\(\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 
25 #include <functional>
26 #include <memory>
27 
29 
32 namespace MemorySpace
33 {
37  template <typename Number, typename MemorySpace>
39  {
41  {
42  static_assert(std::is_same<MemorySpace, Host>::value ||
43  std::is_same<MemorySpace, CUDA>::value,
44  "MemorySpace should be Host or CUDA");
45  }
46 
51  void
52  copy_to(Number *begin, std::size_t n_elements)
53  {
54  (void)begin;
55  (void)n_elements;
56  }
57 
62  void
63  copy_from(Number *begin, std::size_t n_elements)
64  {
65  (void)begin;
66  (void)n_elements;
67  }
68 
72  std::unique_ptr<Number[], std::function<void(Number *)>> values;
73 
77  std::unique_ptr<Number[]> values_dev;
78 
82  std::vector<ArrayView<const Number>> values_sm;
83  };
84 
85 
86 
90  template <typename Number, typename MemorySpace>
91  inline void
94  {
95  static_assert(std::is_same<MemorySpace, Host>::value ||
96  std::is_same<MemorySpace, CUDA>::value,
97  "MemorySpace should be Host or CUDA");
98  }
99 
100 #ifndef DOXYGEN
101 
102  template <typename Number>
103  struct MemorySpaceData<Number, Host>
104  {
106  : values(nullptr, &std::free)
107  {}
108 
109  void
110  copy_to(Number *begin, std::size_t n_elements)
111  {
112  std::copy(values.get(), values.get() + n_elements, begin);
113  }
114 
115  void
116  copy_from(Number *begin, std::size_t n_elements)
117  {
118  std::copy(begin, begin + n_elements, values.get());
119  }
120 
121  std::unique_ptr<Number[], std::function<void(Number *)>> values;
122 
123  // This is not used but it allows to simplify the code until we start using
124  // CUDA-aware MPI.
125  std::unique_ptr<Number[]> values_dev;
126 
127  std::vector<ArrayView<const Number>> values_sm;
128  };
129 
130 
131 
132  template <typename Number>
133  inline void
135  {
136  std::swap(u.values, v.values);
137  }
138 
139 
140 
141 # ifdef DEAL_II_COMPILER_CUDA_AWARE
142 
143  template <typename Number>
144  struct MemorySpaceData<Number, CUDA>
145  {
147  : values(nullptr, &std::free)
148  , values_dev(nullptr, Utilities::CUDA::delete_device_data<Number>)
149  {}
150 
151  void
152  copy_to(Number *begin, std::size_t n_elements)
153  {
154  const cudaError_t cuda_error_code =
155  cudaMemcpy(begin,
156  values_dev.get(),
157  n_elements * sizeof(Number),
158  cudaMemcpyDeviceToHost);
159  AssertCuda(cuda_error_code);
160  }
161 
162  void
163  copy_from(Number *begin, std::size_t n_elements)
164  {
165  const cudaError_t cuda_error_code =
166  cudaMemcpy(values_dev.get(),
167  begin,
168  n_elements * sizeof(Number),
169  cudaMemcpyHostToDevice);
170  AssertCuda(cuda_error_code);
171  }
172 
173  std::unique_ptr<Number[], std::function<void(Number *)>> values;
174  std::unique_ptr<Number[], void (*)(Number *)> values_dev;
175 
179  std::vector<ArrayView<const Number>> values_sm;
180  };
181 
182 
183 
184  template <typename Number>
185  inline void
187  {
188  std::swap(u.values, v.values);
190  }
191 
192 # endif
193 
194 #endif
195 
196 } // namespace MemorySpace
197 
199 
200 #endif
void copy_from(Number *begin, std::size_t n_elements)
std::vector< ArrayView< const Number > > values_sm
#define AssertCuda(error_code)
Definition: exceptions.h:1773
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:372
void swap(MemorySpaceData< Number, MemorySpace > &, MemorySpaceData< Number, MemorySpace > &)
std::unique_ptr< Number[], std::function< void(Number *)> > values
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:371
VectorType::value_type * begin(VectorType &V)
void free(T *&pointer)
Definition: cuda.h:97
void copy(const T *begin, const T *end, U *dest)
void copy_to(Number *begin, std::size_t n_elements)
std::unique_ptr< Number[]> values_dev