Reference documentation for deal.II version 9.1.0-pre
cuda.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 #ifndef dealii_cuda_h
17 #define dealii_cuda_h
18 
19 #include <deal.II/base/config.h>
20 
21 #ifdef DEAL_II_WITH_CUDA
22 
23 # include <deal.II/base/exceptions.h>
24 
25 # include <cusolverDn.h>
26 # include <cusolverSp.h>
27 # include <cusparse.h>
28 
29 # include <vector>
30 
31 DEAL_II_NAMESPACE_OPEN
32 namespace Utilities
33 {
37  namespace CUDA
38  {
43  struct Handle
44  {
48  Handle();
49 
53  Handle(Handle const &) = delete;
54 
59  ~Handle();
60 
61  cusolverDnHandle_t cusolver_dn_handle;
62 
63  cusolverSpHandle_t cusolver_sp_handle;
64 
68  cusparseHandle_t cusparse_handle;
69  };
70 
74  template <typename T>
75  inline void
76  malloc(T *&pointer, const unsigned int n_elements)
77  {
78  cudaError_t cuda_error_code =
79  cudaMalloc(&pointer, n_elements * sizeof(T));
80  AssertCuda(cuda_error_code);
81  }
82 
86  template <typename T>
87  inline void
88  free(T *&pointer)
89  {
90  cudaError_t cuda_error_code = cudaFree(pointer);
91  AssertCuda(cuda_error_code);
92  pointer = nullptr;
93  }
94 
98  template <typename T>
99  inline void
100  copy_to_host(const T *pointer_dev, std::vector<T> &vector_host)
101  {
102  cudaError_t cuda_error_code = cudaMemcpy(vector_host.data(),
103  pointer_dev,
104  vector_host.size() * sizeof(T),
105  cudaMemcpyDeviceToHost);
106  AssertCuda(cuda_error_code);
107  }
108 
113  template <typename T>
114  inline void
115  copy_to_dev(const std::vector<T> &vector_host, T *pointer_dev)
116  {
117  cudaError_t cuda_error_code = cudaMemcpy(pointer_dev,
118  vector_host.data(),
119  vector_host.size() * sizeof(T),
120  cudaMemcpyHostToDevice);
121  AssertCuda(cuda_error_code);
122  }
123  } // namespace CUDA
124 } // namespace Utilities
125 
126 DEAL_II_NAMESPACE_CLOSE
127 
128 #endif
129 
130 #endif
void malloc(T *&pointer, const unsigned int n_elements)
Definition: cuda.h:76
void copy_to_dev(const std::vector< T > &vector_host, T *pointer_dev)
Definition: cuda.h:115
cusparseHandle_t cusparse_handle
Definition: cuda.h:68
#define AssertCuda(error_code)
Definition: exceptions.h:1459
Definition: cuda.h:32
void copy_to_host(const T *pointer_dev, std::vector< T > &vector_host)
Definition: cuda.h:100
void free(T *&pointer)
Definition: cuda.h:88