870 likes | 1.11k Views
Lock-Free Resizeable Concurrent Tries. Aleksandar Prokopec, Phil Bagwell, Martin Odersky LAMP, École Polytechnique Fédérale de Lausanne Switzerland. Motivation. xs.foreach { x => doSomething(x) }. Motivation. xs.foreach { x => doSomething (x) }. ys = xs.map { x => x * (-1) }.
E N D
Lock-Free Resizeable Concurrent Tries Aleksandar Prokopec, Phil Bagwell, Martin Odersky LAMP, École Polytechnique Fédérale de Lausanne Switzerland
Motivation xs.foreach { x => doSomething(x) }
Motivation xs.foreach { x => doSomething(x) } ys = xs.map { x => x * (-1) }
Motivation ys = new ConcurrentMap xs.foreach { x => ys.insert(x * (-1)) }
Hash Array Mapped Tries (HAMT) 0 = 0000002
Hash Array Mapped Tries (HAMT) 16 = 0100002 0
Hash Array Mapped Tries (HAMT) 4 = 0001002 0 16
Hash Array Mapped Tries (HAMT) 4 = 0001002 16 0
Hash Array Mapped Tries (HAMT) 12 = 0011002 16 0 4
Hash Array Mapped Tries (HAMT) 12 = 0011002 16 0 4
Hash Array Mapped Tries (HAMT) 16 0 4 12
Hash Array Mapped Tries (HAMT) 16 33 0 4 12
Hash Array Mapped Tries (HAMT) 16 33 48 0 4 12
Hash Array Mapped Tries (HAMT) 16 48 0 4 12 33 37
Hash Array Mapped Tries (HAMT) 16 48 4 12 33 37 0 3
Hash Array Mapped Tries (HAMT) 4 12 16 20 25 33 37 48 57 0 1 3 8 9
Hash Array Mapped Tries (HAMT) 4 12 16 20 25 33 37 48 57 0 1 3 8 9 Too much space!
Hash Array Mapped Tries (HAMT) 4 12 16 20 25 33 37 48 57 0 1 3 8 9
Hash Array Mapped Tries (HAMT) 4 12 16 20 25 33 37 48 57 0 1 3 8 9 Linear search at every level - slow!
Hash Array Mapped Tries (HAMT) 4 12 16 20 25 33 37 48 57 0 1 3 8 9 Solution – bitmap index! Relying on BITPOP instruction.
Hash Array Mapped Tries (HAMT) 48 57 1 0 1 0 48 57 1 0 1 0 48 57 10 48 57 BITPOP(((1 << ((hc >> lev) & 1F)) – 1) & BMP)
Hash Array Mapped Tries (HAMT) 4 12 16 20 25 33 37 48 57 0 1 3 8 9 For 32-way tries – 32-bit bitmap.
Hash Array Mapped Tries (HAMT) 4 12 16 20 25 33 37 48 57 0 1 3 8 9
Hash Array Mapped Tries (HAMT) 4 12 16 20 25 33 37 48 57 0 1 3 9
Hash Array Mapped Tries (HAMT) 4 9 12 16 20 25 33 37 48 57 0 1 3 Remove compresses the trie.
Hash Array Mapped Tries (HAMT) • advantages: • low space consumption and shrinking • no contiguous memory region required • fast – logarithmic complexity, but with a low constant factor • used as efficient immutable maps • no global resize phase – real time applications, potentially more scalable concurrent operations?
Concurrent Trie (Ctrie) • goals: • thread-safe concurrent trie • maintain the advantages of HAMT • rely solely on CAS instructions • ensure lock-freedom and linearizability • lookup – probably same as for HAMT
CAS instruction CAS(address, expected_value, new_value) Atomically replaces the value at the address with the new_value if it is equal to the expected_value. Returns true if successful, false otherwise. May fail spuriously.
Lock-freedom If multiple threads execute an operation, at least one of them will complete the operation within a finite number of steps.
Lock-freedom If multiple threads execute an operation, at least one of them will complete the operation within a finite number of steps. do { a = READ(addr) b = a + 1 } while (!CAS(addr, a, b))
Lock-freedom If multiple threads execute an operation, at least one of them will complete the operation within a finite number of steps. def counter() do { a = READ(addr) b = a + 1 } while (!CAS(addr, a, b))
Insertion 4 9 12 16 20 25 33 37 48 57 0 1 3 17 = 0100012
Insertion 4 9 12 16 20 25 33 37 48 57 0 1 3 16 17 17 = 0100012 1) allocate
Insertion 4 9 12 20 25 33 37 48 57 0 1 3 16 17 17 = 0100012 2) CAS
Insertion 4 9 12 20 25 33 37 48 57 0 1 3 16 17 17 = 0100012
Insertion 4 9 12 20 25 33 37 48 57 0 1 3 16 17 18 = 0100102
Insertion 4 9 12 20 25 33 37 48 57 1) allocate 0 1 3 16 17 16 17 18 18 = 0100102
Insertion 4 9 12 20 25 33 37 48 57 2) CAS 0 1 3 16 17 18 18 = 0100102
Insertion Unless… 4 9 12 20 25 33 37 48 57 2) CAS 0 1 3 16 17 18 18 = 0100102
Insertion 28 = 0111002 Unless… T2 4 9 12 20 25 33 37 48 57 T1-1) allocate 0 1 3 16 17 16 17 18 T1 18 = 0100102
Insertion 28 = 0111002 Unless… T2 T2-1) allocate 4 9 12 20 25 20 25 28 T1-1) allocate 0 1 3 16 17 16 17 18 T1 18 = 0100102
Insertion 28 = 0111002 T2-2) CAS T2 4 9 12 20 25 20 25 28 T1-1) allocate 0 1 3 16 17 16 17 18 T1 18 = 0100102
Insertion 28 = 0111002 T2-2) CAS T2 4 9 12 20 25 20 25 28 0 1 3 16 17 16 17 18 T1 T1-2) CAS 18 = 0100102
Insertion 28 = 0111002 T2 4 9 12 20 25 28 0 1 3 16 17 T1 20 25 18 = 0100102 Lost insert! 16 17 18
Insertion – 2nd attempt Solution: I-nodes 4 9 12 20 25 0 1 3 16 17
Insertion – 2nd attempt 28 = 0111002 T2 4 9 12 20 25 0 1 3 16 17 T1 18 = 0100102