1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013-2015 Kyle Lutz <kyle.r.lutz@gmail.com>
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
8 // See http://kylelutz.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_UTILITY_INVOKE_HPP
12 #define BOOST_COMPUTE_UTILITY_INVOKE_HPP
14 #include <boost/preprocessor/enum.hpp>
15 #include <boost/preprocessor/repetition.hpp>
17 #include <boost/compute/config.hpp>
18 #include <boost/compute/command_queue.hpp>
19 #include <boost/compute/detail/meta_kernel.hpp>
20 #include <boost/compute/container/detail/scalar.hpp>
21 #include <boost/compute/type_traits/result_of.hpp>
26 #define BOOST_COMPUTE_DETAIL_INVOKE_ARG(z, n, unused) \
27 BOOST_PP_COMMA_IF(n) k.var<BOOST_PP_CAT(T, n)>("arg" BOOST_PP_STRINGIZE(n))
29 #define BOOST_COMPUTE_DETAIL_INVOKE_ADD_ARG(z, n, unused) \
30 k.add_set_arg("arg" BOOST_PP_STRINGIZE(n), BOOST_PP_CAT(arg, n));
32 #define BOOST_COMPUTE_DETAIL_DEFINE_INVOKE(z, n, unused) \
33 template<class Function, BOOST_PP_ENUM_PARAMS(n, class T)> \
34 inline typename result_of<Function(BOOST_PP_ENUM_PARAMS(n, T))>::type \
35 invoke(const Function& function, command_queue& queue, BOOST_PP_ENUM_BINARY_PARAMS(n, const T, &arg)) \
37 typedef typename result_of<Function(BOOST_PP_ENUM_PARAMS(n, T))>::type result_type; \
38 detail::meta_kernel k("invoke"); \
39 detail::scalar<result_type> result(queue.get_context()); \
40 const size_t result_arg = k.add_arg<result_type *>(memory_object::global_memory, "result"); \
41 BOOST_PP_REPEAT(n, BOOST_COMPUTE_DETAIL_INVOKE_ADD_ARG, ~) \
42 k << "*result = " << function( \
43 BOOST_PP_REPEAT(n, BOOST_COMPUTE_DETAIL_INVOKE_ARG, ~) \
45 k.set_arg(result_arg, result.get_buffer()); \
47 return result.read(queue); \
50 BOOST_PP_REPEAT_FROM_TO(1, BOOST_COMPUTE_MAX_ARITY, BOOST_COMPUTE_DETAIL_DEFINE_INVOKE, ~)
52 #undef BOOST_COMPUTE_DETAIL_INVOKE_ARG
53 #undef BOOST_COMPUTE_DETAIL_INVOKE_ADD_ARG
54 #undef BOOST_COMPUTE_DETAIL_DEFINE_INVOKE
56 #ifdef BOOST_COMPUTE_DOXYGEN_INVOKED
57 /// Invokes \p function with \p args on \p queue.
59 /// For example, to invoke the builtin abs() function:
61 /// int result = invoke(abs<int>(), queue, -10); // returns 10
63 template<class Function, class... Args>
64 inline typename result_of<Function(Args...)>::type
65 invoke(const Function& function, command_queue& queue, const Args&... args);
66 #endif // BOOST_COMPUTE_DOXYGEN_INVOKED
68 } // end compute namespace
69 } // end boost namespace
71 #endif // BOOST_COMPUTE_UTILITY_INVOKE_HPP