From fd6b886c6701c90fa94b4627106ab7b73eec84bc Mon Sep 17 00:00:00 2001 From: "Dakota St. Laurent" Date: Fri, 12 Jun 2015 19:00:45 -0400 Subject: [PATCH] add configuration and compilation instructions; properly indent a function in example01 --- README.md | 19 ++++++++++++++++++- example01.cpp | 24 +++++++++++------------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 061172c..c9b2584 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,22 @@ # OpenCL basic examples -here is my feeble attempt at learning OpenCL, please don't make fun of me too much :hamburger +here is my feeble attempt at learning OpenCL, please don't make fun of me too much :hamburger: + +## Configuration +This currently runs on OS X, and I'm using local header files instead of global header files because I'm unfamiliar with C++. Deal with it. Run the following in a terminal to set up: + +``` +git clone git@github.com:SaintDako/OpenCL-examples.git +mkdir CL +curl https://www.khronos.org/registry/cl/api/1.2/cl.hpp -o CL/cl.hpp +``` + +## Compiling + +``` +clang++ -std=c++0x -framework OpenCL example01.cpp -o example01.out +``` + +To ignore the deprecation warnings, add the flag `-Wno-deprecated-declarations`. ## example 00 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. diff --git a/example01.cpp b/example01.cpp index 74ebacb..7496867 100644 --- a/example01.cpp +++ b/example01.cpp @@ -3,16 +3,16 @@ #include - void compareResults (double CPUtime, double GPUtime, int trial) { - double time_ratio = (CPUtime / GPUtime); - std::cout << "VERSION " << trial << " -----------" << std::endl; - std::cout << "CPU time: " << CPUtime << std::endl; - std::cout << "GPU time: " << GPUtime << std::endl; - std::cout << "GPU is "; - if (time_ratio > 1) - std::cout << time_ratio << " times faster!" << std::endl; - else - std::cout << (1/time_ratio) << " times slower :(" << std::endl; +void compareResults (double CPUtime, double GPUtime, int trial) { + double time_ratio = (CPUtime / GPUtime); + std::cout << "VERSION " << trial << " -----------" << std::endl; + std::cout << "CPU time: " << CPUtime << std::endl; + std::cout << "GPU time: " << GPUtime << std::endl; + std::cout << "GPU is "; + if (time_ratio > 1) + std::cout << time_ratio << " times faster!" << std::endl; + else + std::cout << (1/time_ratio) << " times slower :(" << std::endl; } @@ -39,6 +39,7 @@ double timeAddVectorsCPU(int n, int k) { return duration; } + int main() { // get all platforms (drivers), e.g. NVIDIA std::vector all_platforms; @@ -66,9 +67,6 @@ int main() { cl::Context context({default_device}); cl::Program::Sources sources; - std::cout << CL_DEVICE_MAX_WORK_ITEM_SIZES << std::endl; - exit(1); - // calculates for each element; C = A + B std::string kernel_code= // is equivalent to the host's "time_add_vectors" function, except the