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_ACCUMULATE_HPP
12 #define BOOST_COMPUTE_ALGORITHM_DETAIL_SERIAL_ACCUMULATE_HPP
14 #include <boost/compute/command_queue.hpp>
15 #include <boost/compute/detail/meta_kernel.hpp>
16 #include <boost/compute/detail/iterator_range_size.hpp>
22 template<class InputIterator, class OutputIterator, class T, class BinaryFunction>
23 inline void serial_accumulate(InputIterator first,
25 OutputIterator result,
27 BinaryFunction function,
30 const context &context = queue.get_context();
31 size_t count = detail::iterator_range_size(first, last);
33 meta_kernel k("serial_accumulate");
34 size_t init_arg = k.add_arg<T>("init");
35 size_t count_arg = k.add_arg<cl_uint>("count");
38 k.decl<T>("result") << " = init;\n" <<
39 "for(uint i = 0; i < count; i++)\n" <<
40 " result = " << function(k.var<T>("result"),
41 first[k.var<cl_uint>("i")]) << ";\n" <<
42 result[0] << " = result;\n";
44 kernel kernel = k.compile(context);
46 kernel.set_arg(init_arg, init);
47 kernel.set_arg(count_arg, static_cast<cl_uint>(count));
49 queue.enqueue_task(kernel);
52 } // end detail namespace
53 } // end compute namespace
54 } // end boost namespace
56 #endif // BOOST_COMPUTE_ALGORITHM_DETAIL_SERIAL_ACCUMULATE_HPP