Skip to content

Commit aad0348

Browse files
committed
Add interop support
This adds interoperability support between Boost.Compute and various other C/C++ libraries (Eigen, OpenCV, OpenGL, Qt and VTK). This eases development for users using external libraries with Boost.Compute.
1 parent b47e74d commit aad0348

38 files changed

Lines changed: 2143 additions & 83 deletions

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ include_directories(${OPENCL_INCLUDE_DIRS})
1212
find_package(Boost 1.48 REQUIRED)
1313
include_directories(${Boost_INCLUDE_DIRS})
1414

15+
# optional third-party libraries
16+
option(BOOST_COMPUTE_HAVE_EIGEN "Have Eigen" OFF)
17+
option(BOOST_COMPUTE_HAVE_OPENCV "Have OpenCV" OFF)
18+
option(BOOST_COMPUTE_HAVE_QT "Have Qt" OFF)
19+
option(BOOST_COMPUTE_HAVE_VTK "Have VTK" OFF)
20+
1521
include_directories(include)
1622

1723
if(${OpenCL_HEADER_CL_EXT_FOUND})

cmake/FindEigen.cmake

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# Ceres Solver - A fast non-linear least squares minimizer
2+
# Copyright 2013 Google Inc. All rights reserved.
3+
# http://code.google.com/p/ceres-solver/
4+
#
5+
# Redistribution and use in source and binary forms, with or without
6+
# modification, are permitted provided that the following conditions are met:
7+
#
8+
# * Redistributions of source code must retain the above copyright notice,
9+
# this list of conditions and the following disclaimer.
10+
# * Redistributions in binary form must reproduce the above copyright notice,
11+
# this list of conditions and the following disclaimer in the documentation
12+
# and/or other materials provided with the distribution.
13+
# * Neither the name of Google Inc. nor the names of its contributors may be
14+
# used to endorse or promote products derived from this software without
15+
# specific prior written permission.
16+
#
17+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27+
# POSSIBILITY OF SUCH DAMAGE.
28+
#
29+
# Author: alexs.mac@gmail.com (Alex Stewart)
30+
#
31+
32+
# FindEigen.cmake - Find Eigen library, version >= 3.
33+
#
34+
# This module defines the following variables:
35+
#
36+
# EIGEN_FOUND: TRUE iff Eigen is found.
37+
# EIGEN_INCLUDE_DIRS: Include directories for Eigen.
38+
#
39+
# EIGEN_VERSION: Extracted from Eigen/src/Core/util/Macros.h
40+
# EIGEN_WORLD_VERSION: Equal to 3 if EIGEN_VERSION = 3.2.0
41+
# EIGEN_MAJOR_VERSION: Equal to 2 if EIGEN_VERSION = 3.2.0
42+
# EIGEN_MINOR_VERSION: Equal to 0 if EIGEN_VERSION = 3.2.0
43+
#
44+
# The following variables control the behaviour of this module:
45+
#
46+
# EIGEN_INCLUDE_DIR_HINTS: List of additional directories in which to
47+
# search for eigen includes, e.g: /timbuktu/eigen3.
48+
#
49+
# The following variables are also defined by this module, but in line with
50+
# CMake recommended FindPackage() module style should NOT be referenced directly
51+
# by callers (use the plural variables detailed above instead). These variables
52+
# do however affect the behaviour of the module via FIND_[PATH/LIBRARY]() which
53+
# are NOT re-called (i.e. search for library is not repeated) if these variables
54+
# are set with valid values _in the CMake cache_. This means that if these
55+
# variables are set directly in the cache, either by the user in the CMake GUI,
56+
# or by the user passing -DVAR=VALUE directives to CMake when called (which
57+
# explicitly defines a cache variable), then they will be used verbatim,
58+
# bypassing the HINTS variables and other hard-coded search locations.
59+
#
60+
# EIGEN_INCLUDE_DIR: Include directory for CXSparse, not including the
61+
# include directory of any dependencies.
62+
63+
# Called if we failed to find Eigen or any of it's required dependencies,
64+
# unsets all public (designed to be used externally) variables and reports
65+
# error message at priority depending upon [REQUIRED/QUIET/<NONE>] argument.
66+
MACRO(EIGEN_REPORT_NOT_FOUND REASON_MSG)
67+
UNSET(EIGEN_FOUND)
68+
UNSET(EIGEN_INCLUDE_DIRS)
69+
# Make results of search visible in the CMake GUI if Eigen has not
70+
# been found so that user does not have to toggle to advanced view.
71+
MARK_AS_ADVANCED(CLEAR EIGEN_INCLUDE_DIR)
72+
# Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by FindPackage()
73+
# use the camelcase library name, not uppercase.
74+
IF (Eigen_FIND_QUIETLY)
75+
MESSAGE(STATUS "Failed to find Eigen - " ${REASON_MSG} ${ARGN})
76+
ELSEIF (Eigen_FIND_REQUIRED)
77+
MESSAGE(FATAL_ERROR "Failed to find Eigen - " ${REASON_MSG} ${ARGN})
78+
ELSE()
79+
# Neither QUIETLY nor REQUIRED, use no priority which emits a message
80+
# but continues configuration and allows generation.
81+
MESSAGE("-- Failed to find Eigen - " ${REASON_MSG} ${ARGN})
82+
ENDIF ()
83+
ENDMACRO(EIGEN_REPORT_NOT_FOUND)
84+
85+
# Search user-installed locations first, so that we prefer user installs
86+
# to system installs where both exist.
87+
#
88+
# TODO: Add standard Windows search locations for Eigen.
89+
LIST(APPEND EIGEN_CHECK_INCLUDE_DIRS
90+
/usr/local/include/eigen3
91+
/usr/local/homebrew/include/eigen3 # Mac OS X
92+
/opt/local/var/macports/software/eigen3 # Mac OS X.
93+
/opt/local/include/eigen3
94+
/usr/include/eigen3)
95+
96+
# Search supplied hint directories first if supplied.
97+
FIND_PATH(EIGEN_INCLUDE_DIR
98+
NAMES Eigen/Core
99+
PATHS ${EIGEN_INCLUDE_DIR_HINTS}
100+
${EIGEN_CHECK_INCLUDE_DIRS})
101+
IF (NOT EIGEN_INCLUDE_DIR OR
102+
NOT EXISTS ${EIGEN_INCLUDE_DIR})
103+
EIGEN_REPORT_NOT_FOUND(
104+
"Could not find eigen3 include directory, set EIGEN_INCLUDE_DIR to "
105+
"path to eigen3 include directory, e.g. /usr/local/include/eigen3.")
106+
ENDIF (NOT EIGEN_INCLUDE_DIR OR
107+
NOT EXISTS ${EIGEN_INCLUDE_DIR})
108+
109+
# Mark internally as found, then verify. EIGEN_REPORT_NOT_FOUND() unsets
110+
# if called.
111+
SET(EIGEN_FOUND TRUE)
112+
113+
# Extract Eigen version from Eigen/src/Core/util/Macros.h
114+
IF (EIGEN_INCLUDE_DIR)
115+
SET(EIGEN_VERSION_FILE ${EIGEN_INCLUDE_DIR}/Eigen/src/Core/util/Macros.h)
116+
IF (NOT EXISTS ${EIGEN_VERSION_FILE})
117+
EIGEN_REPORT_NOT_FOUND(
118+
"Could not find file: ${EIGEN_VERSION_FILE} "
119+
"containing version information in Eigen install located at: "
120+
"${EIGEN_INCLUDE_DIR}.")
121+
ELSE (NOT EXISTS ${EIGEN_VERSION_FILE})
122+
FILE(READ ${EIGEN_VERSION_FILE} EIGEN_VERSION_FILE_CONTENTS)
123+
124+
STRING(REGEX MATCH "#define EIGEN_WORLD_VERSION [0-9]+"
125+
EIGEN_WORLD_VERSION "${EIGEN_VERSION_FILE_CONTENTS}")
126+
STRING(REGEX REPLACE "#define EIGEN_WORLD_VERSION ([0-9]+)" "\\1"
127+
EIGEN_WORLD_VERSION "${EIGEN_WORLD_VERSION}")
128+
129+
STRING(REGEX MATCH "#define EIGEN_MAJOR_VERSION [0-9]+"
130+
EIGEN_MAJOR_VERSION "${EIGEN_VERSION_FILE_CONTENTS}")
131+
STRING(REGEX REPLACE "#define EIGEN_MAJOR_VERSION ([0-9]+)" "\\1"
132+
EIGEN_MAJOR_VERSION "${EIGEN_MAJOR_VERSION}")
133+
134+
STRING(REGEX MATCH "#define EIGEN_MINOR_VERSION [0-9]+"
135+
EIGEN_MINOR_VERSION "${EIGEN_VERSION_FILE_CONTENTS}")
136+
STRING(REGEX REPLACE "#define EIGEN_MINOR_VERSION ([0-9]+)" "\\1"
137+
EIGEN_MINOR_VERSION "${EIGEN_MINOR_VERSION}")
138+
139+
# This is on a single line s/t CMake does not interpret it as a list of
140+
# elements and insert ';' separators which would result in 3.;2.;0 nonsense.
141+
SET(EIGEN_VERSION "${EIGEN_WORLD_VERSION}.${EIGEN_MAJOR_VERSION}.${EIGEN_MINOR_VERSION}")
142+
ENDIF (NOT EXISTS ${EIGEN_VERSION_FILE})
143+
ENDIF (EIGEN_INCLUDE_DIR)
144+
145+
# Set standard CMake FindPackage variables if found.
146+
IF (EIGEN_FOUND)
147+
SET(EIGEN_INCLUDE_DIRS ${EIGEN_INCLUDE_DIR})
148+
ENDIF (EIGEN_FOUND)
149+
150+
# Handle REQUIRED / QUIET optional arguments and version.
151+
INCLUDE(FindPackageHandleStandardArgs)
152+
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Eigen
153+
REQUIRED_VARS EIGEN_INCLUDE_DIRS
154+
VERSION_VAR EIGEN_VERSION)
155+
156+
# Only mark internal variables as advanced if we found Eigen, otherwise
157+
# leave it visible in the standard GUI for the user to set manually.
158+
IF (EIGEN_FOUND)
159+
MARK_AS_ADVANCED(FORCE EIGEN_INCLUDE_DIR)
160+
ENDIF (EIGEN_FOUND)

