]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | //---------------------------------------------------------------------------// |
2 | // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com> | |
3 | // | |
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 | |
7 | // | |
8 | // See http://boostorg.github.com/compute for more information. | |
9 | //---------------------------------------------------------------------------// | |
10 | ||
11 | #ifndef BOOST_COMPUTE_ALGORITHM_FOR_EACH_HPP | |
12 | #define BOOST_COMPUTE_ALGORITHM_FOR_EACH_HPP | |
13 | ||
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> | |
18 | ||
19 | namespace boost { | |
20 | namespace compute { | |
21 | namespace detail { | |
22 | ||
23 | template<class InputIterator, class Function> | |
24 | struct for_each_kernel : public meta_kernel | |
25 | { | |
26 | for_each_kernel(InputIterator first, InputIterator last, Function function) | |
27 | : meta_kernel("for_each") | |
28 | { | |
29 | // store range size | |
30 | m_count = detail::iterator_range_size(first, last); | |
31 | ||
32 | // setup kernel source | |
33 | *this << function(first[get_global_id(0)]) << ";\n"; | |
34 | } | |
35 | ||
36 | void exec(command_queue &queue) | |
37 | { | |
38 | exec_1d(queue, 0, m_count); | |
39 | } | |
40 | ||
41 | size_t m_count; | |
42 | }; | |
43 | ||
44 | } // end detail namespace | |
45 | ||
46 | /// Calls \p function on each element in the range [\p first, \p last). | |
47 | /// | |
48 | /// \see transform() | |
49 | template<class InputIterator, class UnaryFunction> | |
50 | inline UnaryFunction for_each(InputIterator first, | |
51 | InputIterator last, | |
52 | UnaryFunction function, | |
53 | command_queue &queue = system::default_queue()) | |
54 | { | |
55 | detail::for_each_kernel<InputIterator, UnaryFunction> kernel(first, last, function); | |
56 | ||
57 | kernel.exec(queue); | |
58 | ||
59 | return function; | |
60 | } | |
61 | ||
62 | } // end compute namespace | |
63 | } // end boost namespace | |
64 | ||
65 | #endif // BOOST_COMPUTE_ALGORITHM_FOR_EACH_HPP |