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_MERGE_HPP
12 #define BOOST_COMPUTE_ALGORITHM_MERGE_HPP
14 #include <boost/compute/system.hpp>
15 #include <boost/compute/command_queue.hpp>
16 #include <boost/compute/algorithm/copy.hpp>
17 #include <boost/compute/algorithm/detail/merge_with_merge_path.hpp>
18 #include <boost/compute/algorithm/detail/serial_merge.hpp>
19 #include <boost/compute/detail/iterator_range_size.hpp>
20 #include <boost/compute/detail/parameter_cache.hpp>
25 /// Merges the sorted values in the range [\p first1, \p last1) with the sorted
26 /// values in the range [\p first2, last2) and stores the result in the range
27 /// beginning at \p result. Values are compared using the \p comp function. If
28 /// no comparision function is given, \c less is used.
30 /// \param first1 first element in the first range to merge
31 /// \param last1 last element in the first range to merge
32 /// \param first2 first element in the second range to merge
33 /// \param last2 last element in the second range to merge
34 /// \param result first element in the result range
35 /// \param comp comparison function (by default \c less)
36 /// \param queue command queue to perform the operation
38 /// \return \c OutputIterator to the end of the result range
40 /// Space complexity: \Omega(distance(\p first1, \p last1) + distance(\p first2, \p last2))
42 /// \see inplace_merge()
43 template<class InputIterator1,
47 inline OutputIterator merge(InputIterator1 first1,
49 InputIterator2 first2,
51 OutputIterator result,
53 command_queue &queue = system::default_queue())
55 typedef typename std::iterator_traits<InputIterator1>::value_type input1_type;
56 typedef typename std::iterator_traits<InputIterator2>::value_type input2_type;
57 typedef typename std::iterator_traits<OutputIterator>::value_type output_type;
59 const device &device = queue.get_device();
61 std::string cache_key =
62 std::string("__boost_merge_") + type_name<input1_type>() + "_"
63 + type_name<input2_type>() + "_" + type_name<output_type>();
64 boost::shared_ptr<detail::parameter_cache> parameters =
65 detail::parameter_cache::get_global_cache(device);
67 // default serial merge threshold depends on device type
68 size_t default_serial_merge_threshold = 32768;
69 if(device.type() & device::gpu) {
70 default_serial_merge_threshold = 2048;
73 // loading serial merge threshold parameter
74 const size_t serial_merge_threshold =
75 parameters->get(cache_key, "serial_merge_threshold",
76 static_cast<uint_>(default_serial_merge_threshold));
78 // choosing merge algorithm
79 const size_t total_count =
80 detail::iterator_range_size(first1, last1)
81 + detail::iterator_range_size(first2, last2);
82 // for small inputs serial merge turns out to outperform
83 // merge with merge path algorithm
84 if(total_count <= serial_merge_threshold){
85 return detail::serial_merge(first1, last1, first2, last2, result, comp, queue);
87 return detail::merge_with_merge_path(first1, last1, first2, last2, result, comp, queue);
91 template<class InputIterator1, class InputIterator2, class OutputIterator>
92 inline OutputIterator merge(InputIterator1 first1,
94 InputIterator2 first2,
96 OutputIterator result,
97 command_queue &queue = system::default_queue())
99 typedef typename std::iterator_traits<InputIterator1>::value_type value_type;
100 less<value_type> less_than;
101 return merge(first1, last1, first2, last2, result, less_than, queue);
104 } // end compute namespace
105 } // end boost namespace
107 #endif // BOOST_COMPUTE_ALGORITHM_MERGE_HPP