From ea3055fbc1cd586013744593a87893df605c6c42 Mon Sep 17 00:00:00 2001 From: "Dakota St. Laurent" Date: Fri, 5 Jun 2015 19:36:32 -0400 Subject: [PATCH] example 01 is compiling and running, and it's freaking fast ... I'm skeptical. --- README.md | 10 +++++- example01.cpp | 95 ++++++++++++++++++++++++++++++++++++--------------- 2 files changed, 77 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 2bef45a..1fb19e0 100644 --- a/README.md +++ b/README.md @@ -5,4 +5,12 @@ here is my feeble attempt at learning OpenCL, please don't make fun of me too mu 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 -(**not complete yet**) timing results of large vectors added together on a CPU vs GPU. +timing results of vectors added together on a CPU vs GPU. the size of the vectors and the number of times they are added together can be specified, as can the number of threads used in the GPU cases. currently implemented: + +- CPU code +- GPU code equivalent + +todo: + +- GPU code where a work-group barrier is initiated after each thread is done its work +- GPU code where the buffers are created and destroyed each iteration (this is guaranteed to be slower, of course, but it would be nice to see the amount of overhead that occurs with inefficient copying...) diff --git a/example01.cpp b/example01.cpp index 229942d..365576a 100644 --- a/example01.cpp +++ b/example01.cpp @@ -51,18 +51,31 @@ int main() { 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) {" - " C[get_global_id(0)] = A[get_global_id(0)] + B[get_global_id(0)];" + // is equivalent to the host's "time_add_vectors" function, except the + // timing will be done on the host. + " void kernel looped_add(global const int* A, global const int* B, global int* C, " + " global const int* constants) {" + " int ID, Nthreads, n, k, ratio, start, stop;" + " ID = get_global_id(0);" + " Nthreads = get_global_size(0);" + " n = constants[0];" // size of vectors + " k = constants[1];" // number of loop iterations + "" + " ratio = (n / Nthreads);" // elements per thread + " start = ratio * ID;" + " stop = ratio * (ID+1);" + "" + " int i, j;" // will the compiler optimize this anyway? probably. + " for (i=0; i 1) + std::cout << time_ratio << " times faster!" << std::endl; + else + std::cout << time_ratio << " times slower :(" << std::endl; return 0; }