# Problem CudaMallocHost

**URL:** <https://forums.developer.nvidia.com/t/problem-cudamallochost/38737>\
**Category:** CUDA Programming and Performance\
**Created:** [July 14, 2015, 10:17am UTC](https://forums.developer.nvidia.com/t/problem-cudamallochost/38737 "2015-07-14T10:17:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mons91](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@mons91](https://forums.developer.nvidia.com/u/mons91)\
**Post date:** [July 14, 2015, 10:17am UTC](https://forums.developer.nvidia.com/t/problem-cudamallochost/38737/1 "2015-07-14T10:17:12Z")

</div>

Hi everybody,  
trying to use the CudaMallocHost function I encountered a problem, that is, if I allocate Host memory using CudaMallocHost in my main and then I pass the variable to a function in a .cu file where I execute my Cudamemcpy the overall program crashes, but if I substitute the CudaMallocHost with a simple malloc everything goes fine…Do you have any idea to solve this problem?

Thanks!

---

<div class="post-metadata">

**Author:** ![nikkadim](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@nikkadim](https://forums.developer.nvidia.com/u/nikkadim)\
**Post date:** [July 14, 2015, 1:20pm UTC](https://forums.developer.nvidia.com/t/problem-cudamallochost/38737/2 "2015-07-14T13:20:13Z")

</div>

```auto
cudaError_t cudaMallocHost (void ** ptr, size_t size )

```

Check for cudaError\_t, may be it’s not possible to allocate this amount of data in PINNED memory.

---

<div class="post-metadata">

**Author:** ![mons91](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@mons91](https://forums.developer.nvidia.com/u/mons91)\
**Post date:** [July 14, 2015, 2:02pm UTC](https://forums.developer.nvidia.com/t/problem-cudamallochost/38737/3 "2015-07-14T14:02:04Z")

</div>

unfortunately the cudaMallocHost doesn’t return any error.

---

<div class="post-metadata">

**Author:** ![Robert\_Crovella](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/robert_crovella/32/14043_2.png) [@Robert\_Crovella](https://forums.developer.nvidia.com/u/Robert_Crovella)\
**Post date:** [July 14, 2015, 3:34pm UTC](https://forums.developer.nvidia.com/t/problem-cudamallochost/38737/4 "2015-07-14T15:34:25Z")

</div>

I guess the problem is in something you haven’t described. Based on your description, the following test case seems to work fine for me:

```auto
$ cat t842.cpp
#include <cuda_runtime.h>
#define DSIZE 100000
void cudaTest(int *data, size_t dsize);

int main(){

  int *data;
  cudaMallocHost(&data, DSIZE*sizeof(int));
  for (int i = 0; i < DSIZE; i++){data[i] = i;}
  cudaTest(data, DSIZE);
  return 0;
}

$ cat t842.cu
#include <stdio.h>

#define DSIZE 10
__global__ void test(int *data, size_t dsize){
  for (int i = 0; i < dsize; i++) printf("data[%d] = %d\n", i, data[i]);
}

void cudaTest(int *data, size_t dsize){

  int *d_data;
  cudaMalloc(&d_data, dsize*sizeof(int));
  cudaMemcpy(d_data, data, DSIZE*sizeof(int), cudaMemcpyHostToDevice);
  test<<<1,1>>>(d_data, DSIZE);
  cudaDeviceSynchronize();
}
$ nvcc -o t842 t842.cu t842.cpp
$ ./t842
data[0] = 0
data[1] = 1
data[2] = 2
data[3] = 3
data[4] = 4
data[5] = 5
data[6] = 6
data[7] = 7
data[8] = 8
data[9] = 9
$

```

---

<div class="post-metadata">

**Author:** ![allanmac](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/allanmac/32/12124_2.png) [@allanmac](https://forums.developer.nvidia.com/u/allanmac)\
**Post date:** [July 14, 2015, 3:47pm UTC](https://forums.developer.nvidia.com/t/problem-cudamallochost/38737/5 "2015-07-14T15:47:40Z")

</div>

@mons91, are you performing a DeviceToDevice memcpy?

If so, you would need to use cudaHostGetDevicePointer() to obtain a device pointer to the cudaMallocHost() allocated memory.
