Reference documentation for deal.II version 9.1.1
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
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 #include <deal.II/base/exceptions.h>
22 
23 #ifdef DEAL_II_COMPILER_CUDA_AWARE
24 # include <cusolverDn.h>
25 # include <cusolverSp.h>
26 # include <cusparse.h>
27 
28 # include <vector>
29 
30 DEAL_II_NAMESPACE_OPEN
31 namespace Utilities
32 {
36  namespace CUDA
37  {
43  struct Handle
44  {
48  Handle();
49 
53  Handle(Handle const &) = delete;
54 
58  ~Handle();
59 
64  cusolverDnHandle_t cusolver_dn_handle;
65 
70  cusolverSpHandle_t cusolver_sp_handle;
71 
76  cusparseHandle_t cusparse_handle;
77  };
78 
82  template <typename T>
83  inline void
84  malloc(T *&pointer, const unsigned int n_elements)
85  {
86  cudaError_t cuda_error_code =
87  cudaMalloc(&pointer, n_elements * sizeof(T));
88  AssertCuda(cuda_error_code);
89  }
90 
94  template <typename T>
95  inline void
96  free(T *&pointer)
97  {
98  cudaError_t cuda_error_code = cudaFree(pointer);
99  AssertCuda(cuda_error_code);
100  pointer = nullptr;
101  }
102 
106  template <typename Number>
107  Number *
108  allocate_device_data(const std::size_t size)
109  {
110  Number *device_ptr;
111  Utilities::CUDA::malloc(device_ptr, size);
112  return device_ptr;
113  }
114 
118  template <typename Number>
119  void
120  delete_device_data(Number *device_ptr) noexcept
121  {
122  const cudaError_t error_code = cudaFree(device_ptr);
123  AssertNothrowCuda(error_code);
124  }
125 
129  template <typename T>
130  inline void
131  copy_to_host(const T *pointer_dev, std::vector<T> &vector_host)
132  {
133  cudaError_t cuda_error_code = cudaMemcpy(vector_host.data(),
134  pointer_dev,
135  vector_host.size() * sizeof(T),
136  cudaMemcpyDeviceToHost);
137  AssertCuda(cuda_error_code);
138  }
139 
144  template <typename T>
145  inline void
146  copy_to_dev(const std::vector<T> &vector_host, T *pointer_dev)
147  {
148  cudaError_t cuda_error_code = cudaMemcpy(pointer_dev,
149  vector_host.data(),
150  vector_host.size() * sizeof(T),
151  cudaMemcpyHostToDevice);
152  AssertCuda(cuda_error_code);
153  }
154  } // namespace CUDA
155 } // namespace Utilities
156 
157 DEAL_II_NAMESPACE_CLOSE
158 #endif
159 #endif
Number * allocate_device_data(const std::size_t size)
Definition: cuda.h:108
void malloc(T *&pointer, const unsigned int n_elements)
Definition: cuda.h:84
void copy_to_dev(const std::vector< T > &vector_host, T *pointer_dev)
Definition: cuda.h:146
cusparseHandle_t cusparse_handle
Definition: cuda.h:76
cusolverSpHandle_t cusolver_sp_handle
Definition: cuda.h:70
#define AssertCuda(error_code)
Definition: exceptions.h:1722
Definition: cuda.h:31
void copy_to_host(const T *pointer_dev, std::vector< T > &vector_host)
Definition: cuda.h:131
void delete_device_data(Number *device_ptr) noexcept
Definition: cuda.h:120
cusolverDnHandle_t cusolver_dn_handle
Definition: cuda.h:64
void free(T *&pointer)
Definition: cuda.h:96
#define AssertNothrowCuda(error_code)
Definition: exceptions.h:1750