recap prefix sums
play

Recap: Prefix Sums Given A : set of n integers Find B : prefix sums - PowerPoint PPT Presentation

Recap: Prefix Sums Given A : set of n integers Find B : prefix sums A: 3 1 1 7 2 5 9 2 4 3 3 B: 3 4 5 12 14 19 28 30 34 37 40 1 / 86 Recap: Parallel Prefix Sums Recursive algorithm Recursively computes sums Use


  1. Recap: Prefix Sums ● Given A : set of n integers ● Find B : prefix sums A: 3 1 1 7 2 5 9 2 4 3 3 B: 3 4 5 12 14 19 28 30 34 37 40 1 / 86

  2. Recap: Parallel Prefix Sums ● Recursive algorithm – Recursively computes sums – Use partial sums to get prefix sums ● T( n ) = O(log n ) ● W( n ) = O( n ) ● Hard to get intuition ● Iterative algorithm easier to grasp? 2 / 86

  3. Iterative prefix sum ● 2 phases: up-sweep, down-sweep ● Up-sweep pseudocode: 3 / 86

  4. Up-sweep phase B[0] 3 1 1 7 2 5 9 2 A 3 1 1 7 2 5 9 2 4 / 86

  5. Up-sweep phase B[1] B[0] 3 1 1 7 2 5 9 2 5 / 86

  6. Up-sweep phase B[2] B[1] B[0] 3 1 1 7 2 5 9 2 6 / 86

  7. Up-sweep phase B[3] B[2] B[1] B[0] 3 1 1 7 2 5 9 2 7 / 86

  8. Up-sweep phase B[3] B[2] B[1] 4 8 7 11 B[0] 3 1 1 7 2 5 9 2 8 / 86

  9. Up-sweep phase B[3] 12 18 B[2] B[1] 4 8 7 11 B[0] 3 1 1 7 2 5 9 2 9 / 86

  10. Up-sweep phase B[3] 30 12 18 B[2] B[1] 4 8 7 11 B[0] 3 1 1 7 2 5 9 2 10 / 86

  11. Down-sweep phase 11 / 86

  12. Down-sweep phase C[3] 0 12 18 B[2] B[1] 4 8 7 11 B[0] 3 1 1 7 2 5 9 2 12 / 86

  13. Down-sweep phase C[3] 0 12 18 B[2] B[1] 4 8 7 11 B[0] 3 1 1 7 2 5 9 2 13 / 86

  14. Down-sweep phase C[3] 0 12 18 B[2] B[1] 4 8 7 11 B[0] 3 1 1 7 2 5 9 2 14 / 86

  15. Down-sweep phase C[3] 0 0 12 C[2] B[1] 4 8 7 11 B[0] 3 1 1 7 2 5 9 2 15 / 86

  16. Down-sweep phase C[3] 0 0 12 C[2] B[1] 4 8 7 11 B[0] 3 1 1 7 2 5 9 2 16 / 86

  17. Down-sweep phase C[3] 0 0 12 C[2] C[1] 0 4 12 19 B[0] 3 1 1 7 2 5 9 2 17 / 86

  18. Down-sweep phase C[3] 0 0 12 C[2] C[1] 0 4 12 19 B[0] 3 1 1 7 2 5 9 2 18 / 86

  19. Down-sweep phase C[3] 0 0 12 C[2] C[1] 0 4 12 19 C[0] 0 3 4 5 12 17 19 28 19 / 86

  20. Down-sweep phase C[0] 0 3 4 5 12 17 19 28 A 3 1 1 7 2 5 9 2 20 / 86

  21. Down-sweep phase C[0] 0 3 4 5 12 17 19 28 A 3 4 5 12 14 22 28 30 21 / 86

  22. Applications of prefix sums ● More useful than it seems: – Create an array of 1s and 0s – Prefix sums gives # of 1s up to each point – Used to separate an array into 2 ● Using almost any criteria! ● Examples: – separate array into upper-case and lower-case letters – separate array into numbers >x and <x 22 / 86

  23. Example: string separation ● Separate array A into lower-case and upper-case: A a P r e R E c F I o X o S U l M S 23 / 86

  24. Example: string separation ● Create bitstring B: ● 1 if upper-case, 0 otherwise A a P r e R E c F I o X o S U l M S 24 / 86

  25. Example: string separation ● Create bitstring B: ● 1 if upper-case, 0 otherwise A a P r e R E c F I o X o S U l M S B 0 1 0 0 1 1 0 1 1 0 1 0 1 1 0 1 1 ● Time/work to do this in parallel? 25 / 86

  26. Example: string separation ● Create bitstring B: ● 1 if upper-case, 0 otherwise A a P r e R E c F I o X o S U l M S B 0 1 0 0 1 1 0 1 1 0 1 0 1 1 0 1 1 ● Time/work to do this in parallel? W(n) = O(n) T(n) = O(1) 26 / 86

  27. Example: string separation ● Perform prefix sums on B A a P r e R E c F I o X o S U l M S B 0 1 0 0 1 1 0 1 1 0 1 0 1 1 0 1 1 27 / 86

  28. Example: string separation ● Perform prefix sums on B A a P r e R E c F I o X o S U l M S B 0 1 1 1 2 3 3 4 5 5 6 6 7 8 8 9 10 ● What is B[i]? 28 / 86

  29. Example: string separation ● Perform prefix sums on B A a P r e R E c F I o X o S U l M S B 0 1 1 1 2 3 3 4 5 5 6 6 7 8 8 9 10 ● What is B[i]? – The number of capital letters with index ≤ i 29 / 86

  30. Example: string separation ● Copy capital letters into C A a P r e R E c F I o X o S U l M S B 0 1 1 1 2 3 3 4 5 5 6 6 7 8 8 9 10 C ● How can we use B to write only capitals into C? 30 / 86

  31. Example: string separation ● Copy capital letters into C A a P r e R E c F I o X o S U l M S B 0 1 1 1 2 3 3 4 5 5 6 6 7 8 8 9 10 C ● How can we use B to write only capitals into C? – B[i] is the index of each capital in C! 31 / 86

  32. Example: string separation ● Copy capital letters into C A a P r e R E c F I o X o S U l M S B 0 1 1 1 2 3 3 4 5 5 6 6 7 8 8 9 10 C P R E F I X S U M S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ● How can we use B to write only capitals into C? – B[i] is the index of each capital in C! 32 / 86

  33. Example: string separation ● Create B‘ ● 1 for lower-case, 0 otherwise A a P r e R E c F I o X o S U l M S B‘ 1 0 1 1 0 0 1 0 0 1 0 1 0 0 1 0 0 C P R E F I X S U M S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 33 / 86

  34. Example: string separation ● Prefix sums on B‘ A a P r e R E c F I o X o S U l M S B‘ 1 1 2 3 3 3 4 4 4 5 5 6 6 6 7 7 7 C P R E F I X S U M S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 34 / 86

  35. Example: string separation ● Copy lower-case into the rest of C A a P r e R E c F I o X o S U l M S B‘ 1 1 2 3 3 3 4 4 4 5 5 6 6 6 7 7 7 C P R E F I X S U M S 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 35 / 86

  36. Example: string separation ● Copy lower-case into the rest of C A a P r e R E c F I o X o S U l M S B‘ 1 1 2 3 3 3 4 4 4 5 5 6 6 6 7 7 7 C P R E F I X S U M S a r e c o o l 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 2 3 4 5 6 7 ● A[i] = C[j] – where j = B[n] + B‘[i] = 10 + B‘[i] 36 / 86

  37. Example: string separation Create B and B‘ Prefix sums Copy into C Total algorithm 37 / 86

  38. Example: string separation Create B and B‘ Prefix sums Copy into C Total algorithm 38 / 86

  39. Example: string separation Create B and B‘ Prefix sums Copy into C Total algorithm 39 / 86

  40. Quicksort Review ● Quicksort is a popular sorting algorithm – Works in-place – O( n 2 ) worst-case – BUT O( n log n ) expected ● Each recursive call: – Find pivot – Partition around pivot 40 / 86

  41. Sequential Quicksort 41 / 86

  42. Select pivot A 3 9 1 7 4 5 8 2 pivot 42 / 86

  43. Select pivot A 4 9 1 7 3 5 8 2 43 / 86

  44. Partition elements A 4 9 1 7 3 5 8 2 44 / 86

  45. Partition elements A 4 9 1 7 3 5 8 2 part 45 / 86

  46. Partition elements i A 4 9 1 7 3 5 8 2 part 46 / 86

  47. Partition elements FALSE i A 4 9 1 7 3 5 8 2 part 47 / 86

  48. Partition elements TRUE i A 4 9 1 7 3 5 8 2 part 48 / 86

  49. Partition elements TRUE i A 4 1 9 7 3 5 8 2 part 49 / 86

  50. Partition elements FALSE i A 4 1 9 7 3 5 8 2 part 50 / 86

  51. Partition elements TRUE i A 4 1 9 7 3 5 8 2 part 51 / 86

  52. Partition elements TRUE i A 4 1 3 7 9 5 8 2 part 52 / 86

  53. Partition elements FALSE i A 4 1 3 7 9 5 8 2 part 53 / 86

  54. Partition elements FALSE i A 4 1 3 7 9 5 8 2 part 54 / 86

  55. Partition elements TRUE i A 4 1 3 7 9 5 8 2 part 55 / 86

  56. Partition elements TRUE i A 4 1 3 2 9 5 8 7 part 56 / 86

  57. Recurse A 4 1 3 2 9 5 8 7 part 57 / 86

  58. Recursion sorts sublists A 1 2 3 4 5 7 8 9 part 58 / 86

  59. How can we parallelize? O(1) ??? Parallel calls 59 / 86

  60. Parallel partition ● Separate all elements ≤ pivot A 4 9 1 7 3 5 8 2 pivot ● How can we do this in parallel? 60 / 86

  61. Parallel partition ● Separate all elements ≤ pivot A 4 9 1 7 3 5 8 2 pivot ● How can we do this in parallel? – Prefix sums! 61 / 86

  62. Parallel partition ● Create B[i] by comparing A[i] to pivot – 1 if A[i] ≤ A[0] – 0 otherwise A 4 9 1 7 3 5 8 2 B 1 0 1 0 1 0 0 1 62 / 86

  63. Parallel partition ● Prefix sums on B A 4 9 1 7 3 5 8 2 B 1 1 2 2 3 3 3 4 63 / 86

  64. Parallel partition ● Write each A[i] ≤ A[0] to array C – C[B[i]] = A[i] A 4 9 1 7 3 5 8 2 B 1 1 2 2 3 3 3 4 C 4 1 3 2 64 / 86

  65. Parallel partition ● Create B‘ as opposite of B – B‘[i] = 1 if A[i] > A[0] – B‘[i] = 0 otherwise A 4 9 1 7 3 5 8 2 B‘ 0 1 0 1 0 1 1 0 C 4 1 3 2 65 / 86

  66. Parallel partition ● Prefix sums on B‘ A 4 9 1 7 3 5 8 2 B‘ 0 1 1 2 2 3 4 4 C 4 1 3 2 66 / 86

  67. Parallel partition ● Write remaining elements to C – C[B[n-1] + B‘[i]] = A[i] A 4 9 1 7 3 5 8 2 B‘ 0 1 1 2 2 3 4 4 C 4 1 3 2 9 7 5 8 67 / 86

  68. Parallel quicksort analysis ● Each recursive call performs prefix sum ● Worst-case, pivot is always min or max: ● If we assume “good“ pivot is chosen: 68 / 86

Recommend


More recommend