example/CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,27 @@ foreach(EXAMPLE ${EXAMPLES})
2525
add_executable(${EXAMPLE} ${EXAMPLE}.cpp)
2626
target_link_libraries(${EXAMPLE} ${OPENCL_LIBRARIES} ${Boost_LIBRARIES})
2727
endforeach()
28+
29+
# opencv examples
30+
if(${BOOST_COMPUTE_HAVE_OPENCV})
31+
find_package(OpenCV REQUIRED)
32+
include_directories(${OpenCV_INCLUDE_DIRS})
33+
add_executable(opencv_filter opencv_filter.cpp)
34+
target_link_libraries(opencv_filter ${OPENCL_LIBRARIES} ${OpenCV_LIBRARIES})
35+
endif()
36+
37+
# opengl/vtk examples
38+
if(${BOOST_COMPUTE_HAVE_VTK})
39+
find_package(VTK REQUIRED)
40+
include(${VTK_USE_FILE})
41+
add_executable(opengl_sphere opengl_sphere.cpp)
42+
target_link_libraries(opengl_sphere ${OPENCL_LIBRARIES} ${VTK_LIBRARIES} GL)
43+
endif()
44+
45+
# qt examples
46+
if(${BOOST_COMPUTE_HAVE_QT})
47+
find_package(Qt REQUIRED COMPONENTS QtCore QtGui)
48+
include(${QT_USE_FILE})
49+
add_executable(qimage_blur qimage_blur.cpp)
50+
target_link_libraries(qimage_blur ${OPENCL_LIBRARIES} ${QT_LIBRARIES})
51+
endif()

