Reference documentation for deal.II version 9.5.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\}}\)
Loading...
Searching...
No Matches
cuda.h
Go to the documentation of this file.
1// ---------------------------------------------------------------------
2//
3// Copyright (C) 2018 - 2023 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
23
24#ifdef DEAL_II_WITH_CUDA
25# include <cusolverDn.h>
26# include <cusolverSp.h>
27# include <cusparse.h>
28
29# include <vector>
30
32namespace Utilities
33{
37 namespace CUDA
38 {
44 struct Handle
45 {
49 Handle();
50
54 Handle(Handle const &) = delete;
55
59 ~Handle();
60
65 cusolverDnHandle_t cusolver_dn_handle;
66
71 cusolverSpHandle_t cusolver_sp_handle;
72
77 cusparseHandle_t cusparse_handle;
78 };
79
83 template <typename T>
84 inline void
85 malloc(T *&pointer, const unsigned int n_elements)
86 {
87 cudaError_t cuda_error_code =
88 cudaMalloc(&pointer, n_elements * sizeof(T));
89 AssertCuda(cuda_error_code);
90 }
91
95 template <typename T>
96 inline void
97 free(T *&pointer)
98 {
99 cudaError_t cuda_error_code = cudaFree(pointer);
100 AssertCuda(cuda_error_code);
101 pointer = nullptr;
102 }
103
107 template <typename Number>
108 Number *
109 allocate_device_data(const std::size_t size)
110 {
111 Number *device_ptr;
112 Utilities::CUDA::malloc(device_ptr, size);
113 return device_ptr;
114 }
115
119 template <typename Number>
120 void
121 delete_device_data(Number *device_ptr) noexcept
122 {
123 const cudaError_t error_code = cudaFree(device_ptr);
124 AssertNothrowCuda(error_code);
125 }
126
130 template <typename T>
131 inline void
134 {
135 AssertDimension(in.size(), out.size());
136 cudaError_t cuda_error_code = cudaMemcpy(out.data(),
137 in.data(),
138 in.size() * sizeof(T),
139 cudaMemcpyDeviceToHost);
140 AssertCuda(cuda_error_code);
141 }
142
146 template <typename T>
147 inline void
150 {
151 AssertDimension(in.size(), out.size());
152 cudaError_t cuda_error_code = cudaMemcpy(out.data(),
153 in.data(),
154 in.size() * sizeof(T),
155 cudaMemcpyHostToDevice);
156 AssertCuda(cuda_error_code);
157 }
158
162 template <typename T>
163 inline void
164 copy_to_host(const T *pointer_dev, std::vector<T> &vector_host)
165 {
166 ArrayView<const T, MemorySpace::CUDA> in(pointer_dev, vector_host.size());
167 auto out = make_array_view(vector_host);
168 copy_to_host(in, out);
169 }
170
175 template <typename T>
176 inline void
177 copy_to_dev(const std::vector<T> &vector_host, T *pointer_dev)
178 {
179 auto in = make_array_view(vector_host);
180 ArrayView<T, MemorySpace::CUDA> out(pointer_dev, vector_host.size());
181 copy_to_dev(in, out);
182 }
183 } // namespace CUDA
184} // namespace Utilities
185
187#endif
188#endif
ArrayView< typename std::remove_reference< typename std::iterator_traits< Iterator >::reference >::type, MemorySpaceType > make_array_view(const Iterator begin, const Iterator end)
Definition array_view.h:704
value_type * data() const noexcept
Definition array_view.h:553
std::size_t size() const
Definition array_view.h:576
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:472
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:473
#define AssertNothrowCuda(error_code)
#define AssertDimension(dim1, dim2)
#define AssertCuda(error_code)
void malloc(T *&pointer, const unsigned int n_elements)
Definition cuda.h:85
Number * allocate_device_data(const std::size_t size)
Definition cuda.h:109
void delete_device_data(Number *device_ptr) noexcept
Definition cuda.h:121
void copy_to_host(const ArrayView< const T, MemorySpace::CUDA > &in, ArrayView< T, MemorySpace::Host > &out)
Definition cuda.h:132
void copy_to_dev(const ArrayView< const T, MemorySpace::Host > &in, ArrayView< T, MemorySpace::CUDA > &out)
Definition cuda.h:148
void free(T *&pointer)
Definition cuda.h:97
cusolverDnHandle_t cusolver_dn_handle
Definition cuda.h:65
cusolverSpHandle_t cusolver_sp_handle
Definition cuda.h:71
Handle(Handle const &)=delete
cusparseHandle_t cusparse_handle
Definition cuda.h:77