Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:56

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_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         // store range size
0033         m_count = detail::iterator_range_size(first, last);
0034 
0035         // setup kernel source
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 } // end detail namespace
0048 
0049 /// Calls \p function on each element in the range [\p first, \p last).
0050 ///
0051 /// Space complexity: \Omega(1)
0052 ///
0053 /// \see transform()
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 } // end compute namespace
0070 } // end boost namespace
0071 
0072 #endif // BOOST_COMPUTE_ALGORITHM_FOR_EACH_HPP