1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
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
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_ALGORITHM_ADJACENT_DIFFERENCE_HPP
12 #define BOOST_COMPUTE_ALGORITHM_ADJACENT_DIFFERENCE_HPP
16 #include <boost/compute/system.hpp>
17 #include <boost/compute/command_queue.hpp>
18 #include <boost/compute/detail/meta_kernel.hpp>
19 #include <boost/compute/detail/iterator_range_size.hpp>
20 #include <boost/compute/functional/operator.hpp>
21 #include <boost/compute/container/vector.hpp>
28 template<class InputIterator, class OutputIterator, class BinaryFunction>
30 dispatch_adjacent_difference(InputIterator first,
32 OutputIterator result,
34 command_queue &queue = system::default_queue())
36 size_t count = detail::iterator_range_size(first, last);
37 detail::meta_kernel k("adjacent_difference");
39 k << "const uint i = get_global_id(0);\n"
41 << " " << result[k.var<uint_>("0")] << " = " << first[k.var<uint_>("0")] << ";\n"
44 << " " << result[k.var<uint_>("i")] << " = "
45 << op(first[k.var<uint_>("i")], first[k.var<uint_>("i-1")]) << ";\n"
48 k.exec_1d(queue, 0, count, 1);
50 return result + count;
53 } // end detail namespace
55 /// Stores the difference of each pair of consecutive values in the range
56 /// [\p first, \p last) to the range beginning at \p result. If \p op is not
57 /// provided, \c minus<T> is used.
59 /// \param first first element in the input range
60 /// \param last last element in the input range
61 /// \param result first element in the output range
62 /// \param op binary difference function
63 /// \param queue command queue to perform the operation
65 /// \return \c OutputIterator to the end of the result range
67 /// \see adjacent_find()
68 template<class InputIterator, class OutputIterator, class BinaryFunction>
70 adjacent_difference(InputIterator first,
72 OutputIterator result,
74 command_queue &queue = system::default_queue())
76 typedef typename std::iterator_traits<InputIterator>::value_type value_type;
82 if (first == result) {
83 vector<value_type> temp(detail::iterator_range_size(first, last),
85 copy(first, last, temp.begin(), queue);
87 return ::boost::compute::detail::dispatch_adjacent_difference(
88 temp.begin(), temp.end(), result, op, queue
92 return ::boost::compute::detail::dispatch_adjacent_difference(
93 first, last, result, op, queue
99 template<class InputIterator, class OutputIterator>
100 inline OutputIterator
101 adjacent_difference(InputIterator first,
103 OutputIterator result,
104 command_queue &queue = system::default_queue())
106 typedef typename std::iterator_traits<InputIterator>::value_type value_type;
108 return ::boost::compute::adjacent_difference(
109 first, last, result, ::boost::compute::minus<value_type>(), queue
113 } // end compute namespace
114 } // end boost namespace
116 #endif // BOOST_COMPUTE_ALGORITHM_ADJACENT_DIFFERENCE_HPP