Skip to content

Commit

Permalink
Merge pull request #172 from ddemidov/compute-backend
Browse files Browse the repository at this point in the history
Boost.Compute backend
  • Loading branch information
ddemidov committed Apr 5, 2015
2 parents f6058d0 + 362c9f2 commit bda0961
Show file tree
Hide file tree
Showing 40 changed files with 1,173 additions and 115 deletions.
19 changes: 15 additions & 4 deletions CMakeLists.txt
Expand Up @@ -30,9 +30,13 @@ option(BOOST_TEST_DYN_LINK "Link tests against dynamic version of boost unittest
# Interoperation with Boost.compute
#----------------------------------------------------------------------------
option(BOOST_COMPUTE "Use Boost.Compute algorithms" OFF)
if (BOOST_COMPUTE)
function(find_boost_compute)
find_path(BOOST_COMPUTE_INCLUDE boost/compute.hpp)
include_directories(${BOOST_COMPUTE_INCLUDE})
endfunction()

if (BOOST_COMPUTE)
find_boost_compute()
add_definitions(-DHAVE_BOOST_COMPUTE)
endif ()

Expand Down Expand Up @@ -85,7 +89,7 @@ if (WIN32)
endif ()

set(VEXCL_BACKEND "OpenCL" CACHE STRING "Select VexCL backend (OpenCL/CUDA)")
set_property(CACHE VEXCL_BACKEND PROPERTY STRINGS "OpenCL" "CUDA")
set_property(CACHE VEXCL_BACKEND PROPERTY STRINGS "OpenCL" "CUDA" "Compute")

#----------------------------------------------------------------------------
# Find Backend
Expand All @@ -96,6 +100,13 @@ if ("${VEXCL_BACKEND}" STREQUAL "OpenCL")
include_directories( ${OPENCL_INCLUDE_DIRS} )
set(BACKEND_LIBS ${OPENCL_LIBRARIES})
add_definitions(-DVEXCL_BACKEND_OPENCL)
elseif ("${VEXCL_BACKEND}" STREQUAL "Compute")
find_boost_compute()
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
find_package(OpenCL REQUIRED)
include_directories( ${OPENCL_INCLUDE_DIRS} )
set(BACKEND_LIBS ${OPENCL_LIBRARIES})
add_definitions(-DVEXCL_BACKEND_COMPUTE)
elseif ("${VEXCL_BACKEND}" STREQUAL "CUDA")
find_package(CUDA REQUIRED)
include_directories( ${CUDA_INCLUDE_DIRS} )
Expand All @@ -116,11 +127,11 @@ endif ()
# Enable C++11 support, set compilation flags
#----------------------------------------------------------------------------
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -Wall -Wclobbered -Wempty-body -Wignored-qualifiers -Wmissing-field-initializers -Wsign-compare -Wtype-limits -Wuninitialized -Wunused-parameter -Wunused-but-set-parameter -Wno-comment -Wno-type-limits -Wno-strict-aliasing")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -Wall -Wclobbered -Wempty-body -Wmissing-field-initializers -Wsign-compare -Wtype-limits -Wuninitialized -Wunused-but-set-parameter -Wno-comment -Wno-type-limits -Wno-strict-aliasing -Wno-missing-braces")
endif ()

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -Wall -Wempty-body -Wignored-qualifiers -Wmissing-field-initializers -Wsign-compare -Wtype-limits -Wuninitialized -Wunused-parameter -Wno-comment -Wno-tautological-compare")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -Wall -Wempty-body -Wmissing-field-initializers -Wsign-compare -Wtype-limits -Wuninitialized -Wno-comment -Wno-tautological-compare -Wno-missing-braces")

option(USE_LIBCPP "Use libc++ with Clang" OFF)
if (USE_LIBCPP)
Expand Down
28 changes: 20 additions & 8 deletions README.md
Expand Up @@ -64,13 +64,26 @@ Other talks may be found at

## <a name="selecting-backend"></a>Selecting backend

VexCL provides two backends: OpenCL and CUDA. In order to choose either of those,
user has to define `VEXCL_BACKEND_OPENCL` or `VEXCL_BACKEND_CUDA` macros. In
case neither of those are defined, OpenCL backend is chosen by default. One
also has to link to either libOpenCL.so (OpenCL.dll) or libcuda.so (cuda.dll).

