Barriers in Memo

Th Christopher
Memo home page

Memo Prog Tech

Barriers. In parallelizing a loop, it is often important to synchronize the processes executing the loop at one or more points within it using a barrier. If a barrier is initialized with a count N, then N processes must wait at the barrier before any of them are allowed to proceed. The barrier is automatically reinitialized so that they can synchronize over and over again at the same barrier. In Memo, a barrier can be implemented with two folders and a single memo containing an integer count. The presence of the memo in a folder allows the processes to proceed past the barrier.

The processes alternate which folder they look for the memo in. When a process comes to a barrier, it gets the memo and decrements the count in it. If the count is greater than zero, this is not the last process to arrive, so it puts the memo back and tries to get a copy of the memo in the other folder, which is not present yet.

If the process decrements the count to zero, then this is the last process to arrive. It puts a memo with the full count of the number of processes to synchronize into the other folder and continues executing.

Code for this follows:

FOLDER_NAME barrier[2] = {{BARR0,0,0,0},{BARR1,0,0,0}};
int which=0,num_at_barrier=NUM_TO_SYNCHRONIZE;
...
/* initialize the barrier*/
memo_write(barrier[0],&num_at_barrier,sizeof(int));
...
while (TRUE) {
int *p;
...
/* synchronize at the barrier: */
p = (int *) memo_get(barrier[which]);
(*p)--;
if (*p==0) {
/* I'm the last to arrive */
which = 1-which;
memo_write(barrier[which],&num_at_barrier,
sizeof(int));
} else {
/* I am not the last to arrive */
memo_put(barrier[which],p);
which = 1-which;
p = (int*) memo_get_copy(barrier[which]);
}
memo_free(p);
...
}

This page last updated 09/30/97