]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/compute/include/boost/compute/algorithm/adjacent_find.hpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / compute / include / boost / compute / algorithm / adjacent_find.hpp
CommitLineData
7c673cae
FG
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_FIND_HPP
12#define BOOST_COMPUTE_ALGORITHM_ADJACENT_FIND_HPP
13
14#include <iterator>
15
16#include <boost/compute/command_queue.hpp>
17#include <boost/compute/lambda.hpp>
18#include <boost/compute/system.hpp>
19#include <boost/compute/container/detail/scalar.hpp>
20#include <boost/compute/detail/iterator_range_size.hpp>
21#include <boost/compute/detail/meta_kernel.hpp>
22#include <boost/compute/functional/operator.hpp>
23#include <boost/compute/type_traits/vector_size.hpp>
24
25namespace boost {
26namespace compute {
27namespace detail {
28
29template<class InputIterator, class Compare>
30inline InputIterator
31serial_adjacent_find(InputIterator first,
32 InputIterator last,
33 Compare compare,
34 command_queue &queue)
35{
36 if(first == last){
37 return last;
38 }
39
40 const context &context = queue.get_context();
41
42 detail::scalar<uint_> output(context);
43
44 detail::meta_kernel k("serial_adjacent_find");
45
46 size_t size_arg = k.add_arg<const uint_>("size");
47 size_t output_arg = k.add_arg<uint_ *>(memory_object::global_memory, "output");
48
49 k << k.decl<uint_>("result") << " = size;\n"
50 << "for(uint i = 0; i < size - 1; i++){\n"
51 << " if(" << compare(first[k.expr<uint_>("i")],
52 first[k.expr<uint_>("i+1")]) << "){\n"
53 << " result = i;\n"
54 << " break;\n"
55 << " }\n"
56 << "}\n"
57 << "*output = result;\n";
58
59 k.set_arg<const uint_>(
60 size_arg, static_cast<uint_>(detail::iterator_range_size(first, last))
61 );
62 k.set_arg(output_arg, output.get_buffer());
63
64 k.exec_1d(queue, 0, 1, 1);
65
66 return first + output.read(queue);
67}
68
69template<class InputIterator, class Compare>
70inline InputIterator
71adjacent_find_with_atomics(InputIterator first,
72 InputIterator last,
73 Compare compare,
74 command_queue &queue)
75{
76 if(first == last){
77 return last;
78 }
79
80 const context &context = queue.get_context();
81 size_t count = detail::iterator_range_size(first, last);
82
83 // initialize output to the last index
84 detail::scalar<uint_> output(context);
85 output.write(static_cast<uint_>(count), queue);
86
87 detail::meta_kernel k("adjacent_find_with_atomics");
88
89 size_t output_arg = k.add_arg<uint_ *>(memory_object::global_memory, "output");
90
91 k << "const uint i = get_global_id(0);\n"
92 << "if(" << compare(first[k.expr<uint_>("i")],
93 first[k.expr<uint_>("i+1")]) << "){\n"
94 << " atomic_min(output, i);\n"
95 << "}\n";
96
97 k.set_arg(output_arg, output.get_buffer());
98
99 k.exec_1d(queue, 0, count - 1, 1);
100
101 return first + output.read(queue);
102}
103
104} // end detail namespace
105
106/// Searches the range [\p first, \p last) for two identical adjacent
107/// elements and returns an iterator pointing to the first.
108///
109/// \param first first element in the range to search
110/// \param last last element in the range to search
111/// \param compare binary comparison function
112/// \param queue command queue to perform the operation
113///
114/// \return \c InputIteratorm to the first element which compares equal
115/// to the following element. If none are equal, returns \c last.
116///
117/// \see find(), adjacent_difference()
118template<class InputIterator, class Compare>
119inline InputIterator
120adjacent_find(InputIterator first,
121 InputIterator last,
122 Compare compare,
123 command_queue &queue = system::default_queue())
124{
125 size_t count = detail::iterator_range_size(first, last);
126 if(count < 32){
127 return detail::serial_adjacent_find(first, last, compare, queue);
128 }
129 else {
130 return detail::adjacent_find_with_atomics(first, last, compare, queue);
131 }
132}
133
134/// \overload
135template<class InputIterator>
136inline InputIterator
137adjacent_find(InputIterator first,
138 InputIterator last,
139 command_queue &queue = system::default_queue())
140{
141 typedef typename std::iterator_traits<InputIterator>::value_type value_type;
142
143 using ::boost::compute::lambda::_1;
144 using ::boost::compute::lambda::_2;
145 using ::boost::compute::lambda::all;
146
147 if(vector_size<value_type>::value == 1){
148 return ::boost::compute::adjacent_find(
149 first, last, _1 == _2, queue
150 );
151 }
152 else {
153 return ::boost::compute::adjacent_find(
154 first, last, all(_1 == _2), queue
155 );
156 }
157}
158
159} // end compute namespace
160} // end boost namespace
161
162#endif // BOOST_COMPUTE_ALGORITHM_ADJACENT_FIND_HPP