cl::Kernel CLWrapper::compileKernel(const char *kernelFile, const char *kernelName) { try { // load opencl source std::ifstream cl_file(kernelFile); std::string cl_string(std::istreambuf_iterator<char>(cl_file), (std::istreambuf_iterator<char>())); cl::Program::Sources source(1, std::make_pair(cl_string.c_str(), cl_string.length() + 1)); // create program cl::Program program(context, source); // compile opencl source try { program.build(devices); } catch(cl::Error e) { std::cerr << "Build Status:\t " << program.getBuildInfo<CL_PROGRAM_BUILD_STATUS>(devices[0]) << std::endl; std::cerr << "Build Options:\t" << program.getBuildInfo<CL_PROGRAM_BUILD_OPTIONS>(devices[0]) << std::endl; std::cerr << "Build Log:\n" << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(devices[0]) << std::endl; throw e; } // load named kernel from opencl source return cl::Kernel(program, kernelName); } catch(cl::Error e) { std::cerr << std::endl << e.what() << " : " << e.err() << std::endl; exit(EXIT_FAILURE); } }
// Loads the file as a new OpenCL program, builds it on the device, and then // creates an OpenCL kernel with the result. cl::Kernel OpenclImageProcessor::LoadKernel(string filename, string kernel_name, bool use_gpu) { ifstream cl_file(KernelPath(filename, use_gpu)); if (!cl_file.good()) cerr << "Couldn't open " << KernelPath(filename, use_gpu) << endl; // Read the kernel file and create a cl::Program with it. string cl_string(istreambuf_iterator<char>(cl_file), (istreambuf_iterator<char>())); cl::Program::Sources source( 1, make_pair(cl_string.c_str(), cl_string.length() + 1)); cl::Program program(context_, source); try { program.build(devices_); } catch (cl::Error e) { // If there's a build error, print out the build log to see what // exactly the problem was. cerr << "Build Status:\t" << program.getBuildInfo<CL_PROGRAM_BUILD_STATUS>(selected_device_) << endl << "Build Options:\t" << program.getBuildInfo<CL_PROGRAM_BUILD_OPTIONS>(selected_device_) << endl << "Build Log:\t " << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(selected_device_) << endl; } return cl::Kernel(program, kernel_name.c_str()); }
cl_environment(const std::string &name) { cl::Platform platform; cl::Platform::get(&platform); std::vector<cl::Device> devices; platform.getDevices(CL_DEVICE_TYPE_ALL, &devices); context = cl::Context(devices); queue = cl::CommandQueue(context, devices[0], CL_QUEUE_PROFILING_ENABLE); std::ifstream cl_file(name); std::string cl_string(std::istreambuf_iterator<char>(cl_file), (std::istreambuf_iterator<char>())); cl::Program::Sources source(1, std::make_pair(cl_string.c_str(), cl_string.length() + 1)); program = cl::Program(context, source); try { program.build(); } catch (const cl::Error &e) { std::string log_str = program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(devices[0]); std::cout << log_str; throw; } }
int main() { try { std::vector<cl::Device> devices; // select platform cl::Platform platform = selectPlatform(); // select device platform.getDevices(CL_DEVICE_TYPE_ALL, &devices); cl::Device device = selectDevice(devices); // create context context = cl::Context(devices); // create command queue queue = cl::CommandQueue(context, device, CL_QUEUE_PROFILING_ENABLE); // load opencl source std::ifstream cl_file("inclusive_scan.cl"); std::string cl_string{std::istreambuf_iterator<char>(cl_file), std::istreambuf_iterator<char>()}; cl::Program::Sources source(1, std::make_pair(cl_string.c_str(), cl_string.length() + 1)); // create programm program = cl::Program(context, source); // compile opencl source try { program.build(devices); size_t input_size; std::ifstream input_file("input.txt"); input_file >> input_size; std::vector<float> input(input_size); // for (size_t i = 0; i < input_size; ++i) { // input[i] = i % 10; // } for (int i = 0; i < input_size; i++) { input_file >> input[i]; } std::vector<float> output(input_size, 0); cl::Buffer dev_input (context, CL_MEM_READ_ONLY, sizeof(float) * input_size); queue.enqueueWriteBuffer(dev_input, CL_TRUE, 0, sizeof(float) * input_size, &input[0]); cl::Buffer dev_output = inclusive_scan(dev_input, input_size); queue.enqueueReadBuffer(dev_output, CL_TRUE, 0, sizeof(float) * input_size, &output[0]); queue.finish(); cpu_check(input, output); std::ofstream output_file("output.txt"); for (int i = 0; i < input_size; i++) { output_file << output[i] << " "; } } catch (cl::Error const & e) { std::string log_str = program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device); std::cout << std::endl << e.what() << " : " << e.err() << std::endl; std::cout << log_str; return 0; } } catch (cl::Error const & e) { std::cout << "Error: " << e.what() << " #" << e.err() << std::endl; } return 0; }