在Visual Studio中构建opencl内核失败

时间:2016-07-10 09:12:38

标签: opencl

我在Visual Studio中使用opencl并且遇到错误说

  

无法构建程序可执行文件

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

这是我的代码:

cl_int err;

// 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;
}

1 个答案:

答案 0 :(得分:0)

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

// 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。当您可能想知道为什么内核没有返回正确的结果时,它会节省大量时间。