GPGPU OpenCL使用结构体数据

OpenCL编程中可以使用结构体,只需要在核函数kernel中提供同样的结构体申明就可以啦。

 如果在主函数中定义了结构体:

1 typedef struct studentNode{

2     int age;

3     float height;

4 }student;

 主函数中定义数据,并传输给OpenCL kernel:

 1     student *stu_input=(student*)malloc(sizeof(studentNode));

 2     stu_input->age=25;

 3     stu_input->height=1.8l;

 4     student *stu_output=(student*)malloc(sizeof(studentNode));

 5 

 6     cl_mem inputBuffer = clCreateBuffer(context, CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR, sizeof(studentNode),(void *)stu_input, NULL);

 7     cl_mem outputBuffer = clCreateBuffer(context, CL_MEM_WRITE_ONLY ,sizeof(studentNode), NULL, NULL);

 8 

 9     cl_kernel kernel = clCreateKernel(program,"structTest", NULL);

10 

11     status = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&inputBuffer);

12     status = clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&outputBuffer);

下面是具体的OpenCL kernel,可以对学生的年龄、身高进行修改:

 1 typedef struct studentNode{

 2     int age;

 3     float height;

 4 }student;

 5 

 6 int growUp(__global student *stu_input ,__global student *stu_output)

 7 {

 8     stu_output->age=stu_input->age+1;

 9     stu_output->height=stu_input->height + 0.1;

10     return 0;

11 }

12 

13 __kernel void structTest(__global student *stu_input ,__global student *stu_output)

14 {

15     growUp(stu_input,stu_output);

16 }

执行输出:

GPGPU OpenCL使用结构体数据

注意:

  OpenCL中不支持字符串,如char string[32]="Hello World"。OpenCL不能确定字符串中有多少个有效字符,必须给定字符数量。

你可能感兴趣的:(open)