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/static_assert.hpp>
18 #include <boost/compute/system.hpp>
19 #include <boost/compute/command_queue.hpp>
20 #include <boost/compute/algorithm/copy.hpp>
21 #include <boost/compute/algorithm/reverse.hpp>
22 #include <boost/compute/type_traits/is_device_iterator.hpp>
28 template<class Iterator, class OutputIterator>
29 struct reverse_copy_kernel : public meta_kernel
31 reverse_copy_kernel(Iterator first, Iterator last, OutputIterator result)
32 : meta_kernel("reverse_copy")
34 // store size of the range
35 m_size = detail::iterator_range_size(first, last);
36 add_set_arg<const cl_uint>("size", static_cast<const cl_uint>(m_size));
39 decl<cl_uint>("i") << " = get_global_id(0);\n" <<
40 decl<cl_uint>("j") << " = size - get_global_id(0) - 1;\n" <<
41 result[var<cl_uint>("j")] << "=" << first[var<cl_uint>("i")] << ";\n";
44 void exec(command_queue &queue)
46 exec_1d(queue, 0, m_size);
52 } // end detail namespace
54 /// Copies the elements in the range [\p first, \p last) in reversed
55 /// order to the range beginning at \p result.
57 /// Space complexity: \Omega(1)
60 template<class InputIterator, class OutputIterator>
62 reverse_copy(InputIterator first,
64 OutputIterator result,
65 command_queue &queue = system::default_queue())
67 BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
68 BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
70 typedef typename std::iterator_traits<OutputIterator>::difference_type difference_type;
72 difference_type count = std::distance(first, last);
74 detail::reverse_copy_kernel<InputIterator, OutputIterator>
75 kernel(first, last, result);
80 // return iterator to the end of result
81 return result + count;
84 } // end compute namespace
85 } // end boost namespace
87 #endif // BOOST_COMPUTE_ALGORITHM_REVERSE_COPY_HPP