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_SERIAL_COUNT_IF_HPP
12 #define BOOST_COMPUTE_ALGORITHM_DETAIL_SERIAL_COUNT_IF_HPP
16 #include <boost/compute/container/detail/scalar.hpp>
17 #include <boost/compute/detail/meta_kernel.hpp>
18 #include <boost/compute/detail/iterator_range_size.hpp>
24 // counts values that match the predicate using a single thread
25 template<class InputIterator, class Predicate>
26 inline size_t serial_count_if(InputIterator first,
31 typedef typename std::iterator_traits<InputIterator>::value_type value_type;
33 const context &context = queue.get_context();
34 size_t size = iterator_range_size(first, last);
36 meta_kernel k("serial_count_if");
37 k.add_set_arg("size", static_cast<uint_>(size));
38 size_t result_arg = k.add_arg<uint_ *>(memory_object::global_memory, "result");
41 "uint count = 0;\n" <<
42 "for(uint i = 0; i < size; i++){\n" <<
43 k.decl<const value_type>("value") << "="
44 << first[k.var<uint_>("i")] << ";\n" <<
45 "if(" << predicate(k.var<const value_type>("value")) << "){\n" <<
51 kernel kernel = k.compile(context);
53 // setup result buffer
54 scalar<uint_> result(context);
55 kernel.set_arg(result_arg, result.get_buffer());
58 queue.enqueue_task(kernel);
61 return result.read(queue);
64 } // end detail namespace
65 } // end compute namespace
66 } // end boost namespace
68 #endif // BOOST_COMPUTE_ALGORITHM_DETAIL_SERIAL_COUNT_IF_HPP