# What I learned from writing synchronization barriers

Before starting project 2 (for my advanced operating systems course), I took a snapshot of my understanding of synchronization barriers. In retrospect, I’m glad I took 10 minutes out of my day to jot down what I did (and did not) know because now, I get a clearer pictur eof what I learned. Overall, I feel the project was worthwhile and I gained not only some theoretical knowledge of computer science but I was also able to flex my C development skills, writing about 500 lines of code.

# Discovered a subtle race condition with lecture’s pseudo code

Just by looking at the diagram below, it’s not obvious that there’s a subtle race condition hidden. I only was able to identify it after whipping up some code (below) and analyzing the concurrent flows. I elaborate a little more on the race condition — which results in a deadlock — in this blog post.

```
/*
* Race condition possible here. Say 2 threads enter, thread A and
* (count &amp;gt; 0) loop. But just before then, thread B enters (count == 0)
* and sets count = 2. At which point, we have a deadlock, thread A
* cannot break free out of the barrier
*
*/

if (count == 0) {
} else {
while (count &amp;gt; 0) {
printf("Spinning .... count = %d\n", count);
}
printf("Spinning on count\n");
}
}

```

# Data Structures and Algorithms

## How to represent a tree based algorithm using multi-dimensional arrays in C

For both the dissemination and tournament barrier, I had to build multi-dimensional arrays in C. I initially had a difficult time envisioning the data structure described in the research papers, asking myself questions such as “what do I use to index into the first index?”. Initially, my intuition thought that for the tournament barrier, I’d index into the first array using the round ID  but in fact you index into the array using the rank (or thread id) and that array stores the role for each round.

```typedef struct {
bool myflags[PARITY_BIT][MAX_ROUNDS];
bool *partnerflags[PARITY_BIT][MAX_ROUNDS];
} flags_t;

void flags_init(flags_t flags[MAX_ROUNDS])
{
int i,j,k;

for (i = 0; i < MAX_NUM_THREADS; i++) {
for (j = 0; j < PARITY_BIT; j++) {
for (k = 0; k < MAX_NUM_THREADS; k++) {
flags[i].myflags[j][k] = false;
}
}
}
}
```

# OpenMP and OpenMPI

Prior to starting I never heard of neither OpenMP nor OpenMPI. Overall, they are two impressive pieces of software that makes multi-threading (and message passing) way easier, much better than dealing with the Linux pthreads library.

# Summary

Overall, the project was rewarding and my understanding of synchronization barriers (and the various flavors) were strengthen by hands on development. And if I ever need to write concurrent software for a professional project, I’ll definitely consider using OpenMP and OpenMPI instead of low level libraries like PThread.

Matt Chung