algorithm engineering

Algorithm Engineering (aka. How to Write Fast Code) CS26 S260 - PowerPoint PPT Presentation

Algorithm Engineering (aka. How to Write Fast Code) CS26 S260 Lecture cture 6 Yan n Gu I/O Algorithms and Parallel Samplesort Review of Samplesort CS260: Algorithm Semisort Engineering Lecture 6 Course Policy 2 Sample-sort


  1. Algorithm Engineering (aka. How to Write Fast Code) CS26 S260 – Lecture cture 6 Yan n Gu I/O Algorithms and Parallel Samplesort

  2. Review of Samplesort CS260: Algorithm Semisort Engineering Lecture 6 Course Policy 2

  3. Sample-sort outline Analo logou gous s to mult ltiw iway ay quic ickso ksort 1. 1. Sp Spli lit in input ut array in into 𝑂 contiguo iguous us suba barra rrays ys of siz ize 𝑂 . So Sort subar arrays rays recursi sivel vely … 𝑂 , sorted 𝑂

  4. Sample-sort outline Analo logou gous s to mult ltiw iway ay quic ickso ksort 𝑂 , sorted 1. 1. Sp Spli lit in input ut array in into 𝑂 contiguo iguous us suba barra rrays ys of siz ize 𝑂 . So Sort subar arrays rays recursi sivel vely y (sequ equent entia ially lly) …

  5. Sample-sort outline 2. 2. Choo oose se 𝑂 βˆ’ 1 β€œgood” pivots 𝑂 , sorted π‘ž 1 ≀ π‘ž 2 ≀ β‹― ≀ π‘ž π‘‚βˆ’1 3. 3. Dis istribu ribute te su subar barrays rays in into o buckets ckets , , ac accordin ording g to … pivot vots Size β‰ˆ 𝑂 ≀ π‘ž 1 ≀ ≀ π‘ž 2 ≀ β‹― ≀ π‘ž π‘‚βˆ’1 ≀ Bucket 1 Bucket 2 Bucket 𝑂

  6. Sample-sort outline 4. Recurs 4. cursively ively sort rt the buckets ckets ≀ π‘ž 1 ≀ ≀ π‘ž 2 ≀ β‹― ≀ π‘ž π‘‚βˆ’1 ≀ Bucket 1 Bucket 2 Bucket 𝑂 5. 5. Copy py conca oncatenated tenated buckets ckets bac ack k to input put ar arra ray sorted

  7. Review of Samplesort CS260: Algorithm Semisort Engineering Lecture 6 Course Policy 7

  8. What is semisort? key 45 12 45 61 28 61 61 45 28 45 Value 2 5 3 9 5 9 8 1 7 5 β€’ Input: β€’ An array of records with associated keys β€’ Assume keys can be hashed to the range [π‘œ 𝑙 ] β€’ Goal: β€’ All records with equal keys should be adjacent

  9. What is semisort? key 12 61 61 61 45 45 45 45 28 28 Value 5 8 9 9 2 5 1 3 7 5 β€’ Input: β€’ An array of records with associated keys β€’ Assume keys can be hashed to the range [π‘œ 𝑙 ] β€’ Goal: β€’ All records with equal keys should be adjacent

  10. What is semisort? key 45 45 45 45 12 61 61 61 28 28 Value 2 5 1 3 5 8 9 9 7 5 β€’ Input: β€’ An array of records with associated keys β€’ Assume keys can be hashed to the range [π‘œ 𝑙 ] β€’ Goal: β€’ All records with equal keys should be adjacent β€’ Different keys are not necessarily sorted β€’ Records with equal keys do not need to be sorted by their values

  11. What is semisort? key 45 45 45 45 12 61 61 61 28 28 Value 1 5 3 2 5 8 9 9 7 5 β€’ Input: β€’ An array of records with associated keys β€’ Assume keys can be hashed to the range [π‘œ 𝑙 ] β€’ Goal: β€’ All records with equal keys should be adjacent β€’ Different keys are not necessarily sorted β€’ Records with equal keys do not need to be sorted by their values

  12. Semisort is one of the most useful primitives in parallel algorithms Parallel In-Place Algorithms: Theory and Practice Julienne: A Framework for Parallel Graph Algorithms using Work- efficient Bucketing Semi-Asymmetric Parallel Graph Algorithms for NVRAMs Efficient BVH Construction via Approximate Agglomerative Clustering Theoretically-Efficient and Practical Parallel DBSCAN 12

  13. Why is semisort so useful? (albeit not seen before) β€’ Semisorting can be done by sorting, but faster (less restriction) β€’ Theoretically can be done in 𝑃 π‘œ work not 𝑃 π‘œ log π‘œ work β€’ Can be used to implement counting / integer sort β€’ Integer sort: given π‘œ key-value pairs with keys in range [1, … , π‘œ] , query the KV-pairs with a certain key β€’ Counting sort: given π‘œ key-value pairs with keys in range [1, … , π‘œ] , query the number of KV-pairs with a certain key β€’ In database community, this is called the GroupBy operator 13

  14. Why is semisort so useful? (albeit not seen before) β€’ Semisorting can be done by sorting, but faster (less restriction) β€’ Theoretically can be done in 𝑃 π‘œ work not 𝑃 π‘œ log π‘œ work β€’ Can be used to implement counting / integer sort keys 37 … 58 … 92 … 92 56 key value Linked 12 8 11 lists of values 9 19 56 52 14

  15. Attempts – Sequentially: Pre-allocated array 92 56 keys 37 … 58 … 92 … key value Arrays 12 8 11 of values 9 44 19 52 31 56 ο‚’ Problem ο‚— Need to pre-count the number of each key

  16. Another use case for semisrot β€’ Generate adjacency array for a graph Sorted Edge list edge list (3,5) (3,5) 2 6 (1,7) (3,7) (2,3) (3,6) 3 4 (3,6) (5,4) 1 5 (5,4) (1,6) 7 (3,7) (1,7) (1,6) (2,3)

  17. What is semisort? key 45 45 45 45 12 61 61 61 28 28 Value 1 5 3 2 5 8 9 9 7 5 β€’ Input: β€’ An array of records with associated keys β€’ Assume keys can be hashed to the range [π‘œ 𝑙 ] β€’ Goal: β€’ All records with equal keys should be adjacent β€’ Different keys are not necessarily sorted β€’ Records with equal keys do not need to be sorted by their values

  18. Why is semisort hard? key 45 45 45 45 12 61 61 61 28 28 Value 1 5 3 2 5 8 9 9 7 5 β€’ There can be many duplicate keys β€’ Heavy keys β€’ Or, there can be almost no duplicate keys β€’ Light keys

  19. Implement integer sort using semisort key 45 45 45 45 12 61 61 61 28 28 Value 1 5 3 2 5 8 9 9 7 5 β€’ Input: 𝒐 KV-pairs with key in [π‘œ] β€’ Step 1: hash the keys (i.e., for 𝒍 𝒋 , π’˜ 𝒋 , generate π’Š 𝒋 = 𝐒𝐛𝐭𝐒(𝒍 𝒋 ) ) β€’ Step 2: semisort π’Š 𝒋 , (𝒍 𝒋 , π’˜ 𝒋 ) , and resolve conflicts β€’ Step 3: get the pointer for each key 𝒍 𝒋

  20. The Top-Down Parallel Semisort Algorithm 22

  21. The main goal estimate key counts β€’ And tell the heavy keys from light ones. By how? Sampling! β€’ For a key appear more than 𝐨/𝒖 times, we call it a heavy key β€’ Otherwise, we call it a light key β€’ We can treat them separately

  22. The algorithm β€’ Take 𝒖 log 𝒐 samples and sort them β€’ For those keys with more than log 𝒐 appearances, we mark them as heavy keys, others are light keys β€’ We give each heavy key a bucket, and the another 𝒖 buckets for light keys each corresponds to a range of 𝒐 𝒍 /𝒖 β€’ The input keys are hashed into 𝒐 𝒍 β€’ In total we have no more than 2𝑒 buckets β€’ The rest of the algorithm is pretty similar to samplesort

  23. Phase 1: Sampling and sorting 1. Select a sample set 𝑇 with 𝑒 log π‘œ of keys 2. Sort 𝑇 …… Sampling …… S Sorting 17 17 …… 5 5 5 8 8 8 8 8 11 17 (Counting)

  24. Phase 2: Bucket Construction Sorted samples: 17 17 …… 5 5 5 8 8 8 8 8 11 17 Counting & Filtering Light keys Heavy keys Range 0-15 16-31 keys 65 … 8 20 keys 5 11 17 21 26 31 ...

  25. At the end of Phase 2 β€’ In total we have no more than 2𝑒 buckets β€’ 𝑒 of them are for light keys β€’ Then we construct a hash table for the heavy keys β€’ Now we know which bucket each KV-pair (𝒍 𝒋 , π’˜ 𝒋 ) goes to: β€’ If 𝑙 𝑗 is found in the hash table, assign it to the associated heavy bucket β€’ Otherwise, it goes to the light bucket based on the range of 𝑙 𝑗 β€’ The rest of the algorithm is almost identical to samplesort

  26. Sample-sort outline Analogous to multiway quicksort 𝑂/𝑒 1. Split input array into 𝑂 contiguous subarrays of size 𝑂 𝑒 … 𝑂 𝑂

  27. Sample-sort outline Analogous to multiway quicksort Size β‰ˆ 𝑒 1. Split input array into 𝑂/𝑒 contiguous subarrays of size 𝑒 . Sort subarrays recursively (sequentially) …

  28. Sample-sort outline 2. Distribute subarrays into buckets … … ≀ π‘ž 1 ≀ ≀ π‘ž 2 ≀ β‹― ≀ π‘ž π‘‚βˆ’1 ≀ Bucket 1 Bucket 2 Bucket 𝑂

  29. Sample-sort outline Only for the light buckets 3. Recursively sort the buckets … Bucket 1 Bucket 2 Bucket 𝑂 4. Copy concatenated buckets back to input array sorted

  30. Difference 2: subarrays are not sorted β€’ For simplicity, assume 𝒐 = πŸπŸ• , and the input is [𝟐, πŸ‘, πŸ’, πŸ“, 𝟐, 𝟐, πŸ’, πŸ’, 𝟐, πŸ‘, πŸ‘, πŸ“, 𝟐, πŸ‘, πŸ“, πŸ“] β€’ First, get the count for each subarray in each bucket [𝟐, 𝟐, 𝟐, 𝟐, πŸ‘, 𝟏, πŸ‘, 𝟏, 𝟐, πŸ‘, 𝟏, 𝟐, 𝟐, 𝟐, 𝟏, πŸ‘] β€’ Then, transpose the array and scan to compute the offsets [𝟐, πŸ‘, 𝟐, 𝟐, 𝟐, 𝟏, πŸ‘, 𝟐, 𝟐, πŸ‘, 𝟏, 𝟏, 𝟐, 𝟏, 𝟐, πŸ‘] [𝟏, 𝟐, πŸ’, πŸ“, πŸ”, πŸ•, πŸ•, πŸ—, 𝟘, 𝟐𝟏, πŸπŸ‘, πŸπŸ‘, πŸπŸ‘, πŸπŸ’, πŸπŸ’, πŸπŸ“] β€’ Lastly, move each element to the corresponding bucket [βˆ…, βˆ…, βˆ…, βˆ…, βˆ…, βˆ…, βˆ…, βˆ…, βˆ…, βˆ…, βˆ…, βˆ…, βˆ…, βˆ…, βˆ…, βˆ…] [𝟐, βˆ…, βˆ…, βˆ…, βˆ…, πŸ‘, βˆ…, βˆ…, βˆ…, πŸ’, βˆ…, βˆ…, πŸ“, βˆ…, βˆ…, βˆ…] [𝟐, 𝟐, 𝟐, βˆ…, βˆ…, πŸ‘, βˆ…, βˆ…, βˆ…, πŸ’, πŸ’, πŸ’, πŸ“, βˆ…, βˆ…, βˆ…] 32

Recommend


More recommend