#include #include #include #define __CL_ENABLE_EXCEPTIONS #include "cl.hpp" #define DATATYPE double #define ARRAY_SIZE 1000000 struct badfile : public std::exception { virtual const char * what () const throw () { return "Cannot open kernel file"; } }; int main(void) { try { // Open the Kernel source std::ifstream in("ocl-stream-kernels.cl"); if (!in.is_open()) throw badfile(); std::string kernels(std::istreambuf_iterator(in), (std::istreambuf_iterator())); // Setup OpenCL cl::Context context(CL_DEVICE_TYPE_GPU); cl::CommandQueue queue(context); cl::Program program(context, kernels); try { program.build(); } catch (cl::Error& e) { std::vector devices = context.getInfo(); std::string buildlog = program.getBuildInfo(devices[0]); std::cerr << "Build error:" << buildlog << std::endl; throw e; } cl::make_kernel copy(program, "copy"); cl::make_kernel mul(program, "mul"); cl::make_kernel add(program, "add"); cl::make_kernel triad(program, "triad"); // Create host vectors std::vector h_a(ARRAY_SIZE, 1.0); std::vector h_b(ARRAY_SIZE, 2.0); std::vector h_c(ARRAY_SIZE, 0.0); // Create device buffers cl::Buffer d_a(context, CL_MEM_READ_WRITE, sizeof(DATATYPE) * ARRAY_SIZE); cl::Buffer d_b(context, CL_MEM_READ_WRITE, sizeof(DATATYPE) * ARRAY_SIZE); cl::Buffer d_c(context, CL_MEM_READ_WRITE, sizeof(DATATYPE) * ARRAY_SIZE); // Copy host memory to device cl::copy(queue, h_a.begin(), h_a.end(), d_a); cl::copy(queue, h_b.begin(), h_b.end(), d_b); cl::copy(queue, h_c.begin(), h_c.end(), d_c); // Make sure the copies are finished queue.finish(); } // Catch OpenCL Errors and display information catch (cl::Error& e) { std::cerr << "Error: " << e.what() << "(" << e.err() << ")" << std::endl; } catch (std::exception& e) { std::cerr << "Error: " << e.what() << std::endl; } }