U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Grace: Safe Multithreaded Programming for C/C++ Emery Berger University of Massachusetts, UPC/BSC
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science
“”
race condition
atomicity violation
deadlock
order violation
sequential
race conditions
atomicity violations
race conditions atomicity violations deadlock
race conditions atomicity violations deadlock order violations
Grace race conditions atomicity violations deadlock order violations
race conditions atomicity violations deadlock order violations Grace
race conditions atomicity violations deadlock order violations sequential Grace
% g++ myprog.cpp –lpthread –o myprog
Grace % g++ myprog.cpp –lgrace –o myprog
Grace % g++ myprog.cpp –lgrace –o myprog sequential
Grace % g++ myprog.cpp –lgrace –o myprog sequential race conditions atomicity violations deadlock order violations
Grace % g++ myprog.cpp –lgrace –o myprog sequential race conditions atomicity violations deadlock order violations
Safe and (usually) fast…
benign race
Grace sequential +?1.
Grace sequential +?1. 2.
Grace sequential +?
sequential +?1.
sequential +?1. t1 = spawn f(x); t2 = spawn g(y); sync;
sequential +?1. t1 = spawn f(x); t2 = spawn g(y); sync; spawn f(x)
sequential +?1. t1 = spawn f(x); t2 = spawn g(y); sync; f(x) spawn f(x)
sequential +?1. t1 = spawn f(x); t2 = spawn g(y); sync; f(x) spawn f(x) spawn g(y)
sequential +?1. t1 = spawn f(x); t2 = spawn g(y); sync; f(x) g(y) spawn f(x) spawn g(y)
sequential +?1. t1 = spawn f(x); t2 = spawn g(y); sync; f(x) g(y) spawn f(x) spawn g(y) sync
sequential +?1. t1 = spawn f(x); t2 = spawn g(y); sync; f(x) g(y) spawn f(x) spawn g(y) sync
sequential +?1. t1 = spawn f(x); t2 = spawn g(y); sync; f(x) g(y) spawn f(x) spawn g(y) sync left-to-right, depth-first ordering
sequential +?1. t1 = spawn f(x); t2 = spawn g(y); sync; f(x) g(y)
sequential +?1. t1 = spawn f(x); t2 = spawn g(y); sync; f(x) g(y) “”
sequential +?1. f(x) g(y) “”
sequential +?1. f(x) g(y) “” race conditions atomicity violations deadlock order violations
sequential +?1. f(x) g(y) “” race conditions atomicity violations deadlock order violations
sequential +?1. f(x) g(y) “” race conditions atomicity violations deadlock order violations
sequential +?1. f(x) g(y) “” race conditions atomicity violations deadlock order violations
sequential +?1. f(x) g(y) “” race conditions atomicity violations deadlock order violations
sequential +?1. f(x) g(y) “” race conditions atomicity violations deadlock order violations
sequential +?1. f(x) g(y) “” race conditions atomicity violations deadlock order violations
sequential +?1. f(x) g(y) “”
sequential +?1. f(x) g(y) “”
sequential +?1. f(x) g(y) “”
sequential +?1. f(x) g(y) “”
sequential +?1. f(x) g(y) “”
sequential +?1. f(x) g(y) “” Fork-join (e.g., map-reduce, Cilk, TBB…)
? 2. What’s under the hood?
? 2. “Magic!”
? 2. +
?
? t1 = spawn f(x); t2 = spawn g(y); sync;
? 2. t1 = spawn f(x); t2 = spawn g(y); sync; if (!fork()) f(x); if (!fork()) g(y); // check;
? 2. if (!fork()) f(x); if (!fork()) g(y); // check; f(x) g(y) spawn f(x) spawn g(y) sync
? 2. if (!fork()) f(x); if (!fork()) g(y); // check; f(x) g(y) spawn f(x) spawn g(y) sync
? 2. if (!fork()) f(x); if (!fork()) g(y); // check; f(x) g(y) spawn f(x) spawn g(y) sync Note: Nested threads ok
? 2. f(x) g(y) spawn f(x) spawn g(y) sync mmap
? 2. f(x) g(y) spawn f(x) spawn g(y) sync mmap
? 2. f(x) g(y) spawn f(x) spawn g(y) sync mmap
? 2. f(x) g(y) spawn f(x) spawn g(y) sync
? 2. f(x) g(y) spawn f(x) spawn g(y) sync 1
? 2. f(x) g(y) spawn f(x) spawn g(y) sync 1 2
? 2. scalable heap + aligned globals I/O (buffering + ordering)
3. More detailed results…
Grace sequential race conditions atomicity violations deadlock order violations
Grace sequential race conditions atomicity violations deadlock order violations
Grace sequential race conditions atomicity violations deadlock order violations
Grace sequential race conditions atomicity violations deadlock order violations % g++ myprog.cpp –lgrace –o myprog
“Moltes gràcies”
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science ? 2.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science ? 2.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science ? 2. +
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science ? 2. ++