Reference documentation for deal.II version 9.6.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// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 2018 - 2023 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
15#ifndef dealii_cuda_h
16#define dealii_cuda_h
17
18#include <deal.II/base/config.h>
19
22
23#ifdef DEAL_II_WITH_CUDA
24# include <cusolverDn.h>
25# include <cusolverSp.h>
26# include <cusparse.h>
27
28# include <vector>
29
31namespace Utilities
32{
36 namespace CUDA
37 {
43 struct Handle
44 {
48 Handle();
49
53 Handle(const Handle &) = 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
133 {
134 AssertDimension(in.size(), out.size());
135 cudaError_t cuda_error_code = cudaMemcpy(out.data(),
136 in.data(),
137 in.size() * sizeof(T),
138 cudaMemcpyDeviceToHost);
139 AssertCuda(cuda_error_code);
140 }
141
145 template <typename T>
146 inline void
149 {
150 AssertDimension(in.size(), out.size());
151 cudaError_t cuda_error_code = cudaMemcpy(out.data(),
152 in.data(),
153 in.size() * sizeof(T),
154 cudaMemcpyHostToDevice);
155 AssertCuda(cuda_error_code);
156 }
157
161 template <typename T>
162 inline void
163 copy_to_host(const T *pointer_dev, std::vector<T> &vector_host)
164 {
165 ArrayView<const T, MemorySpace::CUDA> in(pointer_dev, vector_host.size());
166 auto out = make_array_view(vector_host);
167 copy_to_host(in, out);
168 }
169
174 template <typename T>
175 inline void
176 copy_to_dev(const std::vector<T> &vector_host, T *pointer_dev)
177 {
178 auto in = make_array_view(vector_host);
179 ArrayView<T, MemorySpace::CUDA> out(pointer_dev, vector_host.size());
180 copy_to_dev(in, out);
181 }
182 } // namespace CUDA
183} // namespace Utilities
184
186#endif
187#endif
ArrayView< std::remove_reference_t< typename std::iterator_traits< Iterator >::reference >, MemorySpaceType > make_array_view(const Iterator begin, const Iterator end)
Definition array_view.h:949
value_type * data() const noexcept
Definition array_view.h:661
std::size_t size() const
Definition array_view.h:684
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:503
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:504
#define AssertNothrowCuda(error_code)
#define AssertDimension(dim1, dim2)
#define AssertCuda(error_code)
void malloc(T *&pointer, const unsigned int n_elements)
Definition cuda.h:84
Number * allocate_device_data(const std::size_t size)
Definition cuda.h:108
void delete_device_data(Number *device_ptr) noexcept
Definition cuda.h:120
void copy_to_host(const ArrayView< const T, MemorySpace::CUDA > &in, ArrayView< T, MemorySpace::Host > &out)
Definition cuda.h:131
void copy_to_dev(const ArrayView< const T, MemorySpace::Host > &in, ArrayView< T, MemorySpace::CUDA > &out)
Definition cuda.h:147
void free(T *&pointer)
Definition cuda.h:96
cusolverDnHandle_t cusolver_dn_handle
Definition cuda.h:64
Handle(const Handle &)=delete
cusolverSpHandle_t cusolver_sp_handle
Definition cuda.h:70
cusparseHandle_t cusparse_handle
Definition cuda.h:76