title: OpenMP with Game of Life visualization using SFML
body: <p>Hello I'm trying to compare speeds between serial and parallel version of 'Game of Life'.
  I used SFML library to visualize game of life like this.
  <a href="https://i.stack.imgur.com/7UB2O.png" rel="nofollow noreferrer">SFML window</a>
  Serial logic is simple like below.</p>
  
  <pre><code>for (int i = 0; i &lt; height; i++) {
              for (int j = 0; j &lt; width; j++) {
                  int neighbor = 0;
  
                  // check 8 cells around.
                  // 1 2 3  -1
                  // 4   5  0
                  // 6 7 8  +1
  
                  // (1)
                  if (gamefieldSerial.isAvailableCell(UP(i), LEFT(j))) {
                      if(gamefieldSerial[UP(i)][LEFT(j)] == LIVE) neighbor++;
                  }
                  // (2)
                  if (gamefieldSerial.isAvailableCell(UP(i), j)) {
                      if (gamefieldSerial[UP(i)][j] == LIVE)      neighbor++;
                  }
                  // (3)
                  if (gamefieldSerial.isAvailableCell(UP(i), RIGHT(j))) {
                      if (gamefieldSerial[UP(i)][RIGHT(j)] == LIVE)   neighbor++;
                  }
                  // (4)
                  if (gamefieldSerial.isAvailableCell(i, LEFT(j))) {
                      if (gamefieldSerial[i][LEFT(j)] == LIVE)        neighbor++;
                  }
                  // (5)
                  if (gamefieldSerial.isAvailableCell(i, RIGHT(j))) {
                      if (gamefieldSerial[i][RIGHT(j)] == LIVE)       neighbor++;
                  }
                  // (6)
                  if (gamefieldSerial.isAvailableCell(DOWN(i), LEFT(j))) {
                      if (gamefieldSerial[DOWN(i)][LEFT(j)] == LIVE)  neighbor++;
                  }
                  // (7)
                  if (gamefieldSerial.isAvailableCell(DOWN(i), j)) {
                      if (gamefieldSerial[DOWN(i)][j] == LIVE)        neighbor++;
                  }
                  // (8)
                  if (gamefieldSerial.isAvailableCell(DOWN(i), RIGHT(j))) {
                      if (gamefieldSerial[DOWN(i)][RIGHT(j)] == LIVE) neighbor++;
                  }
  
                  // -- Rule of Game of Life
                  // Cell borns when exactly 3 neighbor is LIVE
                  // Cell remains alive when 2 or 3 neighbor is LIVE
                  // Cell with more than 3 neighbor dies with overpopulation
                  // Cell with less than 2 neighbor dies with underpopulation
                  if (gamefieldSerial[i][j] == DEAD) {
                      if (neighbor == 3) {
                          gamefieldSerial[i][j] = LIVE;
                      }
                  }
                  else if (gamefieldSerial[i][j] == LIVE) {
                      if (neighbor &lt; 2 || neighbor &gt; 3) {
                          gamefieldSerial[i][j] = DEAD;
                      }
                  }
              }
  </code></pre>
  
  <p>It took 3940ms on 768*256 cells with 100 generations.
  But in parallel version I implemented like below,</p>
  
  <pre><code>#pragma omp parallel for num_threads(4)
          for (int t = 0; t &lt; width * height; t++) {
              int i = t / width;
              int j = t % width;
              int neighbor = 0;
  
              // check 8 cells around.
              // 1 2 3  -1
              // 4   5  0
              // 6 7 8  +1
  
              // (1)
              if (gamefieldParallel.isAvailableCell(UP(i), LEFT(j))) {
                  if (gamefieldParallel[UP(i)][LEFT(j)] == LIVE) neighbor++;
              }
              // (2)
              if (gamefieldParallel.isAvailableCell(UP(i), j)) {
                  if (gamefieldParallel[UP(i)][j] == LIVE)      neighbor++;
              }
              // (3)
              if (gamefieldParallel.isAvailableCell(UP(i), RIGHT(j))) {
                  if (gamefieldParallel[UP(i)][RIGHT(j)] == LIVE)   neighbor++;
              }
              // (4)
              if (gamefieldParallel.isAvailableCell(i, LEFT(j))) {
                  if (gamefieldParallel[i][LEFT(j)] == LIVE)        neighbor++;
              }
              // (5)
              if (gamefieldParallel.isAvailableCell(i, RIGHT(j))) {
                  if (gamefieldParallel[i][RIGHT(j)] == LIVE)       neighbor++;
              }
              // (6)
              if (gamefieldParallel.isAvailableCell(DOWN(i), LEFT(j))) {
                  if (gamefieldParallel[DOWN(i)][LEFT(j)] == LIVE)  neighbor++;
              }
              // (7)
              if (gamefieldParallel.isAvailableCell(DOWN(i), j)) {
                  if (gamefieldParallel[DOWN(i)][j] == LIVE)        neighbor++;
              }
              // (8)
              if (gamefieldParallel.isAvailableCell(DOWN(i), RIGHT(j))) {
                  if (gamefieldParallel[DOWN(i)][RIGHT(j)] == LIVE) neighbor++;
              }
  
              // -- Rule of Game of Life
              // Cell borns when exactly 3 neighbor is LIVE
              // Cell remains alive when 2 or 3 neighbor is LIVE
              // Cell with more than 3 neighbor dies with overpopulation
              // Cell with less than 2 neighbor dies with underpopulation
              if (gamefieldParallel[i][j] == DEAD) {
                  if (neighbor == 3) {
                      gamefieldParallel[i][j] = LIVE;
                  }
              }
              else if (gamefieldParallel[i][j] == LIVE) {
                  if (neighbor &lt; 2 || neighbor &gt; 3) {
                      gamefieldParallel[i][j] = DEAD;
                  }
              }
          }
  </code></pre>
  
  <p>It took 5746ms on same environment.
  I thought applying openMP's 'for' directive in for-loop enhances the performance, but it doesn't.
  Should I have to approach in another way?</p>
  
  <p>=============
  Both gamefieldParallel and gamefieldSerial is instance of GameField class which has dynamically allocated int** field variable for cells. I'm using operator overloading to access it like two dimensional array.(Sorry for bad english!)</p>