1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2015 Jakub Szuppe <j.szuppe@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_DETAIL_MERGE_SORT_ON_CPU_HPP
12 #define BOOST_COMPUTE_ALGORITHM_DETAIL_MERGE_SORT_ON_CPU_HPP
14 #include <boost/compute/kernel.hpp>
15 #include <boost/compute/program.hpp>
16 #include <boost/compute/command_queue.hpp>
17 #include <boost/compute/algorithm/detail/merge_with_merge_path.hpp>
18 #include <boost/compute/container/vector.hpp>
19 #include <boost/compute/detail/meta_kernel.hpp>
20 #include <boost/compute/detail/iterator_range_size.hpp>
26 template<class KeyIterator, class ValueIterator, class Compare>
27 inline void merge_blocks(KeyIterator keys_first,
28 ValueIterator values_first,
29 KeyIterator keys_result,
30 ValueIterator values_result,
33 const size_t block_size,
34 const bool sort_by_key,
40 meta_kernel k("merge_sort_on_cpu_merge_blocks");
41 size_t count_arg = k.add_arg<const uint_>("count");
42 size_t block_size_arg = k.add_arg<uint_>("block_size");
45 k.decl<uint_>("b1_start") << " = get_global_id(0) * block_size * 2;\n" <<
46 k.decl<uint_>("b1_end") << " = min(count, b1_start + block_size);\n" <<
47 k.decl<uint_>("b2_start") << " = min(count, b1_start + block_size);\n" <<
48 k.decl<uint_>("b2_end") << " = min(count, b2_start + block_size);\n" <<
49 k.decl<uint_>("result_idx") << " = b1_start;\n" <<
51 // merging block 1 and block 2 (stable)
52 "while(b1_start < b1_end && b2_start < b2_end){\n" <<
53 " if( " << compare(keys_first[k.var<uint_>("b2_start")],
54 keys_first[k.var<uint_>("b1_start")]) << "){\n" <<
55 " " << keys_result[k.var<uint_>("result_idx")] << " = " <<
56 keys_first[k.var<uint_>("b2_start")] << ";\n";
59 " " << values_result[k.var<uint_>("result_idx")] << " = " <<
60 values_first[k.var<uint_>("b2_start")] << ";\n";
66 " " << keys_result[k.var<uint_>("result_idx")] << " = " <<
67 keys_first[k.var<uint_>("b1_start")] << ";\n";
70 " " << values_result[k.var<uint_>("result_idx")] << " = " <<
71 values_first[k.var<uint_>("b1_start")] << ";\n";
78 "while(b1_start < b1_end){\n" <<
79 " " << keys_result[k.var<uint_>("result_idx")] << " = " <<
80 keys_first[k.var<uint_>("b1_start")] << ";\n";
83 " " << values_result[k.var<uint_>("result_idx")] << " = " <<
84 values_first[k.var<uint_>("b1_start")] << ";\n";
90 "while(b2_start < b2_end){\n" <<
91 " " << keys_result[k.var<uint_>("result_idx")] << " = " <<
92 keys_first[k.var<uint_>("b2_start")] << ";\n";
95 " " << values_result[k.var<uint_>("result_idx")] << " = " <<
96 values_first[k.var<uint_>("b2_start")] << ";\n";
100 " result_idx++;\n" <<
103 const context &context = queue.get_context();
104 ::boost::compute::kernel kernel = k.compile(context);
105 kernel.set_arg(count_arg, static_cast<const uint_>(count));
106 kernel.set_arg(block_size_arg, static_cast<uint_>(block_size));
108 const size_t global_size = static_cast<size_t>(
109 std::ceil(float(count) / (2 * block_size))
111 queue.enqueue_1d_range_kernel(kernel, 0, global_size, 0);
114 template<class Iterator, class Compare>
115 inline void merge_blocks(Iterator first,
119 const size_t block_size,
120 const bool sort_by_key,
121 command_queue &queue)
123 // dummy iterator as it's not sort by key
125 merge_blocks(first, dummy, result, dummy, compare, count, block_size, false, queue);
128 template<class Iterator, class Compare>
129 inline void dispatch_merge_blocks(Iterator first,
133 const size_t block_size,
134 const size_t input_size_threshold,
135 const size_t blocks_no_threshold,
136 command_queue &queue)
138 const size_t blocks_no = static_cast<size_t>(
139 std::ceil(float(count) / block_size)
141 // merge with merge path should used only for the large arrays and at the
142 // end of merging part when there are only a few big blocks left to be merged
143 if(blocks_no <= blocks_no_threshold && count >= input_size_threshold){
144 Iterator last = first + count;
145 for(size_t i = 0; i < count; i+= 2*block_size)
147 Iterator first1 = (std::min)(first + i, last);
148 Iterator last1 = (std::min)(first1 + block_size, last);
149 Iterator first2 = last1;
150 Iterator last2 = (std::min)(first2 + block_size, last);
151 Iterator block_result = (std::min)(result + i, result + count);
152 merge_with_merge_path(first1, last1, first2, last2,
153 block_result, compare, queue);
157 merge_blocks(first, result, compare, count, block_size, false, queue);
161 template<class KeyIterator, class ValueIterator, class Compare>
162 inline void block_insertion_sort(KeyIterator keys_first,
163 ValueIterator values_first,
166 const size_t block_size,
167 const bool sort_by_key,
168 command_queue &queue)
172 typedef typename std::iterator_traits<KeyIterator>::value_type K;
173 typedef typename std::iterator_traits<ValueIterator>::value_type T;
175 meta_kernel k("merge_sort_on_cpu_block_insertion_sort");
176 size_t count_arg = k.add_arg<uint_>("count");
177 size_t block_size_arg = k.add_arg<uint_>("block_size");
180 k.decl<uint_>("start") << " = get_global_id(0) * block_size;\n" <<
181 k.decl<uint_>("end") << " = min(count, start + block_size);\n" <<
183 // block insertion sort (stable)
184 "for(uint i = start+1; i < end; i++){\n" <<
185 " " << k.decl<const K>("key") << " = " <<
186 keys_first[k.var<uint_>("i")] << ";\n";
189 " " << k.decl<const T>("value") << " = " <<
190 values_first[k.var<uint_>("i")] << ";\n";
193 " uint pos = i;\n" <<
194 " while(pos > start && " <<
195 compare(k.var<const K>("key"),
196 keys_first[k.var<uint_>("pos-1")]) << "){\n" <<
197 " " << keys_first[k.var<uint_>("pos")] << " = " <<
198 keys_first[k.var<uint_>("pos-1")] << ";\n";
201 " " << values_first[k.var<uint_>("pos")] << " = " <<
202 values_first[k.var<uint_>("pos-1")] << ";\n";
207 " " << keys_first[k.var<uint_>("pos")] << " = key;\n";
210 " " << values_first[k.var<uint_>("pos")] << " = value;\n";
213 "}\n"; // block insertion sort
215 const context &context = queue.get_context();
216 ::boost::compute::kernel kernel = k.compile(context);
217 kernel.set_arg(count_arg, static_cast<uint_>(count));
218 kernel.set_arg(block_size_arg, static_cast<uint_>(block_size));
220 const size_t global_size = static_cast<size_t>(std::ceil(float(count) / block_size));
221 queue.enqueue_1d_range_kernel(kernel, 0, global_size, 0);
224 template<class Iterator, class Compare>
225 inline void block_insertion_sort(Iterator first,
228 const size_t block_size,
229 command_queue &queue)
231 // dummy iterator as it's not sort by key
233 block_insertion_sort(first, dummy, compare, count, block_size, false, queue);
236 // This sort is stable.
237 template<class Iterator, class Compare>
238 inline void merge_sort_on_cpu(Iterator first,
241 command_queue &queue)
243 typedef typename std::iterator_traits<Iterator>::value_type value_type;
245 size_t count = iterator_range_size(first, last);
249 // for small input size only insertion sort is performed
250 else if(count <= 512){
251 block_insertion_sort(first, compare, count, count, queue);
255 const context &context = queue.get_context();
256 const device &device = queue.get_device();
258 // loading parameters
259 std::string cache_key =
260 std::string("__boost_merge_sort_on_cpu_") + type_name<value_type>();
261 boost::shared_ptr<parameter_cache> parameters =
262 detail::parameter_cache::get_global_cache(device);
264 // When there is merge_with_path_blocks_no_threshold or less blocks left to
265 // merge AND input size is merge_with_merge_path_input_size_threshold or more
266 // merge_with_merge_path() algorithm is used to merge sorted blocks;
267 // otherwise merge_blocks() is used.
268 const size_t merge_with_path_blocks_no_threshold =
269 parameters->get(cache_key, "merge_with_merge_path_blocks_no_threshold", 8);
270 const size_t merge_with_path_input_size_threshold =
271 parameters->get(cache_key, "merge_with_merge_path_input_size_threshold", 2097152);
273 const size_t block_size =
274 parameters->get(cache_key, "insertion_sort_block_size", 64);
275 block_insertion_sort(first, compare, count, block_size, queue);
277 // temporary buffer for merge result
278 vector<value_type> temp(count, context);
279 bool result_in_temporary_buffer = false;
281 for(size_t i = block_size; i < count; i *= 2){
282 result_in_temporary_buffer = !result_in_temporary_buffer;
283 if(result_in_temporary_buffer) {
284 dispatch_merge_blocks(first, temp.begin(), compare, count, i,
285 merge_with_path_input_size_threshold,
286 merge_with_path_blocks_no_threshold,
289 dispatch_merge_blocks(temp.begin(), first, compare, count, i,
290 merge_with_path_input_size_threshold,
291 merge_with_path_blocks_no_threshold,
296 if(result_in_temporary_buffer) {
297 copy(temp.begin(), temp.end(), first, queue);
301 // This sort is stable.
302 template<class KeyIterator, class ValueIterator, class Compare>
303 inline void merge_sort_by_key_on_cpu(KeyIterator keys_first,
304 KeyIterator keys_last,
305 ValueIterator values_first,
307 command_queue &queue)
309 typedef typename std::iterator_traits<KeyIterator>::value_type key_type;
310 typedef typename std::iterator_traits<ValueIterator>::value_type value_type;
312 size_t count = iterator_range_size(keys_first, keys_last);
316 // for small input size only insertion sort is performed
317 else if(count <= 512){
318 block_insertion_sort(keys_first, values_first, compare,
319 count, count, true, queue);
323 const context &context = queue.get_context();
324 const device &device = queue.get_device();
326 // loading parameters
327 std::string cache_key =
328 std::string("__boost_merge_sort_by_key_on_cpu_") + type_name<value_type>()
329 + "_with_" + type_name<key_type>();
330 boost::shared_ptr<parameter_cache> parameters =
331 detail::parameter_cache::get_global_cache(device);
333 const size_t block_size =
334 parameters->get(cache_key, "insertion_sort_by_key_block_size", 64);
335 block_insertion_sort(keys_first, values_first, compare,
336 count, block_size, true, queue);
338 // temporary buffer for merge results
339 vector<value_type> values_temp(count, context);
340 vector<key_type> keys_temp(count, context);
341 bool result_in_temporary_buffer = false;
343 for(size_t i = block_size; i < count; i *= 2){
344 result_in_temporary_buffer = !result_in_temporary_buffer;
345 if(result_in_temporary_buffer) {
346 merge_blocks(keys_first, values_first,
347 keys_temp.begin(), values_temp.begin(),
348 compare, count, i, true, queue);
350 merge_blocks(keys_temp.begin(), values_temp.begin(),
351 keys_first, values_first,
352 compare, count, i, true, queue);
356 if(result_in_temporary_buffer) {
357 copy(keys_temp.begin(), keys_temp.end(), keys_first, queue);
358 copy(values_temp.begin(), values_temp.end(), values_first, queue);
362 } // end detail namespace
363 } // end compute namespace
364 } // end boost namespace
366 #endif // BOOST_COMPUTE_ALGORITHM_DETAIL_MERGE_SORT_ON_CPU_HPP