1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2014 Roshan <thisisroshansmail@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_DETAIL_BINARY_FIND_HPP
12 #define BOOST_COMPUTE_ALGORITHM_DETAIL_BINARY_FIND_HPP
14 #include <boost/compute/functional.hpp>
15 #include <boost/compute/algorithm/find_if.hpp>
16 #include <boost/compute/algorithm/transform.hpp>
17 #include <boost/compute/command_queue.hpp>
18 #include <boost/compute/detail/parameter_cache.hpp>
25 /// \brief Binary find kernel class
27 /// Subclass of meta_kernel to perform single step in binary find.
29 template<class InputIterator, class UnaryPredicate>
30 class binary_find_kernel : public meta_kernel
33 binary_find_kernel(InputIterator first,
35 UnaryPredicate predicate)
36 : meta_kernel("binary_find")
38 typedef typename std::iterator_traits<InputIterator>::value_type value_type;
40 m_index_arg = add_arg<uint_ *>(memory_object::global_memory, "index");
41 m_block_arg = add_arg<uint_>("block");
43 atomic_min<uint_> atomic_min_uint;
46 "uint i = get_global_id(0) * block;\n" <<
47 decl<value_type>("value") << "=" << first[var<uint_>("i")] << ";\n" <<
48 "if(" << predicate(var<value_type>("value")) << ") {\n" <<
49 atomic_min_uint(var<uint_ *>("index"), var<uint_>("i")) << ";\n" <<
58 /// \brief Binary find algorithm
60 /// Finds the end of true values in the partitioned range [first, last).
61 /// \return Iterator pointing to end of true values
63 /// \param first Iterator pointing to start of range
64 /// \param last Iterator pointing to end of range
65 /// \param predicate Predicate according to which the range is partitioned
66 /// \param queue Queue on which to execute
68 template<class InputIterator, class UnaryPredicate>
69 inline InputIterator binary_find(InputIterator first,
71 UnaryPredicate predicate,
72 command_queue &queue = system::default_queue())
74 const device &device = queue.get_device();
76 boost::shared_ptr<parameter_cache> parameters =
77 detail::parameter_cache::get_global_cache(device);
79 const std::string cache_key = "__boost_binary_find";
81 size_t find_if_limit = 128;
82 size_t threads = parameters->get(cache_key, "tpb", 128);
83 size_t count = iterator_range_size(first, last);
85 InputIterator search_first = first;
86 InputIterator search_last = last;
88 scalar<uint_> index(queue.get_context());
90 // construct and compile binary_find kernel
91 binary_find_kernel<InputIterator, UnaryPredicate>
92 binary_find_kernel(search_first, search_last, predicate);
93 ::boost::compute::kernel kernel = binary_find_kernel.compile(queue.get_context());
95 // set buffer for index
96 kernel.set_arg(binary_find_kernel.m_index_arg, index.get_buffer());
98 while(count > find_if_limit) {
99 index.write(static_cast<uint_>(count), queue);
101 // set block and run binary_find kernel
102 uint_ block = static_cast<uint_>((count - 1)/(threads - 1));
103 kernel.set_arg(binary_find_kernel.m_block_arg, block);
104 queue.enqueue_1d_range_kernel(kernel, 0, threads, 0);
106 size_t i = index.read(queue);
109 search_first = search_last - ((count - 1)%(threads - 1));
112 search_last = search_first + i;
113 search_first = search_last - ((count - 1)/(threads - 1));
116 // Make sure that first and last stay within the input range
117 search_last = (std::min)(search_last, last);
118 search_last = (std::max)(search_last, first);
120 search_first = (std::max)(search_first, first);
121 search_first = (std::min)(search_first, last);
123 count = iterator_range_size(search_first, search_last);
126 return find_if(search_first, search_last, predicate, queue);
129 } // end detail namespace
130 } // end compute namespace
131 } // end boost namespace
133 #endif // BOOST_COMPUTE_ALGORITHM_DETAIL_BINARY_FIND_HPP