lundi 29 juin 2015

How to copy struct pointer to cuda memory? [duplicate]


This question already has an answer here:

I'm trying to copy a struct pointer to device memory. The problem is when I try to work with a pointer struct and this pointer struct works with another pointers.

I have this code:

typedef struct   
{  
    int ncols; 
    int nrows; 
    float *data;
}image,*t_image;

__global__ void foo(t_image d_i)  
{  
    for(int i=0;i<d_i->ncols*d_i->nrows;i++)
        d_i->data[i]=i;
}  

int main()  
{  
    int ncols=2;
    int nrows=2;
    int nbytes=sizeof(image)+sizeof(float)*ncols*nrows;
    t_image host_image = (t_image)malloc(nbytes);  
    host_image->ncols=ncols;   
    host_image->nrows=nrows;    
    host_image->data= new float[ncols*nrows]();

    t_image device_image;  

    cudaMalloc((void**)&device_image,nbytes);  
    cudaMemcpy(device_image,host_image,nbytes,cudaMemcpyHostToDevice);  

    foo<<<1,1>>>(device_image);  //launch one kernel just to see how launch properly the kernel
    cudaMemcpy((void**)host_image,device_image,nbytes,cudaMemcpyDeviceToHost);   
    for(int i=0;i<host_image->ncols*host_image->nrows;i++)
        printf("Host image: %f \n",host_image->data[i]);

}  

The output is:

Host image: 0.000000 
Host image: 0.000000 
Host image: 0.000000 
Host image: 0.000000

So, I think that the problem is when I'm trying to move the host data to device memory but I can't understand why doesn't works.

Thanks for your help.


Aucun commentaire:

Enregistrer un commentaire