title: Why does thiscode of matrixmultiplication using MPI run only for 4x4 matrix
body: <p>I wrote this code a few weeks back but still can't figure out how to make it run for more than 4x4 matrix. I don't understand what is wrong with it and how to fix it. The code is below please check it. Someone suggested to dynamically allocate the arrays but how does that work using MPI.</p>
  <pre><code>#define N 4
  #include &lt;stdio.h&gt;
  #include &lt;math.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;
  #include &quot;mpi.h&quot;
  
  
  void print_results(char *prompt, int a[N][N]);
  
  int main(int argc, char *argv[])
  {
      int i, j, k, rank, size, tag = 99, sum = 0;
      int a[N][N];
      int b[N][N];
      int c[N][N];
      int aa[N],cc[N];
      int row,col;
      int dest = 0;
      int source;
      double time1, time2, duration, global;
      MPI_Status status;
  
      MPI_Init(&amp;argc, &amp;argv);
      MPI_Comm_size(MPI_COMM_WORLD, &amp;size);
      MPI_Comm_rank(MPI_COMM_WORLD, &amp;rank);
  
      if(rank == 0){
          
  
          printf(&quot;enter the number of row =&quot;);    
          scanf(&quot;%d&quot;,&amp;row);    
          printf(&quot;enter the number of column =&quot;);    
          scanf(&quot;%d&quot;,&amp;col);    
  
          srand(time(NULL));
          for(i=0;i&lt;row;i++) {
              for(j=0;j&lt;col;j++){
                  a[i][j] = rand() % 10;
              }
          }
  
          for(i=0;i&lt;row;i++){
              for(j=0;j&lt;col;j++){
                  b[i][j] = rand() % 10;
              }
          }
      }
      MPI_Barrier(MPI_COMM_WORLD);
      time1 = MPI_Wtime();
      MPI_Scatter(a, N*N/size, MPI_INT, aa, N*N/size, MPI_INT,0,MPI_COMM_WORLD);
  
      MPI_Bcast(b, N*N, MPI_INT, 0, MPI_COMM_WORLD);
  
      MPI_Barrier(MPI_COMM_WORLD);
  
            for (i = 0; i &lt; N; i++)
              {
                      for (j = 0; j &lt; N; j++)
                      {
                              sum = sum + aa[j] * b[j][i];               
                      }
                      cc[i] = sum;
                      sum = 0;
              }
  
      MPI_Gather(cc, N*N/size, MPI_INT, c, N*N/size, MPI_INT, 0, MPI_COMM_WORLD);
      MPI_Barrier(MPI_COMM_WORLD);
  
      time2 = MPI_Wtime();
      duration = time2 - time1;
      MPI_Reduce(&amp;duration,&amp;global,1,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD);
      if(rank == 0) {
          printf(&quot;Global runtime is %f\n&quot;,global);
      }
      printf(&quot;Runtime at %d is %f \n&quot;, rank,duration);       
      MPI_Finalize();
      if (rank == 0)                      
        print_results(&quot;C = &quot;, c);
  }
  
  void print_results(char *prompt, int a[N][N])
  {
      int i, j;
  
      printf (&quot;\n\n%s\n&quot;, prompt);
      for (i = 0; i &lt; N; i++) {
              for (j = 0; j &lt; N; j++) {
                      printf(&quot; %d&quot;, a[i][j]);
              }
              printf (&quot;\n&quot;);
      }
      printf (&quot;\n\n&quot;);
  }
  
  
  </code></pre>
  <p>Any suggestions or help would be appreciated !!</p>