# zero-copy memory

**URL:** <https://forums.developer.nvidia.com/t/zero-copy-memory/29937>\
**Category:** CUDA Programming and Performance\
**Created:** [July 1, 2013, 4:59am UTC](https://forums.developer.nvidia.com/t/zero-copy-memory/29937 "2013-07-01T04:59:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![yangoose](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@yangoose](https://forums.developer.nvidia.com/u/yangoose)\
**Post date:** [July 1, 2013, 4:59am UTC](https://forums.developer.nvidia.com/t/zero-copy-memory/29937/1 "2013-07-01T04:59:26Z")

</div>

I am having trouble using zero-copy memory. I read an article talking about using zero-copy memory for a first pass at converting a program to cuda. It sounded like a good idea but I am having a great deal of trouble.

Perhaps someone can comment on my understanding or misunderstanding of zero-copy memory.

My understanding is that after the following code has run, I will have one set of data that can be accessed from either the host or the GPU. The data is accessed on the host by referencing \*TF and the data is accessed on the device by referencing \*devPtrTF.

Is this correct?

```auto
if (cudaHostAlloc( (void**)&TF,sizeof( double ) * Nz * Ny *( Nx+2L),cudaHostAllocMapped))
{ printf("\n CUDA error: cudaHostAlloc TF error", xerror);
        return 1;
}

        if(cudaHostGetDevicePointer(&devPtrTF,TF,0))
{ printf("\n CUDA error: cudaHostGetDevicePointer TF error", xerror);
        return 1;
}

```

Also, I think the following would be an equivalent call.

```auto
TF = (double *)malloc(15L*(Nx+2L)*Ny*Nz*sizeof(double));

        if (cudaHostRegister( (void**)&TF,sizeof( double ) * Nz * Ny *( Nx+2L),cudaHostRegisterMapped))
{ printf("\n CUDA error: cudaHostAlloc TF error", xerror);
        return 1;
}

        if(cudaHostGetDevicePointer(&devPtrTF,TF,0))
{ printf("\n CUDA error: cudaHostGetDevicePointer TF error", xerror);
        return 1;
}

```

Is this also correct?

Thanks for any help with this.

---

<div class="post-metadata">

**Author:** ![cmaster.matso](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/cmaster.matso/32/12153_2.png) [@cmaster.matso](https://forums.developer.nvidia.com/u/cmaster.matso)\
**Post date:** [July 4, 2013, 2:35pm UTC](https://forums.developer.nvidia.com/t/zero-copy-memory/29937/2 "2013-07-04T14:35:28Z")

</div>

It should be…

Have You read this: [CUDA C Best Practices Guide - Zero-copy Memory](http://docs.nvidia.com/cuda/cuda-c-best-practices-guide/index.html#zero-copy)? This one is best for starting with many things in CUDA ;)

MK
