//---------------------------------------------------------------------------// // Copyright (c) 2013 Kyle Lutz // // Distributed under the Boost Software License, Version 1.0 // See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt // // See http://boostorg.github.com/compute for more information. //---------------------------------------------------------------------------// #ifndef BOOST_COMPUTE_ALGORITHM_REVERSE_HPP #define BOOST_COMPUTE_ALGORITHM_REVERSE_HPP #include #include #include #include #include #include namespace boost { namespace compute { namespace detail { template struct reverse_kernel : public meta_kernel { reverse_kernel(Iterator first, Iterator last) : meta_kernel("reverse") { typedef typename std::iterator_traits::value_type value_type; // store size of the range m_size = detail::iterator_range_size(first, last); add_set_arg("size", static_cast(m_size)); *this << decl("i") << " = get_global_id(0);\n" << decl("j") << " = size - get_global_id(0) - 1;\n" << decl("tmp") << "=" << first[var("i")] << ";\n" << first[var("i")] << "=" << first[var("j")] << ";\n" << first[var("j")] << "= tmp;\n"; } void exec(command_queue &queue) { exec_1d(queue, 0, m_size / 2); } size_t m_size; }; } // end detail namespace /// Reverses the elements in the range [\p first, \p last). /// /// Space complexity: \Omega(1) /// /// \see reverse_copy() template inline void reverse(Iterator first, Iterator last, command_queue &queue = system::default_queue()) { BOOST_STATIC_ASSERT(is_device_iterator::value); size_t count = detail::iterator_range_size(first, last); if(count < 2){ return; } detail::reverse_kernel kernel(first, last); kernel.exec(queue); } } // end compute namespace } // end boost namespace #endif // BOOST_COMPUTE_ALGORITHM_REVERSE_HPP