How do I use structural data in GPGPU OpenCL?

Source: Internet
Author: User

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;


The main function defines the data and transmits it to the OpenCL kernel:

Student *stu_input= (student*) malloc (sizeof (Studentnode));



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;


int growup (__global student *stu_input, __global student)



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:


  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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.