File indexing completed on 2025-01-18 09:29:56
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_COMPUTE_ALGORITHM_FOR_EACH_HPP
0012 #define BOOST_COMPUTE_ALGORITHM_FOR_EACH_HPP
0013
0014 #include <boost/static_assert.hpp>
0015
0016 #include <boost/compute/system.hpp>
0017 #include <boost/compute/command_queue.hpp>
0018 #include <boost/compute/detail/meta_kernel.hpp>
0019 #include <boost/compute/detail/iterator_range_size.hpp>
0020 #include <boost/compute/type_traits/is_device_iterator.hpp>
0021
0022 namespace boost {
0023 namespace compute {
0024 namespace detail {
0025
0026 template<class InputIterator, class Function>
0027 struct for_each_kernel : public meta_kernel
0028 {
0029 for_each_kernel(InputIterator first, InputIterator last, Function function)
0030 : meta_kernel("for_each")
0031 {
0032
0033 m_count = detail::iterator_range_size(first, last);
0034
0035
0036 *this << function(first[get_global_id(0)]) << ";\n";
0037 }
0038
0039 void exec(command_queue &queue)
0040 {
0041 exec_1d(queue, 0, m_count);
0042 }
0043
0044 size_t m_count;
0045 };
0046
0047 }
0048
0049
0050
0051
0052
0053
0054 template<class InputIterator, class UnaryFunction>
0055 inline UnaryFunction for_each(InputIterator first,
0056 InputIterator last,
0057 UnaryFunction function,
0058 command_queue &queue = system::default_queue())
0059 {
0060 BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
0061
0062 detail::for_each_kernel<InputIterator, UnaryFunction> kernel(first, last, function);
0063
0064 kernel.exec(queue);
0065
0066 return function;
0067 }
0068
0069 }
0070 }
0071
0072 #endif