Unclear Behavior

Hello,

I have a problem.
Im reading in a picture of grey float values, that will be passed to following method in a *cu file

extern “C” void doCudaStuff(int data_h, int width, int height){
const int N = height
width; // Number of elements in arrays
int *data_d;
size_t size = N * sizeof(int);
cudaMalloc((void **) &data_d, size);

… that follows the standard procedure, i know from my results that the data_h array cannot be empty, though the test

for(int i = 0; i < 15;i++){
printf(“%d %f\n”, i, data_h[i]);
}

and the succedinmg device array test fail

cudaMemcpy(data_d, data_h, size, cudaMemcpyHostToDevice);
for(int i = 0; i < 15;i++){
printf(“%d \n”, data_d[i]);
}

all i get is Zeros for the first 15 elements, although they should be filled with 0 <= value <= 255

can anyone help me?

The problem is in the line above. You can not print the values of a device pointer (data_d) in your host code.

You copy the data from data_h to data_d using cudaMemcpyHostToDevice which is correct, but to see

that the values were copied you need to copy back from data_d to data_h1 (for example) and then

print data_h1 which is a host array.

eyal

Thank you very much, that sounds logical
but still the problem remains

extern “C” void doCudaStuff(int *data_h, int width, int height){

const int N = height*width; // Number of elements in arrays
int *data_d;
size_t size = N * sizeof(int);
cudaMalloc((void **) &data_d, size);

for(int i = 0; i < 15;i++){
printf(“%d %f\n”, i, data_h[i]);
}

data_h is my host array, the method is still executed in Host code, residing withing the cu file
why cant i print the data from the host array in the first place?? :(

Regards,Maz

you should use:

printf( “%d %d\n”, i, data_h[i] )

you’re currently using %f to print an int value which gives you zero…

this is standard C behaviour.

eyal

Your printf is wrong you should use:

printf( “%d %d\n”, i, data_h[i] );

instead of “%d %f” → the %f is why you get zeros…

Thank you very much, im not yet so firm with C++! :9

Regards
Maz

I just happen to have another question concerning the acessability of arrays

if i pass my in host code initialized Array to a Kernel, the GPU will not be able to read from it, since it was initialized in Host code or?
So the kernel can only access variables allocated from HostToDevice , right?

Yes - the kernel can only use data allocated on the device…

Thank you again, this hast been very useful!