Reference documentation for deal.II version 9.0.0
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 at
12 // the top level of the deal.II distribution.
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 #include <vector>
29 
30 DEAL_II_NAMESPACE_OPEN
31 namespace Utilities
32 {
36  namespace CUDA
37  {
42  struct Handle
43  {
47  Handle();
48 
52  Handle(Handle const &) = delete;
53 
58  ~Handle();
59 
60  cusolverDnHandle_t cusolver_dn_handle;
61 
62  cusolverSpHandle_t cusolver_sp_handle;
63 
67  cusparseHandle_t cusparse_handle;
68  };
69 
73  template <typename T>
74  inline void malloc(T *&pointer, const unsigned int n_elements)
75  {
76  cudaError_t cuda_error_code = cudaMalloc(&pointer, n_elements * sizeof(T));
77  AssertCuda(cuda_error_code);
78  }
79 
83  template <typename T>
84  inline void free(T *&pointer)
85  {
86  cudaError_t cuda_error_code = cudaFree(pointer);
87  AssertCuda(cuda_error_code);
88  pointer = nullptr;
89  }
90 
94  template <typename T>
95  inline void copy_to_host(const T *pointer_dev,
96  std::vector<T> &vector_host)
97  {
98  cudaError_t cuda_error_code =
99  cudaMemcpy(vector_host.data(), pointer_dev,
100  vector_host.size() * sizeof(T), cudaMemcpyDeviceToHost);
101  AssertCuda(cuda_error_code);
102  }
103 
108  template <typename T>
109  inline void copy_to_dev(const std::vector<T> &vector_host,
110  T *pointer_dev)
111  {
112  cudaError_t cuda_error_code =
113  cudaMemcpy(pointer_dev, vector_host.data(),
114  vector_host.size() * sizeof(T), cudaMemcpyHostToDevice);
115  AssertCuda(cuda_error_code);
116  }
117  }
118 }
119 
120 DEAL_II_NAMESPACE_CLOSE
121 
122 #endif
123 
124 #endif
void malloc(T *&pointer, const unsigned int n_elements)
Definition: cuda.h:74
void copy_to_dev(const std::vector< T > &vector_host, T *pointer_dev)
Definition: cuda.h:109
cusparseHandle_t cusparse_handle
Definition: cuda.h:67
#define AssertCuda(error_code)
Definition: exceptions.h:1329
Definition: cuda.h:31
void copy_to_host(const T *pointer_dev, std::vector< T > &vector_host)
Definition: cuda.h:95
void free(T *&pointer)
Definition: cuda.h:84