English subtitles

← Radix Sort Part 1 - Intro to Parallel Programming

Get Embed Code
2 Languages

Showing Revision 4 created 05/24/2016 by Udacity Robot.

  1. So we've seen some really interesting algorithms so far,
  2. but the GPU performance leader is none of the ones that we've discussed to date.
  3. Instead, typically on the GPU for the highest performing sorts
  4. we use a different sorting algorithm called radix sort.
  5. Now, all of the previous sorting algorithms were comparison sorts,
  6. meaning the only operation we did on an item was compare it to another one.
  7. Radix sort relies on a number representation that uses positional notation.
  8. In this case, bits are more significant as we move further left in the word,
  9. and it's most easily explained using integers.
  10. So the algorithm for radix sort is as follows.
  11. Start with the least significant bit of the integer, split the input into 2 sets,
  12. those that have a 0 with this particular bit location and those that have a 1.
  13. Otherwise, maintain the order.
  14. Then proceed to the next least significant bit and repeat until we run out of bits.
  15. So as usual, we're going to do an example that will make more sense,
  16. and we're going to use unsigned integers.
  17. So what we're going to sort is this column of numbers to the left,
  18. and here is the binary representation of those numbers.
  19. And so we're going to start here with the least significant bit.
  20. So the way that we're going to do this is take all the elements that have a 0 as the least significant bit,
  21. and we're going to otherwise maintain their order, but we're going to put them up top.
  22. Then we're going to take all the rest of the elements,
  23. those that have a 1 as the least significant bit,
  24. and we're going to again keep them in order and append them to the list that we've just created.
  25. So what this creates is a list where all the least significant bits are 0
  26. and then a list where all the least significant bits is 1.
  27. Now we move to the next least significant bit,
  28. so the bit in the middle, and we're going to do the same thing.
  29. We're going to take all the 0s and put them up top,
  30. and then we're going to take all the 1s and put them below.
  31. And here the dotted lines are just showing the data movement that we're looking at.
  32. The green lines are the ones where the middle bits are 0,
  33. and the blue line is the one where the middle bits are 1.
  34. Now we move on to the next most significant bit--
  35. in this case, the very most significant bit--and we do the same operation again.
  36. Zeroes in the most significant bit move up top, 1s move to the bottom,
  37. otherwise, we maintain the order.
  38. And now we have a sorted sequence. Pretty cool, huh?
  39. Now, there's 2 big reasons this code runs great on GPUs.
  40. The first is its work complexity. The best comparison base sorts are O(n log n).
  41. This algorithm, on the other hand, is O(kn),
  42. meaning the runtime is linear in 2 different things.
  43. First, it's linear in the number of bits in the representation.
  44. So this particular integer has 3 bits in its representation,
  45. and it took 3 stages for us to be able to sort the input.
  46. Second, it's linear in the number of items to sort.
  47. So we have 8 items in the representation here,
  48. and so the amount of work is proportional to 8.
  49. Generally k is constant, say a 32-bit word or a 64-bit word for any reasonable applications.
  50. And so in general, the work complexity of this
  51. is mostly proportional to the number of items that we need to sort.
  52. And so that's a superior work complexity to any of the sorts that we've talked about to date,
  53. and so that's 1 reason why this looks so good.
  54. The second is that the underlying operations that we need to do this split of the input at each step
  55. are ones that are actually very efficient.
  56. And in fact, they're efficient operations that you already know.
  57. Let's take a closer look at what we're doing.
  58. We're only going to look at the first stage of the radix sort algorithm,
  59. where we're only considering the value of the least significant bit,
  60. and we're only going to look at the output for which the least significant bit is 0.
  61. Now what are we actually doing here?
  62. We've already learned an algorithm that does this operation today.
  63. So what is the name of the algorithm that takes this input and creates that as the output?