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_REVERSE_COPY_HPP
12 #define BOOST_COMPUTE_ALGORITHM_REVERSE_COPY_HPP
16 #include <boost/compute/system.hpp>
17 #include <boost/compute/command_queue.hpp>
18 #include <boost/compute/algorithm/copy.hpp>
19 #include <boost/compute/algorithm/reverse.hpp>
25 template<class Iterator, class OutputIterator>
26 struct reverse_copy_kernel : public meta_kernel
28 reverse_copy_kernel(Iterator first, Iterator last, OutputIterator result)
29 : meta_kernel("reverse_copy")
31 // store size of the range
32 m_size = detail::iterator_range_size(first, last);
33 add_set_arg<const cl_uint>("size", static_cast<const cl_uint>(m_size));
36 decl<cl_uint>("i") << " = get_global_id(0);\n" <<
37 decl<cl_uint>("j") << " = size - get_global_id(0) - 1;\n" <<
38 result[var<cl_uint>("j")] << "=" << first[var<cl_uint>("i")] << ";\n";
41 void exec(command_queue &queue)
43 exec_1d(queue, 0, m_size);
49 } // end detail namespace
51 /// Copies the elements in the range [\p first, \p last) in reversed
52 /// order to the range beginning at \p result.
55 template<class InputIterator, class OutputIterator>
57 reverse_copy(InputIterator first,
59 OutputIterator result,
60 command_queue &queue = system::default_queue())
62 typedef typename std::iterator_traits<OutputIterator>::difference_type difference_type;
64 difference_type count = std::distance(first, last);
66 detail::reverse_copy_kernel<InputIterator, OutputIterator>
67 kernel(first, last, result);
72 // return iterator to the end of result
73 return result + count;
76 } // end compute namespace
77 } // end boost namespace
79 #endif // BOOST_COMPUTE_ALGORITHM_REVERSE_COPY_HPP