1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 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://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_FUNCTION_HPP
12 #define BOOST_COMPUTE_FUNCTION_HPP
19 #include <boost/assert.hpp>
20 #include <boost/config.hpp>
21 #include <boost/function_types/parameter_types.hpp>
22 #include <boost/preprocessor/repetition.hpp>
23 #include <boost/mpl/for_each.hpp>
24 #include <boost/mpl/size.hpp>
25 #include <boost/mpl/transform.hpp>
26 #include <boost/static_assert.hpp>
27 #include <boost/tuple/tuple.hpp>
28 #include <boost/type_traits/add_pointer.hpp>
29 #include <boost/type_traits/function_traits.hpp>
31 #include <boost/compute/cl.hpp>
32 #include <boost/compute/config.hpp>
33 #include <boost/compute/type_traits/type_name.hpp>
39 template<class ResultType, class ArgTuple>
40 class invoked_function
43 typedef ResultType result_type;
45 BOOST_STATIC_CONSTANT(
46 size_t, arity = boost::tuples::length<ArgTuple>::value
49 invoked_function(const std::string &name,
50 const std::string &source)
56 invoked_function(const std::string &name,
57 const std::string &source,
58 const std::map<std::string, std::string> &definitions)
61 m_definitions(definitions)
65 invoked_function(const std::string &name,
66 const std::string &source,
74 invoked_function(const std::string &name,
75 const std::string &source,
76 const std::map<std::string, std::string> &definitions,
80 m_definitions(definitions),
85 std::string name() const
90 std::string source() const
95 const std::map<std::string, std::string>& definitions() const
100 const ArgTuple& args() const
107 std::string m_source;
108 std::map<std::string, std::string> m_definitions;
112 } // end detail namespace
115 /// \brief A function object.
116 template<class Signature>
122 boost::function_traits<Signature>::result_type result_type;
125 BOOST_STATIC_CONSTANT(
126 size_t, arity = boost::function_traits<Signature>::arity
130 typedef Signature signature;
132 /// Creates a new function object with \p name.
133 function(const std::string &name)
138 /// Destroys the function object.
144 std::string name() const
150 void set_source(const std::string &source)
156 std::string source() const
162 void define(std::string name, std::string value = std::string())
164 m_definitions[name] = value;
167 bool operator==(const function<Signature>& other) const
170 (m_name == other.m_name)
171 && (m_definitions == other.m_definitions)
172 && (m_source == other.m_source);
175 bool operator!=(const function<Signature>& other) const
177 return !(*this == other);
181 detail::invoked_function<result_type, boost::tuple<> >
184 BOOST_STATIC_ASSERT_MSG(
186 "Non-nullary function invoked with zero arguments"
189 return detail::invoked_function<result_type, boost::tuple<> >(
190 m_name, m_source, m_definitions
196 detail::invoked_function<result_type, boost::tuple<Arg1> >
197 operator()(const Arg1 &arg1) const
199 BOOST_STATIC_ASSERT_MSG(
201 "Non-unary function invoked one argument"
204 return detail::invoked_function<result_type, boost::tuple<Arg1> >(
205 m_name, m_source, m_definitions, boost::make_tuple(arg1)
210 template<class Arg1, class Arg2>
211 detail::invoked_function<result_type, boost::tuple<Arg1, Arg2> >
212 operator()(const Arg1 &arg1, const Arg2 &arg2) const
214 BOOST_STATIC_ASSERT_MSG(
216 "Non-binary function invoked with two arguments"
219 return detail::invoked_function<result_type, boost::tuple<Arg1, Arg2> >(
220 m_name, m_source, m_definitions, boost::make_tuple(arg1, arg2)
225 template<class Arg1, class Arg2, class Arg3>
226 detail::invoked_function<result_type, boost::tuple<Arg1, Arg2, Arg3> >
227 operator()(const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3) const
229 BOOST_STATIC_ASSERT_MSG(
231 "Non-ternary function invoked with three arguments"
234 return detail::invoked_function<result_type, boost::tuple<Arg1, Arg2, Arg3> >(
235 m_name, m_source, m_definitions, boost::make_tuple(arg1, arg2, arg3)
241 std::string m_source;
242 std::map<std::string, std::string> m_definitions;
245 /// Creates a function object given its \p name and \p source.
247 /// \param name The function name.
248 /// \param source The function source code.
250 /// \see BOOST_COMPUTE_FUNCTION()
251 template<class Signature>
252 inline function<Signature>
253 make_function_from_source(const std::string &name, const std::string &source)
255 function<Signature> f(name);
256 f.set_source(source);
262 // given a string containing the arguments declaration for a function
263 // like: "(int a, const float b)", returns a vector containing the name
264 // of each argument (e.g. ["a", "b"]).
265 inline std::vector<std::string> parse_argument_names(const char *arguments)
268 arguments[0] == '(' && arguments[std::strlen(arguments)-1] == ')',
269 "Arguments should start and end with parentheses"
272 std::vector<std::string> args;
274 size_t last_space = 0;
275 size_t skip_comma = 0;
276 for(size_t i = 1; i < std::strlen(arguments) - 2; i++){
277 const char c = arguments[i];
282 else if(c == ',' && !skip_comma){
284 arguments + last_space + 1, i - last_space - 1
286 args.push_back(name);
296 std::string last_argument(
297 arguments + last_space + 1, std::strlen(arguments) - last_space - 2
299 args.push_back(last_argument);
304 struct signature_argument_inserter
306 signature_argument_inserter(std::stringstream &s_, const char *arguments, size_t last)
312 m_argument_names = parse_argument_names(arguments);
315 m_argument_names.size() == last,
316 "Wrong number of arguments"
321 void operator()(const T*)
323 s << type_name<T>() << " " << m_argument_names[n];
332 std::stringstream &s;
333 std::vector<std::string> m_argument_names;
336 template<class Signature>
337 inline std::string make_function_declaration(const char *name, const char *arguments)
340 boost::function_traits<Signature>::result_type result_type;
342 boost::function_types::parameter_types<Signature>::type parameter_types;
344 mpl::size<parameter_types>::type arity_type;
347 s << "inline " << type_name<result_type>() << " " << name;
350 if(arity_type::value > 0){
351 signature_argument_inserter i(s, arguments, arity_type::value);
353 typename mpl::transform<parameter_types, boost::add_pointer<mpl::_1>
361 struct argument_list_inserter
363 argument_list_inserter(std::stringstream &s_, const char first, size_t last)
372 void operator()(const T*)
374 s << type_name<T>() << " " << m_name++;
384 std::stringstream &s;
387 template<class Signature>
388 inline std::string generate_argument_list(const char first = 'a')
391 boost::function_types::parameter_types<Signature>::type parameter_types;
393 mpl::size<parameter_types>::type arity_type;
398 if(arity_type::value > 0){
399 argument_list_inserter i(s, first, arity_type::value);
401 typename mpl::transform<parameter_types, boost::add_pointer<mpl::_1>
409 // used by the BOOST_COMPUTE_FUNCTION() macro to create a function
410 // with the given signature, name, arguments, and source.
411 template<class Signature>
412 inline function<Signature>
413 make_function_impl(const char *name, const char *arguments, const char *source)
416 s << make_function_declaration<Signature>(name, arguments);
419 return make_function_from_source<Signature>(name, s.str());
422 } // end detail namespace
423 } // end compute namespace
424 } // end boost namespace
426 /// Creates a function object with \p name and \p source.
428 /// \param return_type The return type for the function.
429 /// \param name The name of the function.
430 /// \param arguments A list of arguments for the function.
431 /// \param source The OpenCL C source code for the function.
433 /// The function declaration and signature are automatically created using
434 /// the \p return_type, \p name, and \p arguments macro parameters.
436 /// The source code for the function is interpreted as OpenCL C99 source code
437 /// which is stringified and passed to the OpenCL compiler when the function
440 /// For example, to create a function which squares a number:
442 /// BOOST_COMPUTE_FUNCTION(float, square, (float x),
448 /// And to create a function which sums two numbers:
450 /// BOOST_COMPUTE_FUNCTION(int, sum_two, (int x, int y),
456 /// \see BOOST_COMPUTE_CLOSURE()
457 #ifdef BOOST_COMPUTE_DOXYGEN_INVOKED
458 #define BOOST_COMPUTE_FUNCTION(return_type, name, arguments, source)
460 #define BOOST_COMPUTE_FUNCTION(return_type, name, arguments, ...) \
461 ::boost::compute::function<return_type arguments> name = \
462 ::boost::compute::detail::make_function_impl<return_type arguments>( \
463 #name, #arguments, #__VA_ARGS__ \
467 #endif // BOOST_COMPUTE_FUNCTION_HPP