gpu_memcpy.hpp
Go to the documentation of this file.
1/*
2 Copyright 2026 Equinor ASA
3
4 This file is part of the Open Porous Media project (OPM).
5 OPM is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 OPM is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with OPM. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#ifndef OPM_GPUISTL_DETAIL_GPU_MEMCPY_HPP
20#define OPM_GPUISTL_DETAIL_GPU_MEMCPY_HPP
21
25
26#include <cuda_runtime.h>
27
28#include <cstddef>
29
31{
32
44template <typename T>
45inline void
46gpuMemcpyHostToDevice(T* dstDevice, const T* srcHost, std::size_t count)
47{
48 if (count == 0) {
49 return;
50 }
53 OPM_GPU_SAFE_CALL(cudaMemcpy(dstDevice, srcHost, count * sizeof(T), cudaMemcpyHostToDevice));
54}
55
67template <typename T>
68inline void
69gpuMemcpyDeviceToHost(T* dstHost, const T* srcDevice, std::size_t count)
70{
71 if (count == 0) {
72 return;
73 }
76 OPM_GPU_SAFE_CALL(cudaMemcpy(dstHost, srcDevice, count * sizeof(T), cudaMemcpyDeviceToHost));
77}
78
90template <typename T>
91inline void
92gpuMemcpyDeviceToDevice(T* dstDevice, const T* srcDevice, std::size_t count)
93{
94 if (count == 0) {
95 return;
96 }
100 cudaMemcpy(dstDevice, srcDevice, count * sizeof(T), cudaMemcpyDeviceToDevice));
101}
102
121template <typename T>
122inline void
123gpuMemcpyHostToDeviceAsync(T* dstDevice, const T* srcHost, std::size_t count, cudaStream_t stream)
124{
125 if (count == 0) {
126 return;
127 }
132 cudaMemcpyAsync(dstDevice, srcHost, count * sizeof(T), cudaMemcpyHostToDevice, stream));
133}
134
153template <typename T>
154inline void
155gpuMemcpyDeviceToHostAsync(T* dstHost, const T* srcDevice, std::size_t count, cudaStream_t stream)
156{
157 if (count == 0) {
158 return;
159 }
164 cudaMemcpyAsync(dstHost, srcDevice, count * sizeof(T), cudaMemcpyDeviceToHost, stream));
165}
166
167} // namespace Opm::gpuistl::detail
168
169#endif // OPM_GPUISTL_DETAIL_GPU_MEMCPY_HPP
#define OPM_GPUISTL_DETAIL_ASSERT_DEVICE_POINTER(x)
Captures the argument name for assertDevicePointer (call site via source_location).
Definition: gpu_pointer_attributes.hpp:223
#define OPM_GPUISTL_DETAIL_ASSERT_HOST_POINTER(x)
Captures the argument name for assertHostPointer (call site via source_location).
Definition: gpu_pointer_attributes.hpp:218
#define OPM_GPU_SAFE_CALL(expression)
OPM_GPU_SAFE_CALL checks the return type of the GPU expression (function call) and throws an exceptio...
Definition: gpu_safe_call.hpp:164
#define OPM_GPUISTL_DETAIL_ASSERT_GPU_STREAM(x)
Captures the argument name for assertCudaStream (call site via source_location).
Definition: gpu_stream.hpp:89
Definition: autotuner.hpp:30
void gpuMemcpyHostToDeviceAsync(T *dstDevice, const T *srcHost, std::size_t count, cudaStream_t stream)
gpuMemcpyHostToDeviceAsync copies count elements of type T from host to device asynchronously.
Definition: gpu_memcpy.hpp:123
void gpuMemcpyHostToDevice(T *dstDevice, const T *srcHost, std::size_t count)
gpuMemcpyHostToDevice copies count elements of type T from host to device.
Definition: gpu_memcpy.hpp:46
void gpuMemcpyDeviceToHostAsync(T *dstHost, const T *srcDevice, std::size_t count, cudaStream_t stream)
gpuMemcpyDeviceToHostAsync copies count elements of type T from device to host asynchronously.
Definition: gpu_memcpy.hpp:155
void gpuMemcpyDeviceToHost(T *dstHost, const T *srcDevice, std::size_t count)
gpuMemcpyDeviceToHost copies count elements of type T from device to host.
Definition: gpu_memcpy.hpp:69
void gpuMemcpyDeviceToDevice(T *dstDevice, const T *srcDevice, std::size_t count)
gpuMemcpyDeviceToDevice copies count elements of type T from device to device.
Definition: gpu_memcpy.hpp:92