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