user1280671
user1280671

Reputation: 69

CUDA using host array to device array

I would like some help with the following. I have an array on host code, a kernel function and a device function called from the kernel. I want only the device function to use the host array. Can I make my host array "visible" from the device function directly in some way or do I have to copy it to the kernel first?

Upvotes: 1

Views: 2395

Answers (3)

djmj
djmj

Reputation: 5554

You could have a look at this answer:

Default Pinned Memory Vs Zero-Copy Memory

or search for:

Mapped Memory and Page-Locked Host Memory

http://developer.download.nvidia.com/compute/DevZone/docs/html/C/doc/CUDA_C_Programming_Guide.pdf#page=40

Upvotes: 1

Ilia K.
Ilia K.

Reputation: 4962

CUDA kernel is a code executing on GPU (device). GPU can only access the memory located on the graphics card, not the main CPU memory. CPU can copy data between main and device memory. The usual scenario is:

  1. CPU: prepare input data in main memory
  2. CPU: copy input data to device
  3. GPU: run the kernel
  4. CPU: copy output from the device to main memory

I recommend you to read the official documentation. If you prefer slides, you can start from the introductory presentation.

Upvotes: 0

Brendan Wood
Brendan Wood

Reputation: 6450

I don't believe there is any way for the device function to access host memory directly. Even if you could, the latency and bandwidth would be brutal and likely not worthwhile.

If you mean that you want the device function to be run by the CPU on the host side, then that is possible. You can prefix __host__ __device__ to the function definition, and that function will be compiled for both the host and device so that you can use it directly on the CPU. The only catch is that the function can't make use of any CUDA features not available on the CPU (eg., shared memory, etc.).

Upvotes: 0

Related Questions