cudamallocpitch how to use cudamallocpitch

good afternoon all

i have two dimensional structure in the host memory and i need to transfer it to the device memory by allocating memory in the device and then copying from host to device.

Can some one help me with a sample code of using cudamallocpitch .

my structure looks like this

struct rngen
int seed[2];
int seed[2];
int init_seed;
int prime;
int prime_position;
int prime_next;
char *gentype;
int parameter;
int *multiplier;

now i have declared struct rngen **new and allocated for new.This new i need to transfer to the device memory first by allocating and then copying it into the device memory.

Can some body help me urgently.

The simplePitchLinearTexture example in the SDK has a complete example of using cudaMallocPitch and cudaMemcpy2D/cudaMemset2D.

But be warned that you will not be able to copy that structure from host memory directly to the device because it contains host pointers.

thank you for your reply. Then is there any way to copy directly the structure from host to device

waiting for your reply

As you have written it, no there is not. The problem is the pointers inside the structure. You cannot use host pointers on the device.

As avidday mentioned, you cannot do that. You HAVE to do a ‘deep-copy’ on that structure, in order to transfer all the contents from CPU to GPU (and vice-versa).

can you please elaborate on deep copy

waiting for your reply

thanking you

Here’s an example on what I meant by deep-copy: (untested)
You copy both the elements of the struct, as well as malloc and cudaMemcpy all the pointers inside it.

struct myStruct {
int* a;
int b;
myStruct h_data, d_data;
// ----- DEEP-COPY ----- //
h_data = (myStruct) malloc(sizeof(myStruct) * 1);;
h_data->b = 10;
h_data->a = (int) malloc(sizeof(int) * h_data->b);
) &d_data, sizeof(myStruct) * 1);
cudaMemcpy(&h_data, &d_data, sizeof(myStruct) * 1, cudaMemcpyHostToDevice);
cudaMalloc((void**) &(d_data->a), sizeof(int) * h_data->b);
cudaMemcpy(&(d_data->a), &(h_data->a), sizeof(int) * d_data->b, cudaMemcpyHostToDevice);

thank you very much for your help.

So this is the only way an array of structures are copied.?

once again thank you

AFAIK, yes, this is the only way one can copy the array of structures… :(

Thank you very much for your kind help