• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
3 //
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
7 //
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
10 
11 #ifndef BOOST_COMPUTE_ALGORITHM_MERGE_HPP
12 #define BOOST_COMPUTE_ALGORITHM_MERGE_HPP
13 
14 #include <boost/static_assert.hpp>
15 
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/detail/merge_with_merge_path.hpp>
20 #include <boost/compute/algorithm/detail/serial_merge.hpp>
21 #include <boost/compute/detail/iterator_range_size.hpp>
22 #include <boost/compute/detail/parameter_cache.hpp>
23 #include <boost/compute/type_traits/is_device_iterator.hpp>
24 
25 namespace boost {
26 namespace compute {
27 
28 /// Merges the sorted values in the range [\p first1, \p last1) with the sorted
29 /// values in the range [\p first2, last2) and stores the result in the range
30 /// beginning at \p result. Values are compared using the \p comp function. If
31 /// no comparision function is given, \c less is used.
32 ///
33 /// \param first1 first element in the first range to merge
34 /// \param last1 last element in the first range to merge
35 /// \param first2 first element in the second range to merge
36 /// \param last2 last element in the second range to merge
37 /// \param result first element in the result range
38 /// \param comp comparison function (by default \c less)
39 /// \param queue command queue to perform the operation
40 ///
41 /// \return \c OutputIterator to the end of the result range
42 ///
43 /// Space complexity: \Omega(distance(\p first1, \p last1) + distance(\p first2, \p last2))
44 ///
45 /// \see inplace_merge()
46 template<class InputIterator1,
47          class InputIterator2,
48          class OutputIterator,
49          class Compare>
merge(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2,InputIterator2 last2,OutputIterator result,Compare comp,command_queue & queue=system::default_queue ())50 inline OutputIterator merge(InputIterator1 first1,
51                             InputIterator1 last1,
52                             InputIterator2 first2,
53                             InputIterator2 last2,
54                             OutputIterator result,
55                             Compare comp,
56                             command_queue &queue = system::default_queue())
57 {
58     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator1>::value);
59     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator2>::value);
60     BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
61     typedef typename std::iterator_traits<InputIterator1>::value_type input1_type;
62     typedef typename std::iterator_traits<InputIterator2>::value_type input2_type;
63     typedef typename std::iterator_traits<OutputIterator>::value_type output_type;
64 
65     const device &device = queue.get_device();
66 
67     std::string cache_key =
68         std::string("__boost_merge_") + type_name<input1_type>() + "_"
69         + type_name<input2_type>() + "_" + type_name<output_type>();
70     boost::shared_ptr<detail::parameter_cache> parameters =
71         detail::parameter_cache::get_global_cache(device);
72 
73     // default serial merge threshold depends on device type
74     size_t default_serial_merge_threshold = 32768;
75     if(device.type() & device::gpu) {
76         default_serial_merge_threshold = 2048;
77     }
78 
79     // loading serial merge threshold parameter
80     const size_t serial_merge_threshold =
81         parameters->get(cache_key, "serial_merge_threshold",
82                         static_cast<uint_>(default_serial_merge_threshold));
83 
84     // choosing merge algorithm
85     const size_t total_count =
86         detail::iterator_range_size(first1, last1)
87         + detail::iterator_range_size(first2, last2);
88     // for small inputs serial merge turns out to outperform
89     // merge with merge path algorithm
90     if(total_count <= serial_merge_threshold){
91        return detail::serial_merge(first1, last1, first2, last2, result, comp, queue);
92     }
93     return detail::merge_with_merge_path(first1, last1, first2, last2, result, comp, queue);
94 }
95 
96 /// \overload
97 template<class InputIterator1, class InputIterator2, class OutputIterator>
merge(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2,InputIterator2 last2,OutputIterator result,command_queue & queue=system::default_queue ())98 inline OutputIterator merge(InputIterator1 first1,
99                             InputIterator1 last1,
100                             InputIterator2 first2,
101                             InputIterator2 last2,
102                             OutputIterator result,
103                             command_queue &queue = system::default_queue())
104 {
105     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator1>::value);
106     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator2>::value);
107     BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
108     typedef typename std::iterator_traits<InputIterator1>::value_type value_type;
109     less<value_type> less_than;
110     return merge(first1, last1, first2, last2, result, less_than, queue);
111 }
112 
113 } // end compute namespace
114 } // end boost namespace
115 
116 #endif // BOOST_COMPUTE_ALGORITHM_MERGE_HPP
117