Hi there,
I’ve been trying to get some simple OpenCL examples running for hours now. It always fails on clBuildProgram, as mentioned in the thread title. For example this one:
#include <iostream>
#include <cassert>
#include <cstring>
#include "CL/cl.h"
#include "textIO.h"
int main(int argc, char *argv[])
{
cl_int error = 0; // Used to handle error codes
cl_platform_id platforms;
cl_uint num_platforms;
cl_context context;
cl_command_queue queue;
cl_device_id device;
// Platform
error = clGetPlatformIDs(1, &platforms, NULL);
if (error != CL_SUCCESS) {
std::cout << "Error getting platform id: " << error << std::endl;
exit(error);
} else {
std::cout << "Platform found: " << platforms << std::endl;
}
// Device
cl_uint num_devices;
error = clGetDeviceIDs(platforms, CL_DEVICE_TYPE_GPU, 1, &device, &num_devices);
if (error != CL_SUCCESS) {
std::cout << "Error getting device ids: " << error << std::endl;
exit(error);
} else {
std::cout << "Using device ID " << device << std::endl;
std::cout << "Devices found: " << num_devices << std::endl;
}
// Context
context = clCreateContext(0, 1, &device, NULL, NULL, &error);
if (error != CL_SUCCESS) {
std::cout << "Error creating context: " << error << std::endl;
exit(error);
}
// Command-queue
queue = clCreateCommandQueue(context, device, 0, &error);
if (error != CL_SUCCESS) {
std::cout << "Error creating command queue: " << error << std::endl;
exit(error);
}
// Creates the program
// const char* source = TextIO::read("addVecs.cl");
const char* source = "__kernel void mul2(__global float * mem) { int i = get_global_id(0); mem[i] *= 2; }";
cl_program program = clCreateProgramWithSource(context, 1, &source, NULL, &error);
assert(error == CL_SUCCESS);
std::cout << "========== Program source" << std::endl << source << std::endl;
// Builds the program
error = clBuildProgram(program, 1, &device, NULL, NULL, NULL);
// error = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
if (error != CL_SUCCESS) {
std::cout << "Error building the program: " << error << std::endl;
if (error == CL_INVALID_VALUE) {
std::cout << "Invalid value" << std::endl;
} else if (error == CL_INVALID_DEVICE) {
std::cout << "Invalid device" << std::endl;
} else if (error == CL_INVALID_BUILD_OPTIONS) {
std::cout << "Invalid build options";
}
// char buffer[4096];
// size_t length;
// clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &length);
// std::cout << "Program log: " << buffer;
}
// assert(error == CL_SUCCESS);
// Shows the log
char* build_log;
size_t log_size;
// First call to know the proper size
std::cout << "Length: " << strlen(source) << std::endl;
clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
build_log = new char[log_size+1];
// Second call to get the log
clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, log_size, build_log, NULL);
build_log[log_size] = '
include “CL/cl.h”
include “textIO.h”
int main(int argc, char *argv)
{
cl_int error = 0; // Used to handle error codes
cl_platform_id platforms;
cl_uint num_platforms;
cl_context context;
cl_command_queue queue;
cl_device_id device;
// Platform
error = clGetPlatformIDs(1, &platforms, NULL);
if (error != CL_SUCCESS) {
std::cout << "Error getting platform id: " << error << std::endl;
exit(error);
} else {
std::cout << "Platform found: " << platforms << std::endl;
}
// Device
cl_uint num_devices;
error = clGetDeviceIDs(platforms, CL_DEVICE_TYPE_GPU, 1, &device, &num_devices);
if (error != CL_SUCCESS) {
std::cout << "Error getting device ids: " << error << std::endl;
exit(error);
} else {
std::cout << "Using device ID " << device << std::endl;
std::cout << "Devices found: " << num_devices << std::endl;
}
// Context
context = clCreateContext(0, 1, &device, NULL, NULL, &error);
if (error != CL_SUCCESS) {
std::cout << "Error creating context: " << error << std::endl;
exit(error);
}
// Command-queue
queue = clCreateCommandQueue(context, device, 0, &error);
if (error != CL_SUCCESS) {
std::cout << "Error creating command queue: " << error << std::endl;
exit(error);
}
// Creates the program
// const char* source = TextIO::read(“addVecs.cl”);
const char* source = "__kernel void mul2(__global float * mem) { int i = get_global_id(0); mem[i] *= 2; }";
cl_program program = clCreateProgramWithSource(context, 1, &source, NULL, &error);
assert(error == CL_SUCCESS);
std::cout << “========== Program source” << std::endl << source << std::endl;
// Builds the program
error = clBuildProgram(program, 1, &device, NULL, NULL, NULL);
// error = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
if (error != CL_SUCCESS) {
std::cout << "Error building the program: " << error << std::endl;
if (error == CL_INVALID_VALUE) {
std::cout << "Invalid value" << std::endl;
} else if (error == CL_INVALID_DEVICE) {
std::cout << "Invalid device" << std::endl;
} else if (error == CL_INVALID_BUILD_OPTIONS) {
std::cout << "Invalid build options";
}
// char buffer[4096];
// size_t length;
// clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &length);
// std::cout << "Program log: " << buffer;
}
// assert(error == CL_SUCCESS);
// Shows the log
char* build_log;
size_t log_size;
// First call to know the proper size
std::cout << "Length: " << strlen(source) << std::endl;
clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
build_log = new char[log_size+1];
// Second call to get the log
clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, log_size, build_log, NULL);
build_log[log_size] = '\0';
std::cout << build_log << std::endl;
delete[] build_log;
// Extracting the kernel
cl_kernel vector_add_kernel = clCreateKernel(program, "addVectors", &error);
assert(error == CL_SUCCESS);
}
';
std::cout << build_log << std::endl;
delete[] build_log;
// Extracting the kernel
cl_kernel vector_add_kernel = clCreateKernel(program, "addVectors", &error);
assert(error == CL_SUCCESS);
}
(clGetProgramBuildInfo is deactivated since it just segfaults when calling.)
I took the example from here.
Can anyone tell me what is going on here? Or how to find out? I’ve got the same problems with the SDK examples:
I’m on Linux, 64-bit, with a 9800 GT.
thanks
Simon