For the CUDA backend to work, CUDA Toolkit has to be installed, NVIDIA CUDA
compiler driver `nvcc` has to be in executable PATH and usable at runtime.
VexCL provides the following backends:

* **OpenCL**, built on top of [Khronos C++ API][]. The backend is selected when
`VEXCL_BACKEND_OPENCL` macro is defined, or by default. Link with
`libOpenCL.so` on unix-like systems or with `OpenCL.dll` on Windows.
* **Boost.compute**. The backend is also based on OpenCL, but uses core
functionality of the [Boost.compute][] library instead of somewhat outdated
Khronos C++ API. The additional advantage is the increased interoperability
between VexCL and Boost.Compute. The backend is selected when
`VEXCL_BACKEND_COMPUTE` macro is defined. Link with
`libOpenCL.so`/`OpenCL.dll` and make sure that Boost.Compute headers are in
the include path.
* **CUDA**, uses NVIDIA CUDA technology. The backend is selected when
`VEXCL_BACKEND_CUDA` macro is defined. Link with `libcuda.so`/`cuda.dll`.
For the CUDA backend to work, CUDA Toolkit has to be installed, and NVIDIA
CUDA compiler driver `nvcc` has to be in executable PATH and usable at
runtime.

[Khronos C++ API]: https://www.khronos.org/registry/cl
[Boost.compute]: https://github.com/kylelutz/compute

## <a name="context-initialization"></a>Context initialization

Expand Down Expand Up @@ -1176,7 +1189,6 @@ easily interoperable with other OpenCL libraries. In particular, VexCL provides
some glue code for the [ViennaCL][], [Boost.compute][] and [CLOGS][] libraries.

