• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 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_COUNT_IF_HPP
12 #define BOOST_COMPUTE_ALGORITHM_COUNT_IF_HPP
13 
14 #include <boost/static_assert.hpp>
15 
16 #include <boost/compute/device.hpp>
17 #include <boost/compute/system.hpp>
18 #include <boost/compute/command_queue.hpp>
19 #include <boost/compute/algorithm/detail/count_if_with_ballot.hpp>
20 #include <boost/compute/algorithm/detail/count_if_with_reduce.hpp>
21 #include <boost/compute/algorithm/detail/count_if_with_threads.hpp>
22 #include <boost/compute/algorithm/detail/serial_count_if.hpp>
23 #include <boost/compute/detail/iterator_range_size.hpp>
24 #include <boost/compute/type_traits/is_device_iterator.hpp>
25 
26 namespace boost {
27 namespace compute {
28 
29 /// Returns the number of elements in the range [\p first, \p last)
30 /// for which \p predicate returns \c true.
31 ///
32 /// Space complexity on CPUs: \Omega(1)<br>
33 /// Space complexity on GPUs: \Omega(n)
34 template<class InputIterator, class Predicate>
count_if(InputIterator first,InputIterator last,Predicate predicate,command_queue & queue=system::default_queue ())35 inline size_t count_if(InputIterator first,
36                        InputIterator last,
37                        Predicate predicate,
38                        command_queue &queue = system::default_queue())
39 {
40     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
41     const device &device = queue.get_device();
42 
43     size_t input_size = detail::iterator_range_size(first, last);
44     if(input_size == 0){
45         return 0;
46     }
47 
48     if(device.type() & device::cpu){
49         if(input_size < 1024){
50             return detail::serial_count_if(first, last, predicate, queue);
51         }
52         else {
53             return detail::count_if_with_threads(first, last, predicate, queue);
54         }
55     }
56     else {
57         if(input_size < 32){
58             return detail::serial_count_if(first, last, predicate, queue);
59         }
60         else {
61             return detail::count_if_with_reduce(first, last, predicate, queue);
62         }
63     }
64 }
65 
66 } // end compute namespace
67 } // end boost namespace
68 
69 #endif // BOOST_COMPUTE_ALGORITHM_COUNT_IF_HPP
70