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 {
46  if( !cpuPtr || !gpuPtr || size == 0 )
47  return false;
48 
49  //CUDA(cudaSetDeviceFlags(cudaDeviceMapHost));
50 
51  if( CUDA_FAILED(cudaHostAlloc(cpuPtr, size, cudaHostAllocMapped)) )
52  return false;
53 
54  if( CUDA_FAILED(cudaHostGetDevicePointer(gpuPtr, *cpuPtr, 0)) )
55  return false;
56 
57  memset(*cpuPtr, 0, size);
58  LogDebug(LOG_CUDA "cudaAllocMapped %zu bytes, CPU %p GPU %p\n", size, *cpuPtr, *gpuPtr);
59  return true;
60 }
61 
62 
75 inline bool cudaAllocMapped( void** ptr, size_t size )
76 {
77  void* cpuPtr = NULL;
78  void* gpuPtr = NULL;
79 
80  if( !ptr || size == 0 )
81  return false;
82 
83  if( !cudaAllocMapped(&cpuPtr, &gpuPtr, size) )
84  return false;
85 
86  if( cpuPtr != gpuPtr )
87  {
88  LogError(LOG_CUDA "cudaAllocMapped() - addresses of CPU and GPU pointers don't match\n");
89  return false;
90  }
91 
92  *ptr = gpuPtr;
93  return true;
94 }
95 
111 inline bool cudaAllocMapped( void** ptr, size_t width, size_t height, imageFormat format )
112 {
113  return cudaAllocMapped(ptr, imageFormatSize(format, width, height));
114 }
115 
116 
131 inline bool cudaAllocMapped( void** ptr, const int2& dims, imageFormat format )
132 {
133  return 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 {
153  return cudaAllocMapped((void**)ptr, width * height * sizeof(T));
154 }
155 
156 
170 template<typename T> inline bool cudaAllocMapped( T** ptr, const int2& dims )
171 {
172  return 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 {
191  return cudaAllocMapped((void**)ptr, size);
192 }
193 
194 #endif
cudaUtility.h
LOG_CUDA
#define LOG_CUDA
LOG_CUDA string.
Definition: cudaUtility.h:65
imageFormatSize
size_t imageFormatSize(imageFormat format, size_t width, size_t height)
Compute the size of an image (in bytes)
cudaAllocMapped
bool cudaAllocMapped(void **cpuPtr, void **gpuPtr, size_t size)
Allocate ZeroCopy mapped memory, shared between CUDA and CPU.
Definition: cudaMappedMemory.h:44
logging.h
LogError
#define LogError(format, args...)
Log a printf-style error message (Log::ERROR)
Definition: logging.h:150
LogDebug
#define LogDebug(format, args...)
Log a printf-style debug message (Log::DEBUG)
Definition: logging.h:180
CUDA_FAILED
#define CUDA_FAILED(x)
Evaluates to true on failure.
Definition: cudaUtility.h:53
imageFormat.h
imageFormat
imageFormat
The imageFormat enum is used to identify the pixel format and colorspace of an image.
Definition: imageFormat.h:49