OpenCL 支援結構體傳遞,需在主機端與核心函數雙方定義相同結構體配置。
typedef struct studentNode {
int age;
float height;
} student;
student *stu_input = (student*)malloc(sizeof(student));
stu_input->age = 25;
stu_input->height = 1.8f;
student *stu_output = (student*)malloc(sizeof(student));
cl_mem inputBuffer = clCreateBuffer(context,
CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
sizeof(student), (void*)stu_input, NULL);
cl_mem outputBuffer = clCreateBuffer(context,
CL_MEM_WRITE_ONLY, sizeof(student), NULL, NULL);
cl_kernel kernel = clCreateKernel(program, "structTest", NULL);
clSetKernelArg(kernel, 0, sizeof(cl_mem), &inputBuffer);
clSetKernelArg(kernel, 1, sizeof(cl_mem), &outputBuffer);
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.1f;
return 0;
}
__kernel void structTest(__global student *stu_input, __global student *stu_output) {
growUp(stu_input, stu_output);
}

OpenCL 不支援字串(如 char str[32] = "Hello"),無法判定有效長度,必須改用固定長度字元陣列並額外傳遞長度。