1 / 58

Practical concurrent algorithms

Practical concurrent algorithms. Mihai Letia Concurrent Algorithms 2012 Distributed Programming Laboratory. Slides by Aleksandar Dragojevic. Practical. Course oriented towards theory Today: A glimpse of practice Use practical operations Implement practical data structure Linked list

brina
Download Presentation

Practical concurrent 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. Practical concurrent algorithms Mihai Letia Concurrent Algorithms 2012 Distributed Programming Laboratory Slides by Aleksandar Dragojevic

  2. Practical • Course oriented towards theory • Today: A glimpse of practice • Use practical operations • Implement practical data structure • Linked list • Give a feeling of “real-world” concurrent algorithm

  3. Outline • Compare-and-swap • Practical system model • Lock-freedom • Linked-list

  4. Outline • Compare-and-swap • Practical system model • Lock-freedom • Linked-list

  5. Compare-and-swap (CAS) shared val = BOT; CAS(old, new): ret = val; if val == old: val = new; return ret;

  6. Why is it important? • We can implement consensus among any number of processes shared CAS cas; Consensus(val): res = cas.CAS(BOT, val); if res != BOT: val = res; return val;

  7. Why is it important? (cont) • Consensus allows processes to agree on something • Intuitively this is powerful • CAS allows any number of processes to agree on something

  8. Why is it important also? • It is available in hardware • On (probably) all architectures • It is a staple of concurrent programming • Linked lists • Queues • Etc.

  9. Outline • Compare-and-swap • Practical system model • Lock-freedom • Linked-list

  10. Practical system model • So far: processes operate on shared objects • Object are linearizable • Sequential specification • This lecture: processes operate on memory locations • Each location supports multiple instructions • Sequential specification

  11. Practical system model (cont) • Processes access memory by issuing atomic load, store and CAS instructions • Processes can perform local computations on the loaded values • Processes are equivalent to OS threads • All processes share the same address space

  12. Practical system model (cont) Main memory 0x0 P0 load() CAS(v0, v1) 0x4 0x8 store(v3) … P1 0xff…f8 0xff…fc

  13. CAS in practice w_t CAS(w_t *addr, w_t old, w_t new) { w_t curr = *addr; if(curr == old) *addr = new; return curr; }

  14. From practical model to other model • It is straightforward to use practical model as the model used in the course • For operations supported by practical model • Each location is treated as an object • Only restricted operations are allowed on each location • Register: load/store • CAS: CAS

  15. From practical model to other model (cont) class CAS { w_t value = BOT; w_t CAS(w_t old, w_t new) { return CASInst(&value, old, new); } }

  16. CAS example • Shared variable initialization • We have a shared variable initialized to BOT • Multiple processes want to initialize it to a value • Each process proposes its value (e.g. a problem it needs solved) • Only one initialization should succeed • Processes need to agree on the initialized value

  17. CAS example (cont) w_t *prob = BOT; process() { w_t *prop = init_problem(rnd()); w_t *res = CAS(&prob, BOT, prop); if(res != BOT) uninit_problem(prop); work_on(prob); }

  18. Other available operations? • Depends on the architecture • Available on x86: • Test-and-set • Fetch-and-increment • Fetch-and-add

  19. Outline • Compare-and-swap • Practical system model • Lock-freedom • Linked-list

  20. Linked list • A list of nodes that are linked using references • Dynamic data structure • Nodes can be allocated and deallocated • No need to know maximum size upfront • Base for other data structures • Queue • Hash table • Skip list

  21. Linked list API struct Node { int key; int val; Node *next; }; bool Insert(Node *node); Node *Remove(int key); Node *Lookup(int key);

  22. Linked list 10 20 30 BOT 15 15 Insert() Remove(20)

  23. Our goal today • Implement the linked list in our model using load, store and CAS instructions

  24. Progress • We are not going to implement a wait-free linked list • We assumed wait-free implementations so far in the course • We will implement a lock-free linked list • High performance • Wait-free linked list is a hard problem • In next lectures

  25. Outline • Compare-and-swap • Practical system model • Lock-freedom • Linked-list (we will return to this)

  26. Lock-freedom • If a process performs steps of an algorithm for sufficiently long time, some process will make progress • What is difference to wait-freedom?

  27. Difference to wait-freedom • With wait-free algorithms, when a process makes steps it is guaranteed to make progress • With lock-free algorithms, some process is guaranteed to make progress • But not necessarily the process performing the steps

  28. Intuition for lock-freedom • Lock-freedom guarantees overall progress • No guarantee of overall progress is weaker than lock-freedom (more on this in next lectures) • Eliminates live-lock • It is usually fine in practice • In practice the “winner” is likely to go do something else and permit the “looser” to perform the operation later

  29. Which is better? • Wait-freedom is a stronger property • It is harder to achieve in efficient way • General wait-free algorithms in next lectures • Lock-freedom is easier to achieve in practice efficiently • It is more often used for that reason • If we had equally efficient algorithms Alock-free and Await-free, we would choose Await-free

  30. Lock-free and Linearizable • Having lock-free algorithms doesn’t change the requirement for linearizable algorithms • If the operation succeeds, there has to be a linearization point • Lock-freedom just means that the operation will not necessarily succeed

  31. Lock-free strong counter w_t count = 0; w_t increment() { while(true) { w_tval = count; // implied load if(CAS(&count, val, val + 1) == val) return val; } }

  32. Lock-free strong counter (cont) • Simple algorithm • Starvation is possible • A process could be prevented from returning a value by another process that always succeeds • Live-lock is not possible • A process is prevented from returning by a successful process • In practice, starvation is not probable

  33. Lock-free strong counter (cont) count 1 0 3 2 inc() inc() P0 inc() load←0 cas←ok cas←ok load←1 load←2 cas←ok inc() P1 … load←1 cas←fail load←2 cas←fail CAS can continue to fail

  34. Wait-free counter? • General technique exists • More complex and less efficient • Next lecture • Intuition • Processes have to help each other • A process that succeeds the CAS needs to help the other processes to finish their operations

  35. Outline • Compare-and-swap • Practical system model • Lock-freedom • Linked-list

  36. Our goal today • Implement the lock-free linked list in our model using load, store and CAS instructions

  37. Lock-freedom • If a process performs steps of an algorithm for sufficiently long, some process will make progress

  38. Linked list API struct Node { int key; int val; Node *next; }; bool Insert(Node *node); Node *Remove(int key); Node *Lookup(int key);

  39. What should we do? 10 20 30 BOT

  40. Overview • Fields key and val are not changed after the element has been inserted into the list • We only need to change next field concurrently • During insert and remove • We need to “swing” it from one node to another • Keep the list sorted

  41. Lookup (version 1) 10 20 30 BOT BOT Lookup(20)

  42. Insert • First lookup an element • If the node with the same key exists, return false • If not, we found the position for element insertion • Locally prepare node’s next field • Use CAS to make sure previous node hasn’t changed and to update it to the new node

  43. Insert (version 1) What if another node was inserted? If CAS fails, redo the search. If it succeeds, the node was inserted. Use CAS to check the previous node is the same and to update it. CAS 10 20 30 BOT 25 25 22 Locally! Insert(25)

  44. Remove • First lookup an element • If node with the key doesn’t exist, return false • If it does, we found the node to remove and the previous node • Use CAS to make sure previous node hasn’t changed and to remove the found node

  45. Remove (version 1) What if another node was inserted in the meantime after node 20? As before, use CAS to unlink the node from list Then we accidentally removed that node too! CAS 10 20 30 BOT 25 Lookup finds node Remove(20)

  46. Is Remove (version 1) correct then? • It is not linerizable (lost operation) • Which means that it is not correct • What should we do about it?

  47. Why is there a problem? • We update the previous node’s next field and make it point to what we saw was the next field of the removed node • But that doesn’t mean this is still the next field of the removed node • CAS doesn’t ensure the next field of the removed node is unchanged • It ensures the next field of the previous node is unchanged (this is also necessary)

  48. Why is there a problem? (cont) • We need to ensure two locations remain the same while we update one of them • CAS operates on a single location • We need double-compare-single-swap operation to (simply) solve this problem • This is not available in hardware

  49. How to solve the problem? • Mark the removed nodes using a spare bit from the next field • There is a spare bit if we assume word-aligned data structures • Marking of the node is the linearization point of the operation • After the node is marked, try to unlink it too • Other operations can help with the unlinking

  50. Remove (version 2) What if unlink fails? First mark the node’s next pointer Then, try to unlink the node That is ok if lookup ignores marked nodes CAS CAS 10 20 30 BOT x Lookup finds node Remove(20)

More Related