singly linked list

Hi all,

I would like to creat a singly linked list in cuda, defining a structure that contains pointers to array.
here is the equivalent C code:

typedef struct
{
float *u;
float *uf;
int size;
}data;


data *mdata = (data *)malloc(sizeof(mdata) );

mdata->size = 1024;

mdata -> u = (float *)malloc(sizeof(float)*mdata->N);
mdata -> uf = (float *)malloc(sizeof(float)*mdata->N);

and below is the struct definition in cuda code:

typedef struct
{
Real2 *u;
Real2 *uf;
int size;
}data;

CUDA_SAFE_CALL(cudaMalloc((void**) &mdata,sizeof(data)));

but I dont know how i can allocate memory to the two pointers.

can anyone please help me with this???

You can’t - or a least not in the fashion you might be imagining. The fundamental problem is that the host cannot dereference and manipulate device pointers. The only way to do it is to allocate a pool or pools of device memory with host code and then use a trivial device kernel to assign the memory to the pointers. The alternative would be to store indices, rather than pointers, and traverse the list that way. Which leads to a side question: how are you going to use that structure to form a linked list?

thanks for your reply. can you tell me how I can allocate pools of device memory. among the two pointer in the structure, I ll need host memory allocated for only the first one. as for the second one, data will be produced and used in device memory.

I have already tried allocating fixed size memory to the two pointers but I got runtime segmentation fault error.

here s the code:

CUDA_SAFE_CALL(cudaMalloc((void**) &mdata,sizeof(data)));

mdata->size=1024;

CUDA_SAFE_CALL(cudaMalloc((void**) &(mdata->u), sizeof(float2) * mdata->size);

CUDA_SAFE_CALL(cudaMalloc((void**) &(mdata->uf), sizeof(float2) * mdata->size));

if I define a pointer in device memory and allocate memory to it. how can i set the mdata->u pointing to this memory?? is that possible?? if not what should I do??

thanks for all your help :)

Allocate the memory on the host, and copy the contents of the memory pointer (which points to device memory) to the device. It works, I’ve done this before - but it’s a hassle.

thanks for your reply, I have tried defining a pointer and an array and used cudaMemcpyDeviceToDevice to copy the array to pointer that works perfectly fine but when my pointer is inside the struct and i try to assign the array to it, it doesnt work (segmentation error).I think cuda has problems with defining pointer to pointer to pointer. :unsure:

typedef struct

{

float *u;

float *uf;

int size;

}data;


data *mdata;

cudaMalloc((void**) &mdata,sizeof(data));

mdata->size = 1024;

float2 *u, *uf;

cudaMalloc((void**) &u, sizeof(float2) * size);

cudaMalloc((void**) &uf, sizeof(float2) * size);

cudaMemcpy((mdata->u), u,sizeof(float2) * size, cudaMemcpyDeviceToDevice);

cudaMemcpy((mdata->uf), uf,sizeof(float2) * size, cudaMemcpyDeviceToDevice);

any ideas on what I have done wrong???