• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013-2014 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_ADJACENT_DIFFERENCE_HPP
12 #define BOOST_COMPUTE_ALGORITHM_ADJACENT_DIFFERENCE_HPP
13 
14 #include <iterator>
15 
16 #include <boost/static_assert.hpp>
17 
18 #include <boost/compute/system.hpp>
19 #include <boost/compute/command_queue.hpp>
20 #include <boost/compute/detail/meta_kernel.hpp>
21 #include <boost/compute/detail/iterator_range_size.hpp>
22 #include <boost/compute/functional/operator.hpp>
23 #include <boost/compute/container/vector.hpp>
24 #include <boost/compute/type_traits/is_device_iterator.hpp>
25 
26 namespace boost {
27 namespace compute {
28 
29 namespace detail {
30 
31 template<class InputIterator, class OutputIterator, class BinaryFunction>
32 inline OutputIterator
dispatch_adjacent_difference(InputIterator first,InputIterator last,OutputIterator result,BinaryFunction op,command_queue & queue=system::default_queue ())33 dispatch_adjacent_difference(InputIterator first,
34                              InputIterator last,
35                              OutputIterator result,
36                              BinaryFunction op,
37                              command_queue &queue = system::default_queue())
38 {
39     size_t count = detail::iterator_range_size(first, last);
40     detail::meta_kernel k("adjacent_difference");
41 
42     k << "const uint i = get_global_id(0);\n"
43       << "if(i == 0){\n"
44       << "    " << result[k.var<uint_>("0")] << " = " << first[k.var<uint_>("0")] << ";\n"
45       << "}\n"
46       << "else {\n"
47       << "    " << result[k.var<uint_>("i")] << " = "
48       <<               op(first[k.var<uint_>("i")], first[k.var<uint_>("i-1")]) << ";\n"
49       << "}\n";
50 
51     k.exec_1d(queue, 0, count, 1);
52 
53     return result + count;
54 }
55 
56 } // end detail namespace
57 
58 /// Stores the difference of each pair of consecutive values in the range
59 /// [\p first, \p last) to the range beginning at \p result. If \p op is not
60 /// provided, \c minus<T> is used.
61 ///
62 /// \param first first element in the input range
63 /// \param last last element in the input range
64 /// \param result first element in the output range
65 /// \param op binary difference function
66 /// \param queue command queue to perform the operation
67 ///
68 /// \return \c OutputIterator to the end of the result range
69 ///
70 /// Space complexity: \Omega(1)<br>
71 /// Space complexity when \p result == \p first: \Omega(n)
72 ///
73 /// \see adjacent_find()
74 template<class InputIterator, class OutputIterator, class BinaryFunction>
75 inline OutputIterator
adjacent_difference(InputIterator first,InputIterator last,OutputIterator result,BinaryFunction op,command_queue & queue=system::default_queue ())76 adjacent_difference(InputIterator first,
77                     InputIterator last,
78                     OutputIterator result,
79                     BinaryFunction op,
80                     command_queue &queue = system::default_queue())
81 {
82     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
83     BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
84     typedef typename std::iterator_traits<InputIterator>::value_type value_type;
85 
86     if(first == last) {
87         return result;
88     }
89 
90     if (first == result) {
91         vector<value_type> temp(detail::iterator_range_size(first, last),
92                                 queue.get_context());
93         copy(first, last, temp.begin(), queue);
94 
95         return ::boost::compute::detail::dispatch_adjacent_difference(
96             temp.begin(), temp.end(), result, op, queue
97         );
98     }
99     else {
100         return ::boost::compute::detail::dispatch_adjacent_difference(
101             first, last, result, op, queue
102         );
103     }
104 }
105 
106 /// \overload
107 template<class InputIterator, class OutputIterator>
108 inline OutputIterator
adjacent_difference(InputIterator first,InputIterator last,OutputIterator result,command_queue & queue=system::default_queue ())109 adjacent_difference(InputIterator first,
110                     InputIterator last,
111                     OutputIterator result,
112                     command_queue &queue = system::default_queue())
113 {
114     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
115     BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
116     typedef typename std::iterator_traits<InputIterator>::value_type value_type;
117 
118     return ::boost::compute::adjacent_difference(
119         first, last, result, ::boost::compute::minus<value_type>(), queue
120     );
121 }
122 
123 } // end compute namespace
124 } // end boost namespace
125 
126 #endif // BOOST_COMPUTE_ALGORITHM_ADJACENT_DIFFERENCE_HPP
127