Reference documentation for deal.II version 9.0.0
cuda_matrix_free.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2016 - 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 at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 
17 #ifndef dealii_cuda_matrix_free_h
18 #define dealii_cuda_matrix_free_h
19 
20 #include <deal.II/base/config.h>
21 
22 #ifdef DEAL_II_WITH_CUDA
23 
24 #include <deal.II/base/quadrature.h>
25 #include <deal.II/base/tensor.h>
26 #include <deal.II/dofs/dof_handler.h>
27 #include <deal.II/fe/mapping.h>
28 #include <deal.II/fe/mapping_q1.h>
29 #include <deal.II/fe/fe_update_flags.h>
30 #include <deal.II/lac/constraint_matrix.h>
31 #include <deal.II/lac/cuda_vector.h>
32 #include <cuda_runtime_api.h>
33 
34 
35 DEAL_II_NAMESPACE_OPEN
36 
37 namespace CUDAWrappers
38 {
39  // forward declaration
40  namespace internal
41  {
42  template <int dim, typename Number>
43  class ReinitHelper;
44  }
45 
72  template <int dim, typename Number=double>
73  class MatrixFree : public Subscriptor
74  {
75  public:
77  // TODO this should really be a CUDAWrappers::Point
79 
80  // Use Number2 so we don't hide the template parameter Number
81  template <typename Number2>
83 
89  enum ParallelizationScheme {parallel_in_elem, parallel_over_elem};
90 
91  struct AdditionalData
92  {
93  AdditionalData (
94  const ParallelizationScheme parallelization_scheme = parallel_in_elem,
95  const UpdateFlags mapping_update_flags = update_gradients | update_JxW_values)
96  :
98  mapping_update_flags(mapping_update_flags)
99  {}
100 
104  unsigned int n_colors;
119  UpdateFlags mapping_update_flags;
120  };
121 
126  struct Data
127  {
128  point_type *q_points;
129  unsigned int *local_to_global;
130  Number *inv_jacobian;
131  Number *JxW;
132  unsigned int n_cells;
133  unsigned int padding_length;
134  unsigned int row_start;
135  unsigned int *constraint_mask;
136  };
137 
141  MatrixFree();
142 
143  unsigned int get_padding_length() const;
144 
152  void reinit(const Mapping<dim> &mapping,
153  const DoFHandler<dim> &dof_handler,
154  const ConstraintMatrix &constraints,
155  const Quadrature<1> &quad,
156  const AdditionalData additional_data = AdditionalData());
157 
161  void reinit(const DoFHandler<dim> &dof_handler,
162  const ConstraintMatrix &constraints,
163  const Quadrature<1> &quad,
164  const AdditionalData AdditionalData = AdditionalData());
165 
169  Data get_data(unsigned int color) const;
170 
175  template <typename functor>
176  void cell_loop(const functor &func,
177  const CUDAVector<Number> &src,
178  CUDAVector<Number> &dst) const;
179 
180  void copy_constrained_values(const CUDAVector<Number> &src,
181  CUDAVector<Number> &dst) const;
182 
183  void set_constrained_values(const Number val, CUDAVector<Number> &dst) const;
184 
188  void free();
189 
193  std::size_t memory_consumption() const;
194 
195  private:
204  unsigned int fe_degree;
208  unsigned int dofs_per_cell;
212  unsigned int n_constrained_dofs;
216  unsigned int q_points_per_cell;
220  unsigned int n_colors;
224  std::vector<unsigned int> n_cells;
229  std::vector<point_type *> q_points;
234  std::vector<unsigned int *> local_to_global;
239  std::vector<Number *> inv_jacobian;
244  std::vector<Number *> JxW;
245 
246  // Constraints
247  unsigned int *constrained_dofs;
248  std::vector<unsigned int *> constraint_mask;
253  std::vector<dim3> grid_dim;
258  std::vector<dim3> block_dim;
259 
260  // Parallelization parameter
261  unsigned int cells_per_block;
262  dim3 constraint_grid_dim;
263  dim3 constraint_block_dim;
264 
265  unsigned int padding_length;
266  std::vector<unsigned int> row_start;
267 
268  friend class internal::ReinitHelper<dim,Number>;
269  };
270 
271 
272 
273  // TODO find a better place to put these things
274  // Structure to pass the shared memory into a general user function.
275  template <int dim, typename Number>
276  struct SharedData
277  {
278  __device__ SharedData(Number *vd,
279  Number *gq[dim])
280  :
281  values(vd)
282  {
283  for (int d=0; d<dim; ++d)
284  gradients[d] = gq[d];
285  }
286 
287  Number *values;
288  Number *gradients[dim];
289  };
290 
291 
292 
293  // This function determines the number of cells per block, possibly at compile
294  // time
295  // TODO this function should be rewritten using meta-programming
296  __host__ __device__ constexpr unsigned int cells_per_block_shmem(int dim,
297  int fe_degree)
298  {
299  return dim==2 ? (fe_degree==1 ? 32 :
300  fe_degree==2 ? 8 :
301  fe_degree==3 ? 4 :
302  fe_degree==4 ? 4 :
303  1) :
304  dim==3 ? (fe_degree==1 ? 8 :
305  fe_degree==2 ? 2 :
306  1) : 1;
307  }
308 }
309 
310 DEAL_II_NAMESPACE_CLOSE
311 
312 #endif
313 
314 #endif
Transformed quadrature weights.
ParallelizationScheme parallelization_scheme
void cell_loop(const functor &func, const CUDAVector< Number > &src, CUDAVector< Number > &dst) const
Data get_data(unsigned int color) const
std::vector< unsigned int * > local_to_global
std::vector< point_type * > q_points
UpdateFlags
std::vector< Number * > inv_jacobian
std::vector< dim3 > block_dim
std::vector< dim3 > grid_dim
Definition: mpi.h:53
std::size_t memory_consumption() const
Shape function gradients.
std::vector< Number * > JxW
void reinit(const Mapping< dim > &mapping, const DoFHandler< dim > &dof_handler, const ConstraintMatrix &constraints, const Quadrature< 1 > &quad, const AdditionalData additional_data=AdditionalData())
std::vector< unsigned int > n_cells