1 / 42

Word RAM Algorithms

International PhD School in Algorithms for Advanced Processor Architectures - AFAPA. Word RAM Algorithms. Gerth Stølting Brodal University of Aarhus. Monday J une 9, 2008, IT University of Copenhagen, Denmark. Lecture Material. Background .

darryl
Download Presentation

Word RAM Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. International PhD School inAlgorithms for Advanced Processor Architectures - AFAPA Word RAM Algorithms Gerth Stølting Brodal University of Aarhus MondayJune 9, 2008, IT University of Copenhagen, Denmark

  2. LectureMaterial

  3. Background... Computer wordsizeshave increased over time (4 bits, 8 bits, 12 bits, 16 bits, 32 bits, 64 bits, 128 bits, ...GPU...) What is the power and limitations of wordcomputations? Howcanweexploitwordparallellism?

  4. Overview • Word RAM model • Words as sets • Bit-manipulationonwords • Trees • Searching • Sorting • Word RAM results

  5. Word RAM Model

  6. Word RAM (Random Access Machine) n 0 011001101 1 101111101 • Unlimitedmemory • Word = n bits • CPU, O(1) registers • CPU, read & writememorywords • set[i,v], get[i] • CPU, computation: • Boolean operations • Arithmetic operations: +, -, (*) • Shifting: x<<k = x∙2k, x>>k = x/ 2k • Operations take O(1) time 2 001011101 3 100101000 4 011001101 5 011001101 6 CPU 101111101 7 001011101 100101000 … 011001101 101111101 001011101 100101000 i 101111101 001011101 100101000 011001101 101111101 001011101 100101000 0110011 111000 01101 1111 01

  7. Word RAM – Boolean operations 0 = False, 1 = True Correspondingword operations workon all n bits in oneortwowords in parallel. Example: Clear a set of bits using AND

  8. The first tricks...

  9. Exercise 1 Consider a double-linked list, whereeach node has threefields: prev, next, and an element. Usuallyprev and nextrequireonewordeach. Question.Describehowprev and next for a node canbecombinedintooneword, suchthat navigation in a double-linked list is still possible. x1 x4 x2 x3 prev next prev(p) p

  10. Exercise 2 Question. Howcanwepack an array of N 5-bit integersinto an array of 64-bit words, suchthat • weonlyuseN∙5/64 words, and • wecanaccess the i’th 5-bit integerefficiently? 64 bits 01011 01011 01011 01011 01011 waste 01011 01011 01011 01011 01011 how not to do it

  11. Words as Sets

  12. Words as Sets Wouldlike to store subsetsof {0,1,2,...,n-1} in an n-bitword. The set {2,5,7,13} cane.g. berepresented by the followingword (bit-vector):

  13. Exercise 3 Question. Howcanweperform the following set operations efficiently, given twowordsrepresentingS1 and S2: • S3 = S1  S2 • S3 = S1  S2 • S3 = S1 \ S2

  14. Exercise 4 Question. Howcanweperform the following set queries, given wordsrepresenting the sets: • x  S ? • S1  S2 ? • Disjoint(S1, S2) ? • Disjoint(S1, S2,..., Sk ) ?

  15. Exercise 5 Question. Howcanweperformcompute |S|, given S as a word (i.e. numer of bits = 1)? • withoutusingmultiplication • usingmultiplication • S • |S|= 4

  16. Bit-manipulationson Words

  17. Exercise 6 Question. Describehow to efficientlyreverse a wordS. • S • reverse(S)

  18. Exercise 7 Question. Howcanweefficientlycompute the zipper yn/2-1xn/2-1...y2x2y1x1y0x0 of twohalf-wordsxn/2-1...x2x1x0 and yn/2-1...y2y1y0 ? Whitcomb Judson developed the first commercial zipper (named the Clasp Locker) in 1893.

  19. Exercise 8 Question. Describehow to compress a subset of the bits w.r.t. an arbitrary set of bit positions ik>∙∙∙>i2>i1: compress(xn-1,...,x2,x1,x0) = 0....0xik...xi2xi1 i4=14, i3=7, i2=5, i1=2 • compress(x)

  20. Exercise 9 Question. • Describehow to remove the rightmost 1 • Describehow to extract the rightmost 1 remove extract

  21. Exercise 10 Question. Describehow to compute the positionρ(x) of the rightmost 1 in a wordx • withoutusingmultiplication • usingmultiplication • usinginteger-to-floatconversion x ρ(x) = 4

  22. Exercise 11 • Letλ(x)be the positionof the leftmost 1 in a wordx (i.e. λ(x) = log2(x)). • Question. • Describehow to test ifλ(x)= λ(y), • withoutactuallycomputingλ(x) and λ(y). x λ(x) = 11

  23. Exercise 12* Question. • Describehow to compute the positionλ(x) of the leftmost 1 in a wordx (i.e. λ(x) = log2(x)) • withoutusingmultiplication • usingmultiplication • usinginteger-to-floatconversion x λ(x) = 11

  24. n = g∙g, g a power of 2 Fredman & Willard Computation of λ(x) in O(1) steps using 5 multiplications

  25. Exercise 13 Question. • Describehow to compute the length of the longestcommonprefix of twowords • xn-1...x2x1x0 and yn-1...y2y1y0 x y lcp(x,y) = 6

  26. Trees

  27. Exercise 14 Question. Consider the nodes of a completebinarytreebeingnumberedlevel-by-level and the rootbeingnumbered 1. • Whatare the numbers of the children of node i ? • What is the number of the parent of node i ? 1 2 3 4 6 7 5 8 9 10 11 12 13 14 15

  28. Exercise 15 Question. • Howcan the height of the treebecomputed from a leafnumber? • Howcan LCA(x,y) of two leavesx and ybecomputed (lowestcommonancestor)? 1 LCA(x,y) 2 3 4 6 7 5 8 9 10 11 12 13 14 15 x y

  29. Exercise 16* Question. Describehow to assign O(1) words to each node in an arbitrarytree, suchthatLCA(x,y) queriescan beanswered in O(1) time. LCA(x,y) x y

  30. Searching

  31. Exercise 17 Question.Consider a n-bitwordxstoringk n/k-bitvaluesv0,...,vk-1 • Describehow to decideif all viarenon-zero • Describehow to find the firstviequal to zero • DescribehowimplementSearch(x,u), that returns a isuchthatvi=u (ifsuch a viexists) x

  32. Sorting :Sorting Networks

  33. Exercise 18 Question.Construct a comparisonnetworkthat outputs the minimum of 8 input lines. What is the number of comparators and the depth of the comparisonnetwork? x1 x2 x3 x4 x5 x6 x7 x8 minimum

  34. Exercise 19 Question.Construct a comparisonnetworkthat outputs the minimum and maximum of 8 input lines. What is the number of comparators and the depth of the comparisonnetwork? x1 maximum x2 x3 x4 x5 x6 x7 x8 minimum

  35. Odd-EvenMerge Sort K.E. Batcher 1968 Odd-evenmerge sort for N=8. Size O(N∙(log N)2) and depth O((log N)2) Fact. At eachdepth all compators have equallength [ Ajtai, Komlós, Szemerédi 1983: depth O(log N), size O(N∙logN) ]

  36. Sorting :Word RAM implementations ofSorting Networks

  37. Exercise 20 Question. Descibehow to sort twosub-wordsstored in a single wordon a Word RAM ― withoutusingbranch-instructions (implementation of a comparator) x y input output min(x,y) max(x,y)

  38. Exercise 21 Question. Consider a n-bitwordxstoringn/k-bitvaluesv0,...,vk-1. Describe a Word RAM implementation of odd-evenmerge sort withrunning O((log k)2). • Odd-evenmerge sort for N=8.

  39. More aboutSorting & Searching

  40. More aboutSorting & Searching SortingNwords DynamicdictionariesstoringNwords

  41. Summary

  42. Summary Many operations onwordscanbeefficiently withoutusingmultiplication λ(x) and ρ(x)canbecomputed in O(1) time usingmultiplication, and O(loglog n) time without mult. Parallellismcanbeachieved by packingseveral elements intooneword The great (theory) question: Can N wordsbesortedon a Word RAM in O(N) time?

More Related