Back to home page

EIC code displayed by LXR

 
 

    


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

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_DETAIL_SERIAL_COUNT_IF_HPP
0012 #define BOOST_COMPUTE_ALGORITHM_DETAIL_SERIAL_COUNT_IF_HPP
0013 
0014 #include <iterator>
0015 
0016 #include <boost/compute/container/detail/scalar.hpp>
0017 #include <boost/compute/detail/meta_kernel.hpp>
0018 #include <boost/compute/detail/iterator_range_size.hpp>
0019 
0020 namespace boost {
0021 namespace compute {
0022 namespace detail {
0023 
0024 // counts values that match the predicate using a single thread
0025 template<class InputIterator, class Predicate>
0026 inline size_t serial_count_if(InputIterator first,
0027                               InputIterator last,
0028                               Predicate predicate,
0029                               command_queue &queue)
0030 {
0031     typedef typename std::iterator_traits<InputIterator>::value_type value_type;
0032 
0033     const context &context = queue.get_context();
0034     size_t size = iterator_range_size(first, last);
0035 
0036     meta_kernel k("serial_count_if");
0037     k.add_set_arg("size", static_cast<uint_>(size));
0038     size_t result_arg = k.add_arg<uint_ *>(memory_object::global_memory, "result");
0039 
0040     k <<
0041         "uint count = 0;\n" <<
0042         "for(uint i = 0; i < size; i++){\n" <<
0043             k.decl<const value_type>("value") << "="
0044                 << first[k.var<uint_>("i")] << ";\n" <<
0045             "if(" << predicate(k.var<const value_type>("value")) << "){\n" <<
0046                 "count++;\n" <<
0047             "}\n"
0048         "}\n"
0049         "*result = count;\n";
0050 
0051     kernel kernel = k.compile(context);
0052 
0053     // setup result buffer
0054     scalar<uint_> result(context);
0055     kernel.set_arg(result_arg, result.get_buffer());
0056 
0057     // run kernel
0058     queue.enqueue_task(kernel);
0059 
0060     // read index
0061     return result.read(queue);
0062 }
0063 
0064 } // end detail namespace
0065 } // end compute namespace
0066 } // end boost namespace
0067 
0068 #endif // BOOST_COMPUTE_ALGORITHM_DETAIL_SERIAL_COUNT_IF_HPP