1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 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 
11 #ifndef BOOST_COMPUTE_FUNCTION_HPP
12 #define BOOST_COMPUTE_FUNCTION_HPP
13 
14 #include <map>
15 #include <string>
16 #include <sstream>
17 #include <vector>
18 
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>
30 
31 #include <boost/compute/cl.hpp>
32 #include <boost/compute/config.hpp>
33 #include <boost/compute/type_traits/type_name.hpp>
34 
35 namespace boost {
36 namespace compute {
37 namespace detail {
38 
39 template<class ResultType, class ArgTuple>
40 class invoked_function
41 {
42 public:
43     typedef ResultType result_type;
44 
45     BOOST_STATIC_CONSTANT(
46         size_t, arity = boost::tuples::length<ArgTuple>::value
47     );
48 
invoked_function(const std::string & name,const std::string & source)49     invoked_function(const std::string &name,
50                      const std::string &source)
51         : m_name(name),
52           m_source(source)
53     {
54     }
55 
invoked_function(const std::string & name,const std::string & source,const std::map<std::string,std::string> & definitions)56     invoked_function(const std::string &name,
57                      const std::string &source,
58                      const std::map<std::string, std::string> &definitions)
59         : m_name(name),
60           m_source(source),
61           m_definitions(definitions)
62     {
63     }
64 
invoked_function(const std::string & name,const std::string & source,const ArgTuple & args)65     invoked_function(const std::string &name,
66                      const std::string &source,
67                      const ArgTuple &args)
68         : m_name(name),
69           m_source(source),
70           m_args(args)
71     {
72     }
73 
invoked_function(const std::string & name,const std::string & source,const std::map<std::string,std::string> & definitions,const ArgTuple & args)74     invoked_function(const std::string &name,
75                      const std::string &source,
76                      const std::map<std::string, std::string> &definitions,
77                      const ArgTuple &args)
78         : m_name(name),
79           m_source(source),
80           m_definitions(definitions),
81           m_args(args)
82     {
83     }
84 
name() const85     std::string name() const
86     {
87         return m_name;
88     }
89 
source() const90     std::string source() const
91     {
92         return m_source;
93     }
94 
definitions() const95     const std::map<std::string, std::string>& definitions() const
96     {
97         return m_definitions;
98     }
99 
args() const100     const ArgTuple& args() const
101     {
102         return m_args;
103     }
104 
105 private:
106     std::string m_name;
107     std::string m_source;
108     std::map<std::string, std::string> m_definitions;
109     ArgTuple m_args;
110 };
111 
112 } // end detail namespace
113 
114 /// \class function
115 /// \brief A function object.
116 template<class Signature>
117 class function
118 {
119 public:
120     /// \internal_
121     typedef typename
122         boost::function_traits<Signature>::result_type result_type;
123 
124     /// \internal_
125     BOOST_STATIC_CONSTANT(
126         size_t, arity = boost::function_traits<Signature>::arity
127     );
128 
129     /// \internal_
130     typedef Signature signature;
131 
132     /// Creates a new function object with \p name.
function(const std::string & name)133     function(const std::string &name)
134         : m_name(name)
135     {
136     }
137 
138     /// Destroys the function object.
~function()139     ~function()
140     {
141     }
142 
143     /// \internal_
name() const144     std::string name() const
145     {
146         return m_name;
147     }
148 
149     /// \internal_
set_source(const std::string & source)150     void set_source(const std::string &source)
151     {
152         m_source = source;
153     }
154 
155     /// \internal_
source() const156     std::string source() const
157     {
158         return m_source;
159     }
160 
161     /// \internal_
define(std::string name,std::string value=std::string ())162     void define(std::string name, std::string value = std::string())
163     {
164         m_definitions[name] = value;
165     }
166 
operator ==(const function<Signature> & other) const167     bool operator==(const function<Signature>& other) const
168     {
169         return
170             (m_name == other.m_name)
171                 && (m_definitions == other.m_definitions)
172                 && (m_source == other.m_source);
173     }
174 
operator !=(const function<Signature> & other) const175     bool operator!=(const function<Signature>& other) const
176     {
177         return !(*this == other);
178     }
179 
180     /// \internal_
181     detail::invoked_function<result_type, boost::tuple<> >
operator ()() const182     operator()() const
183     {
184         BOOST_STATIC_ASSERT_MSG(
185             arity == 0,
186             "Non-nullary function invoked with zero arguments"
187         );
188 
189         return detail::invoked_function<result_type, boost::tuple<> >(
190             m_name, m_source, m_definitions
191         );
192     }
193 
194     /// \internal_
195     template<class Arg1>
196     detail::invoked_function<result_type, boost::tuple<Arg1> >
operator ()(const Arg1 & arg1) const197     operator()(const Arg1 &arg1) const
198     {
199         BOOST_STATIC_ASSERT_MSG(
200             arity == 1,
201             "Non-unary function invoked one argument"
202         );
203 
204         return detail::invoked_function<result_type, boost::tuple<Arg1> >(
205             m_name, m_source, m_definitions, boost::make_tuple(arg1)
206         );
207     }
208 
209     /// \internal_
210     template<class Arg1, class Arg2>
211     detail::invoked_function<result_type, boost::tuple<Arg1, Arg2> >
operator ()(const Arg1 & arg1,const Arg2 & arg2) const212     operator()(const Arg1 &arg1, const Arg2 &arg2) const
213     {
214         BOOST_STATIC_ASSERT_MSG(
215             arity == 2,
216             "Non-binary function invoked with two arguments"
217         );
218 
219         return detail::invoked_function<result_type, boost::tuple<Arg1, Arg2> >(
220             m_name, m_source, m_definitions, boost::make_tuple(arg1, arg2)
221         );
222     }
223 
224     /// \internal_
225     template<class Arg1, class Arg2, class Arg3>
226     detail::invoked_function<result_type, boost::tuple<Arg1, Arg2, Arg3> >
operator ()(const Arg1 & arg1,const Arg2 & arg2,const Arg3 & arg3) const227     operator()(const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3) const
228     {
229         BOOST_STATIC_ASSERT_MSG(
230             arity == 3,
231             "Non-ternary function invoked with three arguments"
232         );
233 
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)
236         );
237     }
238 
239 private:
240     std::string m_name;
241     std::string m_source;
242     std::map<std::string, std::string> m_definitions;
243 };
244 
245 /// Creates a function object given its \p name and \p source.
246 ///
247 /// \param name The function name.
248 /// \param source The function source code.
249 ///
250 /// \see BOOST_COMPUTE_FUNCTION()
251 template<class Signature>
252 inline function<Signature>
make_function_from_source(const std::string & name,const std::string & source)253 make_function_from_source(const std::string &name, const std::string &source)
254 {
255     function<Signature> f(name);
256     f.set_source(source);
257     return f;
258 }
259 
260 namespace detail {
261 
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"]).
parse_argument_names(const char * arguments)265 inline std::vector<std::string> parse_argument_names(const char *arguments)
266 {
267     BOOST_ASSERT_MSG(
268         arguments[0] == '(' && arguments[std::strlen(arguments)-1] == ')',
269         "Arguments should start and end with parentheses"
270     );
271 
272     std::vector<std::string> args;
273 
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];
278 
279         if(c == ' '){
280             last_space = i;
281         }
282         else if(c == ',' && !skip_comma){
283             std::string name(
284                 arguments + last_space + 1, i - last_space - 1
285             );
286             args.push_back(name);
287         }
288         else if(c == '<'){
289             skip_comma++;
290         }
291         else if(c == '>'){
292             skip_comma--;
293         }
294     }
295 
296     std::string last_argument(
297         arguments + last_space + 1, std::strlen(arguments) - last_space - 2
298     );
299     args.push_back(last_argument);
300 
301     return args;
302 }
303 
304 struct signature_argument_inserter
305 {
signature_argument_inserterboost::compute::detail::signature_argument_inserter306     signature_argument_inserter(std::stringstream &s_, const char *arguments, size_t last)
307         : s(s_)
308     {
309         n = 0;
310         m_last = last;
311 
312         m_argument_names = parse_argument_names(arguments);
313 
314         BOOST_ASSERT_MSG(
315             m_argument_names.size() == last,
316             "Wrong number of arguments"
317         );
318     }
319 
320     template<class T>
operator ()boost::compute::detail::signature_argument_inserter321     void operator()(const T*)
322     {
323         s << type_name<T>() << " " << m_argument_names[n];
324         if(n+1 < m_last){
325             s << ", ";
326         }
327         n++;
328     }
329 
330     size_t n;
331     size_t m_last;
332     std::stringstream &s;
333     std::vector<std::string> m_argument_names;
334 };
335 
336 template<class Signature>
make_function_declaration(const char * name,const char * arguments)337 inline std::string make_function_declaration(const char *name, const char *arguments)
338 {
339     typedef typename
340         boost::function_traits<Signature>::result_type result_type;
341     typedef typename
342         boost::function_types::parameter_types<Signature>::type parameter_types;
343     typedef typename
344         mpl::size<parameter_types>::type arity_type;
345 
346     std::stringstream s;
347     s << "inline " << type_name<result_type>() << " " << name;
348     s << "(";
349 
350     if(arity_type::value > 0){
351         signature_argument_inserter i(s, arguments, arity_type::value);
352         mpl::for_each<
353             typename mpl::transform<parameter_types, boost::add_pointer<mpl::_1>
354         >::type>(i);
355     }
356 
357     s << ")";
358     return s.str();
359 }
360 
361 struct argument_list_inserter
362 {
argument_list_inserterboost::compute::detail::argument_list_inserter363     argument_list_inserter(std::stringstream &s_, const char first, size_t last)
364         : s(s_)
365     {
366         n = 0;
367         m_last = last;
368         m_name = first;
369     }
370 
371     template<class T>
operator ()boost::compute::detail::argument_list_inserter372     void operator()(const T*)
373     {
374         s << type_name<T>() << " " << m_name++;
375         if(n+1 < m_last){
376             s << ", ";
377         }
378         n++;
379     }
380 
381     size_t n;
382     size_t m_last;
383     char m_name;
384     std::stringstream &s;
385 };
386 
387 template<class Signature>
generate_argument_list(const char first='a')388 inline std::string generate_argument_list(const char first = 'a')
389 {
390     typedef typename
391         boost::function_types::parameter_types<Signature>::type parameter_types;
392     typedef typename
393         mpl::size<parameter_types>::type arity_type;
394 
395     std::stringstream s;
396     s << '(';
397 
398     if(arity_type::value > 0){
399         argument_list_inserter i(s, first, arity_type::value);
400         mpl::for_each<
401             typename mpl::transform<parameter_types, boost::add_pointer<mpl::_1>
402         >::type>(i);
403     }
404 
405     s << ')';
406     return s.str();
407 }
408 
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>
make_function_impl(const char * name,const char * arguments,const char * source)413 make_function_impl(const char *name, const char *arguments, const char *source)
414 {
415     std::stringstream s;
416     s << make_function_declaration<Signature>(name, arguments);
417     s << source;
418 
419     return make_function_from_source<Signature>(name, s.str());
420 }
421 
422 } // end detail namespace
423 } // end compute namespace
424 } // end boost namespace
425 
426 /// Creates a function object with \p name and \p source.
427 ///
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.
432 ///
433 /// The function declaration and signature are automatically created using
434 /// the \p return_type, \p name, and \p arguments macro parameters.
435 ///
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
438 /// is invoked.
439 ///
440 /// For example, to create a function which squares a number:
441 /// \code
442 /// BOOST_COMPUTE_FUNCTION(float, square, (float x),
443 /// {
444 ///     return x * x;
445 /// });
446 /// \endcode
447 ///
448 /// And to create a function which sums two numbers:
449 /// \code
450 /// BOOST_COMPUTE_FUNCTION(int, sum_two, (int x, int y),
451 /// {
452 ///     return x + y;
453 /// });
454 /// \endcode
455 ///
456 /// \see BOOST_COMPUTE_CLOSURE()
457 #ifdef BOOST_COMPUTE_DOXYGEN_INVOKED
458 #define BOOST_COMPUTE_FUNCTION(return_type, name, arguments, source)
459 #else
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__ \
464         )
465 #endif
466 
467 #endif // BOOST_COMPUTE_FUNCTION_HPP
468