Locking versus Transactional Memory Topics n n Example













- Slides: 13
Locking versus Transactional Memory Topics n n Example of Data Race (recap) Lock-based Synchronization Solution l Fine grained versus Coarse grained locking n Transactional Memory
Dependencies cause data races Recap: a dependence means that ordering between two statements matters for program correctness Or, in other words … A race occurs when correctness of the program depends on one thread reaching point x before another thread reaches point y – 2–
Data Race Example /* a threaded program with a race */ int main() { pthread_t tid[N]; int i; for (i = 0; i < N; i++) pthread_create(&tid[i], NULL, thread, &i); for (i = 0; i < N; i++) pthread_join(tid[i], NULL); exit(0); } /* thread routine */ void *thread(void *vargp) { int myid = *((int *)vargp); printf("Hello from thread %dn", myid); } – 3–
Output: why? Hello Hello Hello – 4– courtesy of Ding Yuan from from from thread thread thread 1 2 6 3 7 5 9 4 9 0
Another worry: Deadlock Threads wait for condition that will never be true /* a threaded program with a potential deadlock*/ Thread 1(){ lock(a); lock(b); do_work(); unlock(b); unlock(a); } Thread 2(){ lock(b); lock(a); do_work(); unlock(a); unlock(b); }
Parallelization Options Coarse-grained locking e. g. , one big lock for the critical section n Easy to program n Limited parallelism Fine-grained locking e. g. , one lock per variable used in critical section n Good parallelism n Hard to get right l eg. , deadlock, fault tolerance, priority inversion, etc.
Problems with Locks Deadlocks Fault Tolerance: if thread hangs or die holding lock … whole program stalls indefinitely Priority inversion: assuming per-thread priority – it can’t really be respected. A low priority thread can grab a lock and make a higher priority thread wait. – 7–
Parallelization Options Coarse-grained locking n Easy to program n Limited parallelism Fine-grained locking n Good parallelism n Hard to get right l eg. , deadlock, fault tolerance, priority inversion, etc. The promise of Transactional Memory n As easy to program as coarse-grained locking n Provides the parallelism of fine-grained locking
Transactional Memory ™ Basics Transactions: Source Code: . . { atomic {. . r/w_shared_data(); . . . }}}. . TM System
Transactional Memory ™ Basics Transactions: Source Code: . . . atomic {. . r/w_shared_data(); . . }}}. . TM System Programmer: Specifies transactions in source code TM System: Executes transactions optimistically in parallel
Transactional Memory ™ Basics Transactions: Source Code: . . { atomic TM System atomic {. . access_shared_data(); . . . }}. . . ? ? Exploits available parallelism while maintaining correctness! TM System: 1) Checkpoints execution 2) Detects conflicts 3) Commits or aborts and reexecutes
TM Example atomic { …=B … if (. . . ){ A=… } … B=… } easy Time atomic { …=A … if (. . . ){ B=… } … A=… } P fast P
TM Implementations Software TM (STM) n Implemented entirely in software n Often integrated in a compiler Hardware TM (HTM) n Some/all TM mechanisms implemented in hardware gcc-4. 7 n n Supports STM: use compiler flag -fgnu-tm Wrap critical section with: __transaction_atomic {}