closure.hpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2014 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://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #ifndef BOOST_COMPUTE_CLOSURE_HPP
  11. #define BOOST_COMPUTE_CLOSURE_HPP
  12. #include <string>
  13. #include <sstream>
  14. #include <boost/config.hpp>
  15. #include <boost/fusion/adapted/boost_tuple.hpp>
  16. #include <boost/fusion/algorithm/iteration/for_each.hpp>
  17. #include <boost/mpl/for_each.hpp>
  18. #include <boost/mpl/transform.hpp>
  19. #include <boost/typeof/typeof.hpp>
  20. #include <boost/static_assert.hpp>
  21. #include <boost/algorithm/string.hpp>
  22. #include <boost/tuple/tuple.hpp>
  23. #include <boost/type_traits/function_traits.hpp>
  24. #include <boost/compute/cl.hpp>
  25. #include <boost/compute/function.hpp>
  26. #include <boost/compute/type_traits/type_name.hpp>
  27. #include <boost/compute/type_traits/detail/capture_traits.hpp>
  28. namespace boost {
  29. namespace compute {
  30. namespace detail {
  31. template<class ResultType, class ArgTuple, class CaptureTuple>
  32. class invoked_closure
  33. {
  34. public:
  35. typedef ResultType result_type;
  36. BOOST_STATIC_CONSTANT(
  37. size_t, arity = boost::tuples::length<ArgTuple>::value
  38. );
  39. invoked_closure(const std::string &name,
  40. const std::string &source,
  41. const std::map<std::string, std::string> &definitions,
  42. const ArgTuple &args,
  43. const CaptureTuple &capture)
  44. : m_name(name),
  45. m_source(source),
  46. m_definitions(definitions),
  47. m_args(args),
  48. m_capture(capture)
  49. {
  50. }
  51. std::string name() const
  52. {
  53. return m_name;
  54. }
  55. std::string source() const
  56. {
  57. return m_source;
  58. }
  59. const std::map<std::string, std::string>& definitions() const
  60. {
  61. return m_definitions;
  62. }
  63. const ArgTuple& args() const
  64. {
  65. return m_args;
  66. }
  67. const CaptureTuple& capture() const
  68. {
  69. return m_capture;
  70. }
  71. private:
  72. std::string m_name;
  73. std::string m_source;
  74. std::map<std::string, std::string> m_definitions;
  75. ArgTuple m_args;
  76. CaptureTuple m_capture;
  77. };
  78. } // end detail namespace
  79. /// \internal_
  80. template<class Signature, class CaptureTuple>
  81. class closure
  82. {
  83. public:
  84. typedef typename
  85. boost::function_traits<Signature>::result_type result_type;
  86. BOOST_STATIC_CONSTANT(
  87. size_t, arity = boost::function_traits<Signature>::arity
  88. );
  89. closure(const std::string &name,
  90. const CaptureTuple &capture,
  91. const std::string &source)
  92. : m_name(name),
  93. m_source(source),
  94. m_capture(capture)
  95. {
  96. }
  97. ~closure()
  98. {
  99. }
  100. std::string name() const
  101. {
  102. return m_name;
  103. }
  104. /// \internal_
  105. std::string source() const
  106. {
  107. return m_source;
  108. }
  109. /// \internal_
  110. void define(std::string name, std::string value = std::string())
  111. {
  112. m_definitions[name] = value;
  113. }
  114. /// \internal_
  115. detail::invoked_closure<result_type, boost::tuple<>, CaptureTuple>
  116. operator()() const
  117. {
  118. BOOST_STATIC_ASSERT_MSG(
  119. arity == 0,
  120. "Non-nullary closure function invoked with zero arguments"
  121. );
  122. return detail::invoked_closure<result_type, boost::tuple<>, CaptureTuple>(
  123. m_name, m_source, m_definitions, boost::make_tuple(), m_capture
  124. );
  125. }
  126. /// \internal_
  127. template<class Arg1>
  128. detail::invoked_closure<result_type, boost::tuple<Arg1>, CaptureTuple>
  129. operator()(const Arg1 &arg1) const
  130. {
  131. BOOST_STATIC_ASSERT_MSG(
  132. arity == 1,
  133. "Non-unary closure function invoked with one argument"
  134. );
  135. return detail::invoked_closure<result_type, boost::tuple<Arg1>, CaptureTuple>(
  136. m_name, m_source, m_definitions, boost::make_tuple(arg1), m_capture
  137. );
  138. }
  139. /// \internal_
  140. template<class Arg1, class Arg2>
  141. detail::invoked_closure<result_type, boost::tuple<Arg1, Arg2>, CaptureTuple>
  142. operator()(const Arg1 &arg1, const Arg2 &arg2) const
  143. {
  144. BOOST_STATIC_ASSERT_MSG(
  145. arity == 2,
  146. "Non-binary closure function invoked with two arguments"
  147. );
  148. return detail::invoked_closure<result_type, boost::tuple<Arg1, Arg2>, CaptureTuple>(
  149. m_name, m_source, m_definitions, boost::make_tuple(arg1, arg2), m_capture
  150. );
  151. }
  152. /// \internal_
  153. template<class Arg1, class Arg2, class Arg3>
  154. detail::invoked_closure<result_type, boost::tuple<Arg1, Arg2, Arg3>, CaptureTuple>
  155. operator()(const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3) const
  156. {
  157. BOOST_STATIC_ASSERT_MSG(
  158. arity == 3,
  159. "Non-ternary closure function invoked with three arguments"
  160. );
  161. return detail::invoked_closure<result_type, boost::tuple<Arg1, Arg2, Arg3>, CaptureTuple>(
  162. m_name, m_source, m_definitions, boost::make_tuple(arg1, arg2, arg3), m_capture
  163. );
  164. }
  165. private:
  166. std::string m_name;
  167. std::string m_source;
  168. std::map<std::string, std::string> m_definitions;
  169. CaptureTuple m_capture;
  170. };
  171. namespace detail {
  172. struct closure_signature_argument_inserter
  173. {
  174. closure_signature_argument_inserter(std::stringstream &s_,
  175. const char *capture_string,
  176. size_t last)
  177. : s(s_)
  178. {
  179. n = 0;
  180. m_last = last;
  181. size_t capture_string_length = std::strlen(capture_string);
  182. BOOST_ASSERT(capture_string[0] == '(' &&
  183. capture_string[capture_string_length-1] == ')');
  184. std::string capture_string_(capture_string + 1, capture_string_length - 2);
  185. boost::split(m_capture_names, capture_string_ , boost::is_any_of(","));
  186. }
  187. template<class T>
  188. void operator()(const T&) const
  189. {
  190. BOOST_ASSERT(n < m_capture_names.size());
  191. // get captured variable name
  192. std::string variable_name = m_capture_names[n];
  193. // remove leading and trailing whitespace from variable name
  194. boost::trim(variable_name);
  195. s << capture_traits<T>::type_name() << " " << variable_name;
  196. if(n+1 < m_last){
  197. s << ", ";
  198. }
  199. n++;
  200. }
  201. mutable size_t n;
  202. size_t m_last;
  203. std::vector<std::string> m_capture_names;
  204. std::stringstream &s;
  205. };
  206. template<class Signature, class CaptureTuple>
  207. inline std::string
  208. make_closure_declaration(const char *name,
  209. const char *arguments,
  210. const CaptureTuple &capture_tuple,
  211. const char *capture_string)
  212. {
  213. typedef typename
  214. boost::function_traits<Signature>::result_type result_type;
  215. typedef typename
  216. boost::function_types::parameter_types<Signature>::type parameter_types;
  217. typedef typename
  218. mpl::size<parameter_types>::type arity_type;
  219. std::stringstream s;
  220. s << "inline " << type_name<result_type>() << " " << name;
  221. s << "(";
  222. // insert function arguments
  223. signature_argument_inserter i(s, arguments, arity_type::value);
  224. mpl::for_each<
  225. typename mpl::transform<parameter_types, boost::add_pointer<mpl::_1>
  226. >::type>(i);
  227. s << ", ";
  228. // insert capture arguments
  229. closure_signature_argument_inserter j(
  230. s, capture_string, boost::tuples::length<CaptureTuple>::value
  231. );
  232. fusion::for_each(capture_tuple, j);
  233. s << ")";
  234. return s.str();
  235. }
  236. // used by the BOOST_COMPUTE_CLOSURE() macro to create a closure
  237. // function with the given signature, name, capture, and source.
  238. template<class Signature, class CaptureTuple>
  239. inline closure<Signature, CaptureTuple>
  240. make_closure_impl(const char *name,
  241. const char *arguments,
  242. const CaptureTuple &capture,
  243. const char *capture_string,
  244. const std::string &source)
  245. {
  246. std::stringstream s;
  247. s << make_closure_declaration<Signature>(name, arguments, capture, capture_string);
  248. s << source;
  249. return closure<Signature, CaptureTuple>(name, capture, s.str());
  250. }
  251. } // end detail namespace
  252. } // end compute namespace
  253. } // end boost namespace
  254. /// Creates a closure function object with \p name and \p source.
  255. ///
  256. /// \param return_type The return type for the function.
  257. /// \param name The name of the function.
  258. /// \param arguments A list of arguments for the function.
  259. /// \param capture A list of variables to capture.
  260. /// \param source The OpenCL C source code for the function.
  261. ///
  262. /// For example, to create a function which checks if a 2D point is
  263. /// contained in a circle of a given radius:
  264. /// \code
  265. /// // radius variable declared in C++
  266. /// float radius = 1.5f;
  267. ///
  268. /// // create a closure function which returns true if the 2D point
  269. /// // argument is contained within a circle of the given radius
  270. /// BOOST_COMPUTE_CLOSURE(bool, is_in_circle, (const float2_ p), (radius),
  271. /// {
  272. /// return sqrt(p.x*p.x + p.y*p.y) < radius;
  273. /// });
  274. ///
  275. /// // vector of 2D points
  276. /// boost::compute::vector<float2_> points = ...
  277. ///
  278. /// // count number of points in the circle
  279. /// size_t count = boost::compute::count_if(
  280. /// points.begin(), points.end(), is_in_circle, queue
  281. /// );
  282. /// \endcode
  283. ///
  284. /// \see BOOST_COMPUTE_FUNCTION()
  285. #ifdef BOOST_COMPUTE_DOXYGEN_INVOKED
  286. #define BOOST_COMPUTE_CLOSURE(return_type, name, arguments, capture, source)
  287. #else
  288. #define BOOST_COMPUTE_CLOSURE(return_type, name, arguments, capture, ...) \
  289. ::boost::compute::closure< \
  290. return_type arguments, BOOST_TYPEOF(boost::tie capture) \
  291. > name = \
  292. ::boost::compute::detail::make_closure_impl< \
  293. return_type arguments \
  294. >( \
  295. #name, #arguments, boost::tie capture, #capture, #__VA_ARGS__ \
  296. )
  297. #endif
  298. #endif // BOOST_COMPUTE_CLOSURE_HPP