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;

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.

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.