diff --git a/.gitignore b/.gitignore index f99e19e..b4e66d2 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,4 @@ # THIS IS MY SWAP # (vim swap files) -*.swp -*.swo +.*.s?? diff --git a/README.md b/README.md index d9615f3..a16c5d0 100644 --- a/README.md +++ b/README.md @@ -7,12 +7,18 @@ This code uses OpenCL 1.1 on a NVIDIA GPU. ### Linux (Only tested on Ubuntu). For NVIDIA GPUs, I've installed the following packages: `nvidia-346 nvidia-346-dev nvidia-346-uvm nvidia-libopencl1-346 nvidia-modprobe nvidia-opencl-icd-346 nvidia-settings`. Since the `opencl-headers` package in the main repository is for OpenCL 1.2, you can get the OpenCL 1.1 header files from [here](http://packages.ubuntu.com/precise/opencl-headers). -Then to compile: +Then to compile the C++ code: ``` g++ -std=c++0x main.cpp -o main.out -lOpenCL ``` +To compile the C code: + +``` +gcc main.c -o main.out -lOpenCL +``` + ### OS X OpenCL is installed on OS X by default, but since this code uses the C++ bindings, you'll need to get that too. Get the [official C++ bindings from the OpenCL registr](https://www.khronos.org/registry/cl/api/1.1/cl.hpp) and copy it to the OpenCL framework directory, or do the following: diff --git a/example02/main.c b/example02/main.c new file mode 100644 index 0000000..6380748 --- /dev/null +++ b/example02/main.c @@ -0,0 +1,94 @@ +#include +#include +#include + +const char *kernel_code = + "__kernel void multiply_by(__global int* A, const int c) {" + " A[get_global_id(0)] = c * A[get_global_id(0)];" + "}"; + + +int factorial(int n) { + return (n <= 1) ? 1 : n * factorial(n-1); +} + + +int main( void ) { + // OpenCL related declarations + cl_int err; + cl_platform_id platform; + cl_device_id device; + cl_context_properties props[3] = { CL_CONTEXT_PLATFORM, 0, 0 }; + cl_context ctx; + cl_program program; + cl_command_queue queue; + cl_event event = NULL; + cl_kernel k_multiplyby; + int i; + + // + const size_t N = 1024; // vector size + const int c_max = 5; // max value to iterate to + const int coeff = factorial(c_max); + + int *A, *B, *C; // A is initial, B is result, C is expected result + A = (int*) malloc(N * sizeof(*A)); + B = (int*) malloc(N * sizeof(*B)); + C = (int*) malloc(N * sizeof(*C)); + for (i=0; i