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_DETAIL_COUNT_IF_WITH_BALLOT_HPP
12 #define BOOST_COMPUTE_ALGORITHM_DETAIL_COUNT_IF_WITH_BALLOT_HPP
14 #include <boost/compute/context.hpp>
15 #include <boost/compute/command_queue.hpp>
16 #include <boost/compute/container/vector.hpp>
17 #include <boost/compute/algorithm/reduce.hpp>
18 #include <boost/compute/functional/detail/nvidia_ballot.hpp>
19 #include <boost/compute/functional/detail/nvidia_popcount.hpp>
20 #include <boost/compute/detail/meta_kernel.hpp>
26 template<class InputIterator, class Predicate>
27 inline size_t count_if_with_ballot(InputIterator first,
32 size_t count = iterator_range_size(first, last);
33 size_t block_size = 32;
34 size_t block_count = count / block_size;
35 if(block_count * block_size != count){
39 const ::boost::compute::context &context = queue.get_context();
41 ::boost::compute::vector<uint_> counts(block_count, context);
43 ::boost::compute::detail::nvidia_popcount<uint_> popc;
44 ::boost::compute::detail::nvidia_ballot<uint_> ballot;
46 meta_kernel k("count_if_with_ballot");
48 "const uint gid = get_global_id(0);\n" <<
50 "bool value = false;\n" <<
51 "if(gid < count)\n" <<
52 " value = " << predicate(first[k.var<const uint_>("gid")]) << ";\n" <<
54 "uint bits = " << ballot(k.var<const uint_>("value")) << ";\n" <<
56 "if(get_local_id(0) == 0)\n" <<
57 counts.begin()[k.var<uint_>("get_group_id(0)") ]
58 << " = " << popc(k.var<uint_>("bits")) << ";\n";
60 k.add_set_arg<const uint_>("count", count);
62 k.exec_1d(queue, 0, block_size * block_count, block_size);
65 ::boost::compute::reduce(
74 } // end detail namespace
75 } // end compute namespace
76 } // end boost namespace
78 #endif // BOOST_COMPUTE_ALGORITHM_DETAIL_COUNT_IF_WITH_BALLOT_HPP