clCompileProgram&clLinkProgram

来源:互联网 发布:js关闭微信浏览器 编辑:程序博客网 时间:2024/05/16 12:20

   在OpenCL1.1中,创建program,直接用clBuildProgram即可。

   在OpenCL1.2中,新添加了一种方式:先compiler(clCompileProgram),再linker(clLinkProgram)。

   具体用法,请看下面的例子:

   kernel.cl文件,调用了add.h中的add函数,

#include"add.h"__kernel void call_test(__global float* A,const float b){    int index=get_global_id(0);     A[index]=add(A[index],b);}
add.h文件内容

float add(float a,float b){    return (a+b)+100.0;}

对于此种方式,采用先compiler再linker创建program:

cl_program program_cl,program_head,program;  std::ifstream srcFile("kernel.cl");  string srcProg(istreambuf_iterator<char>(srcFile),(istreambuf_iterator<char>()));  const char *src=srcProg.c_str();  size_t src_length=srcProg.length();  program_cl=clCreateProgramWithSource(context,1,&src,&src_length,&err);std::ifstream srcFile_head("add.h");string srcProg_head(istreambuf_iterator<char>(srcFile_head),(istreambuf_iterator<char>()));  const char *src_head=srcProg_head.c_str();   size_t src_length_head=srcProg_head.length();   program_head=clCreateProgramWithSource(context,1,&src_head,&src_length_head,&err);const char *input_head_names[1]={"add.h"};cl_program input_head[1]={program_head};err=clCompileProgram(program_cl,0,NULL,0,1,input_head,input_head_names,NULL,NULL);program=clLinkProgram(context,num_device,devices,NULL,1,&program_cl,NULL,NULL,&err);


0 0
原创粉丝点击