0

In the book CUDA By Example on page no 26 it written that:

You can pass pointers allocated with cudaMalloc() to functions that execute on the host.

You cannot use pointers allocated with cudaMalloc() to read or write memory from code that executes on the host.

To demonstrate the first concept I am writing the code here:

main() { int * ad, N; cudaMalloc((void **)&ad, Sizeof(int) *N); GPUFunction<<<...>>>(ad); Hostfunction(ad); } Hostfunction(int * AD) { int c ; c=AD[N-1]; printf("%d", c); } __global__ void GPUFunction(int *AD) { AD[threadIdx.x]= threadidx.x; } 

Is this what the point# 1 above explains? If so it in contradiction to point# 2 above, as you can see the host function is reading memory ad. So where is my understanding going wrong?

2 Answers 2

2

The two points are not in contradiction.

The first point simply means that you can pass the AD pointer to the HostFunction exactly as you are doing, but this does not mean that you can direcly access device memory by

c=AD[N-1]; 

as the second point is telling you.

What you can do is to use the pointer to perform, for example, other cudaMemcpy operations or to pass that pointer to a __global__ function.

So, a possible HostFunction could be

__global__ void GPUFunction(int *AD) { AD[threadIdx.x]= threadidx.x; } Hostfunction(int * AD) { GPUFunction<<<...,...>>>(AD); } 
Sign up to request clarification or add additional context in comments.

Comments

2

I cannot see that the host function is reading memory ad. You've certainly written some code that attempts to do that, but it's invalid code and it will seg fault.

Since the code you've provided here is not complete or compilable, it doesn't demonstrate anything.

Here's a compilable example pretty close to what you have typed in, which demonstrates that your Hostfunction will not work correctly when passed ad:

$ cat t251.cu #include <stdio.h> __global__ void GPUFunction(int *AD) { AD[threadIdx.x]= threadIdx.x; } void Hostfunction(int * AD, int N) { int c ; printf("preparing to read ad in host code\n"); c=AD[N-1]; printf("%d", c); } int main() { int * ad, N; N = 1; cudaMalloc((void **)&ad, sizeof(int) *N); GPUFunction<<<1,1>>>(ad); Hostfunction(ad, N); } $ nvcc -arch=sm_20 -o t251 t251.cu $ ./t251 preparing to read ad in host code Segmentation fault (core dumped) $ 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.