[ViennaCL]: http://viennacl.sourceforge.net/
[Boost.compute]: https://github.com/kylelutz/compute
[CLOGS]: http://clogs.sourceforge.net/
[examples/viennacl/solvers.cpp]: https://github.com/ddemidov/vexcl/blob/master/examples/viennacl/solvers.cpp
[vexcl/external/boost_compute.hpp]: https://github.com/ddemidov/vexcl/blob/master/vexcl/external/boost_compute.hpp
Expand Down
9 changes: 7 additions & 2 deletions examples/devlist.cpp
Expand Up @@ -11,7 +11,7 @@ int main() {

auto dev = vex::backend::device_list(vex::Filter::Any);

#ifdef VEXCL_BACKEND_OPENCL
#if defined(VEXCL_BACKEND_OPENCL)
cout << "OpenCL devices:" << endl << endl;
for (auto d = dev.begin(); d != dev.end(); d++) {
cout << " " << d->getInfo<CL_DEVICE_NAME>() << endl
Expand Down Expand Up @@ -58,11 +58,16 @@ int main() {
}
cout << endl << endl;
}
#elif VEXCL_BACKEND_CUDA
#elif defined(VEXCL_BACKEND_CUDA)
cout << "CUDA devices:" << endl << endl;
unsigned pos = 0;
for(auto d = dev.begin(); d != dev.end(); d++)
cout << ++pos << ". " << *d << endl;
#elif defined(VEXCL_BACKEND_COMPUTE)
cout << "Compute devices:" << endl << endl;
unsigned pos = 0;
for(auto d = dev.begin(); d != dev.end(); d++)
cout << ++pos << ". " << d->name() << " (" << d->platform().name() << ")" << endl;
#else
#error Unsupported backend
#endif
Expand Down
4 changes: 2 additions & 2 deletions examples/fft_benchmark.cpp
Expand Up @@ -105,10 +105,10 @@ watch test_clfft(Context &ctx, cl_float2 *data, size_t n, size_t m, size_t runs,

watch w;
for(size_t i = 0 ; i < runs ; i++) {
ctx.queue()[0].finish();
ctx.finish();
w.tic();
b = fft(a);
ctx.queue()[0].finish();
ctx.finish();
w.toc();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/cast.cpp
Expand Up @@ -15,7 +15,7 @@ BOOST_AUTO_TEST_CASE(casted_expession)
check_sample(x, [](size_t, double a) { BOOST_CHECK_EQUAL(a, 5); });
}

#ifdef VEXCL_BACKEND_OPENCL
#ifndef VEXCL_BACKEND_CUDA
BOOST_AUTO_TEST_CASE(convert_functions)
{
const size_t N = 1024;
Expand Down
6 changes: 3 additions & 3 deletions tests/context.cpp
Expand Up @@ -4,10 +4,10 @@
#include <vexcl/vector.hpp>

void local_context() {
#ifdef VEXCL_BACKEND_OPENCL
vex::Context ctx( vex::Filter::Exclusive( vex::Filter::Env ) );
#else
#ifdef VEXCL_BACKEND_CUDA
vex::Context ctx( vex::Filter::Env );
#else
vex::Context ctx( vex::Filter::Exclusive( vex::Filter::Env ) );
#endif
std::cout << ctx << std::endl;

Expand Down
2 changes: 1 addition & 1 deletion tests/fft.cpp
Expand Up @@ -104,7 +104,7 @@ size_t random_dim(double p, double s) {

BOOST_AUTO_TEST_CASE(test_dimensions)
{
#ifdef VEXCL_BACKEND_OPENCL
#ifndef VEXCL_BACKEND_CUDA
// TODO: POCL fails this test.
if (vex::Filter::Platform("Portable Computing Language")(ctx.device(0)))
return;
Expand Down
2 changes: 1 addition & 1 deletion tests/spmv.cpp
Expand Up @@ -436,7 +436,7 @@ BOOST_AUTO_TEST_CASE(ccsr_multivector_product)
});
}

#ifdef VEXCL_BACKEND_OPENCL
#ifndef VEXCL_BACKEND_CUDA
BOOST_AUTO_TEST_CASE(vector_valued_matrix)
{
const size_t n = 1024;
Expand Down
18 changes: 3 additions & 15 deletions tests/vector_arithmetics.cpp
Expand Up @@ -9,7 +9,7 @@
#include <vexcl/element_index.hpp>
#include <vexcl/tagged_terminal.hpp>
#include <vexcl/function.hpp>
#ifdef VEXCL_BACKEND_OPENCL
#ifndef VEXCL_BACKEND_CUDA
#include <vexcl/vector_view.hpp>
#include <vexcl/constant_address_space.hpp>
#endif
Expand Down Expand Up @@ -77,18 +77,6 @@ BOOST_AUTO_TEST_CASE(reduce_expression)
BOOST_CHECK_EQUAL( max( fabs(X - X) ), 0.0);
}

BOOST_AUTO_TEST_CASE(static_reductor)
{
const size_t N = 1024;

vex::vector<double> X(ctx, random_vector<double>(N));

const vex::Reductor<double,vex::SUM> sum(ctx);
const vex::Reductor<double,vex::SUM> &static_sum = vex::get_reductor<double, vex::SUM>(ctx);

BOOST_CHECK_CLOSE(sum(X), static_sum(X), 1e-6);
}

BOOST_AUTO_TEST_CASE(builtin_functions)
{
const size_t N = 1024;
Expand Down Expand Up @@ -146,7 +134,7 @@ BOOST_AUTO_TEST_CASE(element_index)
check_sample(x, [](size_t idx, double a) { BOOST_CHECK_CLOSE(a, sin(0.5 * idx), 1e-6); });
}

#ifdef VEXCL_BACKEND_OPENCL
#ifndef VEXCL_BACKEND_CUDA
BOOST_AUTO_TEST_CASE(vector_values)
{
const size_t N = 1024;
Expand Down Expand Up @@ -287,7 +275,7 @@ BOOST_AUTO_TEST_CASE(constants)
check_sample(x, [](size_t, double v) { BOOST_CHECK_CLOSE(v, boost::math::constants::pi<double>(), 1e-8); });
}

#ifdef VEXCL_BACKEND_OPENCL
#ifndef VEXCL_BACKEND_CUDA
BOOST_AUTO_TEST_CASE(constant_address_space)
{
std::vector<vex::command_queue> queue(1, ctx.queue(0));
Expand Down
2 changes: 1 addition & 1 deletion tests/vector_pointer.cpp
Expand Up @@ -80,7 +80,7 @@ BOOST_AUTO_TEST_CASE(manual_stencil)
});
}

#ifdef VEXCL_BACKEND_OPENCL
#ifndef VEXCL_BACKEND_CUDA
BOOST_AUTO_TEST_CASE(constant_pointer)
{
std::vector<vex::command_queue> queue(1, ctx.queue(0));
Expand Down
17 changes: 16 additions & 1 deletion vexcl/backend.hpp
Expand Up @@ -47,7 +47,22 @@ namespace vex {

#include <vexcl/backend/cuda.hpp>

#else // defined(VEXCL_BACKEND_OPENCL)
#elif defined(VEXCL_BACKEND_COMPUTE)

namespace vex {
namespace backend {
namespace compute {}
using namespace compute;
}
}

#ifdef VEXCL_CACHE_KERNELS
# define BOOST_COMPUTE_USE_OFFLINE_CACHE
#endif

#include <vexcl/backend/compute.hpp>

#else // either defined(VEXCL_BACKEND_OPENCL) or by default

namespace vex {
namespace backend {
Expand Down
59 changes: 59 additions & 0 deletions vexcl/backend/compute.hpp
@@ -0,0 +1,59 @@
#ifndef VEXCL_BACKEND_COMPUTE_HPP
#define VEXCL_BACKEND_COMPUTE_HPP

/*
The MIT License
Copyright (c) 2012-2015 Denis Demidov <dennis.demidov@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

/**
* \file vexcl/backend/compute.hpp
* \author Denis Demidov <dennis.demidov@gmail.com>
* \brief OpenCL backend based on Boost.Compute core API.
*/

#ifndef VEXCL_BACKEND_COMPUTE
# define VEXCL_BACKEND_COMPUTE
#endif

#include <vexcl/backend/compute/error.hpp>
#include <vexcl/backend/compute/context.hpp>
#include <vexcl/backend/compute/filter.hpp>
#include <vexcl/backend/compute/device_vector.hpp>

// Since Boost.Compute is based on OpenCL,
// we can reuse source generator from the OpenCL backend.
#include <vexcl/backend/opencl/source.hpp>

namespace vex {
namespace backend {
namespace compute {
using vex::backend::opencl::standard_kernel_header;
using vex::backend::opencl::source_generator;
}
}
}

#include <vexcl/backend/compute/compiler.hpp>
#include <vexcl/backend/compute/kernel.hpp>

#endif
74 changes: 74 additions & 0 deletions vexcl/backend/compute/compiler.hpp
@@ -0,0 +1,74 @@
#ifndef VEXCL_BACKEND_COMPUTE_COMPILER_HPP
#define VEXCL_BACKEND_COMPUTE_COMPILER_HPP

/*
The MIT License
Copyright (c) 2012-2015 Denis Demidov <dennis.demidov@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

/**
* \file vexcl/backend/compute/compiler.hpp
* \author Denis Demidov <dennis.demidov@gmail.com>
* \brief OpenCL source code compilation wrapper for Boost.Compute backend.
*/

#include <cstdlib>

#include <boost/thread.hpp>
#include <boost/compute/core.hpp>

#include <vexcl/backend/common.hpp>
#include <vexcl/detail/backtrace.hpp>

namespace vex {
namespace backend {
namespace compute {

/// Create and build a program from source string.
/**
* If VEXCL_CACHE_KERNELS macro is defined, then program binaries are cached
* in filesystem and reused in the following runs.
*/
inline boost::compute::program build_sources(
const boost::compute::command_queue &queue,
const std::string &source,
const std::string &options = ""
)
{
#ifdef VEXCL_SHOW_KERNELS
std::cout << source << std::endl;
#else
if (boost::compute::detail::getenv("VEXCL_SHOW_KERNELS"))
std::cout << source << std::endl;
#endif

return boost::compute::program::build_with_source(
source, queue.get_context(),
options + " " + get_compile_options(queue)
);
}

} // namespace compute
} // namespace backend
} // namespace vex

#endif

0 comments on commit bda0961

Please sign in to comment.