title: MPI Segmentation Fault in MPI_Comm_rank()
body: <p>I am a beginner in MPI and this code seems to generate a segmentation fault.</p>
  
  <pre><code>int luDecomposeP(double *LU, int n)
  {
      int i, j, k;
      int sendcount, recvcount, remaining, rank, numProcs, status;
      double *row, *rowFinal, *start, factor;
  
      MPI_Comm_rank(MPI_COMM_WORLD, &amp;rank);
      MPI_Comm_size(MPI_COMM_WORLD, &amp;numProcs);
  
      MPI_Bcast(&amp;n, 1, MPI_INT, 0, MPI_COMM_WORLD);
  
      row = (double *)malloc(n*sizeof(double));
      rowFinal = (double *)malloc(n*n*sizeof(double));
  
      for(i=0; i&lt;n-1; i++)
      {
          if(rank == 0)
          {
              status = pivot(LU,i,n);
  
              for(j=0; j&lt;n; j++)
              row[j] = LU[n*i+j];
          }
  
          MPI_Bcast(&amp;status, 1, MPI_INT, 0, MPI_COMM_WORLD);
  
          if(status == -1)
              return -1;
  
          MPI_Bcast(row, n, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  
      sendcount = (n-i-1)/numProcs;  
      recvcount = (n-i-1)/numProcs;
      remaining = (n-i-1)%numProcs;
  
      if(rank == 0)
          start = LU + n*(i+1);
      else
          start = NULL;
  
      MPI_Scatter(start, sendcount*n, MPI_DOUBLE, rowFinal, recvcount*n, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  
      for(j=0; j&lt;recvcount; j++)
      {
          factor = rowFinal[n*j+i]/row[i];
  
          for(k=i+1; k&lt;n; k++)
              rowFinal[n*j+k] -= row[k]*factor;
  
          rowFinal[n*j+i] = factor;
      }
  
      MPI_Gather(rowFinal, recvcount*n, MPI_DOUBLE, start, sendcount*n, MPI_DOUBLE, 0, MPI_COMM_WORLD);
  
  if(rank == 0)
      {
          int ctr = 0;
  
          while(ctr&lt;remaining)
          {
              int index = sendcount*numProcs + ctr + i + 1;
  
              factor = LU[n*index+i]/row[i];
  
              for(k=i+1; k&lt;n; k++)
                  LU[n*index+k] -= row[k]*factor;
  
              LU[n*index+i] = factor;
  
              ctr++;
          }
      }
  }   
  free(row);
  free(rowFinal);
  
  return 0;
  }
  </code></pre>
  
  <p>This code results in segmentation fault. I read a lot of answers and tried to fix it
  but that did not happen. <strong>I read about the problem of dereferencing a NULL pointer, which I fixed it by using a pointer called start</strong>. But the segmentation errors still keep showing up. </p>
  
  <p>The error :</p>
  
  <blockquote>
    <p>[sheshnag:32334] <strong>* Process received signal *</strong> </p>
    
    <p>[sheshnag:32334] Signal: Segmentation fault (11) </p>
    
    <p>[sheshnag:32334] Signal code: Address not mapped (1) </p>
    
    <p>[sheshnag:32334] Failing at address: 0x44000098</p>
    
    <p>[sheshnag:32334] [ 0] /lib/libpthread.so.0(+0xf8f0) [0x2b082eafe8f0]</p>
    
    <p>[sheshnag:32334] [ 1] /usr/lib/openmpi/lib/libmpi.so.0(MPI_Comm_rank+0x5e) [0x2b082d5ff6ee]</p>
    
    <p>[sheshnag:32334] [ 2] ./libluDecompose.so(luDecomposeP+0x2f) [0x2b082d17ea2f]</p>
    
    <p>[sheshnag:32334] [ 3] _tmp/bench.mpi.exe(main+0x2e7) [0x40b61d]</p>
    
    <p>[sheshnag:32334] [ 4] /lib/libc.so.6(__libc_start_main+0xfd) [0x2b082ed2ac4d]</p>
    
    <p>[sheshnag:32334] [ 5] _tmp/bench.mpi.exe() [0x40ac49]</p>
  </blockquote>