0

I am new to CUDA programming.Can you please help me to know how can we copy structure of host to Device using cuda? stucture contains velocity,pressure,temperaure.

Robert Crovella
  • 143,785
  • 11
  • 213
  • 257

1 Answers1

1

If the structures have no pointers in them (i.e. no dynamically allocated data) then copying is relatively straightforward:

#define DSIZE 100

typedef struct {
  float velocity;
  float temperature;
  float pressure;
} mystruct;

int main ()
{
  mystruct *h_data, *d_data;
  h_data = (mystruct *) malloc(DSIZE * sizeof(mystruct));
  // populate h_data
  cudaMalloc((void **)&d_data, DSIZE * sizeof(mystruct));
  cudaMemcpy(d_data, h_data, DSIZE * sizeof(mystruct), cudaMemcpyHostToDevice);
  ...
}

If the structures contain dynamically allocated data:

typedef struct {
  float *velocity;
  float *temperature;
  float *pressure;
} mystruct;

Then the process involves extra steps.

Community
  • 1
  • 1
Robert Crovella
  • 143,785
  • 11
  • 213
  • 257