title: How do i scatter a 3d array using MPI_Scatterv?
body: <p>I am trying to distribute a 16<em>16</em>3(X<em>Y</em>Z) array into 16 processes as 4<em>4</em>3 arrays. I have used MPI_Create_subarray to scatter the blocks. For row-major ordering of a 3D matrix, I have taken the elements in the contiguous allocation as [Z*(Y*i+j)+ k]. The displacements required for MPI_Scatterv were calculated from this. But the scattering is only successful for process rank 0. Is the problem arising from the allocation of the 3d array?</p>
  <pre><code>int N = 16;
      int d = 3;
      int NPROWS = 4;  /* number of rows in _decomposition_ */
      int NPCOLS = 4;  /* number of cols in _decomposition_ */
      int BLOCKROWS = ROWS / NPROWS;  /* number of rows in _block_ */
      int BLOCKCOLS = COLS / NPCOLS; /* number of cols in _block_ */
  
      int* data = malloc(N * N * d * sizeof(int));
      int*** array = malloc(d * sizeof(int**));
      for (int i = 0; i &lt; N; i++) {
          array[i] = (int**)malloc(N * sizeof(int*)) ;
          for (int j = 0; j &lt; N; j++) {
              array[i][j] = &amp;(data[(i * N + j) * d]);
          }
      }
  
      int* data2 = malloc(BLOCKROWS * BLOCKCOLS * d * sizeof(int));
      int*** array2 = malloc(d * sizeof(int**));
      for (int i = 0; i &lt; BLOCKROWS; i++) {
          array2[i] = (int**)malloc(BLOCKROWS * sizeof(int*));
          for (int j = 0; j &lt; BLOCKCOLS; j++) {
              array2[i][j] = &amp;(data2[(i * BLOCKCOLS + j) *d]);
          }
      }
  
      if (rank == 0)
      {
          for (int i = 0; i &lt; N; i++) {
              for (int j = 0; j &lt; N; j++) {
                   for (int k = 0; k &lt; d; k++) {
                      array[i][j][k] = (d*(i*N +j)+ k) ;
                  }
              }
          }
      }           
          
      MPI_Datatype mysubarray;
      MPI_Datatype mysubarray2;
      int starts[3] = { 0, 0, 0 };
      int subsizes[3] = {BLOCKROWS, BLOCKCOLS,d};
      int bigsizes[3] = {N, N, d};
      MPI_Type_create_subarray(3, bigsizes, subsizes, starts, MPI_ORDER_C, MPI_INT, &amp;mysubarray2);
      MPI_Type_create_resized(mysubarray2, 0, 1 * sizeof(int), &amp;mysubarray);
      MPI_Type_commit(&amp;mysubarray);
  
      int* disps3d = malloc(NPROWS * NPCOLS * sizeof(int));
      int* counts3d = malloc(NPROWS * NPCOLS * sizeof(int));
  
      for (int ii = 0; ii &lt; NPROWS; ii++) {
          for (int jj = 0; jj &lt; NPCOLS; jj++) {
              disps3d[ii * NPCOLS + jj] = d*(ii * N * BLOCKROWS + jj * BLOCKCOLS);
              counts3d[ii * NPCOLS + jj] = 1;
          }
      }
  
      MPI_Scatterv(array, counts3d, disps3d, mysubarray, array2, (BLOCKCOLS*BLOCKROWS*d), MPI_INT, 0, MPI_COMM_WORLD);
  </code></pre>