title: MPI_Gather 2D array
body: <p><code>N</code> is 4, so is <code>N_glob</code>. It happens to be of the same size. <code>p</code> is 4.</p>
  
  <p>Here is a small portion of the code:</p>
  
  <pre><code>float **global_grid;
  float **gridPtr; 
  lengthSubN = N/pSqrt;
  subN = lengthSubN + 2;
  grid = allocate2D(grid, subN, subN);
  ..
  MPI_Type_contiguous(lengthSubN, MPI_FLOAT, &amp;rowType);
  MPI_Type_commit(&amp;rowType);
  ..
  gridPtr = grid;
  ..
  MPI_Barrier(MPI_COMM_WORLD);
  if(id == 0) {
      global_grid = allocate2D(global_grid, N_glob, N_glob);
  }
  MPI_Barrier(MPI_COMM_WORLD);
  MPI_Gather(&amp;(gridPtr[0][0]), 1, rowType,
             &amp;(global_grid[0][0]), 1, rowType, 0, MPI_COMM_WORLD);
  MPI_Barrier(MPI_COMM_WORLD);
  if(id == 0)
      print(global_grid, N_glob, N_glob);
  </code></pre>
  
  <p>where I have <code>p</code> submatrices and I am trying to gather them all in the root process, where the global matrix waits for them. However, it will just throw an error, any ideas?</p>
  
  <p>I am receiving a seg fault:</p>
  
  <blockquote>
    <p>BAD TERMINATION OF ONE OF YOUR APPLICATION PROCESSES
    PID 29058 RUNNING AT linux16
    EXIT CODE: 139
    YOUR APPLICATION TERMINATED WITH THE EXIT STRING: Segmentation fault (signal 11)</p>
  </blockquote>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>I found this question <a href="https://stackoverflow.com/questions/16569567/mpi-gather-segmentation-fault">MPI_Gather segmentation fault</a> and I initialized <code>global_grid</code> to NULL, but no luck. However, if I do:</p>
  
  <pre><code>//if(id == 0) {
      global_grid = allocate2D(global_grid, N_glob, N_glob);
  //}
  </code></pre>
  
  <p>then everything works. But shouldn't the global matrix live only in the root process?</p>
  
  <hr>
  
  <p>EDIT_2:</p>
  
  <p>IF I do:</p>
  
  <pre><code>if(id == 0) {
      global_grid = allocate2D(global_grid, N_glob, N_glob);
  } else {
      global_grid = NULL;
  }
  </code></pre>
  
  <p>then it will crash here:</p>
  
  <pre><code>MPI_Gather(&amp;gridPtr[0][0], 1, rowType,
                  global_grid[0], 1, rowType, 0, MPI_COMM_WORLD);
  </code></pre>