//---------------------------------------------------------------------------// // 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_STABLE_SORT_HPP #define BOOST_COMPUTE_ALGORITHM_STABLE_SORT_HPP #include #include #include #include #include #include #include #include #include #include namespace boost { namespace compute { namespace detail { template inline void dispatch_gpu_stable_sort(Iterator first, Iterator last, Compare compare, command_queue &queue) { size_t count = detail::iterator_range_size(first, last); if(count < 32){ detail::serial_insertion_sort( first, last, compare, queue ); } else { detail::merge_sort_on_gpu( first, last, compare, true /* stable */, queue ); } } template inline typename boost::enable_if_c::value>::type dispatch_gpu_stable_sort(buffer_iterator first, buffer_iterator last, less, command_queue &queue) { ::boost::compute::detail::radix_sort(first, last, queue); } template inline typename boost::enable_if_c::value>::type dispatch_gpu_stable_sort(buffer_iterator first, buffer_iterator last, greater, command_queue &queue) { // radix sorts in descending order ::boost::compute::detail::radix_sort(first, last, false, queue); } } // end detail namespace /// Sorts the values in the range [\p first, \p last) according to /// \p compare. The relative order of identical values is preserved. /// /// Space complexity: \Omega(n) /// /// \see sort(), is_sorted() template inline void stable_sort(Iterator first, Iterator last, Compare compare, command_queue &queue = system::default_queue()) { if(queue.get_device().type() & device::gpu) { ::boost::compute::detail::dispatch_gpu_stable_sort( first, last, compare, queue ); return; } ::boost::compute::detail::merge_sort_on_cpu(first, last, compare, queue); } /// \overload template inline void stable_sort(Iterator first, Iterator last, command_queue &queue = system::default_queue()) { typedef typename std::iterator_traits::value_type value_type; ::boost::compute::less less; ::boost::compute::stable_sort(first, last, less, queue); } } // end compute namespace } // end boost namespace #endif // BOOST_COMPUTE_ALGORITHM_STABLE_SORT_HPP