350 likes | 362 Views
Explore the importance of theory in teaching concurrent programming for multicore systems, including homework challenges and key concepts like mutual exclusion and linearizability.
E N D
It Ain’t the Meat, it’s the NotionWhy Theory is Essential to Teaching Concurrent Programming Maurice Herlihy Brown University
In many areas of CS • Systems research fought all the hard battles • FORTRAN complexity theory • UDP/IP network algorithms • Theory researchers arrived later to shoot the wounded • But concurrency is different Multicore Programming Education
Vive la Différence • Concurrent programmers are made, not born (mostly) • Students have little experience with concurrency (today) • Develop intuition by • Idealized examples first • Logical reasoning Multicore Programming Education
Homework Challenge Devise Wait-Free 1-Enqueuer, 1-Dequeuer Buffer using only reads & writes Multicore Programming Education
Another Homework Challenge How about two dequeuers? Multicore Programming Education
Shared Memory Atomic reads and writes to individual variables write read Multicore Programming Education 6
Snapshot Challenge Wait-Free algorithm to read multiple variables atomically? Read both Read both Multicore Programming Education 7
Snapshot Challenge Wait-Free algorithm to write multiple variables atomically? writeboth write both Multicore Programming Education 8
Bakery Algorithm class Bakery implements Lock { boolean[] flag; Label[] label; public Bakery (int n) { flag = new boolean[n]; label = new Label[n]; for (int i = 0; i < n; i++) { flag[i] = false; label[i] = 0; } } … 6 2 n-1 0 f f t f f t f f 0 0 4 0 0 5 0 0 Classic, first-come-first served mutual exclusion algorithm Critical Section Multicore Programming Education 9
Is Locked? isLocked() True means was locked at some instant, false means was free at some instant. Bakery Lock Multicore Programming Education
I Can Has Lock? tryLock() Acquire lock if free & return true, otherwise immediately return false. Bakery Lock Multicore Programming Education
Our Experience Teaching Concurrency • Brown (10 years) • Tel-Aviv (10 years) • Industry Multicore Programming Education
Mutual Exclusion • Peterson, Bakery, space lower bounds • Teaches • Deadlock • Livelock • Fairness, first-come-first-served, … Multicore Programming Education
Linearizability • How to describe a concurrent object? • By “equivalence” to sequential • What is a concurrent API? • How do we reason about correctness? • Even informally! • Learn the rules before you break them Multicore Programming Education
Consensus Numbers • Certain synchronization primitives are mathematically stronger than others • No, you can’t build lock-free X from Y • So do not waste your time trying • Yes, you can build lock-free X from Z • The rest is optimization Multicore Programming Education
Inexplicable in idealized model Well, kids … We didn’t actually lie to you But the architects gave us caches And you have to outwit them … Uh, Oh, Spin Locks bad Test&test&set less bad Test&set Multicore Programming Education
Then What? • Implement simple T&S lock • Dreadful performance • Implement T&T&S lock • Improved performance Multicore Programming Education
Homework Challenge Devise Wait-Free 1-Enqueuer, 1-Dequeuer Buffer using only reads & writes Multicore Programming Education
Homework Challenge Yes, you can Multicore Programming Education
Another Homework Challenge How about two dequeuers? Multicore Programming Education
Maybe? “hence [any wait-free object] can be written without a CAS/LL/SC. Instead LL/SC is used to make it more efficient but LL/SC isn't *required* to make it wait-free.” Found in a blog Multicore Programming Education
Another Homework Challenge Alas, no, you can’t Multicore Programming Education
What If we have Test&Set? Multicore Programming Education
What If we have Test&Set? No, you still can’t Multicore Programming Education
What If we have Compare&Swap? Multicore Programming Education
What if We Have Compare&Sswap? Yes, you can Multicore Programming Education
Snapshot Challenge Wait-Free algorithm to read multiple variables atomically? Reads from 0 and 1 Reads from 1 and 2 Multicore Programming Education 27
Snapshot Challenge Wait-Free algorithm to read multiple variables atomically? Reads from 0 and 1 Yes, you can Reads from 1 and 2 Multicore Programming Education 28
Multiple Assignment Challenge Wait-Free algorithm to write multiple variables atomically? Writes to 0 and 1 Writes to 1 and 2 Multicore Programming Education 29
Multiple Assignment Challenge Wait-Free algorithm to write multiple variables atomically? Writes to 0 and 1 No, you can’t Writes to 1 and 2 Multicore Programming Education 30
Is Locked? isLocked() True means was locked at some instant, false means was free at some instant. Bakery Lock Multicore Programming Education
Is Locked? isLocked() Yes, you can True means was locked at some instant, false means was free at some instant. Bakery Lock Multicore Programming Education
I Can Has Lock? tryLock() Acquire lock if free & return true, otherwise immediately return false. Bakery Lock Multicore Programming Education
I Can Has Lock? tryLock() No, you can’t Acquire lock if free & return true, otherwise immediately return false. Bakery Lock Multicore Programming Education
My Very Last Slide • Teaching theory first • Develops intuition • Imposes order on chaos • Idealized problems • Prepare for realistic, complicated ones • Impossibility results • Save time and effort Multicore Programming Education