Advertisement
candale

Bitonic Sort

Jun 24th, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.60 KB | None | 0 0
  1. Bitonic Sort
  2. ============
  3.  
  4.  
  5. Bitonic sort is a sorting method that is very well suited for running in distributed systems because of the way the
  6. algorithm is built.
  7.  
  8.  
  9. Bitonic sequence
  10. ----------------
  11.  
  12. We should start by defining what a bitonic sequence actually is. A bitonic sequence if a list of numbers with the
  13. property that, given K(i) the i'th element and N the length of the list, we have
  14. K(0) <= K(1) <= ... <= K(N/2 - 1) <= K(N/2) >= K(N/2 + 1) >= ... >= K(N - 1) OR
  15. K(0) >= K(1) >= ... >= K(N/2 - 1) >= K(N/2) <= K(N/2 + 1) <= ... <= K(N - 1)
  16.  
  17. This property gives us a very interesting leverage in sorting these numbers. Let's consider a list of random numbers
  18. that are are bitonic sequence and try to see how we would go about sorting it.
  19. Let's consider the following list:
  20. 3 4 9 21 18 8 3 1
  21. The list can now be split into two different pars: the ascending part and descending one, each of size four.
  22. the way the algorithm works is that we take the first element of the first sequence (the ascending one) ad compare it
  23. with the first element of the second sequence (the descending one) and if the first if greater then than the second, we
  24. swap them. the visual representation of this process looks like this:
  25. 3 4 9 21 18 8 3 1
  26. ------------->
  27. ------------->
  28. ------------->
  29. ---------->
  30.  
  31. Each line represents a so called comparator to which the consequence may be a swap between the values that it is
  32. comparing. So, given the bitonic sequence above, we would end up with the following sequence, after we have applied
  33. the comparisons and the swapping:
  34. 3 4 3 1 18 8 9 21
  35.  
  36. Now, if we look closely, we now 4 items sequences that can be considered bitonic:
  37. 3 4 3 1 18 8 9 21
  38. / \ \ /
  39. / \ \ /
  40.  
  41. What we do with these is exactly the same as to what we did with the first bitonic sequence: we create comparators and
  42. we swap accordingly, only that we bitonic sequences of smaller sizes:
  43. 3 4 3 1 18 8 9 21
  44. ------> ----->
  45. ------> ----->
  46. After we swap the necessary items we end up with bitonic sequences of size two which again can be compared and in the
  47. end we are left with the sorted list.
  48.  
  49.  
  50. Applying the algorithm on real life lists
  51. -----------------------------------------
  52.  
  53.  
  54. As one may think, we don't often find these kind, bitonic, sequences out there in the wild and we are more often made to
  55. deal with ugly looking lists that also need sorting. In order to cope with those, a tweak was added to the algorithm so
  56. that it can transform the list in a bitonic sequence, starting from small subsets of the list to the whole list.
  57.  
  58. As explained above, the algorithm starts from large to small, comparing and swapping. The actual sorting algorithm
  59. starts by taking elements of the list, two by two in sets of two and transforming them in bitonic sequences and then
  60. applies the algorithm above to merge them and end up with a subset of the whole list that is sorted.
  61. It transforms a subset of the list into a bitonic sequence by, again, applying comparators only that in opposing
  62. directions, as in the figure below; the direction of the arrow tells the direction of the comparator, to the right
  63. ascending and to the left descending:
  64. 5 89 3 32 34 23 6 1
  65. ----> <---- -----> <----
  66.  
  67.  
  68. This will result in a new list which has two sets of bitonic sequences:
  69. 5 89 32 3 23 34 6 1
  70. / \ / \
  71. / \ / \
  72.  
  73. On these sequences the algorithm we first explained is applied, for the first sequence in ascending order and for the
  74. second in descending order transforming it again in a bitonic sequence that can be ordered.
  75.  
  76. The complexity of this algorithm is O(n long(n)^2).
  77. The one limitation this algorithm has is that it can only be applied to lists that have a number of elements that is a
  78. power of two.
  79.  
  80.  
  81.  
  82. Running Bitonic Sort in Parallel
  83. --------------------------------
  84.  
  85. Bitonic Sort is a very well suited algorithm for parallel processing because of the recursive nature it has. For this
  86. version, as well, the number of elements in the list must be a power to two and the number processes as well.
  87.  
  88. The algorithm works by spawning the required number of processes and evenly distributing the elements of the list among
  89. all of them. After that, the algorithm defines a way to identify an pair up two processes that will work together and
  90. exchange and compare data. After having this strategy, the algorithm begins and each process, together with it's partner
  91. exchange lists. Each of these pairs if responsible for either getting the smaller number from the combined list of the
  92. two or the higher numbers. Code looks something like this:
  93.  
  94. int index1 = 0;
  95. int index2 = 0;
  96.  
  97. for (i = 0; i < list_size; i++)
  98. if (my_list[index1] <= partner_list[index2]) {
  99. scratch_list[i] = my_list[index1];
  100. index1++;
  101. } else {
  102. scratch_list[i] = partner_list[index2];
  103. index2++;
  104. }
  105. for (i = 0; i < list_size; i++)
  106. my_list[i] = scratch_list[i];
  107.  
  108. So after they exchange lists, each of the processes takes the number they are supposed to take and holds them on for the
  109. next stage. This process repeats a number of times which is calculated based on the number of processes involved and the
  110. number of elements.
  111.  
  112.  
  113. The parallel version of bitonic sort can run tens of times faster than the sequential sort given enough number of
  114. processes. The nature of the algorithm allows for huge scaling and very high performances.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement