# Memeory allocation on Host Memory allocation to Host to Device Transfer

**URL:** <https://forums.developer.nvidia.com/t/memeory-allocation-on-host-memory-allocation-to-host-to-device-transfer/13720>\
**Category:** CUDA Programming and Performance\
**Created:** [December 10, 2009, 2:26pm UTC](https://forums.developer.nvidia.com/t/memeory-allocation-on-host-memory-allocation-to-host-to-device-transfer/13720 "2009-12-10T14:26:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![newport\_j](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@newport\_j](https://forums.developer.nvidia.com/u/newport_j)\
**Post date:** [December 10, 2009, 2:26pm UTC](https://forums.developer.nvidia.com/t/memeory-allocation-on-host-memory-allocation-to-host-to-device-transfer/13720/1 "2009-12-10T14:26:41Z")

</div>

In several examples of the CUDA Programming Guide 2.3.1 they

cudaMalloc((void\*\*)&dd\_A, size);

then they later cudaMemcpy(d\_A, h\_A, size, cudaMemcpyHostToDevice):

In other words they allocate the memory on the host then copy it to the device where it s used in this case by a matrix multiplication kernel. Why bother with allocating on the host system? Why not just allocate on the device and skip the host to device transfer. It seems a lot easier. They may not be a command to allow cudaMalllloc on the device and that could be a valid point. However, the question is still valid. Maybe there should be such as command if there currently is not one.

Respectfully,

Newport\_j

---

<div class="post-metadata">

**Author:** ![LSChien](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@LSChien](https://forums.developer.nvidia.com/u/LSChien)\
**Post date:** [December 10, 2009, 2:34pm UTC](https://forums.developer.nvidia.com/t/memeory-allocation-on-host-memory-allocation-to-host-to-device-transfer/13720/2 "2009-12-10T14:34:32Z")

</div>

```auto
cudaMalloc((void**)&d_A, size);

```

allocates a device memory with “size” bytes and put the address into d\_A

where d\_A is a host variable, its content is an device address.

In most case, you need host to device transfer or vice versa since

you need to report results either to File or to screen.

---

<div class="post-metadata">

**Author:** ![avidday](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/avidday/32/59219_2.png) [@avidday](https://forums.developer.nvidia.com/u/avidday)\
**Post date:** [December 10, 2009, 2:45pm UTC](https://forums.developer.nvidia.com/t/memeory-allocation-on-host-memory-allocation-to-host-to-device-transfer/13720/3 "2009-12-10T14:45:28Z")

</div>

> [@](#):
>
> In several examples of the CUDA Programming Guide 2.3.1 they
> 
> cudaMalloc((void\*\*)&dd\_A, size);
> 
> then they later cudaMemcpy(d\_A, h\_A, size, cudaMemcpyHostToDevice):
> 
> In other words they allocate the memory on the host then copy it to the device where it s used in this case by a matrix multiplication kernel.

Err no. cudaMalloc allocates memory on the device, not the host. The cudaMemcpy is copying from another piece of host memory into the memory allocated by cudaMalloc. This is all covered in Ch 3 of the programming guide…
