1 / 85

Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer

Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer. Lecture 3: Introduction. Material From. The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit. Moore‘s Law. Transistor count still rising. Clock speed flattening sharply. Uniprocessor. cpu. memory. cache.

dalila
Download Presentation

Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer

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. Concurrent Object-Oriented ProgrammingProf. Dr. Bertrand Meyer Lecture 3: Introduction

  2. Material From • The Art of Multiprocessor Programming • by Maurice Herlihy & NirShavit

  3. Moore‘s Law Transistor count still rising Clock speed flattening sharply

  4. Uniprocessor cpu memory

  5. cache cache cache Bus Bus shared memory Shared Memory Multiprocessor (SMP)

  6. Multicore Processor (CMP) Sun T2000 Niagara All on the same chip

  7. Why do we care about multicore processors? • Time no longer cures software bloat • The “free ride” is over • When you double your program’s path length • You can’t just wait 6 months • Your software must somehow exploit twice as much concurrency

  8. Traditional Scaling Process 7x Speedup 3.6x 1.8x User code Traditional Uniprocessor Time: Moore’s law

  9. 7x 3.6x Speedup 1.8x Multicore Scaling Process User code Multicore Unfortunately, not so simple…

  10. Real-World Scaling Process Speedup 2.9x 2x 1.8x User code Multicore Parallelization and Synchronization require great care…

  11. Sequential Computation thread memory object object

  12. Concurrent Computation memory object object

  13. Asynchrony • Sudden unpredictable delays • Cache misses (short) • Page faults (long) • Scheduling quantum used up (really long)

  14. Model Summary • Multiple threads • Sometimes called processes • Single shared memory • Objects live in memory • Unpredictable asynchronous delays

  15. Concurrency Jargon • Hardware • Processors • Software • Threads, processes • Sometimes OK to confuse them, sometimes not.

  16. Parallel Primality Testing • Challenge • Print primes from 1 to 1010 • Given • Ten-processor multiprocessor • One thread per processor • Goal • Get ten-fold speedup (or close)

  17. 1 1010 Load Balancing • Split the work evenly • Each thread tests range of 109 109 2·109 … … P0 P1 P9

  18. Procedure for Thread i voidprimePrint { inti = ThreadID.get(); // IDs in {0..9} for(j = i*109+1, j<(i+1)*109; j++) { if(isPrime(j)) print(j); } }

  19. Issues • Higher ranges have fewer primes • Yet larger numbers harder to test • Thread workloads • Uneven • Hard to predict • Need dynamic load balancing rejected

  20. Shared Counter 19 each thread takes a number 18 17

  21. Procedure for Thread i Counter counter = new Counter(1); void primePrint { long j = 0; while (j < 1010) { j = counter.getAndIncrement(); if (isPrime(j)) print(j); } } Shared counter object

  22. cache cache cache Bus Bus Where Things Reside voidprimePrint { inti = ThreadID.get(); // IDs in {0..9} for(j = i*109+1, j<(i+1)*109; j++) { if(isPrime(j)) print(j); } } local variables code shared memory 1 shared counter

  23. Procedure for Thread i Counter counter = new Counter(1); void primePrint { long j = 0; while (j < 1010) { j = counter.getAndIncrement(); if (isPrime(j)) print(j); } } Stop when every value taken Increment & return each new value

  24. Counter Implementation public class Counter{ private long value; public long getAndIncrement() { return value++; } } temp = value; value = temp + 1; return temp; OK for single thread, not for concurrent threads

  25. time Not so good… Value… 1 2 3 2 read 1 write 2 read 2 write 3 read 1 write 2

  26. Is this problem inherent? write read read write If we could only glue reads and writes…

  27. Challenge public class Counter { private long value; public longgetAndIncrement() { temp = value; value = temp + 1; return temp; } } Make these steps atomic (indivisible)

  28. Hardware Solution public class Counter { private long value; public long getAndIncrement() { temp = value; value = temp + 1; return temp; } } ReadModifyWrite() instruction

  29. An Aside: Java™ public class Counter { private long value; public longgetAndIncrement() { synchronized { temp = value; value = temp + 1; } return temp; } } Mutual Exclusion Synchronized block

  30. Mutual Exclusion or “Alice & Bob share a pond” A B

  31. Alice has a pet A B

  32. Bob has a pet A B

  33. The Problem A B The pets don’t get along

  34. Formalizing the Problem • Two types of formal properties in asynchronous computation: • Safety Properties • Nothing bad happens ever • Liveness Properties • Something good happens eventually

  35. Formalizing our Problem • Mutual Exclusion • Both pets never in pond simultaneously • This is a safetyproperty • No Deadlock • if only one wants in, it gets in • if both want in, one gets in. • This is a livenessproperty

  36. Simple Protocol • Idea • Just look at the pond • Gotcha • Not atomic • Trees obscure the view

  37. Interpretation • Threads can’t “see” what other threads are doing • Explicit communication required for coordination

  38. Cell Phone Protocol • Idea • Bob calls Alice (or vice-versa) • Gotcha • Bob takes shower • Alice recharges battery • Bob out shopping for pet food …

  39. Interpretation • Message-passing doesn’t work • Recipient might not be • Listening • There at all • Communication must be • Persistent (like writing) • Not transient (like speaking)

  40. cola cola Can Protocol

  41. Bob conveys a bit A B cola

  42. cola Bob conveys a bit A B

  43. Can Protocol • Idea • Cans on Alice’s windowsill • Strings lead to Bob’s house • Bob pulls strings, knocks over cans • Gotcha • Cans cannot be reused • Bob runs out of cans

  44. Interpretation • Cannot solve mutual exclusion with interrupts • Sender sets fixed bit in receiver’s space • Receiver resets bit when ready • Requires unbounded number of interrupt bits

  45. Flag Protocol A B

  46. Alice’s Protocol (sort of) A B

  47. Bob’s Protocol (sort of) A B

  48. Alice’s Protocol • Raise flag • Wait until Bob’s flag is down • Unleash pet • Lower flag when pet returns

  49. Bob’s Protocol • Raise flag • Wait until Alice’s flag is down • Unleash pet • Lower flag when pet returns danger!

  50. Bob’s Protocol (2nd try) • Raise flag • While Alice’s flag is up • Lower flag • Wait for Alice’s flag to go down • Raise flag • Unleash pet • Lower flag when pet returns Bob defers to Alice

More Related