OpenCL programming can use a struct, just provide the same structure declaration in the kernel function kernel.
If you define a struct in the main function:
1 typedef struct studentnode{
2 int age;
3 float height;
4}student;
The main function defines the data and transmits it to the 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);
The following is a specific OpenCL kernel, which can be modified to the age and height of the student:
typedef struct studentnode{
int age;
float height;
}student;
int growup (__global student *stu_input, __global student)
{
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)
{
Growup (Stu_input,stu_output);
}
To perform the output:
Attention:
strings, such as Char string[32]= "Hello World", are not supported in OpenCL. OpenCL cannot determine how many valid characters are in a string, and the number of characters must be given.