From ef9df7f67c9158f696367b1b4a48f16d74391eb8 Mon Sep 17 00:00:00 2001 From: "Dakota St. Laurent" Date: Tue, 9 Jun 2015 16:07:18 -0400 Subject: [PATCH] add code to 01 that repeatedly writes buffers to show overhead; change vector names in kernel code to v1,v2,v3 to show that they're not the same as A,B,C in the C++ code --- README.md | 10 +++++++-- example01.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 58 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 1fb19e0..730074e 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,14 @@ timing results of vectors added together on a CPU vs GPU. the size of the vector - CPU code - GPU code equivalent +- 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...) 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...) +- GPU code where a work-group barrier is initiated after each thread is done its work (equivalent to the regular GPU code, but with one extra line...) + +## TODO + +- figure out how OpenCL manages memory (when are buffers cleared on the GPU?) +- figure out how to view the OpenCL assembly code if possible (is warp divergence happening?) + diff --git a/example01.cpp b/example01.cpp index 365576a..f8ea162 100644 --- a/example01.cpp +++ b/example01.cpp @@ -3,7 +3,7 @@ #include -double time_add_vectors(int n, int k) { +double timeAddVectorsCPU(int n, int k) { // adds two vectors of size n, k times, returns total duration std::clock_t start; double duration; @@ -26,7 +26,6 @@ double time_add_vectors(int n, int k) { return duration; } - int main() { // get all platforms (drivers), e.g. NVIDIA std::vector all_platforms; @@ -58,7 +57,7 @@ int main() { std::string kernel_code= // 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, " + " void kernel looped_add(global const int* v1, global const int* v2, global int* v3, " " global const int* constants) {" " int ID, Nthreads, n, k, ratio, start, stop;" " ID = get_global_id(0);" @@ -73,9 +72,23 @@ int main() { " 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; + + time_ratio = (CPUtime / GPUtime2); + std::cout << "\nVERSION 2 -----------" << std::endl; + std::cout << "CPU time: " << CPUtime << std::endl; + std::cout << "GPU time: " << GPUtime2 << std::endl; + std::cout << "GPU is "; if (time_ratio > 1) std::cout << time_ratio << " times faster!" << std::endl; else