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

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   ;
  double        t1, t2             ;
  t_type        i,j,k                ;
  double        t3, t4             ;
  int NN[4];
   
   bsp_begin(bsp_nprocs());
   if (argc==1) {
    /* String length must be set as in SELEM  in bsp1.h not 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;
  for (i = 0 ; i <IT; i ++)
    {
     t3=t4=0.0;
     init_params();
     genrandoms(data, d, NN[i], range, offset) ;
     t1 = bsp_time() ;      
     qsort(data, NN[i], d, (int (*)(const void *,const void*))compare) ; 
     t2 = bsp_time() ;   
     t3+=(t2-t1);
     WRITEX(Qsort);
#ifndef NO_PRINT
   printrandoms (data, NN[i], d);
#endif
     printf("QSortAv Time = %9.5f secs, N = %d (%d,%d,%d)\n", t3,NN[i],range,offset,SELEM) ;     
     fflush(stdout);
    }

bsp_end();
}
