Why can't I correctly copy data to a structure in the openCL cl_mem buffer?

OK, so I highlighted this for a very specific problem.

I had the impression that you could pass OpenCL any data types in the array buffer; ints, chars, your own custom structures, unless it is just data and does not contain pointers to a bunch of objects that the GPU cannot retrieve.

Now I tried this, and I think that it works for a large int array, but not for my array of structures. In particular,

cl_mem log_buffer = clCreateBuffer(context, CL_MEM_READ_WRITE, 
  num_elements * sizeof(int), NULL, NULL);

int* error_codes_in = (int*)malloc(num_elements * sizeof(int));

for (i = 0; i < num_elements; i++) {
  error_codes_in[i] = i;
}

error = clEnqueueWriteBuffer(command_queue, log_buffer, CL_TRUE,
  0, num_elements * sizeof(int), error_codes_in, 0, NULL, NULL);

this works fine, and I get an array of numbers on the GPU and can successfully process them in parallel.

However, when I use my own structure of my own:

typedef struct {
  float position[2];
  float velocity[2];
  float radius;
  float resultant_force[2];
} ocl_element_2d_t;

(also defined in the kernel, as)

const char* kernel_string = 
  "typedef struct { float position[2]; float velocity[2]; float radius; float resultant_force[2]; } ocl_element_2d_t;"...

and I use the same / very similar code to write to the GPU version of my struct array:

cl_mem gpu_buffer = clCreateBuffer(context, CL_MEM_READ_WRITE,
  num_elements * sizeof(ocl_element_2d_t), NULL, NULL);

error = clEnqueueWriteBuffer(command_queue, (cl_mem)gpu_buffer, CL_TRUE,
  0, num_elements * sizeof(ocl_element_2d_t), host_buffer, 0, NULL, NULL);

, ( 350) float . : CL_SUCCESS.

, ? , GPU , , . , - 64- (OS X Lion) i7 ( ), GPU 32-, ? ATI Radeon HD 5750, , 128- ( , , .)

? FORTRAN 7 , , ?

+5
2

@0A0D . .

, , ,

ocl_element_2d_t element = host_buffer[i];
element.position[0] = 1.2;
element.position[1] = 5.7;

. google , C, http://www.asic-world.com/scripting/structs_c.html, ,

struct_instance = other_struct_instance;

, .

, , , host_buffer.

, , :

  • StackOverflow, , .
  • , , OpenCL, , - !
+2

, "float", gcc :

#pragma pack(1)

.

:

#pragma pack()

, :

typedef struct {
  float position[2];
  float velocity[2];
  float resultant_force[2];
  float radius;
} ocl_element_2d_t;
0

All Articles