[compute] 31/49: Add invoke() utility function
Ghislain Vaillant
ghisvail-guest at moszumanska.debian.org
Fri Dec 18 17:58:19 UTC 2015
This is an automated email from the git hooks/post-receive script.
ghisvail-guest pushed a commit to branch master
in repository compute.
commit ef109b75d58537235361de1281afa51552f2e010
Author: Kyle Lutz <kyle.r.lutz at gmail.com>
Date: Wed Aug 19 07:56:16 2015 -0700
Add invoke() utility function
This adds an invoke() function which calls an OpenCL function on a
device with the given arguments.
---
include/boost/compute/utility.hpp | 1 +
include/boost/compute/utility/invoke.hpp | 71 ++++++++++++++++++++++++++++++++
test/CMakeLists.txt | 1 +
test/test_invoke.cpp | 59 ++++++++++++++++++++++++++
4 files changed, 132 insertions(+)
diff --git a/include/boost/compute/utility.hpp b/include/boost/compute/utility.hpp
index aa7f21b..e6d1f6e 100644
--- a/include/boost/compute/utility.hpp
+++ b/include/boost/compute/utility.hpp
@@ -13,6 +13,7 @@
#include <boost/compute/utility/dim.hpp>
#include <boost/compute/utility/extents.hpp>
+#include <boost/compute/utility/invoke.hpp>
#include <boost/compute/utility/program_cache.hpp>
#include <boost/compute/utility/source.hpp>
#include <boost/compute/utility/wait_list.hpp>
diff --git a/include/boost/compute/utility/invoke.hpp b/include/boost/compute/utility/invoke.hpp
new file mode 100644
index 0000000..b03162a
--- /dev/null
+++ b/include/boost/compute/utility/invoke.hpp
@@ -0,0 +1,71 @@
+//---------------------------------------------------------------------------//
+// Copyright (c) 2013-2015 Kyle Lutz <kyle.r.lutz at gmail.com>
+//
+// Distributed under the Boost Software License, Version 1.0
+// See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt
+//
+// See http://kylelutz.github.com/compute for more information.
+//---------------------------------------------------------------------------//
+
+#ifndef BOOST_COMPUTE_UTILITY_INVOKE_HPP
+#define BOOST_COMPUTE_UTILITY_INVOKE_HPP
+
+#include <boost/preprocessor/enum.hpp>
+#include <boost/preprocessor/repetition.hpp>
+
+#include <boost/compute/config.hpp>
+#include <boost/compute/command_queue.hpp>
+#include <boost/compute/detail/meta_kernel.hpp>
+#include <boost/compute/container/detail/scalar.hpp>
+#include <boost/compute/type_traits/result_of.hpp>
+
+namespace boost {
+namespace compute {
+
+#define BOOST_COMPUTE_DETAIL_INVOKE_ARG(z, n, unused) \
+ BOOST_PP_COMMA_IF(n) k.var<BOOST_PP_CAT(T, n)>("arg" BOOST_PP_STRINGIZE(n))
+
+#define BOOST_COMPUTE_DETAIL_INVOKE_ADD_ARG(z, n, unused) \
+ k.add_set_arg("arg" BOOST_PP_STRINGIZE(n), BOOST_PP_CAT(arg, n));
+
+#define BOOST_COMPUTE_DETAIL_DEFINE_INVOKE(z, n, unused) \
+template<class Function, BOOST_PP_ENUM_PARAMS(n, class T)> \
+inline typename result_of<Function(BOOST_PP_ENUM_PARAMS(n, T))>::type \
+invoke(const Function& function, command_queue& queue, BOOST_PP_ENUM_BINARY_PARAMS(n, const T, &arg)) \
+{ \
+ typedef typename result_of<Function(BOOST_PP_ENUM_PARAMS(n, T))>::type result_type; \
+ detail::meta_kernel k("invoke"); \
+ detail::scalar<result_type> result(queue.get_context()); \
+ const size_t result_arg = k.add_arg<result_type *>(memory_object::global_memory, "result"); \
+ BOOST_PP_REPEAT(n, BOOST_COMPUTE_DETAIL_INVOKE_ADD_ARG, ~) \
+ k << "*result = " << function( \
+ BOOST_PP_REPEAT(n, BOOST_COMPUTE_DETAIL_INVOKE_ARG, ~) \
+ ) << ";"; \
+ k.set_arg(result_arg, result.get_buffer()); \
+ k.exec(queue); \
+ return result.read(queue); \
+}
+
+BOOST_PP_REPEAT_FROM_TO(1, BOOST_COMPUTE_MAX_ARITY, BOOST_COMPUTE_DETAIL_DEFINE_INVOKE, ~)
+
+#undef BOOST_COMPUTE_DETAIL_INVOKE_ARG
+#undef BOOST_COMPUTE_DETAIL_INVOKE_ADD_ARG
+#undef BOOST_COMPUTE_DETAIL_DEFINE_INVOKE
+
+#ifdef BOOST_COMPUTE_DOXYGEN_INVOKED
+/// Invokes \p function with \p args on \p queue.
+///
+/// For example, to invoke the builtin abs() function:
+/// \code
+/// int result = invoke(abs<int>(), queue, -10); // returns 10
+/// \endcode
+template<class Function, class... Args>
+inline typename result_of<Function(Args...)>::type
+invoke(const Function& function, command_queue& queue, const Args&... args);
+#endif // BOOST_COMPUTE_DOXYGEN_INVOKED
+
+} // end compute namespace
+} // end boost namespace
+
+#endif // BOOST_COMPUTE_UTILITY_INVOKE_HPP
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index ff2532c..14fc7a6 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -59,6 +59,7 @@ add_compute_test("core.type_traits" test_type_traits.cpp)
add_compute_test("core.user_event" test_user_event.cpp)
add_compute_test("utility.extents" test_extents.cpp)
+add_compute_test("utility.invoke" test_invoke.cpp)
add_compute_test("utility.program_cache" test_program_cache.cpp)
add_compute_test("utility.wait_list" test_wait_list.cpp)
diff --git a/test/test_invoke.cpp b/test/test_invoke.cpp
new file mode 100644
index 0000000..87f3f26
--- /dev/null
+++ b/test/test_invoke.cpp
@@ -0,0 +1,59 @@
+//---------------------------------------------------------------------------//
+// Copyright (c) 2013-2015 Kyle Lutz <kyle.r.lutz at gmail.com>
+//
+// Distributed under the Boost Software License, Version 1.0
+// See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt
+//
+// See http://kylelutz.github.com/compute for more information.
+//---------------------------------------------------------------------------//
+
+#define BOOST_TEST_MODULE TestInvoke
+#include <boost/test/unit_test.hpp>
+
+#include <boost/compute/function.hpp>
+#include <boost/compute/lambda.hpp>
+#include <boost/compute/functional/integer.hpp>
+#include <boost/compute/functional/math.hpp>
+#include <boost/compute/utility/invoke.hpp>
+
+#include "context_setup.hpp"
+
+namespace compute = boost::compute;
+
+BOOST_AUTO_TEST_CASE(invoke_builtin)
+{
+ BOOST_CHECK_EQUAL(compute::invoke(compute::abs<int>(), queue, -3), 3);
+ BOOST_CHECK_CLOSE(compute::invoke(compute::pow<float>(), queue, 2.f, 8.f), 256.f, 1e-4);
+}
+
+BOOST_AUTO_TEST_CASE(invoke_function)
+{
+ BOOST_COMPUTE_FUNCTION(int, plus_two, (int x),
+ {
+ return x + 2;
+ });
+
+ BOOST_CHECK_EQUAL(compute::invoke(plus_two, queue, 2), 4);
+
+ BOOST_COMPUTE_FUNCTION(float, get_max, (float x, float y),
+ {
+ if(x > y)
+ return x;
+ else
+ return y;
+ });
+
+ BOOST_CHECK_EQUAL(compute::invoke(get_max, queue, 10.f, 20.f), 20.f);
+}
+
+BOOST_AUTO_TEST_CASE(invoke_lambda)
+{
+ using boost::compute::lambda::_1;
+ using boost::compute::lambda::_2;
+
+ BOOST_CHECK_EQUAL(compute::invoke(_1 / 2, queue, 4), 2);
+ BOOST_CHECK_EQUAL(compute::invoke(_1 * _2 + 1, queue, 3, 3), 10);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/compute.git
More information about the debian-science-commits
mailing list