-
Notifications
You must be signed in to change notification settings - Fork 300
/
Copy pathcudaMappedMemory.h
229 lines (199 loc) · 8.4 KB
/
cudaMappedMemory.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef __CUDA_MAPPED_MEMORY_H_
#define __CUDA_MAPPED_MEMORY_H_
#include "cudaUtility.h"
#include "imageFormat.h"
#include "logging.h"
/**
* Allocate mapped memory that shares the same physical memory between CPU and GPU,
* as is the case on Jetson devices with unified memory. This eliminates the need
* for memory copies (Zero Copy), although synchronization is still required so that
* both processors are not accessing the same memory simultaneously.
*
* @param[out] ptr Returned pointer to the shared memory, can be accessed from both the CPU
* and in CUDA kernels This memory should be released with cudaFreeHost()
* @param[in] size Size (in bytes) of the shared memory to allocate.
* @param[in] clear If `true` (the default), the memory contents will be filled with zeros.
*
* @note This function is the same as cudaAllocMapped(), but returns cudaError_t instead of bool.
* @returns cudaSuccess on success, cudaError_t on failure.
* @ingroup cudaMemory
*/
inline cudaError_t cudaMallocMapped( void** ptr, size_t size, bool clear=true )
{
void* cpu = NULL;
void* gpu = NULL;
if( !ptr || size == 0 )
return cudaErrorInvalidValue;
//CUDA_ASSERT(cudaSetDeviceFlags(cudaDeviceMapHost));
CUDA_ASSERT(cudaHostAlloc(&cpu, size, cudaHostAllocMapped));
CUDA_ASSERT(cudaHostGetDevicePointer(&gpu, cpu, 0));
if( cpu != gpu )
{
LogError(LOG_CUDA "cudaMallocMapped() - addresses of CPU and GPU pointers don't match (CPU=%p GPU=%p)\n", cpu, gpu);
return cudaErrorInvalidDevicePointer;
}
if( clear )
memset(cpu, 0, size);
*ptr = cpu;
return cudaSuccess;
}
/**
* Allocate ZeroCopy mapped memory, shared between CUDA and CPU.
*
* @note this overload of cudaAllocMapped returns one pointer, assumes that the
* CPU and GPU addresses will match (as is the case with any recent CUDA version).
*
* @param[out] ptr Returned pointer to the shared CPU/GPU memory.
* @param[in] size Size (in bytes) of the shared memory to allocate.
* @param[in] clear If `true` (default), the memory contents will be filled with zeros.
*
* @returns `true` if the allocation succeeded, `false` otherwise.
* @ingroup cudaMemory
*/
inline bool cudaAllocMapped( void** ptr, size_t size, bool clear=true )
{
return CUDA_SUCCESS(cudaMallocMapped(ptr, size, clear));
}
/**
* Allocate ZeroCopy mapped memory, shared between CUDA and CPU.
*
* This overload is for allocating images from an imageFormat type
* and the image dimensions. The overall size of the allocation
* will be calculated with the imageFormatSize() function.
*
* @param[out] ptr Returned pointer to the shared CPU/GPU memory.
* @param[in] width Width (in pixels) to allocate.
* @param[in] height Height (in pixels) to allocate.
* @param[in] format Format of the image.
* @param[in] clear If `true` (default), the memory contents will be filled with zeros.
*
* @returns `true` if the allocation succeeded, `false` otherwise.
* @ingroup cudaMemory
*/
inline bool cudaAllocMapped( void** ptr, size_t width, size_t height, imageFormat format, bool clear=true )
{
return cudaAllocMapped(ptr, imageFormatSize(format, width, height), clear);
}
/**
* Allocate ZeroCopy mapped memory, shared between CUDA and CPU.
*
* This overload is for allocating images from an imageFormat type
* and the image dimensions. The overall size of the allocation
* will be calculated with the imageFormatSize() function.
*
* @param[out] ptr Returned pointer to the shared CPU/GPU memory.
* @param[in] dims `int2` vector where `width=dims.x` and `height=dims.y`
* @param[in] format Format of the image.
* @param[in] clear If `true` (default), the memory contents will be filled with zeros.
*
* @returns `true` if the allocation succeeded, `false` otherwise.
* @ingroup cudaMemory
*/
inline bool cudaAllocMapped( void** ptr, const int2& dims, imageFormat format, bool clear=true )
{
return cudaAllocMapped(ptr, imageFormatSize(format, dims.x, dims.y), clear);
}
/**
* Allocate ZeroCopy mapped memory, shared between CUDA and CPU.
*
* This is a templated version for allocating images from vector types
* like uchar3, uchar4, float3, float4, ect. The overall size of the
* allocation will be calculated as `width * height * sizeof(T)`.
*
* @param[out] ptr Returned pointer to the shared CPU/GPU memory.
* @param[in] width Width (in pixels) to allocate.
* @param[in] height Height (in pixels) to allocate.
* @param[in] clear If `true` (default), the memory contents will be filled with zeros.
*
* @returns `true` if the allocation succeeded, `false` otherwise.
* @ingroup cudaMemory
*/
template<typename T> inline bool cudaAllocMapped( T** ptr, size_t width, size_t height, bool clear=true )
{
return cudaAllocMapped((void**)ptr, width * height * sizeof(T), clear);
}
/**
* Allocate ZeroCopy mapped memory, shared between CUDA and CPU.
*
* This is a templated version for allocating images from vector types
* like uchar3, uchar4, float3, float4, ect. The overall size of the
* allocation will be calculated as `dims.x * dims.y * sizeof(T)`.
*
* @param[out] ptr Returned pointer to the shared CPU/GPU memory.
* @param[in] dims `int2` vector where `width=dims.x` and `height=dims.y`
* @param[in] clear If `true` (default), the memory contents will be filled with zeros.
*
* @returns `true` if the allocation succeeded, `false` otherwise.
* @ingroup cudaMemory
*/
template<typename T> inline bool cudaAllocMapped( T** ptr, const int2& dims, bool clear=true )
{
return cudaAllocMapped((void**)ptr, dims.x * dims.y * sizeof(T), clear);
}
/**
* Allocate ZeroCopy mapped memory, shared between CUDA and CPU.
*
* This is a templated version for allocating images from vector types
* like uchar3, uchar4, float3, float4, ect. The overall size of the
* allocation is specified by the size parameter.
*
* @param[out] ptr Returned pointer to the shared CPU/GPU memory.
* @param[in] size size of the allocation, in bytes.
* @param[in] clear If `true` (default), the memory contents will be filled with zeros.
*
* @returns `true` if the allocation succeeded, `false` otherwise.
* @ingroup cudaMemory
*/
template<typename T> inline bool cudaAllocMapped( T** ptr, size_t size, bool clear=true )
{
return cudaAllocMapped((void**)ptr, size, clear);
}
/**
* Allocate ZeroCopy mapped memory, shared between CUDA and CPU.
*
* @note although two pointers are returned, one for CPU and GPU, they both resolve to the same physical memory.
*
* @param[out] cpuPtr Returned CPU pointer to the shared memory.
* @param[out] gpuPtr Returned GPU pointer to the shared memory.
* @param[in] size Size (in bytes) of the shared memory to allocate.
* @param[in] clear If `true` (the default), the memory contents will be filled with zeros.
*
* @returns `true` if the allocation succeeded, `false` otherwise.
* @ingroup cudaMemory
*/
inline bool cudaAllocMapped( void** cpuPtr, void** gpuPtr, size_t size, bool clear=true )
{
if( !cpuPtr || !gpuPtr || size == 0 )
return false;
//CUDA(cudaSetDeviceFlags(cudaDeviceMapHost));
if( CUDA_FAILED(cudaHostAlloc(cpuPtr, size, cudaHostAllocMapped)) )
return false;
if( CUDA_FAILED(cudaHostGetDevicePointer(gpuPtr, *cpuPtr, 0)) )
return false;
if( clear )
memset(*cpuPtr, 0, size);
LogDebug(LOG_CUDA "cudaAllocMapped %zu bytes, CPU %p GPU %p\n", size, *cpuPtr, *gpuPtr);
return true;
}
#endif