docs/html/cudaMappedMemory_8h_source.html
| | Jetson Inference
DNN Vision Library |
cudaMappedMemory.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 #ifndef __CUDA_MAPPED_MEMORY_H_
24 #define __CUDA_MAPPED_MEMORY_H_
25
26
27 #include "cudaUtility.h"
28 #include "imageFormat.h"
29 #include "logging.h"
30
31
44 inline bool cudaAllocMapped( void** cpuPtr, void** gpuPtr, size_t size )
45 {
46if( !cpuPtr || !gpuPtr || size == 0 )
47return false;
48
49//CUDA(cudaSetDeviceFlags(cudaDeviceMapHost));
50
51if( CUDA_FAILED(cudaHostAlloc(cpuPtr, size, cudaHostAllocMapped)) )
52return false;
53
54if( CUDA_FAILED(cudaHostGetDevicePointer(gpuPtr, *cpuPtr, 0)) )
55return false;
56
57 memset(*cpuPtr, 0, size);
58LogDebug(LOG_CUDA "cudaAllocMapped %zu bytes, CPU %p GPU %p\n", size, *cpuPtr, *gpuPtr);
59return true;
60 }
61
62
75 inline bool cudaAllocMapped( void** ptr, size_t size )
76 {
77void* cpuPtr = NULL;
78void* gpuPtr = NULL;
79
80if( !ptr || size == 0 )
81return false;
82
83if( ! cudaAllocMapped(&cpuPtr, &gpuPtr, size) )
84return false;
85
86if( cpuPtr != gpuPtr )
87 {
88LogError(LOG_CUDA "cudaAllocMapped() - addresses of CPU and GPU pointers don't match\n");
89return false;
90 }
91
92 *ptr = gpuPtr;
93return true;
94 }
95
111 inline bool cudaAllocMapped( void** ptr, size_t width, size_t height, imageFormat format )
112 {
113return cudaAllocMapped(ptr, imageFormatSize(format, width, height));
114 }
115
116
131 inline bool cudaAllocMapped( void** ptr, const int2& dims, imageFormat format )
132 {
133return cudaAllocMapped(ptr, imageFormatSize(format, dims.x, dims.y));
134 }
135
136
151 template<typename T> inline bool cudaAllocMapped( T** ptr, size_t width, size_t height )
152 {
153return cudaAllocMapped((void**)ptr, width * height * sizeof(T));
154 }
155
156
170 template<typename T> inline bool cudaAllocMapped( T** ptr, const int2& dims )
171 {
172return cudaAllocMapped((void**)ptr, dims.x * dims.y * sizeof(T));
173 }
174
175
189 template<typename T> inline bool cudaAllocMapped( T** ptr, size_t size )
190 {
191return cudaAllocMapped((void**)ptr, size);
192 }
193
194 #endif
#define LOG_CUDA
LOG_CUDA string.
Definition: cudaUtility.h:65
size_t imageFormatSize(imageFormat format, size_t width, size_t height)
Compute the size of an image (in bytes)
bool cudaAllocMapped(void **cpuPtr, void **gpuPtr, size_t size)
Allocate ZeroCopy mapped memory, shared between CUDA and CPU.
Definition: cudaMappedMemory.h:44
#define LogError(format, args...)
Log a printf-style error message (Log::ERROR)
Definition: logging.h:150
#define LogDebug(format, args...)
Log a printf-style debug message (Log::DEBUG)
Definition: logging.h:180
#define CUDA_FAILED(x)
Evaluates to true on failure.
Definition: cudaUtility.h:53
imageFormat
The imageFormat enum is used to identify the pixel format and colorspace of an image.
Definition: imageFormat.h:49