//  printf("%s %d %d %d %d %d %d %d\n",__FILE__,__LINE__,send_counts[0],recv_counts[0],send_displs[0],recv_displs[0],sr_types[0],iodesc->llen);
 
////
/// @file pio_rearrange.c
/// @author Jim Edwards
/// @date 2014
/// @brief Code to map IO to model decomposition
///
/// 
/// 
/// 
/// @see  http://code.google.com/p/parallelio/
////
#include <pio.h>
#include <pio_internal.h>
/** internal variable used for debugging */
int tmpioproc=-1;  
/** @internal
 ** Convert an index into a list of dimensions. E.g., for index 4 into a
 ** array defined as a[3][2], will return 1 1.
 ** @endinternal
*/
void idx_to_dim_list(const int ndims, const int gdims[], const PIO_Offset idx,
                     PIO_Offset dim_list[])
{
  int i, curr_idx, next_idx;
  curr_idx = idx;
  // Easiest to start from the right and move left.
  for (i = ndims-1; i >= 0; --i) {
    // This way of doing div/mod is slightly faster than using "/" and "%".
    next_idx = curr_idx / gdims[i];
    dim_list[i] = curr_idx - (next_idx*gdims[i]);
    curr_idx = next_idx;
  }
}
/**
 ** @internal
 ** Expand a region along dimension dim, by incrementing count[i] as much as
 ** possible, consistent with the map.
 **
 ** Once max_size is reached, the map is exhausted, or the next entries fail
 ** to match, expand_region updates the count and calls itself with the next
 ** outermost dimension, until the region has been expanded as much as
 ** possible along all dimensions.
 ** @endinternal
 */
void expand_region(const int dim, const int gdims[], const int maplen,
                   const PIO_Offset map[], const int region_size,
                   const int region_stride, const int max_size[],
                   PIO_Offset count[])