错误代码(-11)::在OpenCL中获取错误“cl_build_program_failure”的所有可能原因是什么?

时间:2012-02-27 11:23:21

标签: debugging opencl gpu

我在linux上使用ATI RV770显卡,OpenCl 1.0和ati-stream-sdk-v2.3-lnx64。

在运行我的主机代码时,包括以下两个部分来构建内核程序,我收到错误代码(-11),即cl_build_program_failure。这是否意味着编译了内核程序,如果没有,那么它是如何编译和调试的?

const char* KernelPath = "abc_kernel.cl";   //kernel program is in separate file but in same directory of host code..

/ * 从内核源代码 * ** * ** <创建程序对象em> * /

char* sProgramSource = readKernelSource(KernelPath);
size_t sourceSize =  strlen(sProgramSource) ;
program = clCreateProgramWithSource(context, 1,(const char **) &sProgramSource,&sourceSize, &err);
checkStatus("error while creating program",err);

/ * 构建(编译和链接)程序 * ** * ** * /

char* options = (char* )malloc(10*sizeof(char));
strcpy(options, "-g");
err = clBuildProgram(program, num_devices, devices_id, options, NULL, NULL);
checkStatus("Build Program Failed", err); //This line throwing the error....

读取内核程序的函数如下::

/ *读取程序源文件* /

char* readKernelSource(const char* kernelSourcePath){
 FILE    *fp = NULL;
 size_t  sourceLength;
 char    *sourceString ;
 fp = fopen( kernelSourcePath , "r");
 if(fp == 0)
 {
        printf("failed to open file");
        return NULL;
 }
 // get the length of the source code
 fseek(fp, 0, SEEK_END);
 sourceLength = ftell(fp);
 rewind(fp);
 // allocate a buffer for the source code string and read it in
 sourceString = (char *)malloc( sourceLength + 1);
 if( fread( sourceString, 1, sourceLength, fp) !=sourceLength )
 {
          printf("\n\t Error : Fail to read file ");
          return 0;
 }
 sourceString[sourceLength+1]='\0';
 fclose(fp);
 return sourceString;

} // readKernelSource的结尾

任何人都可以告诉我们如何修复它吗?

这是否意味着它在运行时或其他地方是OpenCl编译错误?

//使用clGetProgramBuildInfo()打印build_log信息,如下所示,但为什么不打印任何内容?

char * build_log;             size_t log_size;

// First call to know the proper size
        err = clGetProgramBuildInfo(program, devices_id, CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
        build_log = (char* )malloc((log_size+1));

        // Second call to get the log
        err = clGetProgramBuildInfo(program, devices_id, CL_PROGRAM_BUILD_LOG, log_size, build_log, NULL);
        build_log[log_size] = '\0';
        printf("--- Build log ---\n ");
        fprintf(stderr, "%s\n", build_log);
        free(build_log);

1 个答案:

答案 0 :(得分:32)

此错误通常是由内核代码中的语法错误引起的。您可以使用标志 CL_PROGRAM_BUILD_LOG 调用OpenCL函数 clGetProgramBuildInfo 来访问编译器生成的日志。此日志包含在命令行上编译时可能使用的输出(错误,警告等)。

例如,您可以在调用clBuildProgram后添加类似于以下内容的内容:

if (err == CL_BUILD_PROGRAM_FAILURE) {
    // Determine the size of the log
    size_t log_size;
    clGetProgramBuildInfo(program, devices_id[0], CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);

    // Allocate memory for the log
    char *log = (char *) malloc(log_size);

    // Get the log
    clGetProgramBuildInfo(program, devices_id[0], CL_PROGRAM_BUILD_LOG, log_size, log, NULL);

    // Print the log
    printf("%s\n", log);
}

您还可以在AMD APP SDK的SDKCommon.cpp中看到函数buildOpenCLProgram()作为一个真实的例子。