title: MPI Gatherv seems to send/receive wrong values (in C)
body: <p>I have some trouble using MPI (with C). My program calculates the edge list of a huge network. Every process calculates a small portion of this edge list (root process 0 also does calculate an portion of this edge list). The edge list is a 1-D int array with 2*edge_count_slave elements. The edges are ordered by the number of the first node of the pair.
  In the end I want to combine all these small edge lists to the proper complete edge list. In the first iteration of this code I only used MPI_Send and MPI_Recv, now i switched to MPI_Gather and MPI_Gatherv, but no matter how i implemented it, I get the same bug: The master/root edge list misses some edges at the end of some (but not all) of the slave edge lists and instead has some new random edges in between. I made a lot of tests but I cant find a solution to this problem.</p>
  
  <p>Here's the source code of the crucial part:</p>
  
  <pre><code>int *edge_counts=NULL;
  int *displ=NULL;
  int *edge_master=NULL;
  int edge_count_master=0;
  
  if (rank == 0){
      edge_counts = malloc(size*sizeof(int));
      displ = malloc(size*sizeof(int));
      displ[0] = 0;
  }
  
  // first gather the number of edges of each process
  MPI_Gather(&amp;edge_count_slave,1,MPI_INT,edge_counts,1,MPI_INT,0,MPI_COMM_WORLD);
  
  // change from number of edges to number of elements in the array and calculate displacement
  if (rank == 0){
      edge_counts[0]=2*edge_counts[0];
      for (int mpi_step=1;mpi_step&lt;size;mpi_step++){
          edge_counts[mpi_step] = 2*edge_counts[mpi_step];
          displ[mpi_step]=displ[mpi_step-1]+edge_counts[mpi_step-1];
      }
      // the total number of elements over all processes
      edge_count_master = displ[size-1] + edge_counts[size-1];
  }
  
  // looking at the results I found that process 20 misses some edges, so I print parts of the edge list to check it
  if (rank==20){
      printf("checking edge list of process %i with %i elements. \n",rank,2*edge_count_slave);
      for (int istep=0;istep&lt;2*edge_count_slave;istep++){
          if ((istep % 100==0) || ((istep &lt; 100)  &amp;&amp; (istep % 2 ==0))){
              printf("pr.%i: step=%i val=%i-%i \n ",rank,istep,edge_list_slave[istep],edge_list_slave[istep+1]);
          }
      }
  }
  
  printf("pr.%i: edge_count = %i / elements = %i \n",rank,edge_count_slave,2*edge_count_slave);
  
  // allocate edge list master
  if (rank == 0){
      edge_master = (int *) malloc(edge_count_master*sizeof(int));
      printf("INFORMATION ROOT PROCESS HAS\n");
      for (int istep=0;istep&lt;size;istep++){
          printf("pr:%i: edge_counts[%i]=%i -  displ[%i]=%i\n",rank,istep,edge_counts[istep],istep,displ[istep]);
      }
  }
  
  // gather the edge lists    
  MPI_Gatherv(edge_list_slave,2*edge_count_slave,MPI_INT,edge_master,edge_counts,displ,MPI_INT,0,MPI_COMM_WORLD);
  
  if (rank==0){
      int checkpr = 20;
      printf("checking edge list of process %i with %i elements from root rank 0. \n",checkpr,edge_counts[checkpr]);
  
      for (int istep=displ[checkpr];istep&lt;displ[checkpr+1];istep++){
          if (((istep-displ[checkpr]) % 100==0) || (((istep-displ[checkpr]) &lt; 100) &amp;&amp; ((istep-displ[checkpr]) % 2 ==0))){
              printf("pr.%i: step=%i/%i val=%i-%i \n ",rank,istep,istep-displ[checkpr],edge_master[istep],edge_master[istep+1]);
          }
      }
  }
  
  free(edge_list_slave);
  free(edge_counts);
  free(displ);
  </code></pre>
  
  <p>The problem is only strongly showing when looking at big amounts of data / big networks. Here's an output example output calculated with 32 processes. I obviously left out a lot of the printed array elements. You will see that the edge counts are correctly gathered and the displacements also correctly calculated. The edge lists printed share a lot of common elements but are different.   </p>
  
  <pre><code>pr.31: edge_count = 254213 / elements = 508426 
  pr.23: edge_count = 305589 / elements = 611178 
  pr.30: edge_count = 244545 / elements = 489090 
  pr.19: edge_count = 288550 / elements = 577100 
  pr.18: edge_count = 345579 / elements = 691158 
  checking edge list of process 20 with 568698 elements. 
  pr.20: step=0 val=21860-20107 
  pr.20: step=2 val=21860-20108 
  pr.20: step=4 val=21860-20280 
  pr.20: step=6 val=21860-20281 
  pr.20: step=8 val=21860-20282 
  pr.20: step=10 val=21860-20283 
  pr.20: step=12 val=21860-20284 
  pr.20: step=14 val=21860-20285 
  pr.20: step=16 val=21860-20454 
  pr.20: step=18 val=21860-20455 
  pr.20: step=20 val=21860-20456 
  pr.20: step=22 val=21860-20457 
  pr.20: step=24 val=21860-20458 
  pr.20: step=26 val=21860-20459 
  pr.20: step=28 val=21860-20460 
  pr.20: step=30 val=21860-20461 
  pr.20: step=32 val=21860-20628 
  pr.20: step=34 val=21860-20629 
  pr.20: step=36 val=21860-20630 
  pr.20: step=38 val=21860-20631 
  pr.20: step=40 val=21860-20632 
  pr.20: step=42 val=21860-20633 
  pr.20: step=44 val=21860-20634 
  pr.20: step=46 val=21860-20635 
  pr.20: step=48 val=21860-20636 
  pr.20: step=50 val=21860-20801 
  pr.20: step=567700 val=22950-24858 
  pr.20: step=567800 val=22951-22070 
  pr.20: step=567900 val=22951-22940 
  pr.20: step=568000 val=22951-23635 
  pr.20: step=568100 val=22951-24334 
  pr.20: step=568200 val=22951-25210 
  pr.20: step=568300 val=22952-22426 
  pr.20: step=568400 val=22952-23123 
  pr.20: step=568500 val=22952-23819 
  pr.20: step=568600 val=22952-24682 
  pr.20: edge_count = 284349 / elements = 568698 
  pr.22: edge_count = 286565 / elements = 573130 
  pr.29: edge_count = 377078 / elements = 754156 
  pr.21: edge_count = 282447 / elements = 564894 
  pr.24: edge_count = 376232 / elements = 752464 
  pr.14: edge_count = 513467 / elements = 1026934 
  pr.13: edge_count = 645089 / elements = 1290178 
  pr.17: edge_count = 416474 / elements = 832948 
  pr.28: edge_count = 459254 / elements = 918508 
  pr.16: edge_count = 471214 / elements = 942428 
  pr.27: edge_count = 520485 / elements = 1040970 
  pr.15: edge_count = 500240 / elements = 1000480 
  pr.25: edge_count = 459893 / elements = 919786 
  pr.26: edge_count = 523430 / elements = 1046860 
  pr.12: edge_count = 864602 / elements = 1729204 
  pr.11: edge_count = 1150266 / elements = 2300532 
  pr.10: edge_count = 1390092 / elements = 2780184 
  pr.9: edge_count = 1737284 / elements = 3474568 
  pr.1: edge_count = 2206650 / elements = 4413300 
  pr.8: edge_count = 2046545 / elements = 4093090 
  pr.2: edge_count = 2681617 / elements = 5363234 
  pr.7: edge_count = 2398441 / elements = 4796882 
  pr.3: edge_count = 2954885 / elements = 5909770 
  pr.6: edge_count = 2684874 / elements = 5369748 
  pr.5: edge_count = 3086924 / elements = 6173848 
  pr.4: edge_count = 3250719 / elements = 6501438 
  pr.0: edge_count = 1786017 / elements = 3572034 
  INFORMATION ROOT PROCESS HAS
  pr:0: edge_counts[0]=3572034 -  displ[0]=0
  pr:0: edge_counts[1]=4413300 -  displ[1]=3572034
  pr:0: edge_counts[2]=5363234 -  displ[2]=7985334
  pr:0: edge_counts[3]=5909770 -  displ[3]=13348568
  pr:0: edge_counts[4]=6501438 -  displ[4]=19258338
  pr:0: edge_counts[5]=6173848 -  displ[5]=25759776
  pr:0: edge_counts[6]=5369748 -  displ[6]=31933624
  pr:0: edge_counts[7]=4796882 -  displ[7]=37303372
  pr:0: edge_counts[8]=4093090 -  displ[8]=42100254
  pr:0: edge_counts[9]=3474568 -  displ[9]=46193344
  pr:0: edge_counts[10]=2780184 -  displ[10]=49667912
  pr:0: edge_counts[11]=2300532 -  displ[11]=52448096
  pr:0: edge_counts[12]=1729204 -  displ[12]=54748628
  pr:0: edge_counts[13]=1290178 -  displ[13]=56477832
  pr:0: edge_counts[14]=1026934 -  displ[14]=57768010
  pr:0: edge_counts[15]=1000480 -  displ[15]=58794944
  pr:0: edge_counts[16]=942428 -  displ[16]=59795424
  pr:0: edge_counts[17]=832948 -  displ[17]=60737852
  pr:0: edge_counts[18]=691158 -  displ[18]=61570800
  pr:0: edge_counts[19]=577100 -  displ[19]=62261958
  pr:0: edge_counts[20]=568698 -  displ[20]=62839058
  pr:0: edge_counts[21]=564894 -  displ[21]=63407756
  pr:0: edge_counts[22]=573130 -  displ[22]=63972650
  pr:0: edge_counts[23]=611178 -  displ[23]=64545780
  pr:0: edge_counts[24]=752464 -  displ[24]=65156958
  pr:0: edge_counts[25]=919786 -  displ[25]=65909422
  pr:0: edge_counts[26]=1046860 -  displ[26]=66829208
  pr:0: edge_counts[27]=1040970 -  displ[27]=67876068
  pr:0: edge_counts[28]=918508 -  displ[28]=68917038
  pr:0: edge_counts[29]=754156 -  displ[29]=69835546
  pr:0: edge_counts[30]=489090 -  displ[30]=70589702
  pr:0: edge_counts[31]=508426 -  displ[31]=71078792
  checking edge list of process 20 with 568698 elements from root rank 0. 
  pr.0: step=62839058/0 val=21860-20106 
  pr.0: step=62839060/2 val=21860-20107 
  pr.0: step=62839062/4 val=21860-20280 
  pr.0: step=62839064/6 val=21860-20281 
  pr.0: step=62839066/8 val=21860-20282 
  pr.0: step=62839068/10 val=21860-20283 
  pr.0: step=62839070/12 val=21860-20454 
  pr.0: step=62839072/14 val=21860-20455 
  pr.0: step=62839074/16 val=21860-20456 
  pr.0: step=62839076/18 val=21860-20457 
  pr.0: step=62839078/20 val=21860-20458 
  pr.0: step=62839080/22 val=21860-20459 
  pr.0: step=62839082/24 val=21860-20627 
  pr.0: step=62839084/26 val=21860-20628 
  pr.0: step=62839086/28 val=21860-20629 
  pr.0: step=62839088/30 val=21860-20630 
  pr.0: step=62839090/32 val=21860-20631 
  pr.0: step=62839092/34 val=21860-20632 
  pr.0: step=62839094/36 val=21860-20633 
  pr.0: step=62839096/38 val=21860-20634 
  pr.0: step=62839098/40 val=21860-20801 
  pr.0: step=62839100/42 val=21860-20802 
  pr.0: step=62839102/44 val=21860-20803 
  pr.0: step=62839104/46 val=21860-20804 
  pr.0: step=62839106/48 val=21860-20805 
  pr.0: step=62839108/50 val=21860-20806  
  pr.0: step=63406758/567700 val=22821-22989 
  pr.0: step=63406858/567800 val=22821-23681 
  pr.0: step=63406958/567900 val=22821-24377 
  pr.0: step=63407058/568000 val=22821-25081 
  pr.0: step=63407158/568100 val=22822-21945 
  pr.0: step=63407258/568200 val=22822-22809 
  pr.0: step=63407358/568300 val=22822-23338 
  pr.0: step=63407458/568400 val=22822-24028 
  pr.0: step=63407558/568500 val=22822-24725 
  pr.0: step=63407658/568600 val=22822-25601 
  </code></pre>
  
  <p>I really appreciate any help you can provide.</p>