GPGPU OpenCL使用結構體資料

2021-09-08 10:09:11 字數 1463 閱讀 7326

opencl程式設計中可以使用結構體,只需要在核函式kernel中提供同樣的結構體申明就可以啦。

如果在主函式中定義了結構體:

1 typedef struct

studentnodestudent;

主函式中定義資料,並傳輸給opencl kernel:

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));

56 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);

89 cl_kernel kernel = clcreatekernel(program,"

structtest

", null);

1011 status = clsetkernelarg(kernel, 0, sizeof(cl_mem), (void *)&inputbuffer);

12 status = clsetkernelarg(kernel, 1, sizeof(cl_mem), (void *)&outputbuffer);

下面是具體的opencl kernel,可以對學生的年齡、身高進行修改:

1 typedef struct

studentnodestudent;56

int growup(__global student *stu_input ,__global student *stu_output)712

13 __kernel void structtest(__global student *stu_input ,__global student *stu_output)

14

執行輸出:

注意:

opencl中不支援字串,如char string[32]="hello world"。opencl不能確定字串中有多少個有效字元,必須給定字元數量。

GPGPU OpenCL使用結構體資料

opencl程式設計中可以使用結構體,只需要在核函式kernel中提供同樣的結構體申明就可以啦。如果在主函式中定義了結構體 1 typedef struct studentnodestudent 主函式中定義資料,並傳輸給opencl kernel 1 student stu input stude...

使用hostent結構

使用hostent結構 include struct hostent define h addr h addr list 0 for backward compatibility 向後相容 struct hostent gethostbyname const char name gethostbyn...

結構體使用

結構的定義 定義乙個結構的一般形式為 struct 結構名 成員表由若干個成員組成,每個成員都是該結構的乙個組成部分。對每個成員也必須作型別說明。例如 struct stu int num char name 20 int age 結構型別變數的說明 結構體定義並不是定義乙個變數,而是定義了一種資料...