首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 更多程式設計知識

OpenCL 核心函數使用結構體資料:主機端與裝置端定義對應

     編輯:更多程式設計知識

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"),無法判定有效長度,必須改用固定長度字元陣列並額外傳遞長度。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved