GPGPU OpenCL使用結構體數據

OpenCL編程中可使用結構體,只須要在覈函數kernel中提供一樣的結構體申明就能夠啦。編程

 若是在主函數中定義告終構體:函數

1 typedef struct studentNode{
2     int age;
3     float height;
4 }student;

 主函數中定義數據,並傳輸給OpenCL kernel:spa

 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,能夠對學生的年齡、身高進行修改:code

 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 }

執行輸出:blog

注意:字符串

  OpenCL中不支持字符串,如char string[32]="Hello World"。OpenCL不能肯定字符串中有多少個有效字符,必須給定字符數量。input

相關文章
相關標籤/搜索