moore’s law & amdahl's law race conditions...
TRANSCRIPT
Moore’s Law & Amdahl's LawRace ConditionsProtecting CodeDeadlockBuilding a Thread PoolThreading a Shared Scan
Moore’s Law & Amdahl's LawRace ConditionsProtecting CodeDeadlockBuilding a Thread PoolThreading a Shared Scan
Moore’s Law & Amdahl's LawRace ConditionsProtecting CodeDeadlockBuilding a Thread PoolThreading a Shared Scan
Moore’s Law & Amdahl's LawRace ConditionsProtecting CodeDeadlockBuilding a Thread PoolThreading a Shared Scan
Moore’s Law & Amdahl's LawRace ConditionsProtecting CodeDeadlockBuilding a Thread PoolThreading a Shared Scan
Moore’s Law & Amdahl's LawRace ConditionsProtecting CodeDeadlockBuilding a Thread PoolThreading a Shared Scan
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
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
Race Conditions
Thread 1 Thread 2
Tim
e
if (x == 0)++x;
if (x == 0)++x;
Race Conditions
Thread 1 Thread 2
Tim
e
Read X
Increment X
Read X
(Nothing else to be done.)
Race Conditions
Thread 1 Thread 2
Tim
e
Read X
Increment X
Read X
Increment X
Mutex → mutual exclusion
Atomic Operation
Compare-and-swap (x86 CMPXCHG)
A pthreads mutex Snippet
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...");
}
Deadlock
Thread 1 Thread 2
Tim
e
lock(m)
lock(m)
// Do something with B
// Do something with A
unlock(m)
unlock(m)
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)
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)
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)
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
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);
Threading a Shared Scan
T1Q1,Q2,Q3,Q4
T2Q1,Q2,Q3,Q4
T1Q1,Q2,Q3,Q4
T2Q1,Q2,Q3,Q4
T2Q1,Q2,Q3,Q4
Threading a Shared Scan
T1Q1,Q2
T2Q3,Q4