Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:56

0001 //---------------------------------------------------------------------------//
0002 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
0003 //
0004 // Distributed under the Boost Software License, Version 1.0
0005 // See accompanying file LICENSE_1_0.txt or copy at
0006 // http://www.boost.org/LICENSE_1_0.txt
0007 //
0008 // See http://boostorg.github.com/compute for more information.
0009 //---------------------------------------------------------------------------//
0010 
0011 #ifndef BOOST_COMPUTE_ALGORITHM_COUNT_IF_HPP
0012 #define BOOST_COMPUTE_ALGORITHM_COUNT_IF_HPP
0013 
0014 #include <boost/static_assert.hpp>
0015 
0016 #include <boost/compute/device.hpp>
0017 #include <boost/compute/system.hpp>
0018 #include <boost/compute/command_queue.hpp>
0019 #include <boost/compute/algorithm/detail/count_if_with_ballot.hpp>
0020 #include <boost/compute/algorithm/detail/count_if_with_reduce.hpp>
0021 #include <boost/compute/algorithm/detail/count_if_with_threads.hpp>
0022 #include <boost/compute/algorithm/detail/serial_count_if.hpp>
0023 #include <boost/compute/detail/iterator_range_size.hpp>
0024 #include <boost/compute/type_traits/is_device_iterator.hpp>
0025 
0026 namespace boost {
0027 namespace compute {
0028 
0029 /// Returns the number of elements in the range [\p first, \p last)
0030 /// for which \p predicate returns \c true.
0031 ///
0032 /// Space complexity on CPUs: \Omega(1)<br>
0033 /// Space complexity on GPUs: \Omega(n)
0034 template<class InputIterator, class Predicate>
0035 inline size_t count_if(InputIterator first,
0036                        InputIterator last,
0037                        Predicate predicate,
0038                        command_queue &queue = system::default_queue())
0039 {
0040     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
0041     const device &device = queue.get_device();
0042 
0043     size_t input_size = detail::iterator_range_size(first, last);
0044     if(input_size == 0){
0045         return 0;
0046     }
0047 
0048     if(device.type() & device::cpu){
0049         if(input_size < 1024){
0050             return detail::serial_count_if(first, last, predicate, queue);
0051         }
0052         else {
0053             return detail::count_if_with_threads(first, last, predicate, queue);
0054         }
0055     }
0056     else {
0057         if(input_size < 32){
0058             return detail::serial_count_if(first, last, predicate, queue);
0059         }
0060         else {
0061             return detail::count_if_with_reduce(first, last, predicate, queue);
0062         }
0063     }
0064 }
0065 
0066 } // end compute namespace
0067 } // end boost namespace
0068 
0069 #endif // BOOST_COMPUTE_ALGORITHM_COUNT_IF_HPP