/*OpenCLSquares.cpp :calculates the squares of each element in an array The source code is taken from http://simpleopencl.blogspot.com/2013/06/tutorial-simple-start-with-opencl-and-c.html */ #include "stdafx.h" #include //This will eliminate all the errors about depricated functions #define CL_USE_DEPRECATED_OPENCL_2_0_APIS #include #include using namespace std; int main() { //get all platforms (drivers) vector all_platforms; cl::Platform::get(&all_platforms); if (all_platforms.size() == 0) { cout << " No platforms found. Check OpenCL installation!\n"; exit(1); } cl::Platform default_platform = all_platforms[0]; cout << "Using platform: " << default_platform.getInfo() << "\n"; //get default device of the default platform vector all_devices; default_platform.getDevices(CL_DEVICE_TYPE_ALL, &all_devices); if (all_devices.size() == 0) { std::cout << " No devices found. Check OpenCL installation!\n"; exit(1); } cl::Device default_device = all_devices[0]; cout << "Using device: " << default_device.getInfo() << "\n"; cl::Context context({ default_device }); cl::Program::Sources sources; // kernel calculates for each element C=A*A string kernel_code = " void kernel simple_square(global const int* A, global int* C){ " " C[get_global_id(0)]=A[get_global_id(0)]*A[get_global_id(0)]; " " } "; sources.push_back({ kernel_code.c_str(), kernel_code.length() }); cl::Program program(context, sources); if (program.build({ default_device }) != CL_SUCCESS){ cout << " Error building: " << program.getBuildInfo(default_device) << "\n"; exit(1); } // create buffers on the device cl::Buffer buffer_A(context, CL_MEM_READ_WRITE, sizeof(int)* 10); cl::Buffer buffer_C(context, CL_MEM_READ_WRITE, sizeof(int)* 10); int A[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; //create queue to which we will push commands for the device. cl::CommandQueue queue(context, default_device); //writes array A to the device queue.enqueueWriteBuffer(buffer_A, CL_TRUE, 0, sizeof(int)* 10, A); // to run the kernel cl::Kernel kernel_add = cl::Kernel(program, "simple_square"); kernel_add.setArg(0, buffer_A); kernel_add.setArg(1 , buffer_C); queue.enqueueNDRangeKernel(kernel_add, cl::NullRange, cl::NDRange(10), cl::NullRange); queue.finish(); int C[10]; //read result C from the device to array C queue.enqueueReadBuffer(buffer_C, CL_TRUE, 0, sizeof(int)* 10, C); //Displaying the Input and Output arrays cout << "\nInput Array: \n"; for (int i = 0; i<10; i++) { cout << A[i] << " "; } cout << "\n\nOutput Array: \n"; for (int i = 0; i<10; i++) { cout << C[i] << " "; } return 0; }