example/opencv_filter.cpp

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
//---------------------------------------------------------------------------//
2+
// Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
3+
//
4+
// Distributed under the Boost Software License, Version 1.0
5+
// See accompanying file LICENSE_1_0.txt or copy at
6+
// http://www.boost.org/LICENSE_1_0.txt
7+
//
8+
// See http://kylelutz.github.com/compute for more information.
9+
//---------------------------------------------------------------------------//
10+
11+
#include <iostream>
12+
13+
#include <opencv2/core/core.hpp>
14+
#include <opencv2/highgui/highgui.hpp>
15+
#include <opencv2/imgproc/imgproc.hpp>
16+
17+
#include <boost/compute/source.hpp>
18+
#include <boost/compute/system.hpp>
19+
#include <boost/compute/interop/opencv/core.hpp>
20+
#include <boost/compute/interop/opencv/highgui.hpp>
21+
22+
namespace compute = boost::compute;
23+
24+
// this example shows how to read an image with OpenCV, transfer the
25+
// image to the GPU, and apply a simple flip filter written in OpenCL
26+
int main(int argc, char *argv[])
27+
{
28+
// check command line
29+
if(argc < 2){
30+
std::cerr << "usage: " << argv[0] << " FILENAME" << std::endl;
31+
return -1;
32+
}
33+
34+
// read image with opencv
35+
cv::Mat cv_image = cv::imread(argv[1], CV_LOAD_IMAGE_COLOR);
36+
if(!cv_image.data){
37+
std::cerr << "failed to load image" << std::endl;
38+
return -1;
39+
}
40+
41+
// get default device and setup context
42+
compute::device gpu = compute::system::default_device();
43+
compute::context context(gpu);
44+
compute::command_queue queue(context, gpu);
45+
46+
// convert image to BGRA (OpenCL requires 16-byte aligned data)
47+
cv::cvtColor(cv_image, cv_image, CV_BGR2BGRA);
48+
49+
// transfer image to gpu
50+
compute::image2d input_image =
51+
compute::opencv_create_image2d_with_mat(
52+
cv_image, compute::image2d::read_write, queue
53+
);
54+
55+
// create output image
56+
compute::image2d output_image(
57+
context,
58+
compute::image2d::write_only,
59+
input_image.get_format(),
60+
input_image.width(),
61+
input_image.height()
62+
);
63+
64+
// create flip program
65+
const char source[] = BOOST_COMPUTE_STRINGIZE_SOURCE(
66+
__kernel void flip_kernel(__read_only image2d_t input,
67+
__write_only image2d_t output)
68+
{
69+
const sampler_t sampler = CLK_ADDRESS_NONE | CLK_FILTER_NEAREST;
70+
int height = get_image_height(input);
71+
int2 input_coord = { get_global_id(0), get_global_id(1) };
72+
int2 output_coord = { input_coord.x, height - input_coord.y - 1 };
73+
float4 value = read_imagef(input, sampler, input_coord);
74+
write_imagef(output, output_coord, value);
75+
}
76+
);
77+
78+
compute::program flip_program =
79+
compute::program::create_with_source(source, context);
80+
flip_program.build();
81+
82+
// create flip kernel and set arguments
83+
compute::kernel flip_kernel(flip_program, "flip_kernel");
84+
flip_kernel.set_arg(0, input_image);
85+
flip_kernel.set_arg(1, output_image);
86+
87+
// run flip kernel
88+
size_t origin[2] = { 0, 0 };
89+
size_t region[2] = { input_image.width(), input_image.height() };
90+
queue.enqueue_nd_range_kernel(flip_kernel, 2, origin, region, 0);
91+
92+
// show host image
93+
cv::imshow("opencv image", cv_image);
94+
95+
// show gpu image
96+
compute::opencv_imshow("filtered image", output_image, queue);
97+
98+
// wait and return
99+
cv::waitKey(0);
100+
return 0;
101+
}

0 commit comments

Comments
 (0)