Allocate a Linear Struct

Hi All,

I’m a new member of nvidia’s forum and CUDA technology. I have a question, in a cu file I have declared a linear structure:

struct libro{

int ID;

int R_a;

int R_r;

int Delta_rho;

struct libro*pun;

};

And the function used to set the value for element of my struct is:

struct libro*makeList()

{

struct libro*p, *paus;

int i,n;

printf(“\n How many books?”);

scanf(“%d”,&n);

if (n==0) p=NULL; /empty list/

else

{

/make the first element of the list/

p= (struct libro*)malloc(sizeof(struct libro));

printf("\n Introduce first ID: ");

scanf(“%d”,&p->ID);

printf("\n Introduce first R_a: ");

scanf(“%d”,&p->R_a);

printf("\n Introduce first R_r: ");

scanf(“%d”,&p->R_r);

paus=p;

/make other elements/

for(i=2;i<=n;i++)

{

paus->pun = (struct libro*)malloc(sizeof(struct libro));

paus = paus->pun;

printf("\n Introduce ID: ");

scanf(“%d”,&paus->ID);

printf("\n Introduce R_a: ");

scanf(“%d”,&paus->R_a);

printf("\n Introduce R_r: ");

scanf(“%d”,&paus->R_r);

}

paus->pun = NULL; /pointer to the end of list/

}

Now, is possible to copy each element of my structure in Cuda’s memory (global)?

If is possible may some one explain to me the right method?

I tried with:

int N;

size_t size=(int)(struct libro*)malloc(sizeof(struct libro) * n); //where n is the number of ID

cudaMalloc((void**)&libro_d.ID,size);

cudaMalloc((void**)&libro_d.R_a,size);

cudaMalloc((void**)&libro_d.R_r,size);

cudaMalloc((void**)&libro_d.Delta_rho,size);

while(p!=NULL){

cudaMemcpy((void*)libro_d.R_a,(void*)p->ID,size,cudaMemcpyHostToDevice);

cudaMemcpy((void*)libro_d.R_a,(void*)p->R_a,size,cudaMemcpyHostToDevice);

cudaMemcpy((void*)libro_d.R_r,(void*)p->R_r,size,cudaMemcpyHostToDevice);

cudaMemcpy((void*)libro_d.Delta_rho,(void*)p->Delta_rho,size,cudaMemcpyHostToDevice);

p=p->pun; /go to the next element/

N++;

}

int threadsPerBlock = 256;

int blocksPerGrid = (N + threadsPerBlock - 1)/threadsPerBlock;

VecAdd<<<blocksPerGrid,threadsPerBlock>>>(libro_d,N);

[…]

but I have a segmentation fault where I do the cudaMemcpy

Thanks and sorry for my english.