From d954c321a0df0b57283470928a71fd3d35b67208 Mon Sep 17 00:00:00 2001 From: "Dakota St. Laurent" Date: Wed, 24 Jun 2015 13:28:54 -0400 Subject: [PATCH] added example 02 --- README.md | 5 ++- example02/main.cpp | 96 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 example02/main.cpp diff --git a/README.md b/README.md index be868ee..fb1f9d7 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,10 @@ curl https://www.khronos.org/registry/cl/api/1.2/cl.hpp -o CL/cl.hpp this example is based off of [this example](simpleopencl.blogspot.ca/2013/06/tutorial-simple-start-with-opencl-and-c.html) (example-ception), but it goes a bit further. In the blogspot example, two 10-element vectors are created and a thread is used for each pair of elements. In this example, 10 threads are spawned but two 100-element vectors are used, and it is shown how to split up a specific number of elements per thread. ## example 01 -See the README in the folder. +Measures the duration of adding two vectors. See the README in the folder for more details. + +## example 02 +Demonstrates that one array can be modified several times without having to re-read and re-write data to and from the GPU. ## TODO diff --git a/example02/main.cpp b/example02/main.cpp new file mode 100644 index 0000000..725a76e --- /dev/null +++ b/example02/main.cpp @@ -0,0 +1,96 @@ +#include +#include +#include +#include "../CL/cl.hpp" + +using namespace std; +using namespace cl; + + +int factorial(int n) { + return (n <= 1) ? 1 : n * factorial(n-1); +} + + +Platform getPlatform() { + /* Returns the first platform found. */ + std::vector all_platforms; + Platform::get(&all_platforms); + + if (all_platforms.size()==0) { + cout << "No platforms found. Check OpenCL installation!\n"; + exit(1); + } + return all_platforms[0]; +} + + +Device getDevice(cl::Platform platform, int i, bool display=false) { + /* Returns the deviced specified by the index i on platform. + * If display is true, then all of the platforms are listed. + */ + std::vector all_devices; + platform.getDevices(CL_DEVICE_TYPE_ALL, &all_devices); + if(all_devices.size()==0){ + cout << "No devices found. Check OpenCL installation!\n"; + exit(1); + } + + if (display) { + for (int j=0; j().c_str()); + } + return all_devices[i]; +} + + +int main() { + const int n = 1024; // size of vectors + const int c_max = 5; // max value to iterate to + const int coeff = factorial(c_max); + + int A[n], B[n], C[n]; // A is initial, B is result, C is expected result + for (int i=0; i(default_device) << std::endl; + exit(1); + } + + Buffer buffer_A(context, CL_MEM_READ_WRITE, sizeof(int) * n); + CommandQueue queue(context, default_device); + queue.enqueueWriteBuffer(buffer_A, CL_TRUE, 0, sizeof(int)*n, A); + + Kernel multiply_by = cl::Kernel(program, "multiply_by"); + multiply_by.setArg(0, buffer_A); + + for (int c=2; c<=c_max; c++) { + multiply_by.setArg(1, c); + queue.enqueueNDRangeKernel(multiply_by, cl::NullRange, cl::NDRange(n), cl::NDRange(32)); + } + + queue.enqueueReadBuffer(buffer_A, CL_TRUE, 0, sizeof(int)*n, B); + + if (std::equal(std::begin(B), std::end(B), std::begin(C))) + cout << "Arrays are equal!" << endl; + else + cout << "Uh-oh, the arrays aren't equal!" << endl; + + return 0; +} +