Skip to content

Latest commit

 

History

History
99 lines (77 loc) · 2.89 KB

README.md

File metadata and controls

99 lines (77 loc) · 2.89 KB

clew

The OpenCL Extension Wrangler Library

This basically works like glew, but for OpenCL

  • you can build opencl code without needing any opencl library or include files!
  • at runtime, even if there is no opencl-enabled device present, your code will still run! Of course, you wont be able to do anything opencl-related, but you wont get any errors about missing dlls and stuff, no linker errors (at least, not until you try to use a non-existent opencl-enabled device of course)

Code history

  • The code was originally part of a larger project, then was factorized out into a standalone github project

To use in your code

Include it

#include "clew.h"

Initialize it

bool clpresent = 0 == clewInit();
if( !clpresent ) {
    throw std::runtime_error("OpenCL library not found");
}

Use standard OpenCL method calls, as though you were linking directly with OpenCL:

context = new cl_context();
*context = clCreateContext(0, 1, &device, NULL, NULL, &error);
if (error != CL_SUCCESS) {
   throw std::runtime_error( "Error creating context: " + errorMessage(error) );
}
// Command-queue
queue = new cl_command_queue;
*queue = clCreateCommandQueue(*context, device, 0, &error);
if (error != CL_SUCCESS) {
   throw std::runtime_error( "Error creating command queue: " + errorMessage(error) );
}
// etc ...
  • link with libclew.so/.a/.dll/.lib (as appropriate, according to platform and build options)

To build

On linux

You'll need:

  • cmake
  • gcc, g++

Procedure:

mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=../dist
make -j 4 install

To test it works ok. You'll need at least one OpenCL-enabled device to do this bit:

LD_LIBRARY_PATH=../dist/lib ../dist/bin/clewTest

=> should see something like 'num platforms: 1'

On Windows

You'll need:

  • cmake
  • Visual Studio 2010 (actually, can probably use any version, but this was tested with this version)

Procedure:

  • first do the cmake part:
    • open cmake, set the source directory to the location of this source code
    • set the build directory to new subdirectory 'build'
    • click 'configure'
    • choose a generator, eg visual studio 2010
    • change the CMAKE_INSTALL_PREFIX to new sub-directory 'dist'
    • click 'configure' then 'generate'
  • now open visual studio, and open the solution from the build directory
  • change build type from 'Debug' to 'Release'
  • build the solution
  • build the project 'INSTALL'

To test it works ok. You'll need at least one OpenCL-enabled device to do this bit:

  • open a cmd
  • change into the 'dist' directory you created
  • type 'clewTest' => should see something like 'num platforms: 1'

Build options

  • BUILD_TESTS: build test executable, default ON
  • BUILD_SHARED_LIBRARY: build dll/so, instead of .lib/.a. default OFF
  • INSTALL_CL_HEADER: creates include files directory proxy-opencl, which you can give eg to clBLAS during build, and it will build ok, without needing opencl headers installed