1 //---------------------------------------------------------------------------// 2 // Copyright (c) 2013-2015 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 #ifndef BOOST_COMPUTE_UTILITY_INVOKE_HPP 12 #define BOOST_COMPUTE_UTILITY_INVOKE_HPP 13 14 #include <boost/preprocessor/enum.hpp> 15 #include <boost/preprocessor/repetition.hpp> 16 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> 22 23 namespace boost { 24 namespace compute { 25 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)) 28 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)); 31 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)) \ 36 { \ 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, ~) \ 44 ) << ";"; \ 45 k.set_arg(result_arg, result.get_buffer()); \ 46 k.exec(queue); \ 47 return result.read(queue); \ 48 } 49 50 BOOST_PP_REPEAT_FROM_TO(1, BOOST_COMPUTE_MAX_ARITY, BOOST_COMPUTE_DETAIL_DEFINE_INVOKE, ~) 51 52 #undef BOOST_COMPUTE_DETAIL_INVOKE_ARG 53 #undef BOOST_COMPUTE_DETAIL_INVOKE_ADD_ARG 54 #undef BOOST_COMPUTE_DETAIL_DEFINE_INVOKE 55 56 #ifdef BOOST_COMPUTE_DOXYGEN_INVOKED 57 /// Invokes \p function with \p args on \p queue. 58 /// 59 /// For example, to invoke the builtin abs() function: 60 /// \code 61 /// int result = invoke(abs<int>(), queue, -10); // returns 10 62 /// \endcode 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 67 68 } // end compute namespace 69 } // end boost namespace 70 71 #endif // BOOST_COMPUTE_UTILITY_INVOKE_HPP 72