#include <stdio.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);}
#define Radix_Sort(src, nelems, sbytes) rsort_msb(src, nelems, sbytes, sbytes)

int compare (byte *s1, byte *s2)
{
  return memcmp(s1, s2, SELEM) ;
}
 
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];

   bsp_begin(bsp_nprocs());
   if (argc==1) {
    /* String length must be set as in SELEM */
    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;
   NN[0]=64;NN[1]=4096;NN[2]=262144;NN[3]=1048576;
   data= (byte *) malloc(d*n*sizeof(char));

   for (i = 0 ; i <IT ; i ++)
    {
     t3=t4=0.0;
     init_params();
     genrandoms(data, d, NN[i], range, offset) ;
     t1 = bsp_time() ;
     rsort_msb(data, NN[i], SELEM,SELEM);
     t2 = bsp_time() ;
     t3=(t2-t1);
#ifndef NO_PRINT
   printrandoms (data, NN[i], d);
#endif
     WRITEX(Radix1);
     printf("AvfRadix=%9.5f sec, N=%d (%d,%d,%d,%d)\n",t3,NN[i],range,offset,SELEM,SELEM);
     fflush(stdout);
#ifdef EXPERIMENT
     t3=t4=0.0;
     init_params();
     genrandoms(data, d, NN[i], range, offset) ;
     t1 = bsp_time() ;
     rsort_msb(data, NN[i], SELEM,SELEM/2);
     t2 = bsp_time() ;
     t3=(t2-t1);
#ifndef NO_PRINT
   printrandoms (data, i, d);
#endif
     WRITEX(Radix2);
     printf("AvfRadix=%9.5f sec, N=%d (%d,%d,%d,%d)\n",t3,NN[i],range,offset,SELEM/2,SELEM);
     fflush(stdout);
     t3=t4=0.0;
     init_params();
     genrandoms(data, d, NN[i], range, offset) ;
     t1 = bsp_time() ;
     rsort_msb(data, NN[i], SELEM,SELEM/4);
     t2 = bsp_time() ;
     t3=(t2-t1);
#ifndef NO_PRINT
   printrandoms (data, NN[i], d);
#endif
     WRITEX(Radix4);
     printf("AvfRadix=%9.5f sec, N=%d (%d,%d,%d,%d)\n",t3,NN[i],range,offset,SELEM/4,SELEM);
     fflush(stdout);
#endif
    }
bsp_end();
}

