title: how to divide the work on MPI-Processes GameOfLife C++
body: <p>I've bin struggeling for a while with MPI and i am not able to Parallelize GameOfLife in c++ using MPI .
  i dont really understand how will i divide the work for each Process , and how will every Process do its work i think i've reached a close End here .</p>
  <p>the first block is where the root process initiallize 2D-Array , print it and then send for each process its own part of the work(at least i think so) left elements will be given to the last process .</p>
  <pre><code>&gt;&gt;&gt;&gt;
     if(rank==0){
  
      for (int h = 0; h &lt; ROWS; h++){
         for (int w = 0; w &lt;COLS ; w++){
           currentWorld[h][w] = (double(rand()) / RAND_MAX &lt; 0.1) ? 'X' :'.';
         }
       }
  
      //Recieve subarrays and gather them to be printed
      //char recieve_array[][]; 
        
        for (int h = 0; h &lt; ROWS; h++){ // if the iterate starts before rank zero this Code Block will lead to print that array every time we start iterating and that will lead to faulse results
       
           for (int w = 0; w &lt;COLS ; w++){             
             std::cout&lt;&lt;currentWorld[h][w];
           }
         }
         char send_array[elements_per_process][COLS]; //create the array that will be sent to each Process
         int counter=0;
         int rankNum=1;
  
         for(int i=0;i&lt;ROWS;i++){
          for(int j=0;j&lt;COLS;j++){
              send_array[counter][j]=currentWorld[i][j];// send_array which will be send will be from 0 to elements_per_process -1
             }
           //when the first array is full that condition will be executed(counter+1==elements_per_process),counter set to zero and then prepare the next array
           if(counter+1==elements_per_process)// from zero to elemens_per_process-1
             {                  
                 //those sends goes to every Process with his part of array to work on it , with Tag 10
              MPI_Send(&amp;elements_per_process,1,MPI_INT,rankNum,10,MPI_COMM_WORLD);//send the divided work for each Process
              MPI_Send(&amp;send_array,COLS*elements_per_process,MPI_CHAR,rankNum,10,MPI_COMM_WORLD);//send the divided work for each Process
              rankNum++;
              counter=0;
              if(left_elements&gt;0 &amp;&amp; rankNum==size-1)
              { //last process deal with the Reminder    
                 int left_elements=ROWS-(elements_per_process*size)
                 MPI_Send(&amp;left_elements,1,MPI_INT,rankNum,10,MPI_COMM_WORLD);//send the divided work for each Process
                 MPI_Send(&amp;send_array,COLS*left_elements,MPI_CHAR,rankNum,10,MPI_COMM_WORLD);//send the divided work for each Process
               }            
              }
           else{
                 counter++;
               }          
            }     
          } 
  </code></pre>
  <p>in the second  Block i am trying to recieve from each process its own part of work which sent by the root and then count the Neighbors alive to set the next generation
  so when i recieve those parts , should i iterate through those subarrays or through the whole original array and each process will just know its own part ? and at the end what should i send back ? the new subarrays from each process to root process , or the new whole array ? i think thats my problem for now , any thing will help because its hard to find some thing that could explain that(if any one knows any resource that could help i will be thankfull)</p>
  <pre><code>&gt;&gt;&gt;&gt;
           if(rank!=0) {        
                    //                     Determine the uppder and lower Neighbor for every Processe          //
    int upperNeighbor = (rank == 1) ? size - 1 : rank-1; //determine the upper rank Neighbor
    int lowerNeighbor = (rank == size) ? 1 : rank + 1;//determine the lowerNeighbor
    char recieved_array[elements_per_process][COLS];
       
           //Process Recieve his part of work from root Process
           //Process Recieve needed Data from other Processes(if the cell is at the top/bottom of Process then it needs data from the next/previous Process)
           //Tag for thoose Recieves will be 10
    MPI_Recv(&amp;elements_per_process,1,MPI_INT,0,10,MPI_COMM_WORLD,&amp;status);
    MPI_Recv(&amp;recieved_array,elements_per_process*COLS,MPI_CHAR,0,10,MPI_COMM_WORLD,&amp;status);
  
           //send top row above
    MPI_Send(&amp;currentWorld[1][0], COLS, MPI_CHAR, upperNeighbor,20,MPI_COMM_WORLD);
      
      //send bottom row below
    MPI_Send(&amp;currentWorld[elements_per_process][0], COLS, MPI_CHAR, lowerNeighbor, 30, MPI_COMM_WORLD);
  
      //recieve bottom row from below
    MPI_Recv(&amp;currentWorld[elements_per_process + 1][0],COLS, MPI_CHAR, lowerNeighbor,30, MPI_COMM_WORLD,&amp;status);
  
      //recieve top row from above
    MPI_Recv(&amp;currentWorld[0][0], COLS, MPI_CHAR, upperNeighbor, 20, MPI_COMM_WORLD,&amp;status);
  
    int neighbors=0;
    for(int i=0;i&lt;ROWS;i++){// originally for(int i=0;i&lt;elements_per_process;i++)           
      for(int j=0;i&lt;COLS;j++) {
        if(J==0||j==COL-1){
          if(j==0){// J==0 
              // check Neighbors but ignoring the left column
            for (int rows = i - 1; rows &lt;=i+ 1; rows++){ //from -1 untill 1                           
              for (int cols = j + 1; cols &lt;= j + 1; cols++){//from -1 untill 1                              
                if (i == rows &amp;&amp; j == cols)
                {
                  continue;
                }
                 if (i &gt; -1 &amp;&amp; i &lt; ROWS &amp;&amp; j &gt; -1 &amp;&amp; j &lt; COLS){
                   if (recieved_array[i][j] == 'X'){
                     neighbors++;  
                            }
                          } 
                      }
                 }
                      }//J==0
           else{ //check the Neighbours but ignoring the right column(COL-1)                            
             for (int rows = i - 1; rows &lt;=i+ 1; rows++){ //from -1 untill 1                               
               for (int cols = j + 1; cols &lt;= j + 1; cols++){//from -1 untill 1                                      
                 if (i == rows &amp;&amp; j == cols)
                 {
                    continue;
                 }
                 if (i &gt; -1 &amp;&amp; i &lt; ROWS &amp;&amp; j &gt; -1 &amp;&amp; j &lt; COLS){
                   if (recieved_array[i][j] == 'X'){
                     neighbors++;  
                              }
                           } 
                          }
                        }                              
                      }
                   } 
          else//if(j!=0 &amp;&amp; j!=COL-1){                             
             for (int rows2 = i - 1; rows2 &lt;=i+ 1; rows2++){ //from -1 untill 1                         
               for (int cols2 = j + 1; cols2 &lt;= j + 1; cols2++){//from -1 untill 1                              
                 if (i == rows2 &amp;&amp; j == cols2)
                 {
                    continue;
                 }
                 if (i &gt; -1 &amp;&amp; i &lt; ROWS &amp;&amp; j &gt; -1 &amp;&amp; j &lt; COLS){
                   if (recieved_array[i][j] == 'X'){
                     neighbors++; 
                     }
                  } 
                }
               }                
              }
            }
           }
          } 
                                       
  </code></pre>