/* * The Game Of Life */ #define ROOT 0 #define HEIGHT DIM #define WIDTH DIM #define GENERATIONS GEN #include #include #include #include "mpi.h" /* function declarations */ void board_to_file(int (*current)[HEIGHT],int i); /* func declaration uses pointer notation to pass 2d arrays */ void rules(int (*current)[HEIGHT],int generation,int start_col,int end_col); int get_neighbor_count (int i, int j, int (*current)[HEIGHT]); int main(int argc, char** argv) { int my_rank; /* My process rank */ int p; /* The number of processes */ int (*game_board)[HEIGHT]; /* Main board */ int (*temp)[HEIGHT]; /* pointer to 2d array */ int i,j,k,m,n; /* available iterators */ int tmp,per_proc,left_over,my_start_col,my_end_col; int tmp_start_col,tmp_end_col; MPI_Status status; FILE *INIT; MPI_Init(&argc, &argv); /* get number of procs */ MPI_Comm_size(MPI_COMM_WORLD,&p); /* get rank of current process */ MPI_Comm_rank(MPI_COMM_WORLD,&my_rank); /* size game board */ game_board=malloc(HEIGHT*WIDTH*sizeof(int)); if (my_rank == ROOT) { /* * Board initialization!! * 1) look for "init.dat" is not found in CWD * 2) if "init.dat" is not found, ask user interactively what pattern to start with: * a) random * b) alternating rows (starting with all 1) * c) upper triangular matrix of 1's */ /* look for init.dat */ INIT = fopen("init.dat","r"); if (INIT) { printf("Initializing grid from input.dat\n"); for (i=0;i 1) { /* broadcast current game_board to all non-ROOT procs */ (void) MPI_Bcast(game_board,HEIGHT*WIDTH,MPI_INT,ROOT,MPI_COMM_WORLD); } /* apply rules */ rules(game_board,i,my_start_col,my_end_col); if (my_rank != ROOT) { /* <<----------------------------------- send */ /* populate temp game_board cols owned by this process */ for (m=my_start_col;m<=my_end_col;m++) { for (j=0;j 1) { /* <<----------------------------------- receive */ for (k=1;k 3) { current[i][j] = 0; } } else if (temp[i][j] == 0) { if (neighbors == 3) { current[i][j] = 1; } } } } } /* * returns the number of neighbors surrounding cell */ int get_neighbor_count (int i, int j, int (*current)[HEIGHT]) { int a,b,c,d,e,f,x,count; a = j-1; /* bounded by WIDTH bdy */ b = j; /* won't violate bdy */ c = j+1; /* bounded by WIDTH bdy */ d = i-1; /* bounded by HEIGHT bdy */ e = i; /* won't violate bdy */ f = i+1; /* bounded by HEIGHT bdy */ count = 0; for (x=1;x <= 8;x++) { switch (x) { case 1: if (a >= 0 && d >= 0) { count += current[a][d]; } break; case 2: if (a >= 0) { count += current[a][e]; } break; case 3: if (a >= 0 && f < HEIGHT) { count += current[a][f]; } break; case 4: if (f < HEIGHT) { count += current[b][f]; } break; case 5: if (c < WIDTH && f < HEIGHT) { count += current[c][f]; } break; case 6: if (c < WIDTH) { count += current[c][e]; } break; case 7: if (c < WIDTH && d >= 0) { count += current[c][d]; } break; case 8: if (d >= 0) { count += current[b][d]; } break; } } return count; } /* * dump frame to stdout */ void board_to_file(int (*current)[HEIGHT],int generation) { int i; int j; char filename[10]; sprintf(filename,"frames/%d.dat",generation); FILE *OUTPUT = fopen(filename,"w"); for (i=0;i