SHARE
TWEET

Gaussian sort

a guest Jun 9th, 2011 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <thread>
  7. using namespace std;
  8. const size_t size=50000000;
  9. void s(double ** bucketsstart, double** bucketsend, int start,int delta, int numbuckets){
  10.         for (;start<=numbuckets; start+=delta)
  11.                 sort(bucketsstart[start],bucketsend[start]);
  12. }
  13. int main(){
  14.         double *data=new double[size];
  15.         FILE *f = fopen("gaussian.dat", "rb");
  16.         fread(data,8,size,f);
  17.         FILE* h=fopen("helper.txt","rb");
  18.         int numbuckets;
  19.         fread(&numbuckets,sizeof(int),1,h);
  20.         int dscale;
  21.         fread(&dscale,sizeof(int),1,h);
  22.         double dmin; //minimum value of tabularised distribution
  23.         double dmax; //,aximum ditto
  24.         fread(&dmin,sizeof(double),1,h);
  25.         fread(&dmax,sizeof(double),1,h);
  26.         //int minn=dmin*dscale;
  27.         int ile;
  28.         fread(&ile,sizeof(int),1,h);
  29.         int *tab=new int[ile];
  30.         int test=fread(tab,sizeof(int),ile,h);
  31.         if (test!=ile) cout<<"blad\n";
  32.         double * databuckets=new double [size*4+size/numbuckets*4]; //just to be sure
  33.         double ** bucketsstart= new double* [numbuckets+1];// pointer to beggining of every bucket
  34.         double ** bucketsend= new double* [numbuckets+1]; //pointer to end
  35.         int offset=4*size/numbuckets;
  36.         for (int i=0; i<numbuckets+1; i++){
  37.                 bucketsend[i]=bucketsstart[i]=databuckets+i*offset;
  38.         }
  39.         for (int i=0; i<size; i++){
  40.                 if (data[i]>dmax) {
  41.                         *bucketsend[numbuckets]++=data[i];
  42.                 }
  43.                 else if (data[i]<dmin) *bucketsend[0]++=data[i];
  44.                 else {
  45.                         *bucketsend[tab[(int)((data[i]-dmin)*dscale)]]++=data[i];
  46.                 }
  47.         }
  48.         const int numthreads=4;
  49.         thread** thr=new thread*[numthreads];
  50.         for (int i=0; i<numthreads; i++){
  51.                 thr[i] =new thread (s,bucketsstart,bucketsend,i,numthreads,numbuckets);
  52.         }
  53.         for (int i=0; i<numthreads; i++){
  54.                 thr[i]->join();
  55.                 delete thr[i];
  56.         }
  57.         delete [] thr;
  58.         double* currData=data;
  59.         for (int i=0; i<=numbuckets; i++){
  60.                 memcpy(currData,bucketsstart[i],(bucketsend[i]-bucketsstart[i])*8);
  61.                 currData+=bucketsend[i]-bucketsstart[i];
  62.         }
  63.         //for (int i=0; i<size-1; i++){
  64.         //      if (data[i]>data[i+1]) cout<<i<<' '<<data[i]<<' '<<data[i+1]<<endl;
  65.         //}
  66.  
  67.         //fwrite(data,8,size,stdout);
  68.         fclose(h);
  69.         fclose(f);
  70. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top