GPGPU OpenCL中如何利用布局体数据
OpenCL编程中可以利用布局体,只需要在核函数kernel中提供同样的布局体申明就可以啦。
假如在主函数中界说了布局体:
1 typedef struct studentNode{
2 int age;
3 float height;
4 }student;
主函数中界说数据,并传输给OpenCL kernel:
student *stu_input=(student*)malloc(sizeof(studentNode)); stu_input->age=25; stu_input->height=1.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, 0, sizeof(cl_mem), (void *)&inputBuffer); status = clSetKernelArg(kernel, 1, 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+1; stu_output->height=stu_input->height + 0.1; return 0; } __kernel void structTest(__global student *stu_input ,__global student *stu_output) { growUp(stu_input,stu_output); }
执行输出:
留意:
OpenCL中不支持字符串,如char string[32]="Hello World"。OpenCL不能确定字符串中有几多个有效字符,必需给定字符数量。
作者:cnblogs 旭东的博客