# Difference between cudaMallocManaged and zero copy memory function

**URL:** <https://forums.developer.nvidia.com/t/difference-between-cudamallocmanaged-and-zero-copy-memory-function/58660>\
**Category:** CUDA Programming and Performance\
**Created:** [February 28, 2018, 7:23pm UTC](https://forums.developer.nvidia.com/t/difference-between-cudamallocmanaged-and-zero-copy-memory-function/58660 "2018-02-28T19:23:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhargavK](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@bhargavK](https://forums.developer.nvidia.com/u/bhargavK)\
**Post date:** [February 28, 2018, 7:23pm UTC](https://forums.developer.nvidia.com/t/difference-between-cudamallocmanaged-and-zero-copy-memory-function/58660/1 "2018-02-28T19:23:54Z")

</div>

Hello,

I am a CUDA beginner, learning through the Toolkit documentation. However, I am also trying to leverage the code repo [https://github.com/dusty-nv/jetson-inference](https://github.com/dusty-nv/jetson-inference) for my work. While I catch up with the documentation, I just wanted to understand the quick difference between a function(cudaAllocMapped()) used in this repo and cudaMallocManaged().

The function is given here:

```auto
/**
 * Allocate ZeroCopy mapped memory, shared between CUDA and CPU.
 * @ingroup util
 */
inline bool cudaAllocMapped( void **cpuPtr, void** gpuPtr, size_t size )
{
	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;

	memset(*cpuPtr, 0, size);
	printf("[cuda] cudaAllocMapped %zu bytes, CPU %p GPU %p\n", size, *cpuPtr, *gpuPtr);
	return true;
}

```

From the documentation, I find:

> [@](#):
>
> Unified Memory offers a “single-pointer-to-data” model that is conceptually similar to CUDA’s zero-copy memory. One key difference between the two is that with zero-copy allocations the physical location of memory is pinned in CPU system memory such that a program may have fast or slow access to it depending on where it is being accessed from. Unified Memory, on the other hand, decouples memory and execution spaces so that all data accesses are fast.
> 
> Read more at: [CUDA C++ Programming Guide](http://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#ixzz58QrvcAsN)  
> Follow us: @GPUComputing on Twitter | NVIDIA on Facebook

So can someone please give me a scenario/example when one would use zero-copy memory vs. unified memory or vice versa?

Thanks!

---

<div class="post-metadata">

**Author:** ![BulatZiganshin](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@BulatZiganshin](https://forums.developer.nvidia.com/u/BulatZiganshin)\
**Post date:** [March 1, 2018, 7:38am UTC](https://forums.developer.nvidia.com/t/difference-between-cudamallocmanaged-and-zero-copy-memory-function/58660/2 "2018-03-01T07:38:30Z")

</div>

zero-copy: data are allocated on cpu, GPU accesses them via PCI-E on **each** operation

UM: data are moved between CPU and GPU RAM on demand. It’s similar to manual copying before/after kernel call, but automatically managed by the CUDA. You just allocates the single universal pointer and can access it on both sides. So it never faster than manual memory management, sometimes may be slower (when automatic heuristics sucks), but simplifies the program. Essentially, you can remove all explicit memory movу operations, remove any separate allocations of device memory and alloc everything as UM arrays used by both CPU and GPU code

Heuristic AFAIR is the following: when data, which are absent on CPU side, are accessed by CPU, they are moved from GPU on-demand with a page (4KB) granularity. When data that can be potentially accessed by kernel (i.e. available for the stream to which the kernel belongs) are absent on GPU side, entire array is copied from CPU to GPU prior to kernel start

Pascal+ GPUs can use on-demand copying with page granularity for both cases, but ATM it is implemented only by Linux (TCC?) driver

PS: more info at [Unified Memory in CUDA 6: A Brief Overview | Dr Dobb's](http://www.drdobbs.com/parallel/unified-memory-in-cuda-6-a-brief-overvie/240169095)
