1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2016 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_GPU_HPP_
12 #define BOOST_COMPUTE_ALGORITHM_DETAIL_MERGE_SORT_ON_GPU_HPP_
16 #include <boost/compute/kernel.hpp>
17 #include <boost/compute/program.hpp>
18 #include <boost/compute/command_queue.hpp>
19 #include <boost/compute/container/vector.hpp>
20 #include <boost/compute/memory/local_buffer.hpp>
21 #include <boost/compute/detail/meta_kernel.hpp>
22 #include <boost/compute/detail/iterator_range_size.hpp>
28 template<class KeyType, class ValueType>
29 inline size_t pick_bitonic_block_sort_block_size(size_t proposed_wg,
33 size_t n = proposed_wg;
35 size_t lmem_required = n * sizeof(KeyType);
37 lmem_required += n * sizeof(ValueType);
40 // try to force at least 4 work-groups of >64 elements
41 // for better occupancy
42 while(lmem_size < (lmem_required * 4) && (n > 64)) {
44 lmem_required = n * sizeof(KeyType);
46 while(lmem_size < lmem_required && (n != 1)) {
49 lmem_required = n * sizeof(KeyType);
52 if(n < 2) { return 1; }
53 else if(n < 4) { return 2; }
54 else if(n < 8) { return 4; }
55 else if(n < 16) { return 8; }
56 else if(n < 32) { return 16; }
57 else if(n < 64) { return 32; }
58 else if(n < 128) { return 64; }
59 else if(n < 256) { return 128; }
64 /// Performs bitonic block sort according to \p compare.
66 /// Since bitonic sort can be only performed when input size is equal to 2^n,
67 /// in this case input size is block size (\p work_group_size), we would have
68 /// to require \p count be a exact multiple of block size. That would not be
70 /// Instead, bitonic sort kernel is merged with odd-even merge sort so if the
71 /// last block is not equal to 2^n (where n is some natural number) the odd-even
72 /// sort is performed for that block. That way bitonic_block_sort() works for
73 /// input of any size. Block size (\p work_group_size) still have to be equal
76 /// This is NOT stable.
78 /// \param keys_first first key element in the range to sort
79 /// \param values_first first value element in the range to sort
80 /// \param compare comparison function for keys
81 /// \param count number of elements in the range; count > 0
82 /// \param work_group_size size of the work group, also the block size; must be
83 /// equal to n^2 where n is natural number
84 /// \param queue command queue to perform the operation
85 template<class KeyIterator, class ValueIterator, class Compare>
86 inline size_t bitonic_block_sort(KeyIterator keys_first,
87 ValueIterator values_first,
90 const bool sort_by_key,
93 typedef typename std::iterator_traits<KeyIterator>::value_type key_type;
95 meta_kernel k("bitonic_block_sort");
96 size_t count_arg = k.add_arg<const uint_>("count");
98 size_t local_keys_arg = k.add_arg<key_type *>(memory_object::local_memory, "lkeys");
99 size_t local_vals_arg = 0;
101 local_vals_arg = k.add_arg<uchar_ *>(memory_object::local_memory, "lidx");
105 // Work item global and local ids
106 k.decl<const uint_>("gid") << " = get_global_id(0);\n" <<
107 k.decl<const uint_>("lid") << " = get_local_id(0);\n";
109 // declare my_key and my_value
111 k.decl<key_type>("my_key") << ";\n";
112 // Instead of copying values (my_value) in local memory with keys
113 // we save local index (uchar) and copy my_value at the end at
114 // final index. This saves local memory.
118 k.decl<uchar_>("my_index") << " = (uchar)(lid);\n";
123 "if(gid < count) {\n" <<
124 k.var<key_type>("my_key") << " = " <<
125 keys_first[k.var<const uint_>("gid")] << ";\n" <<
128 // load key and index to local memory
130 "lkeys[lid] = my_key;\n";
134 "lidx[lid] = my_index;\n";
137 k.decl<const uint_>("offset") << " = get_group_id(0) * get_local_size(0);\n" <<
138 k.decl<const uint_>("n") << " = min((uint)(get_local_size(0)),(count - offset));\n";
140 // When work group size is a power of 2 bitonic sorter can be used;
141 // otherwise, slower odd-even sort is used.
144 // check if n is power of 2
145 "if(((n != 0) && ((n & (~n + 1)) == n))) {\n";
147 // bitonic sort, not stable
149 // wait for keys and vals to be stored in local memory
150 "barrier(CLK_LOCAL_MEM_FENCE);\n" <<
152 "#pragma unroll\n" <<
154 k.decl<uint_>("length") << " = 1; " <<
158 // direction of sort: false -> asc, true -> desc
159 k.decl<bool>("direction") << "= ((lid & (length<<1)) != 0);\n" <<
161 k.decl<uint_>("k") << " = length; " <<
166 // sibling to compare with my key
167 k.decl<uint_>("sibling_idx") << " = lid ^ k;\n" <<
168 k.decl<key_type>("sibling_key") << " = lkeys[sibling_idx];\n" <<
169 k.decl<bool>("compare") << " = " <<
170 compare(k.var<key_type>("sibling_key"),
171 k.var<key_type>("my_key")) << ";\n" <<
172 k.decl<bool>("swap") <<
173 " = compare ^ (sibling_idx < lid) ^ direction;\n" <<
174 "my_key = swap ? sibling_key : my_key;\n";
178 "my_index = swap ? lidx[sibling_idx] : my_index;\n";
181 "barrier(CLK_LOCAL_MEM_FENCE);\n" <<
182 "lkeys[lid] = my_key;\n";
186 "lidx[lid] = my_index;\n";
189 "barrier(CLK_LOCAL_MEM_FENCE);\n" <<
193 // end of bitonic sort
195 // odd-even sort, not stable
201 k.decl<bool>("lid_is_even") << " = (lid%2) == 0;\n" <<
202 k.decl<uint_>("oddsibling_idx") << " = " <<
203 "(lid_is_even) ? max(lid,(uint)(1)) - 1 : min(lid+1,n-1);\n" <<
204 k.decl<uint_>("evensibling_idx") << " = " <<
205 "(lid_is_even) ? min(lid+1,n-1) : max(lid,(uint)(1)) - 1;\n" <<
207 // wait for keys and vals to be stored in local memory
208 "barrier(CLK_LOCAL_MEM_FENCE);\n" <<
210 "#pragma unroll\n" <<
212 k.decl<uint_>("i") << " = 0; " <<
216 k.decl<uint_>("sibling_idx") <<
217 " = i%2 == 0 ? evensibling_idx : oddsibling_idx;\n" <<
218 k.decl<key_type>("sibling_key") << " = lkeys[sibling_idx];\n" <<
219 k.decl<bool>("compare") << " = " <<
220 compare(k.var<key_type>("sibling_key"),
221 k.var<key_type>("my_key")) << ";\n" <<
222 k.decl<bool>("swap") <<
223 " = compare ^ (sibling_idx < lid);\n" <<
224 "my_key = swap ? sibling_key : my_key;\n";
228 "my_index = swap ? lidx[sibling_idx] : my_index;\n";
231 "barrier(CLK_LOCAL_MEM_FENCE);\n" <<
232 "lkeys[lid] = my_key;\n";
236 "lidx[lid] = my_index;\n";
239 "barrier(CLK_LOCAL_MEM_FENCE);\n"
243 // end of odd-even sort
245 // save key and value
247 "if(gid < count) {\n" <<
248 keys_first[k.var<const uint_>("gid")] << " = " <<
249 k.var<key_type>("my_key") << ";\n";
252 k << values_first[k.var<const uint_>("gid")] << " = " <<
253 values_first[k.var<const uint_>("offset + my_index")] << ";\n";
259 const context &context = queue.get_context();
260 const device &device = queue.get_device();
261 ::boost::compute::kernel kernel = k.compile(context);
263 const size_t work_group_size =
264 pick_bitonic_block_sort_block_size<key_type, uchar_>(
265 kernel.get_work_group_info<size_t>(
266 device, CL_KERNEL_WORK_GROUP_SIZE
268 device.get_info<size_t>(CL_DEVICE_LOCAL_MEM_SIZE),
272 const size_t global_size =
273 work_group_size * static_cast<size_t>(
274 std::ceil(float(count) / work_group_size)
277 kernel.set_arg(count_arg, static_cast<uint_>(count));
278 kernel.set_arg(local_keys_arg, local_buffer<key_type>(work_group_size));
280 kernel.set_arg(local_vals_arg, local_buffer<uchar_>(work_group_size));
283 queue.enqueue_1d_range_kernel(kernel, 0, global_size, work_group_size);
284 // return size of the block
285 return work_group_size;
288 template<class KeyIterator, class ValueIterator, class Compare>
289 inline size_t block_sort(KeyIterator keys_first,
290 ValueIterator values_first,
293 const bool sort_by_key,
295 command_queue &queue)
298 // TODO: Implement stable block sort (stable odd-even merge sort)
301 return bitonic_block_sort(
302 keys_first, values_first,
308 /// space: O(n + m); n - number of keys, m - number of values
309 template<class KeyIterator, class ValueIterator, class Compare>
310 inline void merge_blocks_on_gpu(KeyIterator keys_first,
311 ValueIterator values_first,
312 KeyIterator out_keys_first,
313 ValueIterator out_values_first,
316 const size_t block_size,
317 const bool sort_by_key,
318 command_queue &queue)
320 typedef typename std::iterator_traits<KeyIterator>::value_type key_type;
321 typedef typename std::iterator_traits<ValueIterator>::value_type value_type;
323 meta_kernel k("merge_blocks");
324 size_t count_arg = k.add_arg<const uint_>("count");
325 size_t block_size_arg = k.add_arg<const uint_>("block_size");
329 k.decl<const uint_>("gid") << " = get_global_id(0);\n" <<
330 "if(gid >= count) {\n" <<
334 k.decl<const key_type>("my_key") << " = " <<
335 keys_first[k.var<const uint_>("gid")] << ";\n";
339 k.decl<const value_type>("my_value") << " = " <<
340 values_first[k.var<const uint_>("gid")] << ";\n";
345 k.decl<const uint_>("my_block_idx") << " = gid / block_size;\n" <<
346 k.decl<const bool>("my_block_idx_is_odd") << " = " <<
347 "my_block_idx & 0x1;\n" <<
349 k.decl<const uint_>("other_block_idx") << " = " <<
350 // if(my_block_idx is odd) {} else {}
351 "my_block_idx_is_odd ? my_block_idx - 1 : my_block_idx + 1;\n" <<
353 // get ranges of my block and the other block
354 // [my_block_start; my_block_end)
355 // [other_block_start; other_block_end)
356 k.decl<const uint_>("my_block_start") << " = " <<
357 "min(my_block_idx * block_size, count);\n" << // including
358 k.decl<const uint_>("my_block_end") << " = " <<
359 "min((my_block_idx + 1) * block_size, count);\n" << // excluding
361 k.decl<const uint_>("other_block_start") << " = " <<
362 "min(other_block_idx * block_size, count);\n" << // including
363 k.decl<const uint_>("other_block_end") << " = " <<
364 "min((other_block_idx + 1) * block_size, count);\n" << // excluding
366 // other block is empty, nothing to merge here
367 "if(other_block_start == count){\n" <<
368 out_keys_first[k.var<uint_>("gid")] << " = my_key;\n";
371 out_values_first[k.var<uint_>("gid")] << " = my_value;\n";
379 // left_idx - lower bound
380 k.decl<uint_>("left_idx") << " = other_block_start;\n" <<
381 k.decl<uint_>("right_idx") << " = other_block_end;\n" <<
382 "while(left_idx < right_idx) {\n" <<
383 k.decl<uint_>("mid_idx") << " = (left_idx + right_idx) / 2;\n" <<
384 k.decl<key_type>("mid_key") << " = " <<
385 keys_first[k.var<const uint_>("mid_idx")] << ";\n" <<
386 k.decl<bool>("smaller") << " = " <<
387 compare(k.var<key_type>("mid_key"),
388 k.var<key_type>("my_key")) << ";\n" <<
389 "left_idx = smaller ? mid_idx + 1 : left_idx;\n" <<
390 "right_idx = smaller ? right_idx : mid_idx;\n" <<
392 // left_idx is found position in other block
394 // if my_block is odd we need to get the upper bound
395 "right_idx = other_block_end;\n" <<
396 "if(my_block_idx_is_odd && left_idx != right_idx) {\n" <<
397 k.decl<key_type>("upper_key") << " = " <<
398 keys_first[k.var<const uint_>("left_idx")] << ";\n" <<
400 "!(" << compare(k.var<key_type>("upper_key"),
401 k.var<key_type>("my_key")) <<
403 "!(" << compare(k.var<key_type>("my_key"),
404 k.var<key_type>("upper_key")) <<
406 "left_idx < right_idx" <<
409 k.decl<uint_>("mid_idx") << " = (left_idx + right_idx) / 2;\n" <<
410 k.decl<key_type>("mid_key") << " = " <<
411 keys_first[k.var<const uint_>("mid_idx")] << ";\n" <<
412 k.decl<bool>("equal") << " = " <<
413 "!(" << compare(k.var<key_type>("mid_key"),
414 k.var<key_type>("my_key")) <<
416 "!(" << compare(k.var<key_type>("my_key"),
417 k.var<key_type>("mid_key")) <<
419 "left_idx = equal ? mid_idx + 1 : left_idx + 1;\n" <<
420 "right_idx = equal ? right_idx : mid_idx;\n" <<
421 "upper_key = equal ? upper_key : " <<
422 keys_first[k.var<const uint_>("left_idx")] << ";\n" <<
426 k.decl<uint_>("offset") << " = 0;\n" <<
427 "offset += gid - my_block_start;\n" <<
428 "offset += left_idx - other_block_start;\n" <<
429 "offset += min(my_block_start, other_block_start);\n" <<
430 out_keys_first[k.var<uint_>("offset")] << " = my_key;\n";
433 out_values_first[k.var<uint_>("offset")] << " = my_value;\n";
436 const context &context = queue.get_context();
437 ::boost::compute::kernel kernel = k.compile(context);
439 const size_t work_group_size = (std::min)(
441 kernel.get_work_group_info<size_t>(
442 queue.get_device(), CL_KERNEL_WORK_GROUP_SIZE
445 const size_t global_size =
446 work_group_size * static_cast<size_t>(
447 std::ceil(float(count) / work_group_size)
450 kernel.set_arg(count_arg, static_cast<uint_>(count));
451 kernel.set_arg(block_size_arg, static_cast<uint_>(block_size));
452 queue.enqueue_1d_range_kernel(kernel, 0, global_size, work_group_size);
455 template<class KeyIterator, class ValueIterator, class Compare>
456 inline void merge_sort_by_key_on_gpu(KeyIterator keys_first,
457 KeyIterator keys_last,
458 ValueIterator values_first,
461 command_queue &queue)
463 typedef typename std::iterator_traits<KeyIterator>::value_type key_type;
464 typedef typename std::iterator_traits<ValueIterator>::value_type value_type;
466 size_t count = iterator_range_size(keys_first, keys_last);
473 keys_first, values_first,
475 true /* sort_by_key */, stable /* stable */,
479 // for small input size only block sort is performed
480 if(count <= block_size) {
484 const context &context = queue.get_context();
486 bool result_in_temporary_buffer = false;
487 ::boost::compute::vector<key_type> temp_keys(count, context);
488 ::boost::compute::vector<value_type> temp_values(count, context);
490 for(; block_size < count; block_size *= 2) {
491 result_in_temporary_buffer = !result_in_temporary_buffer;
492 if(result_in_temporary_buffer) {
493 merge_blocks_on_gpu(keys_first, values_first,
494 temp_keys.begin(), temp_values.begin(),
495 compare, count, block_size,
496 true /* sort_by_key */, queue);
498 merge_blocks_on_gpu(temp_keys.begin(), temp_values.begin(),
499 keys_first, values_first,
500 compare, count, block_size,
501 true /* sort_by_key */, queue);
505 if(result_in_temporary_buffer) {
506 copy_async(temp_keys.begin(), temp_keys.end(), keys_first, queue);
507 copy_async(temp_values.begin(), temp_values.end(), values_first, queue);
511 template<class Iterator, class Compare>
512 inline void merge_sort_on_gpu(Iterator first,
516 command_queue &queue)
518 typedef typename std::iterator_traits<Iterator>::value_type key_type;
520 size_t count = iterator_range_size(first, last);
530 false /* sort_by_key */, stable /* stable */,
534 // for small input size only block sort is performed
535 if(count <= block_size) {
539 const context &context = queue.get_context();
541 bool result_in_temporary_buffer = false;
542 ::boost::compute::vector<key_type> temp_keys(count, context);
544 for(; block_size < count; block_size *= 2) {
545 result_in_temporary_buffer = !result_in_temporary_buffer;
546 if(result_in_temporary_buffer) {
547 merge_blocks_on_gpu(first, dummy, temp_keys.begin(), dummy,
548 compare, count, block_size,
549 false /* sort_by_key */, queue);
551 merge_blocks_on_gpu(temp_keys.begin(), dummy, first, dummy,
552 compare, count, block_size,
553 false /* sort_by_key */, queue);
557 if(result_in_temporary_buffer) {
558 copy_async(temp_keys.begin(), temp_keys.end(), first, queue);
562 template<class KeyIterator, class ValueIterator, class Compare>
563 inline void merge_sort_by_key_on_gpu(KeyIterator keys_first,
564 KeyIterator keys_last,
565 ValueIterator values_first,
567 command_queue &queue)
569 merge_sort_by_key_on_gpu(
570 keys_first, keys_last, values_first,
571 compare, false /* not stable */, queue
575 template<class Iterator, class Compare>
576 inline void merge_sort_on_gpu(Iterator first,
579 command_queue &queue)
582 first, last, compare, false /* not stable */, queue
586 } // end detail namespace
587 } // end compute namespace
588 } // end boost namespace
590 #endif /* BOOST_COMPUTE_ALGORITHM_DETAIL_MERGE_SORT_ON_GPU_HPP_ */