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_FOR_EACH_HPP
12 #define BOOST_COMPUTE_ALGORITHM_FOR_EACH_HPP
14 #include <boost/compute/system.hpp>
15 #include <boost/compute/command_queue.hpp>
16 #include <boost/compute/detail/meta_kernel.hpp>
17 #include <boost/compute/detail/iterator_range_size.hpp>
23 template<class InputIterator, class Function>
24 struct for_each_kernel : public meta_kernel
26 for_each_kernel(InputIterator first, InputIterator last, Function function)
27 : meta_kernel("for_each")
30 m_count = detail::iterator_range_size(first, last);
32 // setup kernel source
33 *this << function(first[get_global_id(0)]) << ";\n";
36 void exec(command_queue &queue)
38 exec_1d(queue, 0, m_count);
44 } // end detail namespace
46 /// Calls \p function on each element in the range [\p first, \p last).
49 template<class InputIterator, class UnaryFunction>
50 inline UnaryFunction for_each(InputIterator first,
52 UnaryFunction function,
53 command_queue &queue = system::default_queue())
55 detail::for_each_kernel<InputIterator, UnaryFunction> kernel(first, last, function);
62 } // end compute namespace
63 } // end boost namespace
65 #endif // BOOST_COMPUTE_ALGORITHM_FOR_EACH_HPP