

















#include "bsp_lib.h"
#include "bsp_contentionfree.h"
_BSPcontention contentionfree;













void config_contention_free_communication() {
  int i,j,k,size,temp;

  size = CONTENTION_FREE_INIT_FREQ + CONTENTION_FREE_RANDOM_SIZE;

  contentionfree.size=(_bsp_nprocs > 1)?(size*(_bsp_nprocs-1)):size;
  contentionfree.next=0;
  contentionfree.last=0;

  contentionfree.randoms 
    = CALLOC(contentionfree.size,SIZEOF(int));
  if (contentionfree.randoms==NULL)
    bsp_abort("{config_contention_free} failed to alloc %d bytes",
              contentionfree.size);
  
  contentionfree.size -= (_bsp_nprocs-1)*CONTENTION_FREE_INIT_FREQ;

  if (CONTENTION_REMOVAL_NONE()) {
    i=0;
    for(j=0;j<size;j++) {
      for(k=0;k<_bsp_nprocs;k++) {
        if (k!=_bsp_pid)
          contentionfree.randoms[i++]=k;
      }
    }

  } else if (CONTENTION_REMOVAL_ALL()) {
    /* generate a repetitive contention free permutation. */
    i=0;
    for(j=0;j<size;j++) {
      for(k=1;k<_bsp_nprocs;k++) {
        contentionfree.randoms[i++]=(_bsp_pid+k)%_bsp_nprocs;
      }
    }

    /* Randomise based upon different latin squares. Use a fixed seed so all
       processors generate the same sequence*/

    RAND_SEED(0);  
    for(j=0;j<size;j++) {
      for(k=0;k<(_bsp_nprocs-1);k++) {
        i = RAND() % (_bsp_nprocs-1);
        temp = contentionfree.randoms[(j*(_bsp_nprocs-1))+i];
        contentionfree.randoms[(j*(_bsp_nprocs-1))+i] 
          = contentionfree.randoms[(j*(_bsp_nprocs-1))+k];
        contentionfree.randoms[(j*(_bsp_nprocs-1))+k]=temp;
      } 
    }
  
  } else {
    bsp_abort("{config_contention_free_comm} unknown option");
  }
}






