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_SCAN_HPP
12 #define BOOST_COMPUTE_ALGORITHM_DETAIL_SERIAL_SCAN_HPP
13 
14 #include <iterator>
15 
16 #include <boost/compute/device.hpp>
17 #include <boost/compute/kernel.hpp>
18 #include <boost/compute/command_queue.hpp>
19 #include <boost/compute/detail/meta_kernel.hpp>
20 #include <boost/compute/detail/iterator_range_size.hpp>
21 
22 namespace boost {
23 namespace compute {
24 namespace detail {
25 
26 template<class InputIterator, class OutputIterator, class T, class BinaryOperator>
serial_scan(InputIterator first,InputIterator last,OutputIterator result,bool exclusive,T init,BinaryOperator op,command_queue & queue)27 inline OutputIterator serial_scan(InputIterator first,
28                                   InputIterator last,
29                                   OutputIterator result,
30                                   bool exclusive,
31                                   T init,
32                                   BinaryOperator op,
33                                   command_queue &queue)
34 {
35     if(first == last){
36         return result;
37     }
38 
39     typedef typename
40         std::iterator_traits<InputIterator>::value_type input_type;
41     typedef typename
42         std::iterator_traits<OutputIterator>::value_type output_type;
43 
44     const context &context = queue.get_context();
45 
46     // create scan kernel
47     meta_kernel k("serial_scan");
48 
49     // Arguments
50     size_t n_arg = k.add_arg<ulong_>("n");
51     size_t init_arg = k.add_arg<output_type>("initial_value");
52 
53     if(!exclusive){
54         k <<
55             k.decl<const ulong_>("start_idx") << " = 1;\n" <<
56             k.decl<output_type>("sum") << " = " << first[0] << ";\n" <<
57             result[0] << " = sum;\n";
58     }
59     else {
60         k <<
61             k.decl<const ulong_>("start_idx") << " = 0;\n" <<
62             k.decl<output_type>("sum") << " = initial_value;\n";
63     }
64 
65     k <<
66         "for(ulong i = start_idx; i < n; i++){\n" <<
67         k.decl<const input_type>("x") << " = "
68             << first[k.var<ulong_>("i")] << ";\n";
69 
70     if(exclusive){
71         k << result[k.var<ulong_>("i")] << " = sum;\n";
72     }
73 
74     k << "    sum = "
75         << op(k.var<output_type>("sum"), k.var<output_type>("x"))
76         << ";\n";
77 
78     if(!exclusive){
79         k << result[k.var<ulong_>("i")] << " = sum;\n";
80     }
81 
82     k << "}\n";
83 
84     // compile scan kernel
85     kernel scan_kernel = k.compile(context);
86 
87     // setup kernel arguments
88     size_t n = detail::iterator_range_size(first, last);
89     scan_kernel.set_arg<ulong_>(n_arg, n);
90     scan_kernel.set_arg<output_type>(init_arg, static_cast<output_type>(init));
91 
92     // execute the kernel
93     queue.enqueue_1d_range_kernel(scan_kernel, 0, 1, 1);
94 
95     // return iterator pointing to the end of the result range
96     return result + n;
97 }
98 
99 } // end detail namespace
100 } // end compute namespace
101 } // end boost namespace
102 
103 #endif // BOOST_COMPUTE_ALGORITHM_DETAIL_SERIAL_SCAN_HPP
104