• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_INCLUSIVE_SCAN_HPP
12 #define BOOST_COMPUTE_ALGORITHM_INCLUSIVE_SCAN_HPP
13 
14 #include <boost/static_assert.hpp>
15 
16 #include <boost/compute/functional.hpp>
17 #include <boost/compute/system.hpp>
18 #include <boost/compute/command_queue.hpp>
19 #include <boost/compute/algorithm/detail/scan.hpp>
20 #include <boost/compute/type_traits/is_device_iterator.hpp>
21 
22 namespace boost {
23 namespace compute {
24 
25 /// Performs an inclusive scan of the elements in the range [\p first, \p last)
26 /// and stores the results in the range beginning at \p result.
27 ///
28 /// Each element in the output is assigned to the sum of the current value in
29 /// the input with the sum of every previous value in the input.
30 ///
31 /// \param first first element in the range to scan
32 /// \param last last element in the range to scan
33 /// \param result first element in the result range
34 /// \param binary_op associative binary operator
35 /// \param queue command queue to perform the operation
36 ///
37 /// \return \c OutputIterator to the end of the result range
38 ///
39 /// The default operation is to add the elements up.
40 ///
41 /// \snippet test/test_scan.cpp inclusive_scan_int
42 ///
43 /// But different associative operation can be specified as \p binary_op
44 /// instead (e.g., multiplication, maximum, minimum).
45 ///
46 /// \snippet test/test_scan.cpp inclusive_scan_int_multiplies
47 ///
48 /// Space complexity on GPUs: \Omega(n)<br>
49 /// Space complexity on GPUs when \p first == \p result: \Omega(2n)<br>
50 /// Space complexity on CPUs: \Omega(1)
51 ///
52 /// \see exclusive_scan()
53 template<class InputIterator, class OutputIterator, class BinaryOperator>
54 inline OutputIterator
inclusive_scan(InputIterator first,InputIterator last,OutputIterator result,BinaryOperator binary_op,command_queue & queue=system::default_queue ())55 inclusive_scan(InputIterator first,
56                InputIterator last,
57                OutputIterator result,
58                BinaryOperator binary_op,
59                command_queue &queue = system::default_queue())
60 {
61     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
62     BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
63     typedef typename
64         std::iterator_traits<OutputIterator>::value_type output_type;
65 
66     return detail::scan(first, last, result, false,
67                         output_type(0), binary_op,
68                         queue);
69 }
70 
71 /// \overload
72 template<class InputIterator, class OutputIterator>
73 inline OutputIterator
inclusive_scan(InputIterator first,InputIterator last,OutputIterator result,command_queue & queue=system::default_queue ())74 inclusive_scan(InputIterator first,
75                InputIterator last,
76                OutputIterator result,
77                command_queue &queue = system::default_queue())
78 {
79     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
80     BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
81     typedef typename
82         std::iterator_traits<OutputIterator>::value_type output_type;
83 
84     return detail::scan(first, last, result, false,
85                         output_type(0), boost::compute::plus<output_type>(),
86                         queue);
87 }
88 
89 } // end compute namespace
90 } // end boost namespace
91 
92 #endif // BOOST_COMPUTE_ALGORITHM_INCLUSIVE_SCAN_HPP
93