获取cl_build_program_failure错误

getting cl_build_program_failure error

本文关键字:failure 错误 program build cl 获取      更新时间:2023-10-16

我目前正在进行一个关于OpenCL的项目,在尝试构建该程序时遇到了一些麻烦。所以我有以下代码:

    //Read source file
    std::ifstream sourceFile("calculation_kernel.cl");
    std::string sourceCode(std::istreambuf_iterator<char>(sourceFile), (std::istreambuf_iterator<char>()));
    cl::Program::Sources source(1, std::make_pair(sourceCode.c_str(), sourceCode.length()+1));
    if (sourceFile.is_open()){
        printf("the file is openn");
    }else{
        printf("error opening filen");
    }
    // Make program of the source code in the context
    cl::Program program = cl::Program(context, source);
    // Build program for these specific devices
    program.build(devices);

代码编译得很好,但当我尝试运行它时,会收到一个clBuildProgram(-11)错误。我已经验证了我的内核文件可以成功打开。我是不是遗漏了什么?或者有办法调试这个错误吗?

提前感谢!

错误代码-11对应于CL_BUILD_PROGRAM_FAILURE。这表明您的内核代码未能编译,可能是由于语法错误。假设您已经在OpenCLC++绑定(#define __CL_ENABLE_EXCEPTIONS)中启用了异常,那么您可以使用以下内容检索构建日志:

try
{
  program.build(devices);
}
catch (cl::Error error)
{
  if (error.err() == CL_BUILD_PROGRAM_FAILURE)
  {
    // Get the build log for the first device
    std::string log = program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(devices[0]);
    std::cerr << log << std::endl;
  }
  throw(error);
}