OpenCL编程中可以使用结构体,只需要在核函数kernel中提供同样的结构体申明就可以啦。
如果在主函数中定义了结构体:
typedef struct studentNode{
int age;
float height;
}student;
主函数中定义数据,并传输给OpenCL kernel:
student *stu_input=(student*)malloc(sizeof(studentNode));
stu_input->age=;
stu_input->height=.8l;
student *stu_output=(student*)malloc(sizeof(studentNode)); cl_mem inputBuffer = clCreateBuffer(context, CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR, sizeof(studentNode),(void *)stu_input, NULL);
cl_mem outputBuffer = clCreateBuffer(context, CL_MEM_WRITE_ONLY ,sizeof(studentNode), NULL, NULL); cl_kernel kernel = clCreateKernel(program,"structTest", NULL); status = clSetKernelArg(kernel, , sizeof(cl_mem), (void *)&inputBuffer);
status = clSetKernelArg(kernel, , sizeof(cl_mem), (void *)&outputBuffer);
下面是具体的OpenCL kernel,可以对学生的年龄、身高进行修改:
typedef struct studentNode{
int age;
float height;
}student; int growUp(__global student *stu_input ,__global student *stu_output)
{
stu_output->age=stu_input->age+;
stu_output->height=stu_input->height + 0.1;
return ;
} __kernel void structTest(__global student *stu_input ,__global student *stu_output)
{
growUp(stu_input,stu_output);
}
执行输出:
注意:
OpenCL中不支持字符串,如char string[32]="Hello World"。OpenCL不能确定字符串中有多少个有效字符,必须给定字符数量。