title: MPI_Cart_shift not returning neighbors
body: <p>I asked a question about this project a little while ago, I've run into a new issue. When I attempt to run MPI_cart_shift to find the nearest neighbours to my processes current rank I'm not getting the right target ranks back.</p>
  
  <p>I'm attempting to follow this example code.
  <a href="http://siber.cankaya.edu.tr/ozdogan/GraduateParallelComputing.old/ceng505/node133.html" rel="nofollow">http://siber.cankaya.edu.tr/ozdogan/GraduateParallelComputing.old/ceng505/node133.html</a></p>
  
  <p>Here's my code for creating the coordinate system</p>
  
  <pre><code>MPI_Comm_rank (MPI_COMM_WORLD, &amp;rank);  /* get current process id */
  MPI_Comm_size (MPI_COMM_WORLD, &amp;size);  /* get number of processes */
  .
  .
  .
  
  if(is_perfect_square(size))  dim_size[0] = dim_size[1] = (int) sqrt(size);
  else
  { //if size = 2 then dims = 2, 1; size = 4 then 2,2; 8 = 4, 2...
      dim_size[0] = (int) sqrt(size + size);
      dim_size[1] = dim_size[0] / 2;
  }
  fprintf(stderr, "%d | %d \n", dim_size[0], dim_size[1]);
  MPI_Cart_create(MPI_COMM_WORLD, 2, dim_size, periods, 1, &amp;CannonsCart);
  MPI_Barrier(MPI_COMM_WORLD);
  .
  .
  .
  runCannon(CannonsCart, &amp;a, &amp;b, &amp;c);
  </code></pre>
  
  <p>and here's runCannon.</p>
  
  <pre><code>int runCannon( MPI_Comm comm, Matrix *a, Matrix *b, Matrix *c)
  {
  
      int size, rank, coordinates[2];
      int uprank, downrank, leftrank, rightrank;
      int shiftsource, shiftdest;
      int ix, swap;
      MPI_Status status;
  
      MPI_Comm_size(comm, &amp;size); 
      MPI_Comm_rank(comm, &amp;rank); 
      MPI_Cart_coords(comm, rank, 2, coordinates); 
  
      MPI_Cart_shift(comm, 0, -1, &amp;rightrank, &amp;leftrank); //Cart_shift is doing weird stuff that I don't understand.
      MPI_Cart_shift(comm, 1, -1, &amp;downrank, &amp;uprank); 
  
  
  
      printf("rank = %d, rightrank = %d, downrank = %d, leftrank = %d, uprank = %d\n", rank, rightrank, downrank, leftrank, uprank);
  </code></pre>
  
  <p>if I run the program with 4, 8, 16 processes respectively I get these outputs.</p>
  
  <pre><code>rank = 0, rightrank = 2, downrank = 1, leftrank = 2, uprank = 1
  rank = 1, rightrank = 3, downrank = 0, leftrank = 3, uprank = 0
  rank = 3, rightrank = 1, downrank = 2, leftrank = 1, uprank = 2
  rank = 2, rightrank = 0, downrank = 3, leftrank = 0, uprank = 3
  </code></pre>
  
  <p>0 should have rightrank = 1, downrank = 2, left rank = 1, uprank = 2, etc. I can't just swap the MPI_shift calls however because if I have 8 processes this is my output.</p>
  
  <pre><code>rank = 0, rightrank = 2, downrank = 1, leftrank = 6, uprank = 1
  rank = 1, rightrank = 3, downrank = 0, leftrank = 7, uprank = 0
  rank = 2, rightrank = 4, downrank = 3, leftrank = 0, uprank = 3
  rank = 3, rightrank = 5, downrank = 2, leftrank = 1, uprank = 2
  rank = 4, rightrank = 6, downrank = 5, leftrank = 2, uprank = 5
  rank = 5, rightrank = 7, downrank = 4, leftrank = 3, uprank = 4
  rank = 6, rightrank = 0, downrank = 7, leftrank = 4, uprank = 7
  rank = 7, rightrank = 1, downrank = 6, leftrank = 5, uprank = 6
  </code></pre>
  
  <p>0 should have rightrank = 1, downrank = 4, leftrank = 3, uprank = 4</p>
  
  <p>and here's 16 just for data's sake.</p>
  
  <pre><code>rank = 9, rightrank = 13, downrank = 10, leftrank = 5, uprank = 8
  rank = 3, rightrank = 7, downrank = 0, leftrank = 15, uprank = 2
  rank = 0, rightrank = 4, downrank = 1, leftrank = 12, uprank = 3
  rank = 2, rightrank = 6, downrank = 3, leftrank = 14, uprank = 1
  rank = 1, rightrank = 5, downrank = 2, leftrank = 13, uprank = 0
  rank = 8, rightrank = 12, downrank = 9, leftrank = 4, uprank = 11
  rank = 6, rightrank = 10, downrank = 7, leftrank = 2, uprank = 5
  rank = 7, rightrank = 11, downrank = 4, leftrank = 3, uprank = 6
  rank = 4, rightrank = 8, downrank = 5, leftrank = 0, uprank = 7
  rank = 5, rightrank = 9, downrank = 6, leftrank = 1, uprank = 4
  rank = 12, rightrank = 0, downrank = 13, leftrank = 8, uprank = 15
  rank = 14, rightrank = 2, downrank = 15, leftrank = 10, uprank = 13
  rank = 13, rightrank = 1, downrank = 14, leftrank = 9, uprank = 12
  rank = 10, rightrank = 14, downrank = 11, leftrank = 6, uprank = 9
  rank = 11, rightrank = 15, downrank = 8, leftrank = 7, uprank = 10
  rank = 15, rightrank = 3, downrank = 12, leftrank = 11, uprank = 14
  </code></pre>