]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/compute/include/boost/compute/function.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / compute / include / boost / compute / function.hpp
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
49 invoked_function(const std::string &name,
50 const std::string &source)
51 : m_name(name),
52 m_source(source)
53 {
54 }
55
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
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
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
85 std::string name() const
86 {
87 return m_name;
88 }
89
90 std::string source() const
91 {
92 return m_source;
93 }
94
95 const std::map<std::string, std::string>& definitions() const
96 {
97 return m_definitions;
98 }
99
100 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.
133 function(const std::string &name)
134 : m_name(name)
135 {
136 }
137
138 /// Destroys the function object.
139 ~function()
140 {
141 }
142
143 /// \internal_
144 std::string name() const
145 {
146 return m_name;
147 }
148
149 /// \internal_
150 void set_source(const std::string &source)
151 {
152 m_source = source;
153 }
154
155 /// \internal_
156 std::string source() const
157 {
158 return m_source;
159 }
160
161 /// \internal_
162 void define(std::string name, std::string value = std::string())
163 {
164 m_definitions[name] = value;
165 }
166
167 /// \internal_
168 detail::invoked_function<result_type, boost::tuple<> >
169 operator()() const
170 {
171 BOOST_STATIC_ASSERT_MSG(
172 arity == 0,
173 "Non-nullary function invoked with zero arguments"
174 );
175
176 return detail::invoked_function<result_type, boost::tuple<> >(
177 m_name, m_source, m_definitions
178 );
179 }
180
181 /// \internal_
182 template<class Arg1>
183 detail::invoked_function<result_type, boost::tuple<Arg1> >
184 operator()(const Arg1 &arg1) const
185 {
186 BOOST_STATIC_ASSERT_MSG(
187 arity == 1,
188 "Non-unary function invoked one argument"
189 );
190
191 return detail::invoked_function<result_type, boost::tuple<Arg1> >(
192 m_name, m_source, m_definitions, boost::make_tuple(arg1)
193 );
194 }
195
196 /// \internal_
197 template<class Arg1, class Arg2>
198 detail::invoked_function<result_type, boost::tuple<Arg1, Arg2> >
199 operator()(const Arg1 &arg1, const Arg2 &arg2) const
200 {
201 BOOST_STATIC_ASSERT_MSG(
202 arity == 2,
203 "Non-binary function invoked with two arguments"
204 );
205
206 return detail::invoked_function<result_type, boost::tuple<Arg1, Arg2> >(
207 m_name, m_source, m_definitions, boost::make_tuple(arg1, arg2)
208 );
209 }
210
211 /// \internal_
212 template<class Arg1, class Arg2, class Arg3>
213 detail::invoked_function<result_type, boost::tuple<Arg1, Arg2, Arg3> >
214 operator()(const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3) const
215 {
216 BOOST_STATIC_ASSERT_MSG(
217 arity == 3,
218 "Non-ternary function invoked with three arguments"
219 );
220
221 return detail::invoked_function<result_type, boost::tuple<Arg1, Arg2, Arg3> >(
222 m_name, m_source, m_definitions, boost::make_tuple(arg1, arg2, arg3)
223 );
224 }
225
226 private:
227 std::string m_name;
228 std::string m_source;
229 std::map<std::string, std::string> m_definitions;
230 };
231
232 /// Creates a function object given its \p name and \p source.
233 ///
234 /// \param name The function name.
235 /// \param source The function source code.
236 ///
237 /// \see BOOST_COMPUTE_FUNCTION()
238 template<class Signature>
239 inline function<Signature>
240 make_function_from_source(const std::string &name, const std::string &source)
241 {
242 function<Signature> f(name);
243 f.set_source(source);
244 return f;
245 }
246
247 namespace detail {
248
249 // given a string containing the arguments declaration for a function
250 // like: "(int a, const float b)", returns a vector containing the name
251 // of each argument (e.g. ["a", "b"]).
252 inline std::vector<std::string> parse_argument_names(const char *arguments)
253 {
254 BOOST_ASSERT_MSG(
255 arguments[0] == '(' && arguments[std::strlen(arguments)-1] == ')',
256 "Arguments should start and end with parentheses"
257 );
258
259 std::vector<std::string> args;
260
261 size_t last_space = 0;
262 size_t skip_comma = 0;
263 for(size_t i = 1; i < std::strlen(arguments) - 2; i++){
264 const char c = arguments[i];
265
266 if(c == ' '){
267 last_space = i;
268 }
269 else if(c == ',' && !skip_comma){
270 std::string name(
271 arguments + last_space + 1, i - last_space - 1
272 );
273 args.push_back(name);
274 }
275 else if(c == '<'){
276 skip_comma++;
277 }
278 else if(c == '>'){
279 skip_comma--;
280 }
281 }
282
283 std::string last_argument(
284 arguments + last_space + 1, std::strlen(arguments) - last_space - 2
285 );
286 args.push_back(last_argument);
287
288 return args;
289 }
290
291 struct signature_argument_inserter
292 {
293 signature_argument_inserter(std::stringstream &s_, const char *arguments, size_t last)
294 : s(s_)
295 {
296 n = 0;
297 m_last = last;
298
299 m_argument_names = parse_argument_names(arguments);
300
301 BOOST_ASSERT_MSG(
302 m_argument_names.size() == last,
303 "Wrong number of arguments"
304 );
305 }
306
307 template<class T>
308 void operator()(const T*)
309 {
310 s << type_name<T>() << " " << m_argument_names[n];
311 if(n+1 < m_last){
312 s << ", ";
313 }
314 n++;
315 }
316
317 size_t n;
318 size_t m_last;
319 std::stringstream &s;
320 std::vector<std::string> m_argument_names;
321 };
322
323 template<class Signature>
324 inline std::string make_function_declaration(const char *name, const char *arguments)
325 {
326 typedef typename
327 boost::function_traits<Signature>::result_type result_type;
328 typedef typename
329 boost::function_types::parameter_types<Signature>::type parameter_types;
330 typedef typename
331 mpl::size<parameter_types>::type arity_type;
332
333 std::stringstream s;
334 s << "inline " << type_name<result_type>() << " " << name;
335 s << "(";
336
337 if(arity_type::value > 0){
338 signature_argument_inserter i(s, arguments, arity_type::value);
339 mpl::for_each<
340 typename mpl::transform<parameter_types, boost::add_pointer<mpl::_1>
341 >::type>(i);
342 }
343
344 s << ")";
345 return s.str();
346 }
347
348 struct argument_list_inserter
349 {
350 argument_list_inserter(std::stringstream &s_, const char first, size_t last)
351 : s(s_)
352 {
353 n = 0;
354 m_last = last;
355 m_name = first;
356 }
357
358 template<class T>
359 void operator()(const T*)
360 {
361 s << type_name<T>() << " " << m_name++;
362 if(n+1 < m_last){
363 s << ", ";
364 }
365 n++;
366 }
367
368 size_t n;
369 size_t m_last;
370 char m_name;
371 std::stringstream &s;
372 };
373
374 template<class Signature>
375 inline std::string generate_argument_list(const char first = 'a')
376 {
377 typedef typename
378 boost::function_types::parameter_types<Signature>::type parameter_types;
379 typedef typename
380 mpl::size<parameter_types>::type arity_type;
381
382 std::stringstream s;
383 s << '(';
384
385 if(arity_type::value > 0){
386 argument_list_inserter i(s, first, arity_type::value);
387 mpl::for_each<
388 typename mpl::transform<parameter_types, boost::add_pointer<mpl::_1>
389 >::type>(i);
390 }
391
392 s << ')';
393 return s.str();
394 }
395
396 // used by the BOOST_COMPUTE_FUNCTION() macro to create a function
397 // with the given signature, name, arguments, and source.
398 template<class Signature>
399 inline function<Signature>
400 make_function_impl(const char *name, const char *arguments, const char *source)
401 {
402 std::stringstream s;
403 s << make_function_declaration<Signature>(name, arguments);
404 s << source;
405
406 return make_function_from_source<Signature>(name, s.str());
407 }
408
409 } // end detail namespace
410 } // end compute namespace
411 } // end boost namespace
412
413 /// Creates a function object with \p name and \p source.
414 ///
415 /// \param return_type The return type for the function.
416 /// \param name The name of the function.
417 /// \param arguments A list of arguments for the function.
418 /// \param source The OpenCL C source code for the function.
419 ///
420 /// The function declaration and signature are automatically created using
421 /// the \p return_type, \p name, and \p arguments macro parameters.
422 ///
423 /// The source code for the function is interpreted as OpenCL C99 source code
424 /// which is stringified and passed to the OpenCL compiler when the function
425 /// is invoked.
426 ///
427 /// For example, to create a function which squares a number:
428 /// \code
429 /// BOOST_COMPUTE_FUNCTION(float, square, (float x),
430 /// {
431 /// return x * x;
432 /// });
433 /// \endcode
434 ///
435 /// And to create a function which sums two numbers:
436 /// \code
437 /// BOOST_COMPUTE_FUNCTION(int, sum_two, (int x, int y),
438 /// {
439 /// return x + y;
440 /// });
441 /// \endcode
442 ///
443 /// \see BOOST_COMPUTE_CLOSURE()
444 #ifdef BOOST_COMPUTE_DOXYGEN_INVOKED
445 #define BOOST_COMPUTE_FUNCTION(return_type, name, arguments, source)
446 #else
447 #define BOOST_COMPUTE_FUNCTION(return_type, name, arguments, ...) \
448 ::boost::compute::function<return_type arguments> name = \
449 ::boost::compute::detail::make_function_impl<return_type arguments>( \
450 #name, #arguments, #__VA_ARGS__ \
451 )
452 #endif
453
454 #endif // BOOST_COMPUTE_FUNCTION_HPP