]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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_ALGORITHM_DETAIL_SERIAL_REDUCE_HPP | |
12 | #define BOOST_COMPUTE_ALGORITHM_DETAIL_SERIAL_REDUCE_HPP | |
13 | ||
14 | #include <boost/compute/command_queue.hpp> | |
15 | #include <boost/compute/detail/meta_kernel.hpp> | |
16 | #include <boost/compute/detail/iterator_range_size.hpp> | |
17 | #include <boost/compute/type_traits/result_of.hpp> | |
18 | ||
19 | namespace boost { | |
20 | namespace compute { | |
21 | namespace detail { | |
22 | ||
23 | template<class InputIterator, class OutputIterator, class BinaryFunction> | |
24 | inline void serial_reduce(InputIterator first, | |
25 | InputIterator last, | |
26 | OutputIterator result, | |
27 | BinaryFunction function, | |
28 | command_queue &queue) | |
29 | { | |
30 | typedef typename | |
31 | std::iterator_traits<InputIterator>::value_type T; | |
32 | typedef typename | |
33 | ::boost::compute::result_of<BinaryFunction(T, T)>::type result_type; | |
34 | ||
35 | const context &context = queue.get_context(); | |
36 | size_t count = detail::iterator_range_size(first, last); | |
37 | if(count == 0){ | |
38 | return; | |
39 | } | |
40 | ||
41 | meta_kernel k("serial_reduce"); | |
42 | size_t count_arg = k.add_arg<cl_uint>("count"); | |
43 | ||
44 | k << | |
45 | k.decl<result_type>("result") << " = " << first[0] << ";\n" << | |
46 | "for(uint i = 1; i < count; i++)\n" << | |
47 | " result = " << function(k.var<T>("result"), | |
48 | first[k.var<uint_>("i")]) << ";\n" << | |
49 | result[0] << " = result;\n"; | |
50 | ||
51 | kernel kernel = k.compile(context); | |
52 | ||
53 | kernel.set_arg(count_arg, static_cast<uint_>(count)); | |
54 | ||
55 | queue.enqueue_task(kernel); | |
56 | } | |
57 | ||
58 | } // end detail namespace | |
59 | } // end compute namespace | |
60 | } // end boost namespace | |
61 | ||
62 | #endif // BOOST_COMPUTE_ALGORITHM_DETAIL_SERIAL_REDUCE_HPP |