title: I can't figure out MPI_Scatterv and MPI_Gatherv
body: <p>I was trying to scatter an 1-dim n*n array  to p processors and then used gather to get the data back.but the problem is it displays the garbage output when i run the program.
  i am using an Array A which is of n*n and an array b which is of size n .</p>
  
  <pre><code>// num_rows is the number of rows each process will be receiving,base is the base index and last is the last index .
  // curr_index_proce is the index of the processor which contains the index i of Array A. 
  
  #include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;math.h&gt;
  #include &lt;mpi.h&gt;
  
  #define NUM_EQN 10
  
  #define NUM_ROWS(i,n,p)  ((((i)+1)*(n)/(p)) - ((i)*(n)/(p)))
  #define BASE(i,n,p)     (((i)*(n)/(p)))
  #define LAST(i,n,p) ((((i)+1)*(n)/(p)) - 1)
  #define CUR_INDEX_PROC(i,n,p) ( ((p) * ( (i) + 1 ) - 1 )/(n))
  
  #define MIN(a,n) ((a) &gt; (b) ? (a) : (b))
  
  void gather(float *A,float *b,int n);
  void print_array(float *A,float *b,int n){
          int i,j;
          for ( i = 0 ; i &lt; n;i++){
  //              printf("i = %d ",i);
                  for(j = 0 ; j &lt; n;j++){
                          printf("%6.2f " ,A[i*n+j]);
                  }
                  printf("\n");
                  //printf(" : %6.2f\n",b[i]);
          }
          printf("\n\n\n");
  }
  
  
  
  int SIZE_A(int n,int p ){
      if ( n%p == 0 )
              return n*n/p;
      else
              return n*(n/p+1);
  }
  
  int SIZE_B(int n,int p){
      if ( n%p == 0 )
              return n/p;
      else
              return n/p + 1;
  }
  int main(int argc,char **argv){
          MPI_Init(&amp;argc,&amp;argv);
          int size,rank;
          MPI_Comm_rank(  MPI_COMM_WORLD,&amp;rank);
          MPI_Comm_size(MPI_COMM_WORLD,&amp;size);
          double time_start, time_finish;
          float *A = malloc(sizeof(float)*NUM_EQN*NUM_EQN);
          float *b = malloc(sizeof(float)*NUM_EQN);
          float *X;
          int i,j;
          FILE *fp = fopen("A","r");
          if (fp == NULL ){
                  printf("Error %s Not Found\n",argv[1]);
                  exit(0);
                  MPI_Finalize();
          }
          for ( i = 0 ; i &lt; NUM_EQN;i++)
                  for(j = 0 ; j &lt; NUM_EQN;j++)
                          fscanf(fp,"%f",&amp;A[i*NUM_EQN+j]);
          FILE *fp2 = fopen("b","r");
          if (fp2 == NULL ){
                  printf("Error %s Not Found\n",argv[2]);
                  exit(0);
          }
          for ( i = 0 ; i &lt; NUM_EQN;i++)
                  fscanf(fp2,"%f",&amp;b[i]);
          time_start = - MPI_Wtime();
          gather(A,b,NUM_EQN);
          time_finish = MPI_Wtime();
          char name[100];
          int length;
          MPI_Finalize();
  }
  
  void gather(float *A,float *b,int n){
          int pivot,i,col;
          int row, j;
          int size,rank;
          MPI_Comm_rank(  MPI_COMM_WORLD,&amp;rank);
          MPI_Comm_size(MPI_COMM_WORLD,&amp;size);
          float global_max;
          float *A_recv,*b_recv;
          A_recv = malloc(sizeof(float)*(SIZE_A(n,size)));
          b_recv = malloc(sizeof(float)*(SIZE_B(n,size)));
      printf("%d %d \n",SIZE_A(n,size),SIZE_B(n,size));
      if ( rank == 0 ){
              print_array(A,b,n);
      }
                  int send_count[size];
                  int disp[size];
                  int send_count_b[size];
                  int disp_b[size];
                  for ( i = 0 ; i &lt; size ; i++ ){
                                  send_count[i] = 0 ;
                                  send_count_b[i] = 0;
                  }
                  for ( i = 0 ; i &lt; size ; i++ ){
                                  send_count[i] = n*NUM_ROWS(i,n,size);
                                  if ( i == 0 )
                                          disp[i] = 0;
                                  else
                                          disp[i] = disp[i-1] + send_count[i-1];
                                  send_count_b[i] = NUM_ROWS(i,n,size);
                                  if ( i == 0 )
                                          disp_b[i] = 0 ;
                                  else
                                          disp_b[i] = disp_b[i-1] + send_count_b[i-1];
                  }
                  MPI_Scatterv(A, send_count, disp,MPI_FLOAT, A_recv,SIZE_A(n,size),MPI_FLOAT,0, MPI_COMM_WORLD);
                  MPI_Scatterv(b, send_count_b, disp_b,MPI_FLOAT, b_recv,send_count_b[rank], MPI_FLOAT, 0, MPI_COMM_WORLD);
                  for ( i = 0 ; i &lt; send_count[rank]; i++ ){
                          for ( j = 0 ; j &lt; n ; j++)
                                  printf("%3.2f : ",A_recv[i*n+j]);
                          printf("\n\n");
                  }
                  MPI_Gatherv(A_recv,SIZE_A(n,size),MPI_FLOAT,A,send_count,disp,MPI_FLOAT,0,MPI_COMM_WORLD);
                  MPI_Gatherv(b_recv,send_count_b[rank],MPI_FLOAT,b,send_count_b,disp_b,MPI_FLOAT,0,MPI_COMM_WORLD);
  }
  </code></pre>
  
  <p>please help me out.</p>