1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 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_COUNT_IF_HPP
12 #define BOOST_COMPUTE_ALGORITHM_COUNT_IF_HPP
14 #include <boost/compute/device.hpp>
15 #include <boost/compute/system.hpp>
16 #include <boost/compute/command_queue.hpp>
17 #include <boost/compute/algorithm/detail/count_if_with_ballot.hpp>
18 #include <boost/compute/algorithm/detail/count_if_with_reduce.hpp>
19 #include <boost/compute/algorithm/detail/count_if_with_threads.hpp>
20 #include <boost/compute/algorithm/detail/serial_count_if.hpp>
21 #include <boost/compute/detail/iterator_range_size.hpp>
26 /// Returns the number of elements in the range [\p first, \p last)
27 /// for which \p predicate returns \c true.
29 /// Space complexity on CPUs: \Omega(1)<br>
30 /// Space complexity on GPUs: \Omega(n)
31 template<class InputIterator, class Predicate>
32 inline size_t count_if(InputIterator first,
35 command_queue &queue = system::default_queue())
37 const device &device = queue.get_device();
39 size_t input_size = detail::iterator_range_size(first, last);
44 if(device.type() & device::cpu){
45 if(input_size < 1024){
46 return detail::serial_count_if(first, last, predicate, queue);
49 return detail::count_if_with_threads(first, last, predicate, queue);
54 return detail::serial_count_if(first, last, predicate, queue);
57 return detail::count_if_with_reduce(first, last, predicate, queue);
62 } // end compute namespace
63 } // end boost namespace
65 #endif // BOOST_COMPUTE_ALGORITHM_COUNT_IF_HPP