Index
HPC - Universidad de Sevilla

MPI - Game of life

Domain: orthogonal grid of square cells. Usually considered infinte, we will use periodic boundary conditions in this example.

gameoflife1

Rules (see wikipedia):

Example:

life1

A (not optimized) implementation in C would be

void evolve(int nx, int ny) {
  for (int i = 0; i < nx; i++) {
    for (int j = 0; j < ny; j++) {
      int n = 0;
      for (int i1 = i - 1; i1 <= i + 1; i1++) {
        for (int j1 = j - 1; j1 <= j + 1; j1++) {
          if (univ[(i1 + nx) % nx][(j1 + ny) % ny]) {
            n++;
          }
        }
      }
      if (univ[i][j]) n--;
      new[i][j] = (n == 3 || (n == 2 && univ[i][j]));
    }
  }
  for (int i = 0; i < nx; i++) {
    for (int j = 0; j < ny; j++) {
      univ[i][j] = new[i][j];
    }
  }
}

For the periodic boundary conditions we can use

(i + nx) % nx

or using "shadow cells"

domain1

A simple domain decomposition with shadow cells might be

domain1