Reference documentation for deal.II version 9.2.0
\(\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\}}\)
cuda.h
Go to the documentation of this file.
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 
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 
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 
158 #endif
159 #endif
Utilities::CUDA::Handle
Definition: cuda.h:43
Utilities::CUDA::malloc
void malloc(T *&pointer, const unsigned int n_elements)
Definition: cuda.h:84
Utilities::CUDA::copy_to_dev
void copy_to_dev(const std::vector< T > &vector_host, T *pointer_dev)
Definition: cuda.h:146
Utilities::CUDA::Handle::Handle
Handle()
Utilities::CUDA::allocate_device_data
Number * allocate_device_data(const std::size_t size)
Definition: cuda.h:108
AssertNothrowCuda
#define AssertNothrowCuda(error_code)
Definition: exceptions.h:1762
Utilities::CUDA::delete_device_data
void delete_device_data(Number *device_ptr) noexcept
Definition: cuda.h:120
Utilities::CUDA::Handle::cusparse_handle
cusparseHandle_t cusparse_handle
Definition: cuda.h:76
Utilities::CUDA::free
void free(T *&pointer)
Definition: cuda.h:96
AssertCuda
#define AssertCuda(error_code)
Definition: exceptions.h:1734
LAPACKSupport::T
static const char T
Definition: lapack_support.h:163
DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:358
Utilities::CUDA::Handle::cusolver_sp_handle
cusolverSpHandle_t cusolver_sp_handle
Definition: cuda.h:70
Utilities::CUDA::copy_to_host
void copy_to_host(const T *pointer_dev, std::vector< T > &vector_host)
Definition: cuda.h:131
exceptions.h
Utilities::CUDA::Handle::~Handle
~Handle()
config.h
DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:359
Utilities::CUDA::Handle::cusolver_dn_handle
cusolverDnHandle_t cusolver_dn_handle
Definition: cuda.h:64
Utilities
Definition: cuda.h:31