title: C parallel implementation of Gauss elimination with MPI
body: <p>I'm very new to MPI and I was asked to write a C parallel implementation for Gauss elimination (without pivoting).<br>
  I gave it a try (I used a row-wise decomposition) but my code doesn't work.  I am hoping someone can give me some pointers here. I've been looking for what's wrong for few days already without success :(<br>
  Thank you in advance !  </p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;
  #include &lt;mpi.h&gt;
  
  int main(int argc, char **argv)
  {
      MPI_Init(&amp;argc, &amp;argv);
  
      int i,j,k;
      int map[500];
      float A[500][500],b[500],c[500],x[500],sum=0.0;
      double range=1.0;
      int n=3;
      int rank, nprocs;
      clock_t begin1, end1, begin2, end2;
      MPI_Status status;
  
      MPI_Comm_rank(MPI_COMM_WORLD, &amp;rank);   /* get current process id */
      MPI_Comm_size(MPI_COMM_WORLD, &amp;nprocs); /* get number of processes */
  
  //////////////////////////////////////////////////////////////////////////////////
  
      if (rank==0)
      {
          for (i=0; i&lt;n; i++)
          {
          for (j=0; j&lt;n; j++)
          A[i][j]=range*(1.0-2.0*(double)rand()/RAND_MAX);
          b[i]=range*(1.0-2.0*(double)rand()/RAND_MAX);
          }
          printf("\n Matrix A (generated randomly):\n");
          for (i=0; i&lt;n; i++)
          {
              for (j=0; j&lt;n; j++)
              printf("%9.6lf ",A[i][j]);
          printf("\n");
          }
          printf("\n Vector b (generated randomly):\n");
          for (i=0; i&lt;n; i++)
              printf("%9.6lf ",b[i]);
          printf("\n\n");
      }
  
  //////////////////////////////////////////////////////////////////////////////////
  
      begin1 =clock();
  
      MPI_Bcast (A,n*n,MPI_DOUBLE,0,MPI_COMM_WORLD);
      MPI_Bcast (b,n,MPI_DOUBLE,0,MPI_COMM_WORLD);    
  
      for(i=0; i&lt;n; i++)
      {
          map[i]= i % nprocs;
      } 
  
      for(k=0;k&lt;n;k++)
      {
          MPI_Bcast (&amp;A[k][k],n-k,MPI_DOUBLE,map[k],MPI_COMM_WORLD);
          MPI_Bcast (&amp;b[k],1,MPI_DOUBLE,map[k],MPI_COMM_WORLD);
          for(i= k+1; i&lt;n; i++) 
          {
              if(map[i] == rank)
              {
                  c[i]=A[i][k]/A[k][k];
              }
          }               
          for(i= k+1; i&lt;n; i++) 
          {       
              if(map[i] == rank)
              {
                  for(j=0;j&lt;n;j++)
                  {
                      A[i][j]=A[i][j]-( c[i]*A[k][j] );
                  }
                  b[i]=b[i]-( c[i]*b[k] );
              }
          }
      }
      end1 = clock();
  
  //////////////////////////////////////////////////////////////////////////////////
  
      begin2 =clock();
  
      if (rank==0)
      { 
      x[n-1]=b[n-1]/A[n-1][n-1];
      for(i=n-2;i&gt;=0;i--)
      {
          sum=0;
  
          for(j=i+1;j&lt;n;j++)
          {
              sum=sum+A[i][j]*x[j];
          }
          x[i]=(b[i]-sum)/A[i][i];
      }
  
      end2 = clock();
      }
  //////////////////////////////////////////////////////////////////////////////////
      if (rank==0)
      { 
          printf("\nThe solution is:");
          for(i=0;i&lt;n;i++)
          {
              printf("\nx%d=%f\t",i,x[i]);
  
          }
  
          printf("\n\nLU decomposition time: %f", (double)(end1 - begin1) / CLOCKS_PER_SEC);
          printf("\nBack substitution time: %f\n", (double)(end2 - begin2) / CLOCKS_PER_SEC);
      }
  
      return(0);
  
      MPI_Finalize();
  }
  </code></pre>
  
  <p>And this is the error I'm getting:<br>
  mpirun has exited due to process rank 1 with PID XXXX on node XXXX exiting without calling "finalize". This may have caused other processes in the application to be terminated by signals sent by mpirun (as reported here).</p>