#include <stdio.h>
#include <stdlib.h>
#include "bsp1.h"
#undef NO_PRINT
#define NO_PRINT
/*
#define WRITEX(Y)   for(k=0;k<NN[i]-1;k++) { if (compare((char*)&data[d  *k],(char*)&data[d  *(k+1)])>0) printf("error %s %d\n",#Y,k);}
*/
#define WRITEX(Y)   for(k=0;k<i-1;k++) { if (compare((char*)&data[d  *k],(char*)&data[d  *(k+1)])>0) if (true==0) {printf("error %s \n",#Y);true=1;}}

int compare (byte *s1, byte *s2)
{
  return memcmp(s1, s2, SELEM) ;
}
int compare_tn (byte *s1, byte *s2, t_type n)
{
  return memcmp(s1, s2, n) ;
}


int  main(int argc, char **argv) {
  byte          *data,*data1;
  t_type        n,d,range,offset,nmsb;
  double        t1, t2             ;
  t_type        i,j,k                ;
  double        t3, t4             ;
  int   NN[4],nm[4];
  int true=0;

  bsp_begin(bsp_nprocs());
  init_params();
   if (argc==1) {
    /* String length must be set as in SELEM in bsp1.h despite the line below */
    printf("%s string-length(SELEM) nelements range(1..256) offset(0..255)\n",
            argv[0]);
    bsp_abort("Incorrect argument list\n");
   }
   else {
     d= atoi(argv[1]);
     n= atoi(argv[2]);
     range= atoi(argv[3]);
     offset= atoi(argv[4]);
   }

   d=SELEM;
   data= (byte *) malloc(d*n*sizeof(char));
   NN[0]=64; NN[1]=4096; NN[2]=262144; NN[3]=1048576;
   nm[0]=1;  nm[1]=2;    nm[2]=3; nm[3]=4;
   /**** Note that our implementation of hybridsort works for NN = (range)^nm **/
/* get_params(data,&d,&n,&range,&offset); */
  for (i = 0  ; i <IT ; i ++ ) 
   for(nmsb=(i+1);nmsb<=(i+1);nmsb++){
     t3=t4=0.0;
     genrandoms(data, d, NN[i],range,offset) ;
     t1 = bsp_time() ;
     Hybrid_Sort(data,NN[i],SELEM,nmsb,range,offset,qsort,compare,compare_tn); 
     t2 = bsp_time() ;
     t3+=(double)(t2-t1);
     WRITEX(HYBRIDSORT);
#ifndef NO_PRINT
   printrandoms (data, NN[i], d);
#endif
     printf("AVGHybrid= %12.9f secs, N = %d (%d,%d,%d,%d)\n", t3,NN[i],nmsb,range,offset,SELEM) ;
     fflush(stdout);
      
    }
bsp_end();
}
