02 - Pthread+primitive Sincronizare PDF
02 - Pthread+primitive Sincronizare PDF
02 - Pthread+primitive Sincronizare PDF
▪ Instruction level
▪ Task parallelism
▪ Task parallelism
▪ Task parallelism
4 2 7
▪ Instruction level
▪ Task parallelism
▪ Instruction level
▪ Task parallelism
co S1 || S2 || ... || Sn oc
Ex.1:
x=0; y=0;
co x=x+1 || y=y+1 oc
z=x+y;
co [cuantificator]{Sj}
Ex. 2:
co [j=1 to n] {a[j]=0; b[j]=0;}
pthread_join(thread, NULL);
#include<pthread.h>
#include<semaphore.h>
pthread_join(thread, NULL);
pthread_join(thread, NULL);
pthread_join(thread, NULL);
pthread_join(thread, NULL);
pthread_join(thread, NULL);
Thread 1 Thread 2
a=a+2 a=a+2
Thread 1 Thread 2
a=a+2 a=a+2
Thread 1 Thread 2
a=a+2 a=a+2
Thread 1 Thread 2
a=a+2 a=a+2
Thread 1 Thread 2
eax = eax =
Florin Pop – Cristian Chilipirea
Race condition
a=0
Thread 1 Thread 2
eax = 0 eax =
Florin Pop – Cristian Chilipirea
Race condition
a=0
Thread 1 Thread 2
eax = 0 eax = 0
Florin Pop – Cristian Chilipirea
Race condition
a=0
Thread 1 Thread 2
eax = 2 eax = 0
Florin Pop – Cristian Chilipirea
Race condition
a=0
Thread 1 Thread 2
eax = 2 eax = 2
Florin Pop – Cristian Chilipirea
Race condition
a=2
Thread 1 Thread 2
eax = 2 eax = 2
Florin Pop – Cristian Chilipirea
Race condition
a=2
Thread 1 Thread 2
eax = 2 eax = 2
Florin Pop – Cristian Chilipirea
Florin Pop – Cristian Chilipirea
Race condition
a=0
Thread 1 Thread 2
eax = eax =
Florin Pop – Cristian Chilipirea
Race condition
a=0
Thread 1 Thread 2
eax = 0 eax =
Florin Pop – Cristian Chilipirea
Race condition
a=0
Thread 1 Thread 2
eax = 2 eax =
Florin Pop – Cristian Chilipirea
Race condition
a=2
Thread 1 Thread 2
eax = 2 eax =
Florin Pop – Cristian Chilipirea
Race condition
a=2
Thread 1 Thread 2
eax = 2 eax = 2
Florin Pop – Cristian Chilipirea
Race condition
a=2
Thread 1 Thread 2
eax = 2 eax = 4
Florin Pop – Cristian Chilipirea
Race condition
a=4
Thread 1 Thread 2
eax = 2 eax = 4
Florin Pop – Cristian Chilipirea
Florin Pop – Cristian Chilipirea
Race condition
a=0
Thread 1 Thread 2
eax = eax =
Florin Pop – Cristian Chilipirea
Race condition
a=0
Thread 1 Thread 2
eax = 0 eax = 0
Florin Pop – Cristian Chilipirea
Race condition
a=0
Thread 1 Thread 2
eax = 2 eax = 2
Florin Pop – Cristian Chilipirea
Race condition
a=2
Thread 1 Thread 2
eax = 2 eax = 2
Florin Pop – Cristian Chilipirea
Race condition
a=2
Thread 1 Thread 2
eax = 2 eax = 2
Florin Pop – Cristian Chilipirea
Race condition
a=2
Thread 1 Thread 2
▪Atomics
▪Semaphore
‒ Binary semaphore (Mutex)
‒ Critical section
▪Barrier
load(A, eax)
load(B, ebx)
eax = eax + ebx
write(C, eax)
wants_to_enter[0] = true
while wants_to_enter[1] {
if turn != 0 {
wants_to_enter[0] = false
while turn != 0 { // busy wait }
wants_to_enter[0] = true
}
}
// critical section ...
turn = 1
wants_to_enter[0] = false
Florin Pop – Cristian Chilipirea
Dijsktra’s Solution
b[i] = fals
while(sw[i]) {
sw[i] = F
if (k!=i) {
c[i] = true
if(b[k]) lock()
k=i
sw[i] = T
} else {
c[i] = false
for(j=0;j<N;j++)
if(j!=i && !c[j])
sw[i] = T
}
}
//critical
b[i] = true
c[i] = true unlock()
Florin Pop – Cristian Chilipirea
flag[0] =
;
flag[0] =
;
Peterson’s Solution
flag[0] = true;
P0_gate: turn = 1;
while (flag[1] == && turn == 1) { // busy wait }
// critical section ...
flag[0] = false;
while (test_and_set(lock));
// critical section
lock = 0
ÎN MAIN
Înainte de a porni thread-urile
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, NULL);
pthread_mutex_t mutex;
pthread_mutex_init(&mutex, NULL);
pthread_mutex_lock(&mutex);
load(a, eax)
eax = eax + 2
write(a, eax)
pthread_mutex_unlock(&mutex);
ÎN MAIN
După ce au terminat thread-urile
pthread_mutex_destroy(&mutex);
ÎN MAIN
Înainte de a porni thread-urile
sem_t semaphore;
int semaphore_value= 4;
sem_init(& semaphore, 0, semaphore_value);
4
sem_wait(&semaphore);
by Cristian Chilipirea
Semaphore
4
sem_wait(&semaphore);
by Cristian Chilipirea
Semaphore
4
sem_wait(&semaphore);
by Cristian Chilipirea
Semaphore
3
sem_wait(&semaphore);
by Cristian Chilipirea
Semaphore
Nu contează că doua thread-uri au
ajuns simultan la semafor, acesta
este protejat, la fel ca un mutex.
2
sem_wait(&semaphore);
by Cristian Chilipirea
Semaphore
1
sem_wait(&semaphore);
by Cristian Chilipirea
Semaphore
0
sem_wait(&semaphore);
by Cristian Chilipirea
Semaphore
0
sem_wait(&semaphore);
by Cristian Chilipirea
Semaphore – A different way of thinking
sem_wait(&semaphore);
by Cristian Chilipirea
Semaphore – A different way of thinking
sem_wait(&semaphore);
by Cristian Chilipirea
Semaphore – A different way of thinking
sem_wait(&semaphore);
by Cristian Chilipirea
Semaphore – A different way of thinking
sem_wait(&semaphore);
by Cristian Chilipirea
Semaphore – A different way of thinking
sem_wait(&semaphore);
by Cristian Chilipirea
Semaphore – Signaling
sem_wait(&semaphore); -1
sem_post(&semaphore);
sem_post(&semaphore);
V() sau Verogen
- Dijsktra
by Cristian Chilipirea
Semaphore – Signaling
sem_wait(&semaphore); -1
sem_post(&semaphore);
sem_post(&semaphore);
by Cristian Chilipirea
Semaphore – Signaling
sem_wait(&semaphore); -1
sem_post(&semaphore);
sem_post(&semaphore);
by Cristian Chilipirea
Semaphore – Signaling
sem_wait(&semaphore); 0
sem_post(&semaphore);
sem_post(&semaphore);
by Cristian Chilipirea
Semaphore – Signaling
sem_wait(&semaphore); 1
sem_post(&semaphore);
sem_post(&semaphore);
by Cristian Chilipirea
Semaphore – Signaling
sem_wait(&semaphore); 0
sem_post(&semaphore);
sem_post(&semaphore);
by Cristian Chilipirea
Semaphore
ÎN MAIN
După ce au terminat thread-urile
sem_destroy(& semaphore);
ÎN MAIN
Înainte de a porni thread-urile
pthread_barrier_t barrier;
int num_threads = 5;
pthread_barrier_init(&barrier, NULL, num_threads);
pthread_barrier_wait(&barrier);
by Cristian Chilipirea
Barrier
pthread_barrier_wait(&barrier);
5
For all threads, all code here
is executed before any code here
by Cristian Chilipirea
Barrier
pthread_barrier_wait(&barrier);
by Cristian Chilipirea
Barrier
pthread_barrier_wait(&barrier);
by Cristian Chilipirea
Barrier
pthread_barrier_wait(&barrier);
by Cristian Chilipirea
Barrier
pthread_barrier_wait(&barrier);
by Cristian Chilipirea
Barrier
pthread_barrier_wait(&barrier);
by Cristian Chilipirea
Barrier
pthread_barrier_wait(&barrier);
by Cristian Chilipirea
Barrier
pthread_barrier_wait(&barrier);
by Cristian Chilipirea
Barrier
pthread_barrier_wait(&barrier);
by Cristian Chilipirea
Barrier
pthread_barrier_wait(&barrier);
by Cristian Chilipirea
Barrier
pthread_barrier_wait(&barrier);
by Cristian Chilipirea
Barrier
pthread_barrier_wait(&barrier);
by Cristian Chilipirea
Barrier
Cum știe o barieră când să se reseteze?
O soluție ar fi:
Reusable Barrier in
The Little Book of Semaphores
By Allen B. Downey
pthread_barrier_wait(&barrier);
by Cristian Chilipirea
Barrier
ÎN MAIN
După ce au terminat thread-urile
pthread_barrier_destroy(&barrier);
Huffman decoding
P complete problems
Florin Pop – Cristian Chilipirea
Splitting a problem to make it parallel
Embarrassingly parallel
9 6 9 4 2 7 6 5 6 1
*3
27 18 27 12 6 21 18 15 18 3
9 6 9 4 2 7 6 5 6 1
*3
27 18 27 12 6 21 18 15 18 3
9 6 9 4 2 7 6 5 6 1
9 6 9 4 2 7 6 5 6 … 1
9 6 9 4 2 7 6 5 6 … 1
9 6 9 4 2 7 6 5 6 … 1
9 6 9 4 2 7 6 5 6 … 1
9 6 9 4 2 7 6 5 6 … 1
P << N
9 6 9 4 2 7 6 5 6 … 1
Caz concret: P = 2
Cum împărțim?
9 6 9 4 2 7 6 5 6 … 1
Caz concret: P = 2
Cum împărțim?
9 6 9 4 2 7 6 5 6 … 1
Thread 1 Thread 2
Caz concret: P = 2
Cum împărțim?
9 6 9 4 2 7 6 5 6 … 1
Thread 1 Thread 2
Caz concret: P = 2
Cum împărțim?
9 6 9 4 2 7 6 5 6 … 1
Thread 1 Thread 2
Caz concret: P = 2
Cum împărțim? Putem și random
9 6 9 4 2 7 6 5 6 … 1
Thread 1 Thread 2
Caz concret: P = 2
Cum împărțim?
9 6 9 4 2 7 6 5 6 … 1
Thread 1 Thread 2
Caz concret: P = 2
Cum împărțim?
9 6 9 4 2 7 6 5 6 … 1
Este utilă?
Thread 1 Thread 2
Caz concret: P = 2
Cum împărțim?
9 6 9 4 2 7 6 5 6 … 1
Ce ne dorim?
Thread 1 Thread 2
Caz concret: P = 2
Cum împărțim?
9 6 9 4 2 7 6 5 6 … 1
Ce ne dorim?
Thread 1 Thread 2
9 6 9 4 2 7 6 5 6 … 1
Thread 1 Thread 2
9 6 9 4 2 7 6 5 6 … 1
Dacă N nu se divide perfect la P?
Thread 1 Thread 2
1
6 8
4 2 7 4 9 2
9 6 9 5 6 3
Thread 1 Thread 2
Florin Pop – Cristian Chilipirea
Embarrassingly parallel problems
6 5 8 1
4 2 7 4 9 2
9 6 9 6 3
Thread 1 Thread 2
Florin Pop – Cristian Chilipirea
Embarrassingly parallel problems
A = floor(N/P)
PA
0 A 2A N
A = ceil(N/P)
PA
0 A 2A N
0 A 2A N