title: Is MPI_Gather the best choice?
body: <p>There are 4 processes and one of them (<code>0</code>) is the master which has to build the matrix <code>C</code> as follow</p>
  
  <pre><code>-1  0  0 -1  0
   0 -1  0  0 -1
  -1  1  1 -1  1
   1 -1  1  1 -1
  -1  2  2 -1  2
   2 -1  2  2 -1
  -1  3  3 -1  3
   3 -1  3  3 -1
  </code></pre>
  
  <p>To do so, the matrix is declared as <code>REAL, DIMENSION(:,:), ALLOCATABLE :: C</code> and allocated with</p>
  
  <pre><code>IF (myid == 0) THEN
          ALLOCATE(C(2*nprocs,-2:+2))
  END IF
  </code></pre>
  
  <p>where <code>nprocs</code> is the number of processes. Process 0 also sets <code>C = -1</code>. For the communications I first tried with</p>
  
  <pre><code>CALL MPI_GATHER((/0.0+myid,0.0+myid/),&amp;
                &amp; 2,MPI_REAL,&amp;
                &amp; C(:,0),&amp;
                &amp; 2,MPI_REAL,&amp;
                &amp; 0,MPI_COMM_WORLD,ieri)
  </code></pre>
  
  <p>to fill up the central column, and this worked.
  Then I tried with </p>
  
  <pre><code>CALL MPI_GATHER((/myid, myid, myid, myid/),&amp;
                &amp; 4,MPI_REAL,&amp;
                &amp; (/C(1:2*nprocs:2,-1),C(2:2*nprocs:2,-2),C(1:2*nprocs:2,+2),C(2:2*nprocs:2,+1)/),&amp;
                &amp; 4,MPI_REAL,&amp;
                &amp; 0,MPI_COMM_WORLD,ierr)
  </code></pre>
  
  <p>to fill the other columns, but it didn't work, giving errors like the following</p>
  
  <pre><code>Fortran runtime error: Index '1' of dimension 1 of array 'c' outside of expected range (140735073734712:140735073734712).
  </code></pre>
  
  <p>To understand why, I tried to fill the first column alone with the call</p>
  
  <pre><code>CALL MPI_GATHER((/0.0-myid/),&amp;
                &amp; 1,MPI_REAL,&amp;
                &amp; C(1:2*nprocs:2,-2),&amp;
                &amp; 1,MPI_REAL,&amp;
                &amp; 0,MPI_COMM_WORLD,ierr)
  </code></pre>
  
  <p>but the same happened, more or less.</p>
  
  <p>I solved the problem by allocating <code>C</code> for all the processes (i.e. regardless of the process id). Why does this make the call work?</p>
  
  <p>After this I did a little change (before trying again to fill all the columns at once) simply putting the receive buffer in <code>(/.../)</code></p>
  
  <pre><code>CALL MPI_GATHER((/0.0-myid/),&amp;
                &amp; 1,MPI_REAL,&amp;
                &amp; (/C(1:2*nprocs:2,-2)/),&amp;
                &amp; 1,MPI_REAL,&amp;
                &amp; 0,MPI_COMM_WORLD,ieri)
  </code></pre>
  
  <p>but this makes the call ineffective (no errors, but not even one element in <code>C</code> changed).</p>
  
  <p>Hope someone can explain to me</p>
  
  <ul>
  <li>what's wrong with the constructor <code>(/.../)</code> in the receive buffer?</li>
  <li>why the receive buffer has to be allocated in the non-root processes?</li>
  <li>it is necessary to use <code>mpi_gatherv</code> to accomplish the task?</li>
  <li>is there a better way to build up such a matrix?</li>
  </ul>
  
  <p><strong>EDIT</strong>
  Is it possible to use MPI derived data types to build the matrix?</p>