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_REDUCE_HPP
0012 #define BOOST_COMPUTE_ALGORITHM_DETAIL_SERIAL_REDUCE_HPP
0013 
0014 #include <boost/compute/command_queue.hpp>
0015 #include <boost/compute/detail/meta_kernel.hpp>
0016 #include <boost/compute/detail/iterator_range_size.hpp>
0017 #include <boost/compute/type_traits/result_of.hpp>
0018 
0019 namespace boost {
0020 namespace compute {
0021 namespace detail {
0022 
0023 // Space complexity: O(1)
0024 template<class InputIterator, class OutputIterator, class BinaryFunction>
0025 inline void serial_reduce(InputIterator first,
0026                           InputIterator last,
0027                           OutputIterator result,
0028                           BinaryFunction function,
0029                           command_queue &queue)
0030 {
0031     typedef typename
0032         std::iterator_traits<InputIterator>::value_type T;
0033     typedef typename
0034         ::boost::compute::result_of<BinaryFunction(T, T)>::type result_type;
0035 
0036     const context &context = queue.get_context();
0037     size_t count = detail::iterator_range_size(first, last);
0038     if(count == 0){
0039         return;
0040     }
0041 
0042     meta_kernel k("serial_reduce");
0043     size_t count_arg = k.add_arg<cl_uint>("count");
0044 
0045     k <<
0046         k.decl<result_type>("result") << " = " << first[0] << ";\n" <<
0047         "for(uint i = 1; i < count; i++)\n" <<
0048         "    result = " << function(k.var<T>("result"),
0049                                     first[k.var<uint_>("i")]) << ";\n" <<
0050         result[0] << " = result;\n";
0051 
0052     kernel kernel = k.compile(context);
0053 
0054     kernel.set_arg(count_arg, static_cast<uint_>(count));
0055 
0056     queue.enqueue_task(kernel);
0057 }
0058 
0059 } // end detail namespace
0060 } // end compute namespace
0061 } // end boost namespace
0062 
0063 #endif // BOOST_COMPUTE_ALGORITHM_DETAIL_SERIAL_REDUCE_HPP