]>
Commit | Line | Data |
---|---|---|
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/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> | |
22 | ||
23 | namespace boost { | |
24 | namespace compute { | |
25 | ||
26 | namespace detail { | |
27 | ||
28 | template<class InputIterator, class OutputIterator, class BinaryFunction> | |
29 | inline OutputIterator | |
30 | dispatch_adjacent_difference(InputIterator first, | |
31 | InputIterator last, | |
32 | OutputIterator result, | |
33 | BinaryFunction op, | |
34 | command_queue &queue = system::default_queue()) | |
35 | { | |
36 | size_t count = detail::iterator_range_size(first, last); | |
37 | detail::meta_kernel k("adjacent_difference"); | |
38 | ||
39 | k << "const uint i = get_global_id(0);\n" | |
40 | << "if(i == 0){\n" | |
41 | << " " << result[k.var<uint_>("0")] << " = " << first[k.var<uint_>("0")] << ";\n" | |
42 | << "}\n" | |
43 | << "else {\n" | |
44 | << " " << result[k.var<uint_>("i")] << " = " | |
45 | << op(first[k.var<uint_>("i")], first[k.var<uint_>("i-1")]) << ";\n" | |
46 | << "}\n"; | |
47 | ||
48 | k.exec_1d(queue, 0, count, 1); | |
49 | ||
50 | return result + count; | |
51 | } | |
52 | ||
53 | } // end detail namespace | |
54 | ||
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. | |
58 | /// | |
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 | |
64 | /// | |
65 | /// \return \c OutputIterator to the end of the result range | |
66 | /// | |
67 | /// \see adjacent_find() | |
68 | template<class InputIterator, class OutputIterator, class BinaryFunction> | |
69 | inline OutputIterator | |
70 | adjacent_difference(InputIterator first, | |
71 | InputIterator last, | |
72 | OutputIterator result, | |
73 | BinaryFunction op, | |
74 | command_queue &queue = system::default_queue()) | |
75 | { | |
76 | typedef typename std::iterator_traits<InputIterator>::value_type value_type; | |
77 | ||
78 | if(first == last) { | |
79 | return result; | |
80 | } | |
81 | ||
82 | if (first == result) { | |
83 | vector<value_type> temp(detail::iterator_range_size(first, last), | |
84 | queue.get_context()); | |
85 | copy(first, last, temp.begin(), queue); | |
86 | ||
87 | return ::boost::compute::detail::dispatch_adjacent_difference( | |
88 | temp.begin(), temp.end(), result, op, queue | |
89 | ); | |
90 | } | |
91 | else { | |
92 | return ::boost::compute::detail::dispatch_adjacent_difference( | |
93 | first, last, result, op, queue | |
94 | ); | |
95 | } | |
96 | } | |
97 | ||
98 | /// \overload | |
99 | template<class InputIterator, class OutputIterator> | |
100 | inline OutputIterator | |
101 | adjacent_difference(InputIterator first, | |
102 | InputIterator last, | |
103 | OutputIterator result, | |
104 | command_queue &queue = system::default_queue()) | |
105 | { | |
106 | typedef typename std::iterator_traits<InputIterator>::value_type value_type; | |
107 | ||
108 | return ::boost::compute::adjacent_difference( | |
109 | first, last, result, ::boost::compute::minus<value_type>(), queue | |
110 | ); | |
111 | } | |
112 | ||
113 | } // end compute namespace | |
114 | } // end boost namespace | |
115 | ||
116 | #endif // BOOST_COMPUTE_ALGORITHM_ADJACENT_DIFFERENCE_HPP |