OpenLB 1.7
Loading...
Searching...
No Matches
device.hh
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2022 Adrian Kummerlaender
4 *
5 * E-mail contact: info@openlb.net
6 * The most recent release of OpenLB can be downloaded at
7 * <http://www.openlb.net/>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public
20 * License along with this program; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23*/
24
25#ifndef GPU_CUDA_DEVICE_HH
26#define GPU_CUDA_DEVICE_HH
27
28#include "device.h"
29
30#include <stdexcept>
31
32#include <cuda.h>
33
34namespace olb {
35
36namespace gpu {
37
38namespace cuda {
39
40namespace device {
41
42int getCount() {
43 int devices{};
44 cudaGetDeviceCount(&devices);
45 return devices;
46}
47
48void check() {
49 cudaError_t error = cudaGetLastError();
50 if (error != cudaSuccess) {
51 throw std::runtime_error(cudaGetErrorString(error));
52 }
53}
54
56void check(CUresult result) {
57 if (result != CUDA_SUCCESS) {
58 const char* description{};
59 cuGetErrorString(result, &description);
60 throw std::runtime_error(std::string(description));
61 }
62}
63
65 if (getCount() > 0) {
66 cudaDeviceSynchronize();
67 check();
68 }
69}
70
71int get() {
72 int device{};
73 cudaGetDevice(&device);
74 check();
75 return device;
76}
77
78void copyToHost(void* src, void* dst, std::size_t count) {
79 cudaMemcpy(dst, src, count, cudaMemcpyDeviceToHost);
80 check();
81}
82
83void copyToDevice(void* src, void* dst, std::size_t count) {
84 cudaMemcpy(dst, src, count, cudaMemcpyHostToDevice);
85 check();
86}
87
88template <typename T>
89T* malloc(std::size_t size) {
90 T* ptr{};
91 cudaMalloc(&ptr, size*sizeof(T));
92 check();
93 return ptr;
94}
95
96std::size_t getDevicePageSize() {
97 std::size_t granularity = 0;
98 CUmemAllocationProp prop = {};
99 prop.type = CU_MEM_ALLOCATION_TYPE_PINNED;
100 prop.location.type = CU_MEM_LOCATION_TYPE_DEVICE;
101 prop.location.id = device::get();
102 check(cuMemGetAllocationGranularity(&granularity, &prop, CU_MEM_ALLOC_GRANULARITY_MINIMUM));
103 return granularity;
104}
105
106
107template <typename T>
109 if (_ptr != nullptr) {
110 cudaFree(_ptr);
111 check();
112 }
113}
114
115template <typename T>
117 if (_ptr != nullptr) {
118 cudaFree(_ptr);
119 check();
120 }
121 _ptr = rhs.release();
122 return *this;
123}
124
125
126Stream::Stream(unsigned int flags) {
127 cudaStreamCreateWithFlags(&_stream, flags);
128 check();
129}
130
132 cudaStreamDestroy(_stream);
133 check();
134}
135
137 cudaStreamSynchronize(_stream);
138 check();
139}
140
141void asyncCopyToHost(Stream& stream, void* src, void* dst, std::size_t count) {
142 cudaMemcpyAsync(dst, src, count, cudaMemcpyDeviceToHost, stream.get());
143}
144
145void asyncCopyToDevice(Stream& stream, void* src, void* dst, std::size_t count) {
146 cudaMemcpyAsync(dst, src, count, cudaMemcpyHostToDevice, stream.get());
147}
148
149}
150
151}
152
153}
154
155}
156
157#endif
Basic wrapper for device stream.
Definition device.h:121
Stream(unsigned int flags)
Definition device.hh:126
Managed pointer for device-side memory.
Definition device.h:79
unique_ptr & operator=(unique_ptr &&rhs)
Definition device.hh:116
std::size_t getDevicePageSize()
Returns device memory page size.
Definition device.hh:96
void copyToHost(void *src, void *dst, std::size_t count)
Copy data from device to host.
Definition device.hh:78
void copyToDevice(void *src, void *dst, std::size_t count)
Copy data from host to device.
Definition device.hh:83
int getCount()
Return number of available devices.
Definition device.hh:42
void asyncCopyToDevice(Stream &stream, void *src, void *dst, std::size_t count)
Copy data from host to device (async)
Definition device.hh:145
void asyncCopyToHost(Stream &stream, void *src, void *dst, std::size_t count)
Copy data from device to host (async)
Definition device.hh:141
void check()
Check errors.
Definition device.hh:48
int get()
Get current device.
Definition device.hh:71
T * malloc(std::size_t size)
Allocate data on device.
Definition device.hh:89
void synchronize()
Synchronize device.
Definition device.hh:64
Top level namespace for all of OpenLB.