title: MPI Broadcasting 2d array
body: <p>I was going to study parallel programming with MPI. And I've got some error</p>
  
  <pre><code>#include "mpi.h"
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  
  int main(int argc, char** argv)
  {
      int procNum, procRank;
      int m,n;
      int sumProc = 0, sumAll = 0;
      int** arr;
      MPI_Status status;
  
      MPI_Init ( &amp;argc, &amp;argv );
  
      MPI_Comm_size ( MPI_COMM_WORLD, &amp;procNum ); 
      MPI_Comm_rank ( MPI_COMM_WORLD, &amp;procRank );
  
      if (procRank == 0)
      {   
          printf("Type the array size \n");
          scanf("%i %i", &amp;m, &amp;n); 
      }
      MPI_Bcast(&amp;m, 1, MPI_INT, 0, MPI_COMM_WORLD);
      MPI_Bcast(&amp;n, 1, MPI_INT, 0, MPI_COMM_WORLD);
  
      arr = new int*[m];
      for (int i = 0; i &lt; m; i++)
          arr[i] = new int[n];
  
      if (procRank == 0)
      {
          for (int i = 0; i &lt; m; i++)
          {
              for (int j = 0; j &lt; n; j++)
              {
                      arr[i][j] = rand() % 30;
                      printf("%i ", arr[i][j]);
              }
              printf("\n");
          }
      }
  
      MPI_Bcast(&amp;arr[0][0], m*n, MPI_INT, 0, MPI_COMM_WORLD);
  
      for (int i = procRank; i &lt; n; i += procNum)
          for (int j = 0; j &lt; m; j++)
              sumProc += arr[j][i];
  
      MPI_Reduce(&amp;sumProc,&amp;sumAll,1,MPI_INT,MPI_SUM,0,MPI_COMM_WORLD);
  
      if (procRank == 0)
      {
          printf("sumAll = %i", sumAll);
      }
  
      delete *arr;
  
      MPI_Finalize();
      return 0;
  }
  </code></pre>
  
  <p>I'm trying to pass 2d array to other processes, but when I check it out I get wrong array.
  Something like this:</p>
  
  <pre><code>Original array
  11 17 4
  10 29 4
  18 18 22
  
  Array which camed
  11 17 4
  26 0 0
  28 0 0
  </code></pre>
  
  <p>What the issue is it? Maybe issue is in MPI_Bcast</p>
  
  <p>P.S. I added </p>
  
  <pre><code>for (int i = 0; i &lt; m; i++)
      MPI_Bcast(arr[i], n, MPI_INT, 0, MPI_COMM_WORLD);
  </code></pre>
  
  <p>instead of </p>
  
  <pre><code>MPI_Bcast(&amp;arr[0][0], m*n, MPI_INT, 0, MPI_COMM_WORLD);
  </code></pre>
  
  <p>It solved my question</p>