OpenLB 1.7
Loading...
Searching...
No Matches
device.h
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_H
26#define GPU_CUDA_DEVICE_H
27
28#include <cuda_runtime_api.h>
29
30namespace olb {
31
32namespace gpu {
33
34namespace cuda {
35
37namespace device {
38
40int getCount();
41
43void check();
44
46void synchronize();
47
49int get();
50
52void copyToHost(void* src, void* dst, std::size_t count);
54void copyToDevice(void* src, void* dst, std::size_t count);
55
57template <typename T>
58T* malloc(std::size_t size);
59
61std::size_t getDevicePageSize();
63template <typename T>
64std::size_t getPageAlignedCount(std::size_t count)
65{
66 const std::size_t page_size = getDevicePageSize();
67 const std::size_t size = ((count * sizeof(T) - 1) / page_size + 1) * page_size;
68 const std::size_t volume = size / sizeof(T);
69
70 if (size % page_size != 0) {
71 throw std::invalid_argument("Buffer size must be multiple of PAGE_SIZE");
72 }
73
74 return volume;
75};
76
78template <typename T>
80private:
81 T* _ptr;
82
83public:
85 _ptr{nullptr} { }
86 unique_ptr(T* ptr):
87 _ptr{ptr} { }
89 _ptr{rhs.release()} { }
90
92
94
95 void reset(T* ptr) {
97 }
98
100 reset(ptr);
101 return *this;
102 }
103
104 T* release() {
105 T* ptr = _ptr;
106 _ptr = nullptr;
107 return ptr;
108 }
109
110 const T* get() const {
111 return _ptr;
112 }
113 T* get() {
114 return _ptr;
115 }
116
117};
118
119
121class Stream {
122private:
123 cudaStream_t _stream;
124
125public:
126 Stream(unsigned int flags);
127 ~Stream();
128
129 cudaStream_t& get() {
130 return _stream;
131 }
132
133 void synchronize();
134
135};
136
138void asyncCopyToHost(Stream& stream, void* src, void* dst, std::size_t count);
140void asyncCopyToDevice(Stream& stream, void* src, void* dst, std::size_t count);
141
142}
143
144}
145
146}
147
148}
149
150#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
unique_ptr & operator=(T *ptr)
Definition device.h:99
unique_ptr(unique_ptr &&rhs)
Definition device.h:88
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
std::size_t getPageAlignedCount(std::size_t count)
Returns count rounded up to be a multiple of getDevicePageSize
Definition device.h:64
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.