//---------------------------------------------------------------------------// // Copyright (c) 2013-2015 Kyle Lutz // // 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 #include #include #include #include #include #include namespace boost { namespace compute { #define BOOST_COMPUTE_DETAIL_INVOKE_ARG(z, n, unused) \ BOOST_PP_COMMA_IF(n) k.var("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 \ inline typename result_of::type \ invoke(const Function& function, command_queue& queue, BOOST_PP_ENUM_BINARY_PARAMS(n, const T, &arg)) \ { \ typedef typename result_of::type result_type; \ detail::meta_kernel k("invoke"); \ detail::scalar result(queue.get_context()); \ const size_t result_arg = k.add_arg(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(), queue, -10); // returns 10 /// \endcode template inline typename result_of::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