Parallel Game of Life

asked 2015-11-29 16:53:53 -0600

I am parallelizing the game of life and have an issue displaying my final image after each iteration.

int main(int argc, char** argv)
{
  assert(argc == 4);

  //-----------------------
  // Convert Command Line
  //-----------------------

  int ny = atoi(argv[1]);
  int nx = atoi(argv[2]);
  int maxiter = atoi(argv[3]);

  assert(ny <= MAX_SIZE);
  assert(nx <= MAX_SIZE);

  //---------------------------------
  // Generate the initial image
  //---------------------------------
  srand(clock());
  cv::Mat population(ny, nx, CV_8UC1);
  for (unsigned int iy = 0; iy < ny; iy++)
  {
    for (unsigned int ix = 0; ix < nx; ix++)
    {
      //seed a 1/2 density of alive (just arbitrary really)
      int state = rand()%2;
      if (state == 0) population.at<uchar>(iy,ix) = DEAD; //dead
      else population.at<uchar>(iy,ix) = ALIVE;   //alive
    }  
  }

  cv::Mat newpopulation = population.clone();
  cv::namedWindow("Population", cv::WINDOW_AUTOSIZE );
  cv::Mat image_for_viewing(MAX_SIZE,MAX_SIZE,CV_8UC1);

  int rank, nproc;
  MPI_Init(&argc, &argv);
  MPI_Comm_size(MPI_COMM_WORLD,&nproc);
  MPI_Comm_rank(MPI_COMM_WORLD,&rank);

  // do all the processing here
  std::vector<int> localstart(nproc), localstop(nproc), localcount(nproc);
  parallelRange(0, population.rows-1, rank, nproc, localstart[rank], localstop[rank], localcount[rank]);

  // calculate appropriate bounds for all processors.
  int start = std::max(0, localstart[rank]-1);
  int stop = std::min(population.rows-1, localstop[rank]+1);
  int rows_in_section = stop-start+1;

  for (int iter = 0; iter < maxiter; iter++)
  {    
    // make sure every processor has finished its work 
    MPI_Barrier(MPI_COMM_WORLD);

    // cut out the section we are processing and copy it into the known population
    cv::Mat image_on_proc = newpopulation(cv::Rect(0, start, population.cols, rows_in_section));
    image_on_proc.copyTo(population(cv::Rect(0, start, population.cols, rows_in_section)));

    // make sure all sections are copied
    MPI_Barrier(MPI_COMM_WORLD);

    // display the image
    if(rank == 0){
    cv::resize(population,image_for_viewing,image_for_viewing.size(),cv::INTER_LINEAR);
    cv::imshow("Population", image_for_viewing);
    cvWaitKey(10);
    }

    // play the game
      }
    } 
  }

  MPI_Finalize();

}

I am trying to display my final image on rank 0, however only the rank 0 section is showing any changes. I was hoping someone could tell my why my entire is not displaying all the changes each iteration. I will note that displaying a section on each processor shows all the appropriate changes.

edit retag flag offensive close merge delete

Comments

Okay and now tell us what the fame of life is and why it would be relevant to this forum? Also this seems like an OpenMP problem, not an OpenCV one ...

StevenPuttemans gravatar imageStevenPuttemans ( 2015-11-30 02:58:40 -0600 )edit