invoke.hpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #ifndef BOOST_COMPUTE_UTILITY_INVOKE_HPP
  11. #define BOOST_COMPUTE_UTILITY_INVOKE_HPP
  12. #include <boost/preprocessor/enum.hpp>
  13. #include <boost/preprocessor/repetition.hpp>
  14. #include <boost/compute/config.hpp>
  15. #include <boost/compute/command_queue.hpp>
  16. #include <boost/compute/detail/meta_kernel.hpp>
  17. #include <boost/compute/container/detail/scalar.hpp>
  18. #include <boost/compute/type_traits/result_of.hpp>
  19. namespace boost {
  20. namespace compute {
  21. #define BOOST_COMPUTE_DETAIL_INVOKE_ARG(z, n, unused) \
  22. BOOST_PP_COMMA_IF(n) k.var<BOOST_PP_CAT(T, n)>("arg" BOOST_PP_STRINGIZE(n))
  23. #define BOOST_COMPUTE_DETAIL_INVOKE_ADD_ARG(z, n, unused) \
  24. k.add_set_arg("arg" BOOST_PP_STRINGIZE(n), BOOST_PP_CAT(arg, n));
  25. #define BOOST_COMPUTE_DETAIL_DEFINE_INVOKE(z, n, unused) \
  26. template<class Function, BOOST_PP_ENUM_PARAMS(n, class T)> \
  27. inline typename result_of<Function(BOOST_PP_ENUM_PARAMS(n, T))>::type \
  28. invoke(const Function& function, command_queue& queue, BOOST_PP_ENUM_BINARY_PARAMS(n, const T, &arg)) \
  29. { \
  30. typedef typename result_of<Function(BOOST_PP_ENUM_PARAMS(n, T))>::type result_type; \
  31. detail::meta_kernel k("invoke"); \
  32. detail::scalar<result_type> result(queue.get_context()); \
  33. const size_t result_arg = k.add_arg<result_type *>(memory_object::global_memory, "result"); \
  34. BOOST_PP_REPEAT(n, BOOST_COMPUTE_DETAIL_INVOKE_ADD_ARG, ~) \
  35. k << "*result = " << function( \
  36. BOOST_PP_REPEAT(n, BOOST_COMPUTE_DETAIL_INVOKE_ARG, ~) \
  37. ) << ";"; \
  38. k.set_arg(result_arg, result.get_buffer()); \
  39. k.exec(queue); \
  40. return result.read(queue); \
  41. }
  42. BOOST_PP_REPEAT_FROM_TO(1, BOOST_COMPUTE_MAX_ARITY, BOOST_COMPUTE_DETAIL_DEFINE_INVOKE, ~)
  43. #undef BOOST_COMPUTE_DETAIL_INVOKE_ARG
  44. #undef BOOST_COMPUTE_DETAIL_INVOKE_ADD_ARG
  45. #undef BOOST_COMPUTE_DETAIL_DEFINE_INVOKE
  46. #ifdef BOOST_COMPUTE_DOXYGEN_INVOKED
  47. /// Invokes \p function with \p args on \p queue.
  48. ///
  49. /// For example, to invoke the builtin abs() function:
  50. /// \code
  51. /// int result = invoke(abs<int>(), queue, -10); // returns 10
  52. /// \endcode
  53. template<class Function, class... Args>
  54. inline typename result_of<Function(Args...)>::type
  55. invoke(const Function& function, command_queue& queue, const Args&... args);
  56. #endif // BOOST_COMPUTE_DOXYGEN_INVOKED
  57. } // end compute namespace
  58. } // end boost namespace
  59. #endif // BOOST_COMPUTE_UTILITY_INVOKE_HPP