in place binary counters
play

In-Place Binary Counters Amr Elmasry 1 , 2 and Jyrki Katajainen 2 , 3 - PowerPoint PPT Presentation

In-Place Binary Counters Amr Elmasry 1 , 2 and Jyrki Katajainen 2 , 3 1 Alexandria University 2 University of Copenhagen 3 Jyrki Katajainen and Company These slides are available from my research information system (see http://www.diku.dk/~jyrki/


  1. In-Place Binary Counters Amr Elmasry 1 , 2 and Jyrki Katajainen 2 , 3 1 Alexandria University 2 University of Copenhagen 3 Jyrki Katajainen and Company These slides are available from my research information system (see http://www.diku.dk/~jyrki/ under Presentations). c � Performance Engineering Laboratory MFCS 2013 in Klosterneuburg on 26 August, 2013 (1)

  2. Numeral systems 2 0 1 3 10 Digit set: the values that the + 1 10 digits can take 2 0 1 4 10 Weight set: the weights that Decimal digits: { 0 , 1 , 2 , . . . , 9 } the digits represent when de- Binary weights: w i = 2 i for i = termining the decimal value 0 , 1 , . . . of the underlying integer Regular system: a redundant Rule set: the rules that the rep- binary system where every resentation of each integer two 2’s have at least one 0 must obey in between Operation set: the operations Operations relevant for us: that are to be supported ++ , -- , and + c � Performance Engineering Laboratory MFCS 2013 in Klosterneuburg on 26 August, 2013 (2)

  3. The binary system we love 1 1 1 1 1 1 . . . 1 2 ℓ bits + 1 2 1 0 0 0 0 0 0 . . . 0 2 We have developed a numeral system that Representation: ℓ + O (lg ℓ ) bits • is equally space efficient as ++ / -- : Θ( ℓ/w ) worst-case time the binary system + : ℓ 1 , ℓ 2 bits, Θ(( ℓ 1 + ℓ 2 ) /w ) • supports ++ and -- in O (1) worst-case time worst-case time w : machine word size in bits • supports + in O (( ℓ 1 + ℓ 2 ) /w ) worst-case time. c � Performance Engineering Laboratory MFCS 2013 in Klosterneuburg on 26 August, 2013 (3)

  4. Model of computation A word RAM with the operations available in the C pro- gramming language; time means the number of word op- erations executed: • an infinite array a for storing data • a constant number of variables workspace N = 14 a 14 0011 1110 1010 0110 w = 4 3 2 1 0 • if a counter uses N bits, these must be kept in the first ⌈ N/w ⌉ locations of a • the word size w ≥ ⌈ lg(1 + N ) ⌉ , where N is the problem size. c � Performance Engineering Laboratory MFCS 2013 in Klosterneuburg on 26 August, 2013 (4)

  5. Application: Binomial queues 2 6 29 N = 11 10 = 1011 2 9 26 19 7 Primitives 51 join : 14 10 min + − → 48 B k B k B k +1 Worst-case bounds split : minimum : O (1) insert / borrow : Θ(lg N ) → O (1) + − → union : two queues of size M , N , O (lg M + lg N ) B k +1 B k B k extract - min , delete : ∼ union c � Performance Engineering Laboratory MFCS 2013 in Klosterneuburg on 26 August, 2013 (5)

  6. Current knowledge Binary system Our system [this paper] Representation: ℓ + O (lg ℓ ) bits Representation: ℓ + O (lg ℓ ) bits ++ / -- : Θ( ℓ/w ) worst-case time ++ / -- : O (1) worst-case time + : ℓ 1 , ℓ 2 bits, Θ(( ℓ 1 + ℓ 2 ) /w ) + : ℓ 1 , ℓ 2 bits, O (( ℓ 1 + ℓ 2 ) /w ) worst-case time worst-case time Extended regular system Known space-economic solu- tions Representation: O ( ℓ ) words ++ d i / -- d i : O (1) digit changes in • analysed in the bit-probe the worst case model + : ℓ 1 , ℓ 2 bits, O (min { ℓ 1 , ℓ 2 } ) digit • rely on Gray codes → cannot changes in the worst case be used in data-structural ap- (open how to use word-level plications parallelism) c � Performance Engineering Laboratory MFCS 2013 in Klosterneuburg on 26 August, 2013 (6)

  7. The regular system Digits: { 0 , 1 , 2 } Weights: w i = 2 i for i = 0 , 1 , . . . Rule: the string of digits re- 2 0 5 10 = spects the regular expres- 128 64 32 16 8 4 2 1 sion ((1 | 21 ∗ 0)0 ∗ ) ∗ (seen from 1 0 1 2 1 1 0 1 regular right to left); this is not block exactly the same rule as that given before! Operations: ++ , -- , and + c � Performance Engineering Laboratory MFCS 2013 in Klosterneuburg on 26 August, 2013 (7)

  8. ++ in the regular system Algorithm Proof of correctness 1. d 0 ← d 0 + 1 1. d 0 = 0 2. fix the first 2 1.1. part of a block 1.1.1. after the block comes a Fix: x 2 → ( x + 1)0, i.e. move a singleton 0 carry one position forward 1.1.2. after the block comes a Data structure: a stack of the singleton 1 positions of all 2’s 1.1.3. after the block comes another block 1 2 1 1 0 1 regular 1.2. singleton 0 + 1 regular 2. d 0 = 1 1 2 1 1 1 0 regular 2.1. followed by a singleton 0 2.2. followed by a singleton 1 2.3. followed by a block c � Performance Engineering Laboratory MFCS 2013 in Klosterneuburg on 26 August, 2013 (8)

  9. Motivating questions 1. How would you improve the space efficiency of the regu- lar system? 2. How would you extend the regular system to support -- in O (1) worst-case time? 1 0 0 0 0 0 . . . 0 regular – 1 regular c � Performance Engineering Laboratory MFCS 2013 in Klosterneuburg on 26 August, 2013 (9)

  10. Our in-place system Let = � d ℓ − 1 , . . . , d 1 , d 0 � and d K = � ℓ − 1 i =0 d i w i . Digits: (a) d 0 is a basket of 1’s, call their number x (b) there may exist at most Our system is as the regular sys- one α such that d α = 2 tem, but we only allow at most (c) d i ∈ { 0 , 1 } for all i � = 0 one 2, except that x can become and i � = α as high as ⌈ lg(1 + K ) ⌉ . Weights: w i = 2 i for i = 0 , 1 , . . . Rules: (a) is of the form d ((1(0 | 1) ∗ ) ∗ x | ((1(0 | 1) ∗ ) ∗ 20 ∗ x (b) � ℓ − 1 i =0 d i ≤ ⌈ lg(1 + K ) ⌉ Operations: ++ , -- , and + c � Performance Engineering Laboratory MFCS 2013 in Klosterneuburg on 26 August, 2013 (10)

  11. Actual representation 50 10 when only ++ is supported ℓ = 6 (length of the representation) x = 2 (size of the basket of 1’s) 1 0 1 1 0 – = 1 (indicates that there is a 2) carry b 5 b 4 b 3 b 2 b 1 b 0 α = 2 (position of the 2, if any) ζ = 2 (number of zeros) β = γ (normal mode) = β (normal mode) γ c � Performance Engineering Laboratory MFCS 2013 in Klosterneuburg on 26 August, 2013 (11)

  12. Key ideas Basket ≡ x Modes normal mode: γ = β search mode: d γ = 0, γ ← γ + 2 borrow mode: d γ � = 0, γ ← γ − 2 • not too big: � ℓ − 1 i =0 d i ≈ ℓ • not too small: x > 0 γ β First non-zero digit 1 0 0 0 0 x our • position may be lost γ : position up to which the ↓ search has reached when search- 1 0 0 0 1 x our ing for the first non-zero digit – 1 our β : position up to which the post- 1 0 0 0 0 ( x + 1) our poned borrows should be exe- cuted c � Performance Engineering Laboratory MFCS 2013 in Klosterneuburg on 26 August, 2013 (12)

  13. ++ in our system γ α β Algorithm 1 0 2 0 0 3 our 1. if γ = β (normal mode) + 1 our 2. if there is a 2 (ignore x ) 1 1 0 0 0 4 our fix it + 1 our 3. else if no 2 and x ≥ 2 1 1 0 0 1 3 our x ← x − 2 ++ d 1 4. else (other modes) γ β 5. β ← β + 2 1 1 0 0 0 3 our 6. x ← x + 1 + 1 our 1 1 0 0 0 4 our c � Performance Engineering Laboratory MFCS 2013 in Klosterneuburg on 26 August, 2013 (13)

  14. The idea for -- Implement -- as the reverse of ++ . However, this approach fails when there is a long borrow sequence. Solution: • Keep a basket of 1’s ( x ) • Search for the first d i , d i � = 0 and i ≥ 1, in double speed • Fix the borrows in double speed (until γ meets β ) • Take 1 from the basket in connection with every -- . Key: x ≥ #0’s in the front of the first non-zero digit Note: We should be able to support ++ ’s when we are not in the normal mode. Whenever γ > β , it means that there have been more -- ’s than ++ ’s since we switched to the search mode. c � Performance Engineering Laboratory MFCS 2013 in Klosterneuburg on 26 August, 2013 (14)

  15. Proof of correctness • Show that after each operation the form is right: � (1(0 | 1) ∗ � ∗ x | � (1(0 | 1) ∗ � ∗ 20 ∗ x • Show that a stronger invariant holds: � ℓ − 1 i =0 d i = ⌈ lg(1 + K ) ⌉ − ( γ − β ) / 2 K = � ℓ − 1 i =0 d i w i β : position up to which the postponed borrows should be executed γ : position up to which the search has reached when searching for the first non-zero digit For details, read the proceedings! c � Performance Engineering Laboratory MFCS 2013 in Klosterneuburg on 26 August, 2013 (15)

  16. + in our system • Add the bit representations, the carries, and the values of the least significant digits using binary addition • Convert the resulting binary number back to the re- quired form (in particular, x must be larger than ζ ) ℓ : length of the representation x : size of the basket of 1’s carry : 1 if there is a 2 α : position of the 2, if any ζ : number of zeros β : position up to which the postponed borrows should be executed γ : position up to which the search has reached when searching for the first non-zero digit Claim: The worst-case running time is O (( ℓ 1 + ℓ 2 ) /w ) Question: Which primitives should the machine support? c � Performance Engineering Laboratory MFCS 2013 in Klosterneuburg on 26 August, 2013 (16)

  17. Perspective • Implementation: No random access needed • Digit changes per ++ / -- : O (1) if the basket is in unary form • Other useful operations: == 0: O (1) worst-case time; - , == , < : O (( ℓ 1 + ℓ 2 ) /w ) worst-case time • Primary schools in 2063: Our system in use • Future computer hardware: Our system in use I am done! Thank you! c � Performance Engineering Laboratory MFCS 2013 in Klosterneuburg on 26 August, 2013 (17)

Recommend


More recommend