#include #ifdef __APPLE__ #include #else #include #endif int main() { // get all platforms (drivers), e.g. NVIDIA std::vector all_platforms; cl::Platform::get(&all_platforms); if (all_platforms.size()==0) { std::cout<<" No platforms found. Check OpenCL installation!\n"; exit(1); } cl::Platform default_platform=all_platforms[0]; std::cout << "Using platform: "<()<<"\n"; // get default device (CPUs, GPUs) of the default platform std::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); } // use device[1] because that's a GPU; device[0] is the CPU cl::Device default_device=all_devices[1]; std::cout<< "Using device: "<()<<"\n"; // a context is like a "runtime link" to the device and platform; // i.e. communication is possible cl::Context context({default_device}); // create the program that we want to execute on the device cl::Program::Sources sources; // calculates for each element; C = A + B std::string kernel_code= " void kernel simple_add(global const int* A, global const int* B, global int* C, " " global const int* N) {" " int ID, Nthreads, n, ratio, start, stop;" "" " ID = get_global_id(0);" " Nthreads = get_global_size(0);" " n = N[0];" "" " ratio = (n / Nthreads);" // number of elements for each thread " start = ratio * ID;" " stop = ratio * (ID + 1);" "" " for (int i=start; i(default_device) << std::endl; exit(1); } // apparently OpenCL only likes arrays ... // N holds the number of elements in the vectors we want to add int N[1] = {100}; int n = N[0]; // create buffers on device (allocate space on GPU) cl::Buffer buffer_A(context, CL_MEM_READ_WRITE, sizeof(int) * n); cl::Buffer buffer_B(context, CL_MEM_READ_WRITE, sizeof(int) * n); cl::Buffer buffer_C(context, CL_MEM_READ_WRITE, sizeof(int) * n); cl::Buffer buffer_N(context, CL_MEM_READ_ONLY, sizeof(int)); // create things on here (CPU) int A[n], B[n]; for (int i=0; i