Moore’s Law & Amdahl's Law Race Conditions...
Transcript of Moore’s Law & Amdahl's Law Race Conditions...
![Page 1: Moore’s Law & Amdahl's Law Race Conditions …daslab.seas.harvard.edu/classes/cs165/doc/sections/S10...Race Conditions Protecting Code Deadlock Building a Thread Pool Threading a](https://reader030.fdocuments.net/reader030/viewer/2022040508/5e4e39efce687039c1109e28/html5/thumbnails/1.jpg)
Moore’s Law & Amdahl's LawRace ConditionsProtecting CodeDeadlockBuilding a Thread PoolThreading a Shared Scan
![Page 2: Moore’s Law & Amdahl's Law Race Conditions …daslab.seas.harvard.edu/classes/cs165/doc/sections/S10...Race Conditions Protecting Code Deadlock Building a Thread Pool Threading a](https://reader030.fdocuments.net/reader030/viewer/2022040508/5e4e39efce687039c1109e28/html5/thumbnails/2.jpg)
Moore’s Law & Amdahl's LawRace ConditionsProtecting CodeDeadlockBuilding a Thread PoolThreading a Shared Scan
![Page 3: Moore’s Law & Amdahl's Law Race Conditions …daslab.seas.harvard.edu/classes/cs165/doc/sections/S10...Race Conditions Protecting Code Deadlock Building a Thread Pool Threading a](https://reader030.fdocuments.net/reader030/viewer/2022040508/5e4e39efce687039c1109e28/html5/thumbnails/3.jpg)
Moore’s Law & Amdahl's LawRace ConditionsProtecting CodeDeadlockBuilding a Thread PoolThreading a Shared Scan
![Page 4: Moore’s Law & Amdahl's Law Race Conditions …daslab.seas.harvard.edu/classes/cs165/doc/sections/S10...Race Conditions Protecting Code Deadlock Building a Thread Pool Threading a](https://reader030.fdocuments.net/reader030/viewer/2022040508/5e4e39efce687039c1109e28/html5/thumbnails/4.jpg)
Moore’s Law & Amdahl's LawRace ConditionsProtecting CodeDeadlockBuilding a Thread PoolThreading a Shared Scan
![Page 5: Moore’s Law & Amdahl's Law Race Conditions …daslab.seas.harvard.edu/classes/cs165/doc/sections/S10...Race Conditions Protecting Code Deadlock Building a Thread Pool Threading a](https://reader030.fdocuments.net/reader030/viewer/2022040508/5e4e39efce687039c1109e28/html5/thumbnails/5.jpg)
Moore’s Law & Amdahl's LawRace ConditionsProtecting CodeDeadlockBuilding a Thread PoolThreading a Shared Scan
![Page 6: Moore’s Law & Amdahl's Law Race Conditions …daslab.seas.harvard.edu/classes/cs165/doc/sections/S10...Race Conditions Protecting Code Deadlock Building a Thread Pool Threading a](https://reader030.fdocuments.net/reader030/viewer/2022040508/5e4e39efce687039c1109e28/html5/thumbnails/6.jpg)
Moore’s Law & Amdahl's LawRace ConditionsProtecting CodeDeadlockBuilding a Thread PoolThreading a Shared Scan
![Page 7: Moore’s Law & Amdahl's Law Race Conditions …daslab.seas.harvard.edu/classes/cs165/doc/sections/S10...Race Conditions Protecting Code Deadlock Building a Thread Pool Threading a](https://reader030.fdocuments.net/reader030/viewer/2022040508/5e4e39efce687039c1109e28/html5/thumbnails/7.jpg)
Moore’s Law
The number of transistorsin a dense integrated circuitdoubles approximatelyevery two years.
Image By Wgsimon - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=15193542
curve shows transistor count doubling every two years
2,300
10,000
100,000
1,000,000
10,000,000
100,000,000
1,000,000,000
2,600,000,000
1971 1980 1990 2000 2011
Date of introduction
4004
8008
8080
RCA 1802
8085
8088
Z80
MOS 6502
6809
8086
80186
6800
68000
80286
80386
80486
PentiumAMD K5
Pentium IIPentium III
AMD K6
AMD K6-IIIAMD K7
Pentium 4Barton Atom
AMD K8
Itanium 2 CellCore 2 Duo
AMD K10Itanium 2 with 9MB cache
POWER6
Core i7 (Quad)Six-Core Opteron 2400
8-Core Xeon Nehalem-EXQuad-Core Itanium TukwilaQuad-core z1968-core POWER7
10-Core Xeon Westmere-EX
16-Core SPARC T3
Six-Core Core i7
Six-Core Xeon 7400
Dual-Core Itanium 2
AMD K10
Microprocessor Transistor Counts 1971-2011 & Moore's Law
Tra
nsi
sto
r co
un
t
![Page 8: Moore’s Law & Amdahl's Law Race Conditions …daslab.seas.harvard.edu/classes/cs165/doc/sections/S10...Race Conditions Protecting Code Deadlock Building a Thread Pool Threading a](https://reader030.fdocuments.net/reader030/viewer/2022040508/5e4e39efce687039c1109e28/html5/thumbnails/8.jpg)
Amdahl’s Law
The formula used to calculate the theoretical maximumspeed-up for a given workload.
S latency (s )=1
(1−p)+ps
S latency (4 )=1
(1−0.75)+0.75
4
≈2.28
![Page 9: Moore’s Law & Amdahl's Law Race Conditions …daslab.seas.harvard.edu/classes/cs165/doc/sections/S10...Race Conditions Protecting Code Deadlock Building a Thread Pool Threading a](https://reader030.fdocuments.net/reader030/viewer/2022040508/5e4e39efce687039c1109e28/html5/thumbnails/9.jpg)
Race Conditions
Thread 1 Thread 2
Tim
e
if (x == 0)++x;
if (x == 0)++x;
![Page 10: Moore’s Law & Amdahl's Law Race Conditions …daslab.seas.harvard.edu/classes/cs165/doc/sections/S10...Race Conditions Protecting Code Deadlock Building a Thread Pool Threading a](https://reader030.fdocuments.net/reader030/viewer/2022040508/5e4e39efce687039c1109e28/html5/thumbnails/10.jpg)
Race Conditions
Thread 1 Thread 2
Tim
e
Read X
Increment X
Read X
(Nothing else to be done.)
![Page 11: Moore’s Law & Amdahl's Law Race Conditions …daslab.seas.harvard.edu/classes/cs165/doc/sections/S10...Race Conditions Protecting Code Deadlock Building a Thread Pool Threading a](https://reader030.fdocuments.net/reader030/viewer/2022040508/5e4e39efce687039c1109e28/html5/thumbnails/11.jpg)
Race Conditions
Thread 1 Thread 2
Tim
e
Read X
Increment X
Read X
Increment X
![Page 12: Moore’s Law & Amdahl's Law Race Conditions …daslab.seas.harvard.edu/classes/cs165/doc/sections/S10...Race Conditions Protecting Code Deadlock Building a Thread Pool Threading a](https://reader030.fdocuments.net/reader030/viewer/2022040508/5e4e39efce687039c1109e28/html5/thumbnails/12.jpg)
Mutex → mutual exclusion
Atomic Operation
Compare-and-swap (x86 CMPXCHG)
A pthreads mutex Snippet
![Page 13: Moore’s Law & Amdahl's Law Race Conditions …daslab.seas.harvard.edu/classes/cs165/doc/sections/S10...Race Conditions Protecting Code Deadlock Building a Thread Pool Threading a](https://reader030.fdocuments.net/reader030/viewer/2022040508/5e4e39efce687039c1109e28/html5/thumbnails/13.jpg)
A pthreads mutex Snippet#include <pthread.h>...pthread_mutex_t m;
pthread_mutex_init(&m, NULL);...
pthread_mutex_lock(&m);printf("We got the lock!");... // Other critical codepthread_mutex_unlock(&m);
if (pthread_mutex_trylock(&m)){printf("We got the lock!");pthread_mutex_unlock(&m);
} else {printf("Already locked...");
}
![Page 14: Moore’s Law & Amdahl's Law Race Conditions …daslab.seas.harvard.edu/classes/cs165/doc/sections/S10...Race Conditions Protecting Code Deadlock Building a Thread Pool Threading a](https://reader030.fdocuments.net/reader030/viewer/2022040508/5e4e39efce687039c1109e28/html5/thumbnails/14.jpg)
Deadlock
Thread 1 Thread 2
Tim
e
lock(m)
lock(m)
// Do something with B
// Do something with A
unlock(m)
unlock(m)
![Page 15: Moore’s Law & Amdahl's Law Race Conditions …daslab.seas.harvard.edu/classes/cs165/doc/sections/S10...Race Conditions Protecting Code Deadlock Building a Thread Pool Threading a](https://reader030.fdocuments.net/reader030/viewer/2022040508/5e4e39efce687039c1109e28/html5/thumbnails/15.jpg)
Deadlock
Thread 1 Thread 2
Tim
e
lock(m_A)
// Do something with B// Do something with A
unlock(m_A) unlock(m_B)
lock(m_B)
![Page 16: Moore’s Law & Amdahl's Law Race Conditions …daslab.seas.harvard.edu/classes/cs165/doc/sections/S10...Race Conditions Protecting Code Deadlock Building a Thread Pool Threading a](https://reader030.fdocuments.net/reader030/viewer/2022040508/5e4e39efce687039c1109e28/html5/thumbnails/16.jpg)
Deadlock
Thread 1 Thread 2
Tim
e
lock(m_A)
// Do something with B// Do something with A
unlock(m_A) unlock(m_B)
lock(m_B)
// Do something with B // Do something with A
lock(m_B) lock(m_A)
unlock(m_B) unlock(m_A)
![Page 17: Moore’s Law & Amdahl's Law Race Conditions …daslab.seas.harvard.edu/classes/cs165/doc/sections/S10...Race Conditions Protecting Code Deadlock Building a Thread Pool Threading a](https://reader030.fdocuments.net/reader030/viewer/2022040508/5e4e39efce687039c1109e28/html5/thumbnails/17.jpg)
Deadlock
Thread 1 Thread 2
Tim
e
lock(m)
// Do something with A
lock(m)
// Do something with B
// Do something with C
// Do something with B
// Do something with A
unlock(m)
unlock(m)
![Page 18: Moore’s Law & Amdahl's Law Race Conditions …daslab.seas.harvard.edu/classes/cs165/doc/sections/S10...Race Conditions Protecting Code Deadlock Building a Thread Pool Threading a](https://reader030.fdocuments.net/reader030/viewer/2022040508/5e4e39efce687039c1109e28/html5/thumbnails/18.jpg)
Implementing a Thread Pool
pthread_t
pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
pthread_cond_tpthread_cond_init(pthread_cond_t*,pthread_condattr_t*);pthread_cond_wait(pthread_cond_t*, pthread_mutex_t)pthread_cond_signal(pthread_cond_t*)
Link with -lpthread
![Page 19: Moore’s Law & Amdahl's Law Race Conditions …daslab.seas.harvard.edu/classes/cs165/doc/sections/S10...Race Conditions Protecting Code Deadlock Building a Thread Pool Threading a](https://reader030.fdocuments.net/reader030/viewer/2022040508/5e4e39efce687039c1109e28/html5/thumbnails/19.jpg)
Implementing a Thread Pool
// Worker thread waits for work pthread_mutex_lock(m); while ([queue_is_empty]) pthread_cond_wait(cv, m); // Do critical something pthread_mutex_unlock(m);
// Producer thread adds work and signals pthread_mutex_lock(m);
// Add some work to a shared struct pthread_cond_signal(cv); // wake up a thread
pthread_mutex_unlock(m);
![Page 20: Moore’s Law & Amdahl's Law Race Conditions …daslab.seas.harvard.edu/classes/cs165/doc/sections/S10...Race Conditions Protecting Code Deadlock Building a Thread Pool Threading a](https://reader030.fdocuments.net/reader030/viewer/2022040508/5e4e39efce687039c1109e28/html5/thumbnails/20.jpg)
Threading a Shared Scan
T1Q1,Q2,Q3,Q4
T2Q1,Q2,Q3,Q4
T1Q1,Q2,Q3,Q4
T2Q1,Q2,Q3,Q4
T2Q1,Q2,Q3,Q4
![Page 21: Moore’s Law & Amdahl's Law Race Conditions …daslab.seas.harvard.edu/classes/cs165/doc/sections/S10...Race Conditions Protecting Code Deadlock Building a Thread Pool Threading a](https://reader030.fdocuments.net/reader030/viewer/2022040508/5e4e39efce687039c1109e28/html5/thumbnails/21.jpg)
Threading a Shared Scan
T1Q1,Q2
T2Q3,Q4