【问题标题】:Build opencl kernel failure in Visual Studio在 Visual Studio 中构建 opencl 内核失败
【发布时间】:2016-07-10 17:47:09
【问题描述】:

我在 Visual Studio 中使用 opencl,但遇到错误提示

构建程序可执行文件失败

我无法弄清楚我的代码有什么问题...请注意,任何人都可以帮助我!

这是我的代码:

cl_int 错误;

// Bind to platform
err = clGetPlatformIDs(1, &cpPlatform, NULL);
if (err != CL_SUCCESS) {
    printf("Error: Failed to find a platform\n");
    return EXIT_FAILURE;
}

// Get ID for the device
err = clGetDeviceIDs(cpPlatform, CL_DEVICE_TYPE_GPU, 1, &device_id, NULL);
if (err != CL_SUCCESS) {
    printf("Error: Failed to create a device group\n");
    return EXIT_FAILURE;
}

// Create a context
context = clCreateContext(0, 1, &device_id, NULL, NULL, &err);
if (!context) {
    printf("Error: Failed to create a compute context\n");
    return EXIT_FAILURE;
}

// Create a command queue
queue = clCreateCommandQueue(context, device_id, 0, &err);
if (!queue) {
    printf("Error: Failed to create a command commands\n");
    return EXIT_FAILURE;
}

// Create the compute program from the kernel source file
char *fileName = "GOL-kernels.cl";
FILE *fh = fopen(fileName, "r");
if (!fh) {
    printf("Error: Failed to open file\n");
    return 0;
}
struct stat statbuf;
stat(fileName, &statbuf);
char *kernelSource = (char *)malloc(statbuf.st_size + 1);
fread(kernelSource, statbuf.st_size, 1, fh);
kernelSource[statbuf.st_size] = '\0';
program = clCreateProgramWithSource(context, 1,
    (const char **)& kernelSource, NULL, &err);
if (!program) {
    printf("Error: Failed to create compute program\n");
    return EXIT_FAILURE;
}

// Build the program executable
err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
if (err != CL_SUCCESS) {
    printf("Error: Failed to build program executable %d\n", err);
    system("pause");
    return EXIT_FAILURE;
}

【问题讨论】:

    标签: opencl


    【解决方案1】:

    很可能内核构建失败。检查日志中的内容:

    // Build the program
    ret = clBuildProgram(program, 1, &device_id, "-I. -Werror", NULL, NULL);
    
    size_t len = 0;
    clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, NULL, NULL, &len);
    char *log = new char[len];
    clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, len, log, NULL);
    printf("\n\nBuildlog:   %s\n\n", log);
    

    另外,将所有警告转化为错误也是一个好主意:-Werror。当您可能想知道为什么内核没有返回正确的结果时,它可以节省大量时间。

    【讨论】:

    • 感谢您快速重播它显示错误“无法识别的令牌”我必须做什么?
    • 尝试二进制模式:fopen(fileName, "rb");
    猜你喜欢
    • 2013-09-29
    • 2015-05-15
    • 2012-08-12
    • 2017-11-25
    • 2017-11-26
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多