Download Programming on Parallel Machines - matloff
Transcript
2.10. BARRIER IMPLEMENTATION
thus would not conflict with processor 12’s resetting. Here is a safe barrier function based on this idea:
1
2
3
4
5
struct BarrStruct {
int NNodes, // number of threads participating in the barrier
Count[2], // number of threads that have hit the barrier so far
pthread_mutex_t Lock = PTHREAD_MUTEX_INITIALIZER;
} ;
6
7
8
9
10
11
12
13
14
15
16
17
18
Barrier(struct BarrStruct *PB)
{ int Par,OldCount;
Par = PB->EvenOdd;
pthread_mutex_lock(&PB->Lock);
OldCount = PB->Count[Par]++;
pthread_mutex_unlock(&PB->Lock);
if (OldCount == PB->NNodes-1) {
PB->Count[Par] = 0;
PB->EvenOdd = 1 - Par;
}
else while (PB->Count[Par] > 0) ;
}
2.10.4
2.10.4.1
Refinements
Use of Wait Operations
The code
else while (PB->Count[Par] > 0) ;
49