int main(int argc, char **argv){ unsigned int runs = 400; seed_rand(); std::vector<double> xvalues(runs), yvalues(runs), zvalues(runs); for ( unsigned int i = 0; i < runs ; i++ ) { xvalues[i] = 1.0 * ((double) rand() - (double)RAND_MAX /2.0) /(double)RAND_MAX; yvalues[i] = 1.0 * ((double) rand() - (double)RAND_MAX /2.0) /(double)RAND_MAX; zvalues[i] = 1.0 * ((double) rand() - (double)RAND_MAX /2.0) /(double)RAND_MAX; } for ( unsigned int i = 0; i < runs ; i++ ) { btMatrix3x3 mat; btQuaternion q_baseline(xvalues[i],yvalues[i],zvalues[i]); mat.setRotation(q_baseline); btQuaternion q_from_m; mat.getRotation(q_from_m); std::printf("%f, angle between quaternions\n", q_from_m.angle(q_baseline)); } return 0; }
static void test_part(){/**Compute the covariance of 4 points with various separation.*/ rand_t rstat; int seed=4; double r0=0.2; double dx=1./64.; long N=1+2; long ofx=0; long ofy=0; long nx=N; long ny=N; long nframe=1000000; seed_rand(&rstat, seed); map_t *atm=mapnew(nx, ny, dx,dx, NULL); dmat *vec=dnew(4,1); dmat *cov=NULL; dmat* pp=(dmat*)atm; for(long i=0; i<nframe; i++){ info("%ld of %ld\n", i, nframe); for(long j=0; j<nx*ny; j++){ atm->p[j]=randn(&rstat); } fractal_do((dmat*)atm, dx, r0,L0,ninit); vec->p[0]=IND(pp,ofx+0,ofy+0); vec->p[1]=IND(pp,ofx+1,ofy+0); vec->p[2]=IND(pp,ofx+0,ofy+1); vec->p[3]=IND(pp,ofx+1,ofy+1); dmm(&cov, 1, vec, vec, "nt", 1); } dscale(cov, 1./nframe); writebin(cov,"cov.bin"); }
int main(){ int N=4; dmat *A=dnew(N,N); dcell *B=cellnew(2,2); B->p[0]=dnew(N,N); B->p[2]=dnew(N,N); rand_t rstat; seed_rand(&rstat, 1); drandn(A, 1, &rstat); drandn(B->p[0], 1, &rstat); drandn(B->p[2], 1, &rstat); A->header=strdup("dx=1/64\ndy=1/64\nLong=kaflasdjfl ksflksjlfjasdflkj asldfj als fals fl asfjalsdf jalsf djasldf jal fsalfkjasdflkj asldfkj asldf \nadf\nwvf=1.e-4"); writebin(A, "A.bin"); writebin(A, "A.fits"); writebin(A, "A.fits.gz"); writebin(B, "B.bin"); writebin(B, "B.fits"); dmat *C=dread("A.fits"); writebin(C, "C.fits"); dcell *D=dcellread("B.fits"); writebin(D, "D.fits"); dcell *E=dcellread("A.fits"); writebin(E, "E.fits"); writebin(E, "E.fits.gz"); }
int main(int argv, char **argc) { Vi2c* V = new Vi2c; v->scl = 1; v->sca = 1; int byte_in, bit_out; long int clock = 0; seed_rand(); printf("%d\n", rand()); while(true) { switch(clock%8) { case 0 : byte_in = read_bit_vector(); V->nois = rand()&15; V->in = byte_in; break; case 1 : case 2 : break; case 3 : V->clk = 1; break; case 4 : case 5 : break; case 6 : V->clk = 0; break; case 7 : bit_out = V->out; putc(bit_out + '0', stdout); break; } V->eval(); if(byte_in == EOF) {break;} clock++; } delete V; exit(0); }
int main(int argc, char *argv[]) { int i, j; QDP_initialize(&argc, &argv); QDP_profcontrol(0); seed = time(NULL); j = 0; for(i=1; i<argc; i++) { switch(argv[i][0]) { case 'c' : cgtype=atoi(&argv[i][1]); break; case 'k' : kappa=atof(&argv[i][1]); break; case 'n' : nit=atoi(&argv[i][1]); break; case 's' : seed=atoi(&argv[i][1]); break; case 'S' : style=atoi(&argv[i][1]); break; case 'x' : j=i; while((i+1<argc)&&(isdigit(argv[i+1][0]))) ++i; break; default : usage(argv[0]); } } lattice_size = (int *) malloc(ndim*sizeof(int)); if(j==0) { for(i=0; i<ndim; ++i) lattice_size[i] = 8; } else { if(!isdigit(argv[j][1])) usage(argv[0]); lattice_size[0] = atoi(&argv[j][1]); for(i=1; i<ndim; ++i) { if((++j<argc)&&(isdigit(argv[j][0]))) { lattice_size[i] = atoi(&argv[j][0]); } else { lattice_size[i] = lattice_size[i-1]; } } } if(QDP_this_node==0) { printf("size = %i", lattice_size[0]); for(i=1; i<ndim; i++) { printf(" %i", lattice_size[i]); } printf("\n"); printf("kappa = %g\n", kappa); printf("seed = %i\n", seed); } QDP_set_latsize(ndim, lattice_size); QDP_create_layout(); rs = QDP_create_S(); seed_rand(rs, seed); start(); QDP_finalize(); return 0; }
int init_spinlock_local(uint32_t thread_num, uint32_t* limit) { //assign the thread to the correct core set_cpu(thread_num); *limit = 1; spinlock_seeds = seed_rand(); MEM_BARRIER; return 0; }
static void test_ints(){ rand_t init; seed_rand(&init,1); const int nwfs=6; lrand(&init);/*atm */ rand_t wfs_rand[nwfs]; for(int iwfs=0; iwfs<nwfs; iwfs++){ seed_rand(wfs_rand+iwfs,lrand(&init)); } dcell *mtche=dcellread("powfs0_mtche.bin"); int nsim=500; int nsa=2582; dmat *nea=dnew(nsim,nsa*2); double(*pnea)[nsim]=(void*)nea->p; double rne=3; double bkgrnd=0; double siglev=1000; for(int iwfs=0; iwfs<nwfs; iwfs++){ for(int isim=0; isim<nsim; isim++){ dbg("iwfs %d isim=%d\n",iwfs,isim); /*dcell *ints=dcellread("ints_%d_wfs%d.bin",isim,iwfs); */ dcell *ints=dcellread("ints_%d_wfs%d.bin",isim,iwfs); /*dcell *i0=dcellread("powfs0_i0.bin"); */ dmat *im=NULL, *imy=NULL; double gnf[2], gny[2]; for(int isa=0; isa<nsa; isa++){ dcp(&im,ints->p[isa]); dscale(im,siglev); gnf[0]=0; gnf[1]=0; dmulvec(gnf, mtche->p[isa], im->p,1.); gny[0]=0; gny[1]=0; dcp(&imy, im); addnoise(imy, &wfs_rand[iwfs], bkgrnd, bkgrnd, 0,0,rne); dmulvec(gny, mtche->p[isa], imy->p,1.); P(pnea,isim,isa)=gny[0]-gnf[0]; P(pnea,isim,isa+nsa)=gny[1]-gnf[1]; } } writebin(nea,"test_sanea_wfs%d.bin",iwfs); } }
static void test_cov(){/*not good */ rand_t rstat; int seed=4; double r0=0.2; double dx=1./64; long N=1+1024; long nx=N; long ny=N; long nframe=1; seed_rand(&rstat, seed); map_t *atm=mapnew(nx, ny, dx,dx, NULL); cmat *atmhat=cnew((N+1)*3,(N+1)*3); dmat *atmhattot=dnew((N+1)*3,(N+1)*3); //cfft2plan(atmhat,-1); //cfft2plan(atmhat, 1); dset((dmat*)atm,1); cembedd(atmhat, (dmat*)atm, 0); cfft2(atmhat, -1); cabs22d(&atmhattot, 1, atmhat, 1); ccpd(&atmhat, atmhattot); cfft2i(atmhat, 1); cfftshift(atmhat); dmat *denom=dnew((N+1)*3,(N+1)*3); dmat *cov=dnew((N+1)*3,(N+1)*3); creal2d(&denom, 0, atmhat, 1); writebin(denom, "denom.bin"); dzero(atmhattot); for(long i=0; i<nframe; i++){ info("%ld of %ld\n", i, nframe); for(long j=0; j<nx*ny; j++){ atm->p[j]=randn(&rstat); } fractal_do((dmat*)atm, dx, r0,L0,ninit); /*mapwrite(atm, "atm_%ld.bin", i); */ cembedd(atmhat, (dmat*)atm, 0); cfft2(atmhat, -1); cabs22d(&atmhattot, 1, atmhat, 1); if(i==0 || (i+1)%10==0){ dscale(atmhattot, 1./(i+1)); ccpd(&atmhat, atmhattot); writebin(atmhattot, "atm_psf_%ld.bin",i+1); cfft2i(atmhat, 1); cfftshift(atmhat); creal2d(&cov, 0, atmhat, 1); for(long k=0; k<cov->nx*cov->ny; k++){ cov->p[k]/=denom->p[k]; } writebin(cov, "atm_cov_%ld.bin",i+1); } } }
void *test(void *data) { int rand_max; thread_data_t *d = (thread_data_t *)data; unsigned short seed[3]; seeds = seed_rand(); //#ifdef __sparc__ phys_id = the_cores[d->id]; cluster_id = get_cluster(phys_id); //#else // phys_id = d->id; //#endif /* Initialize seed (use rand48 as rand is poor) */ seed[0] = (unsigned short)rand_r(&d->seed); seed[1] = (unsigned short)rand_r(&d->seed); seed[2] = (unsigned short)rand_r(&d->seed); rand_max = d->bank->size - 1; /* local initialization of locks */ local_th_data[d->id] = init_lock_array_local(phys_id, d->bank->size, the_locks); /* Wait on barrier */ barrier_cross(d->barrier); while (stop == 0) { uint32_t nb = (uint32_t) (my_random(&(seeds[0]),&(seeds[1]),&(seeds[2])) % 100); uint32_t acc = (uint32_t) my_random(&(seeds[0]),&(seeds[1]),&(seeds[2])) & rand_max; account_t* accp = &d->bank->accounts[acc]; if (nb < d->deposit_perc) { deposit(accp, 1, d->id); d->nb_deposit++; } else if (nb < d->withdraw_perc) { withdraw(accp, 1, d->id); d->nb_withdraw++; } else /* nb < balance_perc */ { check(accp, d->id); d->nb_balance++; } } /* Free locks */ //free_local(local_th_data[d->id], d->bank->size); return NULL; }
uint32_t* init_rw_ttas_array_local(uint32_t thread_num, uint32_t size){ set_cpu(thread_num); rw_seeds = seed_rand(); uint32_t* limits; limits = (uint32_t*)malloc(size * sizeof(uint32_t)); uint32_t i; for (i = 0; i < size; i++) { limits[i]=1; } return limits; }
/** * Seed a random number. * @param arg eh? * @return Void... Nothing returned. */ void seed (char *arg) { if (arg == NULL) seed_rand (); else if (arg[0] == '.') old_seed (); else srand (atoi (arg)); return; }
uint32_t* init_ttas_array_local(uint32_t thread_num, uint32_t size){ //assign the thread to the correct core set_cpu(thread_num); ttas_seeds = seed_rand(); uint32_t* limits; limits = (uint32_t*)malloc(size * sizeof(uint32_t)); uint32_t i; for (i = 0; i < size; i++) { limits[i]=1; } return limits; }
bool fpt(unsigned long long n, unsigned long long k) { if(n == 2 || n == 3 ) return true; if(n < 2 || (n != 2 && n%2==0) ) return false; seed_rand(); for(int i=0; i<k; i++) { unsigned long long a = (rand()%(n-1))+1; if(congruent_mod(a,n-1,n)!=1) return false; } return true; }
/** test type I/II filter with ideal measurement to make sure it is implemented correctly. */ dmat* servo_test(dmat *input, double dt, int dtrat, dmat *sigma2n, dmat *gain){ if(input->ny==1){/*single mode. each column is for a mode.*/ input->ny=input->nx; input->nx=1; } int nmod=input->nx; PDMAT(input,pinput); dmat *merr=dnew(nmod,1); dcell *mreal=cellnew(1,1); dmat *mres=dnew(nmod,input->ny); dmat *sigman=NULL; if(dnorm(sigma2n)>0){ sigman=dchol(sigma2n); } dcell *meas=cellnew(1,1); dmat *noise=dnew(nmod, 1); SERVO_T *st2t=servo_new(NULL, NULL, 0, dt*dtrat, gain); rand_t rstat; seed_rand(&rstat, 1); PDMAT(mres,pmres); /*two step delay is ensured with the order of using, copy, acc*/ for(int istep=0; istep<input->ny; istep++){ memcpy(merr->p, pinput[istep], nmod*sizeof(double)); dadd(&merr, 1, mreal->p[0], -1); memcpy(pmres[istep],merr->p,sizeof(double)*nmod); if(istep % dtrat == 0){ dzero(meas->p[0]); } dadd(&meas->p[0], 1, merr, 1);/*average the error. */ dcellcp(&mreal, st2t->mint->p[0]); if((istep+1) % dtrat == 0){ if(dtrat!=1) dscale(meas->p[0], 1./dtrat); if(sigman){ drandn(noise, 1, &rstat); if(sigman->nx>0){ dmm(&meas->p[0], 1, sigman, noise, "nn", 1); }else{ dadd(&meas->p[0], 1, noise, sigman->p[0]); } } servo_filter(st2t, meas); } } dfree(sigman); dfree(merr); dcellfree(mreal); dcellfree(meas); servo_free(st2t); return mres; }
void *test(void *data) { int rand_max; thread_data_t *d = (thread_data_t *)data; uint64_t res; phys_id= the_cores[d->id]; set_cpu(phys_id); seeds = seed_rand(); rand_max = num_entries - 1; /* Init of local data if necessary */ /* Wait on barrier */ barrier_cross(d->barrier); int entry; while (stop == 0) { if (num_entries==1) { entry=0; } else { entry =(int) my_random(&(seeds[0]),&(seeds[1]),&(seeds[2])) & rand_max; } // entry = (int)(erand48(seed) * rand_max) + rand_min; #ifdef TEST_CAS do { res = CAS_U8(&(the_data[entry].data),0,1); } while(res!=0); #elif defined(TEST_TAS) do { res = TAS_U8(&(the_data[entry].data)); } while (res!=0); #elif defined(TEST_FAI) FAI_U8(&(the_data[entry].data)); #else perror("No test primitive specified"); #endif MEM_BARRIER; the_data[entry].data = 0; d->num_operations++; if (op_pause>0) { cpause(op_pause); } } /* Free any local data if necessary */ return NULL; }
static void test_accuracy(){ rand_t rstat; int seed=4; double r0=0.2; double dx=1./64.; long N=1+64; long nx=N; long ny=N; seed_rand(&rstat, seed); map_t *atm=mapnew(nx, ny, dx, dx,NULL); for(long i=0; i<nx*ny; i++){ atm->p[i]=randn(&rstat); } map_t *atm2=mapnew(nx, ny, dx, dx,NULL); for(long i=0; i<nx*ny; i++){ atm2->p[i]=randn(&rstat); } mapwrite(atm, "atm_rand.bin"); mapwrite(atm2, "atm2_rand.bin"); fractal_do((dmat*)atm, dx, r0,L0,ninit); mapwrite(atm, "atm_frac.bin"); fractal_inv((dmat*)atm, dx, r0,L0,ninit); mapwrite(atm, "atm_frac_inv.bin"); fractal_trans((dmat*)atm2, dx, r0,L0,ninit); mapwrite(atm2, "atm2_frac_trans.bin"); fractal_inv_trans((dmat*)atm2, dx, r0,L0,ninit); mapwrite(atm2, "atm2_frac_inv_trans.bin"); /* atm2 is u, atm is v, now comparing u'Av against v'A'u. they should be the same. A^-1Av should be the same as v A'^-1A'u should be the same as u. */ dmat *u=dread("atm2_rand.bin"); dmat *Atu=dread("atm2_frac_trans.bin"); dmat *Av=dread("atm_frac.bin"); dmat *v=dread("atm_rand.bin"); double dot1=dotdbl(u->p, Av->p, NULL, nx*ny); double dot2=dotdbl(v->p, Atu->p, NULL, nx*ny); info("dot1=%g, dot2=%g, diff=%g\n", dot1, dot2, dot1-dot2); dmat *u2=dread("atm2_frac_inv_trans.bin"); dmat *v2=dread("atm_frac_inv.bin"); double d1=ddiff(u, u2); double d2=ddiff(v, v2); info("d1=%g, d2=%g\n", d1, d2); }
void SprayList::init_thread(const size_t nthreads) { if (!initialized) { seeds = seed_rand(); ssalloc_init(); d = new thread_data_t; d->seed = rand(); d->seed2 = rand(); initialized = true; } d->nb_threads = nthreads; }
/* static int test_fft_speed_small(){ int nis=128; int *is=mycalloc(nis,int); dmat *tim=dnew(nis,1); for(int ii=0; ii<nis; ii++){ is[ii]=ii+1; } ccell *ac=cellnew(nis,1); rand_t stat; seed_rand(&stat,1); for(int ii=0; ii<nis; ii++){ ac->p[ii]=cnew(is[ii],is[ii]); //cfft2plan(ac->p[ii],-1); crandn(ac->p[ii],20,&stat); } TIC; for(int ii=0; ii<nis; ii++){ info2("size %4d: ",is[ii]); tic; for(int i=0; i<1000; i++){ cfft2(ac->p[ii],-1); } toc2("fft"); tim->p[ii]=toc3; } writebin(tim,"fft_timing"); } static void test_fft_speed(){ int nis=2048; int *is=mycalloc(nis,int); dmat *tim=dnew(nis,1); for(int ii=0; ii<nis; ii++){ is[ii]=(ii+1)*2; } ccell *ac=cellnew(nis,1); rand_t stat; seed_rand(&stat,1); TIC; for(int ii=0; ii<nis; ii++){ info2("size %4d: ",is[ii]); tic; ac->p[ii]=cnew(is[ii],is[ii]); //cfft2plan(ac->p[ii],-1); crandn(ac->p[ii],20,&stat); toc("plan"); } toc("plan"); for(int ii=0; ii<nis; ii++){ info2("size %4d: ",is[ii]); tic; int nrepeat; if(is[ii]<300) nrepeat=100; else if(is[ii]<1000) nrepeat=10; else nrepeat=1; for(int i=0; i<nrepeat; i++){ cfft2(ac->p[ii],-1); } toc2("fft"); tim->p[ii]=toc3/nrepeat; } writebin(tim,"fft_timing"); }*/ static void test_fft_special(){ int nis=2; int *is=mycalloc(nis,int); dmat *tim=dnew(nis,1); is[0]=3824; is[1]=4096; ccell *ac=ccellnew(nis,1); rand_t rstat; seed_rand(&rstat,1); TIC; for(int ii=0; ii<nis; ii++){ info2("size %4d: ",is[ii]); tic; ac->p[ii]=cnew(is[ii],is[ii]); //cfft2plan(ac->p[ii],-1); //cfft2partialplan(ac->p[ii],512,-1); crandn(ac->p[ii],20,&rstat); toc("plan"); } for(int ii=0; ii<nis; ii++){ info2("size %4d: ",is[ii]); tic; int nrepeat; if(is[ii]<300) nrepeat=100; else if(is[ii]<1000) nrepeat=10; else nrepeat=4; for(int i=0; i<nrepeat; i++){ cfft2(ac->p[ii],-1); } toc2("fft"); for(int i=0; i<nrepeat; i++){ cfft2partial(ac->p[ii],512,-1); } toc2("fft2partial"); tim->p[ii]=toc3/nrepeat; } writebin(tim,"fft_timing"); }
int main(int argc, char* argv[]){ /*dsp *RLMc1=dspread("RLMc_old.bin"); */ if(argc!=2){ error("Need 1 argument\n"); } dspcell *RLM=dspcellread("%s",argv[1]); dsp *RLMc=dspcell2sp(RLM); tic;info2("chol ..."); spchol *R1=chol_factorize(RLMc); toc("done"); rand_t rstat; seed_rand(&rstat,1); dmat *y=dnew(RLMc->m, 1); drandn(y, 1, &rstat); dmat *x=NULL, *x2=NULL, *x3=NULL; chol_convert(R1, 1); tic; chol_solve(&x, R1, y); toc("cholmod");tic; chol_solve(&x, R1, y); toc("cholmod");tic; chol_solve_upper(&x3, R1, y); toc("upper");tic; chol_solve_upper(&x3, R1, y); toc("upper");tic; chol_solve_lower(&x2, R1,y); toc("lower");tic; chol_solve_lower(&x2, R1,y); toc("lower");tic; chol_solve(&x, R1, y); toc("cholmod");tic; chol_solve(&x, R1, y); toc("cholmod");tic; writebin(y,"y"); writebin(x,"x"); writebin(x2,"x2"); writebin(x3,"x3"); chol_free(R1); dspfree(RLMc); dspcellfree(RLM); }
int main(int argc, char **argv){ unsigned int runs = 400; seed_rand(); std::vector<double> xvalues(runs), yvalues(runs), zvalues(runs); for ( unsigned int i = 0; i < runs ; i++ ) { xvalues[i] = 1.0 * ((double) rand() - (double)RAND_MAX /2.0) /(double)RAND_MAX; yvalues[i] = 1.0 * ((double) rand() - (double)RAND_MAX /2.0) /(double)RAND_MAX; zvalues[i] = 1.0 * ((double) rand() - (double)RAND_MAX /2.0) /(double)RAND_MAX; } //Useful Operator Overload for ( unsigned int i = 0; i < runs ; i++ ) { btTransform transform(btQuaternion(0,0,0), btVector3(xvalues[i],yvalues[i],zvalues[i])); btQuaternion initial(xvalues[i],yvalues[i],zvalues[i]); btQuaternion final(xvalues[i],yvalues[i],zvalues[i]); final = transform * initial; std::printf("Useful Operator Overload: %f, angle between quaternions\n", initial.angle(final)); }
TEST(Bullet, Slerp) { unsigned int runs = 100; seed_rand(); btQuaternion q1, q2; q1.setEuler(0,0,0); for (unsigned int i = 0 ; i < runs ; i++) { q2.setEuler(1.0 * ((double) rand() - (double)RAND_MAX /2.0) /(double)RAND_MAX, 1.0 * ((double) rand() - (double)RAND_MAX /2.0) /(double)RAND_MAX, 1.0 * ((double) rand() - (double)RAND_MAX /2.0) /(double)RAND_MAX); btQuaternion q3 = slerp(q1,q2,0.5); EXPECT_NEAR(q3.angle(q1), q2.angle(q3), 1e-5); } }
bool fpt_p(unsigned long long n, unsigned long long k) { if(n == 2 || n == 3 ) return true; if(n < 2 || (n != 2 && n%2==0) ) return false; seed_rand(); bool ans = true; #pragma omp parallel for for(int i=0; i<k; i++) { #pragma omp flush (ans) if(ans) { unsigned long long a = (rand()%(n-1))+1; if(congruent_mod(a,n-1,n)!=1) { ans=false; #pragma omp flush (ans) } } } return ans; }
static void test_corner(){/*Compute the covariance of 4 corner points*/ rand_t rstat; int seed=4; double r0=0.2; double dx=32; long N=1+1; long nx=N; long ny=N; long nframe=1000000; seed_rand(&rstat, seed); map_t *atm=mapnew(nx, ny, dx, dx,NULL); dmat *vec=dref_reshape((dmat*)atm, N*N, 1); dmat *cov=NULL; for(long i=0; i<nframe; i++){ info("%ld of %ld\n", i, nframe); for(long j=0; j<nx*ny; j++){ atm->p[j]=randn(&rstat); } fractal_do((dmat*)atm, dx, r0,L0,ninit); dmm(&cov, 1, vec, vec, "nt", 1); } dscale(cov, 1./nframe); writebin(cov,"cov.bin"); }
uint32_t init_rw_ttas_local(uint32_t thread_num){ set_cpu(thread_num); rw_seeds = seed_rand(); return 1; }
/* Threads will start here */ int worker_listen(worker *worker) { int i, b; monitor *read_mon; /* temp monitor storage */ monitor *write_mon; int in_file = -1; int out_file = -1; /* the files */ char *in_buffer, *out_buffer; /* i/o buffers */ job *read_msg, *write_msg; /* temp job storage */ int res = -1; int what_disc = 0; int what_block = 0; double rand_result; struct drand48_data work_space; #if 0 char in_buff1[BLOCK_SIZE]; /* read-ahead buffers */ char in_buff2[BLOCK_SIZE]; char in_buff3[BLOCK_SIZE]; char in_buff4[BLOCK_SIZE]; char out_buff1[BLOCK_SIZE]; /* write-behind buffers */ char out_buff2[BLOCK_SIZE]; char out_buff3[BLOCK_SIZE]; char out_buff4[BLOCK_SIZE]; #endif seed_rand(&work_space); /* Get in/out file for each repetition desired */ for (i = 0; i < worker->repetition; i++) { in_file = -1; out_file= -1; while (out_file == in_file) { drand48_r(&work_space, &rand_result); in_file = (int)(rand_result * 1000); drand48_r(&work_space, &rand_result); out_file = (int)(rand_result * 1000); } // make made up buffers in_buffer = emalloc(BLOCK_SIZE * sizeof(char)); memset(in_buffer, 0, BLOCK_SIZE); out_buffer = emalloc(BLOCK_SIZE * sizeof (char)); memset(out_buffer, 0, BLOCK_SIZE); for (b=0; b < BLOCKS_PER_FILE; b++) { /* Create read monitor */ read_mon = emalloc(sizeof(monitor)); read_mon->buffer = in_buffer; read_mon->request_time = worker->time; /* Create read job. mainly used for original implementation */ read_msg = emalloc(sizeof(job)); read_msg->message = READ; read_msg->communication_monitor = read_mon; /* Create write monitor */ write_mon = emalloc(sizeof(monitor)); write_mon->buffer = out_buffer; write_mon->request_time = worker->time; /* Create write job. mainly used for original implementation */ write_msg = emalloc(sizeof(job)); write_msg->message = WRITE; write_msg->communication_monitor = write_mon; compute_physical_address(in_file, b, &what_disc, &what_block, worker->number_of_discs); read_mon->block_number = what_block; compute_physical_address(out_file, b, &what_disc, &what_block, worker->number_of_discs); write_mon->block_number = what_block; if (in_file < out_file) { // read pthread_mutex_lock(&worker->all_discs[what_disc].read_lock); cbuffer_add(read_msg,&worker->all_discs[what_disc].read_cbuf); pthread_mutex_unlock(&worker->all_discs[what_disc].read_lock); // write pthread_mutex_lock(&worker->all_discs[what_disc].write_lock); cbuffer_add(write_msg,&worker->all_discs[what_disc].write_cbuf); pthread_mutex_unlock(&worker->all_discs[what_disc].write_lock); } else { // write pthread_mutex_lock(&worker->all_discs[what_disc].write_lock); while (is_cb_full(&worker->all_discs[what_disc].write_cbuf) == 0) { sched_yield(); } cbuffer_add(write_msg,&worker->all_discs[what_disc].write_cbuf); pthread_mutex_unlock(&worker->all_discs[what_disc].write_lock); // read pthread_mutex_lock(&worker->all_discs[what_disc].read_lock); cbuffer_add(read_msg,&worker->all_discs[what_disc].read_cbuf); pthread_mutex_unlock(&worker->all_discs[what_disc].read_lock); } } printf("Sent write requests for file: %d\n",out_file); printf("Sent read requests for file: %d\n",in_file); // TODO TODO TODO - maybe not the below solution. may have to do it for each block // checks replies after all blocks for the file have been queued // Figured its better to send the requests to disc first as it is the choke point // check reply queue and read replies here } printf("Worker took: %ld\n", worker->time); // printf("Worker took %ld\n", ); return 0; }
int main(int argc, char **argv) { set_cpu(the_cores[0]); ssalloc_init(); seeds = seed_rand(); #ifdef PAPI if (PAPI_VER_CURRENT != PAPI_library_init(PAPI_VER_CURRENT)) { printf("PAPI_library_init error.\n"); return 0; } else { printf("PAPI_library_init success.\n"); } if (PAPI_OK != PAPI_query_event(PAPI_L1_DCM)) { printf("Cannot count PAPI_L1_DCM."); } printf("PAPI_query_event: PAPI_L1_DCM OK.\n"); if (PAPI_OK != PAPI_query_event(PAPI_L2_DCM)) { printf("Cannot count PAPI_L2_DCM."); } printf("PAPI_query_event: PAPI_L2_DCM OK.\n"); #endif struct option long_options[] = { // These options don't set a flag {"help", no_argument, NULL, 'h'}, {"duration", required_argument, NULL, 'd'}, {"priority-queue", required_argument, NULL, 'p'}, {"linden", required_argument, NULL, 'L'}, {"spray-list", required_argument, NULL, 'l'}, {"event-simulator", required_argument, NULL, 'e'}, {"initial-size", required_argument, NULL, 'i'}, {"num-threads", required_argument, NULL, 'n'}, {"range", required_argument, NULL, 'r'}, {"seed", required_argument, NULL, 's'}, {"update-rate", required_argument, NULL, 'u'}, {"elasticity", required_argument, NULL, 'x'}, {"nothing", required_argument, NULL, 'l'}, {NULL, 0, NULL, 0} }; sl_intset_t *set; pq_t *linden_set; int i, c, size; val_t last = 0; val_t val = 0; pval_t pval = 0; unsigned long reads, effreads, updates, collisions, effupds, aborts, aborts_locked_read, aborts_locked_write, aborts_validate_read, aborts_validate_write, aborts_validate_commit, add, added, remove, removed, aborts_invalid_memory, aborts_double_write, max_retries, failures_because_contention, depdist; thread_data_t *data; pthread_t *threads; pthread_attr_t attr; barrier_t barrier; struct timeval start, end; struct timespec timeout; int duration = DEFAULT_DURATION; int initial = DEFAULT_INITIAL; int nb_threads = DEFAULT_NB_THREADS; long range = DEFAULT_RANGE; int seed = DEFAULT_SEED; int seed2 = DEFAULT_SEED; int update = DEFAULT_UPDATE; int unit_tx = DEFAULT_ELASTICITY; int alternate = DEFAULT_ALTERNATE; int pq = DEFAULT_PQ; int sl = DEFAULT_SL; int es = DEFAULT_ES; int lin = DEFAULT_LIN; int effective = DEFAULT_EFFECTIVE; sigset_t block_set; while(1) { i = 0; c = getopt_long(argc, argv, "hAplLe:f:d:i:n:r:s:u:x:l:", long_options, &i); if(c == -1) break; if(c == 0 && long_options[i].flag == 0) c = long_options[i].val; switch(c) { case 0: break; case 'h': printf("intset -- STM stress test " "(skip list)\n" "\n" "Usage:\n" " intset [options...]\n" "\n" "Options:\n" " -h, --help\n" " Print this message\n" " -A, --Alternate\n" " Consecutive insert/remove target the same value\n" " -l, --spray-list\n" " Remove via delete_min operations using a spray list\n" " -p, --priority-queue\n" " Remove via delete_min operations using a skip list\n" " -e, --event-simulator\n" " Descrete event simulator experiment, parameter = dependency distance\n" " -L, --linden\n" " Use Linden's priority queue\n" " -f, --effective <int>\n" " update txs must effectively write (0=trial, 1=effective, default=" XSTR(DEFAULT_EFFECTIVE) ")\n" " -d, --duration <int>\n" " Test duration in milliseconds (0=infinite, default=" XSTR(DEFAULT_DURATION) ")\n" " -i, --initial-size <int>\n" " Number of elements to insert before test (default=" XSTR(DEFAULT_INITIAL) ")\n" " -n, --num-threads <int>\n" " Number of threads (default=" XSTR(DEFAULT_NB_THREADS) ")\n" " -r, --range <int>\n" " Range of integer values inserted in set (default=" XSTR(DEFAULT_RANGE) ")\n" " -s, --seed <int>\n" " RNG seed (0=time-based, default=" XSTR(DEFAULT_SEED) ")\n" " -u, --update-rate <int>\n" " Percentage of update transactions (default=" XSTR(DEFAULT_UPDATE) ")\n" " -x, --elasticity (default=4)\n" " Use elastic transactions\n" " 0 = non-protected,\n" " 1 = normal transaction,\n" " 2 = read elastic-tx,\n" " 3 = read/add elastic-tx,\n" " 4 = read/add/rem elastic-tx,\n" " 5 = fraser lock-free\n" ); exit(0); case 'A': alternate = 1; break; case 'l': sl = 1; break; case 'p': pq = 1; break; case 'e': es = 1; depdist = atoi(optarg); break; case 'L': lin = 1; break; case 'f': effective = atoi(optarg); break; case 'd': duration = atoi(optarg); break; case 'i': initial = atoi(optarg); break; case 'n': nb_threads = atoi(optarg); break; case 'r': range = atol(optarg); break; case 's': seed = atoi(optarg); break; case 'u': update = atoi(optarg); break; case 'x': unit_tx = atoi(optarg); break; case '?': printf("Use -h or --help for help\n"); exit(0); default: exit(1); } } assert(duration >= 0); assert(initial >= 0); assert(nb_threads > 0); assert(range > 0); assert(update >= 0 && update <= 100); // if (range < initial) // { range = 100000000; // } printf("Set type : skip list\n"); printf("Duration : %d\n", duration); printf("Initial size : %u\n", initial); printf("Nb threads : %d\n", nb_threads); printf("Value range : %ld\n", range); printf("Seed : %d\n", seed); printf("Update rate : %d\n", update); printf("Elasticity : %d\n", unit_tx); printf("Alternate : %d\n", alternate); printf("Priority Q : %d\n", pq); printf("Spray List : %d\n", sl); printf("Linden : %d\n", lin); printf("Efffective : %d\n", effective); printf("Type sizes : int=%d/long=%d/ptr=%d/word=%d\n", (int)sizeof(int), (int)sizeof(long), (int)sizeof(void *), (int)sizeof(uintptr_t)); timeout.tv_sec = duration / 1000; timeout.tv_nsec = (duration % 1000) * 1000000; if ((data = (thread_data_t *)malloc(nb_threads * sizeof(thread_data_t))) == NULL) { perror("malloc"); exit(1); } if ((threads = (pthread_t *)malloc(nb_threads * sizeof(pthread_t))) == NULL) { perror("malloc"); exit(1); } if (seed == 0) srand((int)time(0)); else srand(seed); *levelmax = floor_log_2((unsigned int) initial); set = sl_set_new(); /* stop = 0; */ *running = 1; // Init STM printf("Initializing STM\n"); TM_STARTUP(); // Populate set printf("Adding %d entries to set\n", initial); i = 0; if (lin) { int offset = 32; // not sure what this does _init_gc_subsystem(); linden_set = pq_init(offset); } if (es) { // event simulator has event ids 1..m // no timeout in ES, finishes when list is empty // timeout.tv_sec = 0; // timeout.tv_nsec = 0; if ((nb_deps = (int *)malloc(initial * sizeof(int))) == NULL) { perror("malloc"); exit(1); } if ((deps = (val_t **)malloc(initial * sizeof(val_t*))) == NULL) { perror("malloc"); exit(1); } while (i < initial) { if ((deps[i] = (val_t*)malloc(MAX_DEPS * sizeof(val_t))) == NULL) { perror("malloc"); exit(1); } int num_deps = 0; nb_deps[i] = 0; if (lin) { insert(linden_set, i, i); } else { sl_add(set, (val_t)i, 0); } while (i < initial-1 && num_deps < MAX_DEPS && rand_range_re(NULL, 3) % 2) { // Add geometrically distributed # of deps TODO: parametrize '2' val_t dep = ((val_t)i)+1; int dep_var = sqrt(depdist); dep += depdist + rand_range_re(NULL,2*dep_var) - dep_var; if (dep >= initial) dep = initial-1; // while (dep < initial-1 && rand_range_re(NULL, 11) % 10) { // dep should be i+GEO(10) TODO: parametrize '10' // dep++; // } deps[i][num_deps] = dep; num_deps++; } nb_deps[i] = num_deps; if (num_deps < MAX_DEPS) { deps[i][num_deps] = -1; // marks last dep } i++; } } else if (lin) { while (i < initial) { #ifdef DISTRIBUTION_EXPERIMENT pval = i; #else pval = rand_range_re(NULL, range); #endif insert(linden_set, pval, pval); last = pval; i++; } } else { while (i < initial) { #ifdef DISTRIBUTION_EXPERIMENT val = i; #else val = rand_range_re(NULL, range); #endif if (sl_add(set, val, 0)) { last = val; i++; } } } #ifdef PRINT_LIST print_skiplist(set); #endif size = sl_set_size(set); printf("Set size : %d\n", size); printf("Level max : %d\n", *levelmax); // Access set from all threads barrier_init(&barrier, nb_threads + 1); pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); printf("Creating threads: "); for (i = 0; i < nb_threads; i++) { printf("%d, ", i); data[i].first = last; data[i].range = range; data[i].update = update; data[i].unit_tx = unit_tx; data[i].alternate = alternate; data[i].pq = pq; data[i].sl = sl; data[i].es = es; data[i].effective = effective; data[i].first_remove = -1; data[i].nb_collisions = 0; data[i].nb_add = 0; data[i].nb_clean = 0; data[i].nb_added = 0; data[i].nb_remove = 0; data[i].nb_removed = 0; data[i].nb_contains = 0; data[i].nb_found = 0; data[i].nb_aborts = 0; data[i].nb_aborts_locked_read = 0; data[i].nb_aborts_locked_write = 0; data[i].nb_aborts_validate_read = 0; data[i].nb_aborts_validate_write = 0; data[i].nb_aborts_validate_commit = 0; data[i].nb_aborts_invalid_memory = 0; data[i].nb_aborts_double_write = 0; data[i].max_retries = 0; data[i].nb_threads = nb_threads; data[i].seed = rand(); data[i].seed2 = rand(); data[i].set = set; data[i].barrier = &barrier; data[i].failures_because_contention = 0; data[i].id = i; /* LINDEN */ data[i].lin = lin; data[i].linden_set = linden_set; if (pthread_create(&threads[i], &attr, test, (void *)(&data[i])) != 0) { fprintf(stderr, "Error creating thread\n"); exit(1); } } pthread_attr_destroy(&attr); // Catch some signals if (signal(SIGHUP, catcher) == SIG_ERR || //signal(SIGINT, catcher) == SIG_ERR || signal(SIGTERM, catcher) == SIG_ERR) { perror("signal"); exit(1); } // Start threads barrier_cross(&barrier); printf("STARTING...\n"); gettimeofday(&start, NULL); #ifndef DISTRIBUTION_EXPERIMENT // don't sleep if doing distro experiment if (duration > 0) { nanosleep(&timeout, NULL); } else { sigemptyset(&block_set); sigsuspend(&block_set); } #endif /* AO_store_full(&stop, 1); */ *running = 0; // if (!es) { gettimeofday(&end, NULL); // } printf("STOPPING...\n"); // Wait for thread completion for (i = 0; i < nb_threads; i++) { if (pthread_join(threads[i], NULL) != 0) { fprintf(stderr, "Error waiting for thread completion\n"); exit(1); } } // if (es) { // gettimeofday(&end, NULL); // time when all threads finish // } printf ("duration = %d\n", duration); duration = (end.tv_sec * 1000 + end.tv_usec / 1000) - (start.tv_sec * 1000 + start.tv_usec / 1000); printf ("duration = %d\n", duration); aborts = 0; aborts_locked_read = 0; aborts_locked_write = 0; aborts_validate_read = 0; aborts_validate_write = 0; aborts_validate_commit = 0; aborts_invalid_memory = 0; aborts_double_write = 0; failures_because_contention = 0; reads = 0; effreads = 0; updates = 0; collisions = 0; add = 0; added = 0; remove = 0; removed = 0; effupds = 0; max_retries = 0; for (i = 0; i < nb_threads; i++) { printf("Thread %d\n", i); printf(" #add : %lu\n", data[i].nb_add); printf(" #added : %lu\n", data[i].nb_added); printf(" #remove : %lu\n", data[i].nb_remove); printf(" #removed : %lu\n", data[i].nb_removed); printf(" #cleaned : %lu\n", data[i].nb_clean); printf("first remove : %d\n", data[i].first_remove); printf(" #collisions : %lu\n", data[i].nb_collisions); printf(" #contains : %lu\n", data[i].nb_contains); printf(" #found : %lu\n", data[i].nb_found); printf(" #aborts : %lu\n", data[i].nb_aborts); printf(" #lock-r : %lu\n", data[i].nb_aborts_locked_read); printf(" #lock-w : %lu\n", data[i].nb_aborts_locked_write); printf(" #val-r : %lu\n", data[i].nb_aborts_validate_read); printf(" #val-w : %lu\n", data[i].nb_aborts_validate_write); printf(" #val-c : %lu\n", data[i].nb_aborts_validate_commit); printf(" #inv-mem : %lu\n", data[i].nb_aborts_invalid_memory); printf(" #dup-w : %lu\n", data[i].nb_aborts_double_write); printf(" #failures : %lu\n", data[i].failures_because_contention); printf(" Max retries : %lu\n", data[i].max_retries); aborts += data[i].nb_aborts; aborts_locked_read += data[i].nb_aborts_locked_read; aborts_locked_write += data[i].nb_aborts_locked_write; aborts_validate_read += data[i].nb_aborts_validate_read; aborts_validate_write += data[i].nb_aborts_validate_write; aborts_validate_commit += data[i].nb_aborts_validate_commit; aborts_invalid_memory += data[i].nb_aborts_invalid_memory; aborts_double_write += data[i].nb_aborts_double_write; failures_because_contention += data[i].failures_because_contention; reads += data[i].nb_contains; effreads += data[i].nb_contains + (data[i].nb_add - data[i].nb_added) + (data[i].nb_remove - data[i].nb_removed); updates += (data[i].nb_add + data[i].nb_remove); collisions += data[i].nb_collisions; add += data[i].nb_add; added += data[i].nb_added; remove += data[i].nb_remove; removed += data[i].nb_removed; effupds += data[i].nb_removed + data[i].nb_added; size += data[i].nb_added - data[i].nb_removed; if (max_retries < data[i].max_retries) max_retries = data[i].max_retries; } printf("Set size : %d (expected: %d)\n", sl_set_size(set), size); printf("Duration : %d (ms)\n", duration); printf("#txs : %lu (%f / s)\n", reads + updates, (reads + updates) * 1000.0 / duration); printf("#read txs : "); if (effective) { printf("%lu (%f / s)\n", effreads, effreads * 1000.0 / duration); printf(" #contains : %lu (%f / s)\n", reads, reads * 1000.0 / duration); } else printf("%lu (%f / s)\n", reads, reads * 1000.0 / duration); printf("#eff. upd rate: %f \n", 100.0 * effupds / (effupds + effreads)); printf("#update txs : "); if (effective) { printf("%lu (%f / s)\n", effupds, effupds * 1000.0 / duration); printf(" #upd trials : %lu (%f / s)\n", updates, updates * 1000.0 / duration); } else printf("%lu (%f / s)\n", updates, updates * 1000.0 / duration); printf("#total_remove : %lu\n", remove); printf("#total_removed: %lu\n", removed); printf("#total_add : %lu\n", add); printf("#total_added : %lu\n", added); printf("#net (rem-add): %lu\n", removed-added); printf("#total_collide: %lu\n", collisions); printf("#norm_collide : %f\n", ((double)collisions)/removed); printf("#aborts : %lu (%f / s)\n", aborts, aborts * 1000.0 / duration); printf(" #lock-r : %lu (%f / s)\n", aborts_locked_read, aborts_locked_read * 1000.0 / duration); printf(" #lock-w : %lu (%f / s)\n", aborts_locked_write, aborts_locked_write * 1000.0 / duration); printf(" #val-r : %lu (%f / s)\n", aborts_validate_read, aborts_validate_read * 1000.0 / duration); printf(" #val-w : %lu (%f / s)\n", aborts_validate_write, aborts_validate_write * 1000.0 / duration); printf(" #val-c : %lu (%f / s)\n", aborts_validate_commit, aborts_validate_commit * 1000.0 / duration); printf(" #inv-mem : %lu (%f / s)\n", aborts_invalid_memory, aborts_invalid_memory * 1000.0 / duration); printf(" #dup-w : %lu (%f / s)\n", aborts_double_write, aborts_double_write * 1000.0 / duration); printf(" #failures : %lu\n", failures_because_contention); printf("Max retries : %lu\n", max_retries); #ifdef PRINT_END print_skiplist(set); #endif #ifdef PAPI long total_L1_miss = 0; unsigned k = 0; for (k = 0; k < nb_threads; k++) { total_L1_miss += g_values[k][0]; //printf("[Thread %d] L1_DCM: %lld\n", i, g_values[i][0]); //printf("[Thread %d] L2_DCM: %lld\n", i, g_values[i][1]); } printf("\n#L1 Cache Misses: %lld\n", total_L1_miss); printf("#Normalized Cache Misses: %f\n", ((double)total_L1_miss)/(reads+updates)); #endif // Delete set sl_set_delete(set); // Cleanup STM TM_SHUTDOWN(); free(threads); free(data); return 0; }
int main(int argc, char **argv) { set_cpu(the_cores[0]); ssalloc_init(); seeds = seed_rand(); pin(pthread_self(), 0); #ifdef PAPI if (PAPI_VER_CURRENT != PAPI_library_init(PAPI_VER_CURRENT)) { printf("PAPI_library_init error.\n"); return 0; } else { printf("PAPI_library_init success.\n"); } if (PAPI_OK != PAPI_query_event(PAPI_L1_DCM)) { printf("Cannot count PAPI_L1_DCM."); } printf("PAPI_query_event: PAPI_L1_DCM OK.\n"); if (PAPI_OK != PAPI_query_event(PAPI_L2_DCM)) { printf("Cannot count PAPI_L2_DCM."); } printf("PAPI_query_event: PAPI_L2_DCM OK.\n"); #endif struct option long_options[] = { // These options don't set a flag {"help", no_argument, NULL, 'h'}, {"num-threads", required_argument, NULL, 'n'}, {"seed", required_argument, NULL, 's'}, {"input-file", required_argument, NULL, 'i'}, {"output-file", required_argument, NULL, 'o'}, {"max-size", required_argument, NULL, 'm'}, {"nothing", required_argument, NULL, 'l'}, {NULL, 0, NULL, 0} }; sl_intset_t *set; pq_t *linden_set; int i, c, size, edges; unsigned long reads, effreads, updates, collisions, effupds, add, added, remove, removed; thread_data_t *data; pthread_t *threads; pthread_attr_t attr; barrier_t barrier; struct timeval start, end; int nb_threads = DEFAULT_NB_THREADS; int seed = DEFAULT_SEED; int seed2 = DEFAULT_SEED; int pq = DEFAULT_PQ; int sl = DEFAULT_SL; int lin = DEFAULT_LIN; char *input = ""; char *output = ""; int src = 0; int max = -1; int weighted = 0; int bimodal = 0; sigset_t block_set; while(1) { i = 0; c = getopt_long(argc, argv, "hplLwbn:s:i:o:m:", long_options, &i); if(c == -1) break; if(c == 0 && long_options[i].flag == 0) c = long_options[i].val; switch(c) { case 0: break; case 'h': printf("SSSP " "(priority queue)\n" "\n" "Usage:\n" " sssp [options...]\n" "\n" "Options:\n" " -h, --help\n" " Print this message\n" " -l, --spray-list\n" " Remove via delete_min operations using a spray list\n" " -p, --priority-queue\n" " Remove via delete_min operations using a skip list\n" " -L, --linden\n" " Use Linden's priority queue\n" " -n, --num-threads <int>\n" " Number of threads (default=" XSTR(DEFAULT_NB_THREADS) ")\n" " -s, --seed <int>\n" " RNG seed (0=time-based, default=" XSTR(DEFAULT_SEED) ")\n" " -i, --input-file <string>\n" " file to read the graph from (required) \n" " -o, --output-file <string>\n" " file to write the resulting shortest paths to\n" " -m, --max-size <int>\n" " if input graph exceeds max-size, use only first max-size nodes\n" " -w, --weighted\n" " use random edge weights uniformly chosen in [0,1]; fixed between trials given fixed seed\n" " -b, --bimodal\n" " use random edge weights chosen in [20,30]U[70,80]; fixed between trials given fixed seed\n" ); exit(0); case 'l': sl = 1; break; case 'p': pq = 1; break; case 'L': lin = 1; break; case 'w': weighted = 1; break; case 'b': bimodal = 1; break; case 'n': nb_threads = atoi(optarg); break; case 's': seed = atoi(optarg); break; case 'i': input = optarg; break; case 'o': output = optarg; break; case 'm': max = atoi(optarg); break; case '?': printf("Use -h or --help for help\n"); exit(0); default: exit(1); } } assert(nb_threads > 0); if (seed == 0) srand((int)time(0)); else srand(seed); printf("Set type : skip list\n"); printf("Nb threads : %d\n", nb_threads); printf("Seed : %d\n", seed); printf("Priority Q : %d\n", pq); printf("Spray List : %d\n", sl); printf("Linden : %d\n", lin); printf("Type sizes : int=%d/long=%d/ptr=%d/word=%d\n", (int)sizeof(int), (int)sizeof(long), (int)sizeof(void *), (int)sizeof(uintptr_t)); if ((data = (thread_data_t *)malloc(nb_threads * sizeof(thread_data_t))) == NULL) { perror("malloc"); exit(1); } if ((threads = (pthread_t *)malloc(nb_threads * sizeof(pthread_t))) == NULL) { perror("malloc"); exit(1); } // TODO: Build graph here FILE* fp = fopen(input, "r"); fscanf(fp, "# Nodes: %d Edges: %d\n", &size, &edges); if (size > max && max != -1) size = max; #ifndef STATIC if ((nodes = (graph_node_t*)malloc(size * sizeof(graph_node_t))) == NULL) { perror("malloc"); exit(1); } #endif for (i = 0;i < size;i++) { nodes[i].deg = 0; nodes[i].dist = -1; nodes[i].times_processed = 0; } int u,v; int cur = 0, count = 0; while (fscanf(fp, "%d %d\n", &u, &v) == 2) { if (u >= size) continue; if (v >= size) continue; nodes[u].deg++; } #ifndef STATIC for (i = 0;i < size;i++) { if ((nodes[i].adj = (int*)malloc(nodes[i].deg * sizeof(int))) == NULL) { perror("malloc"); exit(1); } if ((nodes[i].weights = (int*)malloc(nodes[i].deg * sizeof(int))) == NULL) { perror("malloc"); exit(1); } } #endif fclose(fp); nodes[src].dist = 0; fp = fopen(input, "r"); int tmp; fscanf(fp, "# Nodes: %d Edges: %d\n", &tmp, &edges); int *idx; if ((idx= (int*)malloc(size * sizeof(int))) == NULL) { perror("malloc"); exit(1); } for (i = 0;i < size;i++) { idx[i] = 0; } while (fscanf(fp, "%d %d\n", &u, &v) == 2) { if (u >= size) continue; if (v >= size) continue; assert(idx[u] < nodes[u].deg); nodes[u].adj[idx[u]] = v; if (weighted) { nodes[u].weights[idx[u]] = rand() % 100; } else if (bimodal) { if (rand() % 2) { nodes[u].weights[idx[u]] = (rand() % 11) + 20; } else { nodes[u].weights[idx[u]] = (rand() % 11) + 70; } } else { nodes[u].weights[idx[u]] = 1; } idx[u]++; } free(idx); // for (u = 0; u < size; u++) { // for (count = 0; count < nodes[u].deg; count++) { // printf("%d %d\n", u, nodes[u].adj[count]); // } // } // pq/sl *levelmax = floor_log_2(size)+2; set = sl_set_new(); sl_add_val(set, 0, src, TRANSACTIONAL); // linden if (lin) { int offset = 32; // not sure what this does _init_gc_subsystem(); linden_set = pq_init(offset); insert(linden_set, 1, src); nodes[src].dist = 1; // account for the fact that keys must be positive } printf("Graph size : %d\n", size); printf("Level max : %d\n", *levelmax); // Access set from all threads barrier_init(&barrier, nb_threads + 1); pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); printf("Creating threads: "); for (i = 0; i < nb_threads; i++) { printf("%d, ", i); data[i].pq = pq; data[i].sl = sl; data[i].first_remove = -1; data[i].nb_collisions = 0; data[i].nb_add = 0; data[i].nb_clean = 0; data[i].nb_added = 0; data[i].nb_remove = 0; data[i].nb_removed = 0; data[i].nb_contains = 0; data[i].nb_found = 0; data[i].nb_aborts = 0; data[i].nb_aborts_locked_read = 0; data[i].nb_aborts_locked_write = 0; data[i].nb_aborts_validate_read = 0; data[i].nb_aborts_validate_write = 0; data[i].nb_aborts_validate_commit = 0; data[i].nb_aborts_invalid_memory = 0; data[i].nb_aborts_double_write = 0; data[i].max_retries = 0; data[i].nb_threads = nb_threads; data[i].seed = rand(); data[i].seed2 = rand(); data[i].set = set; data[i].barrier = &barrier; data[i].failures_because_contention = 0; data[i].id = i; /* LINDEN */ data[i].lin = lin; data[i].linden_set = linden_set; if (pthread_create(&threads[i], &attr, sssp, (void *)(&data[i])) != 0) { fprintf(stderr, "Error creating thread\n"); exit(1); } } pthread_attr_destroy(&attr); // Catch some signals if (signal(SIGHUP, catcher) == SIG_ERR || //signal(SIGINT, catcher) == SIG_ERR || signal(SIGTERM, catcher) == SIG_ERR) { perror("signal"); exit(1); } /* stop = 0; */ *running = 1; // Start threads barrier_cross(&barrier); printf("STARTING...\n"); gettimeofday(&start, NULL); // Wait for thread completion for (i = 0; i < nb_threads; i++) { if (pthread_join(threads[i], NULL) != 0) { fprintf(stderr, "Error waiting for thread completion\n"); exit(1); } } gettimeofday(&end, NULL); printf("STOPPING...\n"); long nb_processed = 0; long unreachable = 0; if (strcmp(output,"")) { FILE *out = fopen(output, "w"); for (i = 0;i < size;i++) { fprintf(out, "%d %d\n", i, nodes[i].dist); } fclose(out); } else { for (i = 0;i < size;i++) { printf("%d %d\n", i, nodes[i].dist); } } for (i = 0;i < size;i++) { nb_processed += nodes[i].times_processed; if (nodes[i].times_processed == 0) { unreachable++; } } int duration = (end.tv_sec * 1000 + end.tv_usec / 1000) - (start.tv_sec * 1000 + start.tv_usec / 1000); printf ("duration = %d\n", duration); reads = 0; effreads = 0; updates = 0; collisions = 0; add = 0; added = 0; remove = 0; removed = 0; effupds = 0; for (i = 0; i < nb_threads; i++) { printf("Thread %d\n", i); printf(" #add : %lu\n", data[i].nb_add); printf(" #added : %lu\n", data[i].nb_added); printf(" #remove : %lu\n", data[i].nb_remove); printf(" #removed : %lu\n", data[i].nb_removed); printf(" #cleaned : %lu\n", data[i].nb_clean); printf(" #collisions : %lu\n", data[i].nb_collisions); printf(" #contains : %lu\n", data[i].nb_contains); printf(" #found : %lu\n", data[i].nb_found); reads += data[i].nb_contains; effreads += data[i].nb_contains + (data[i].nb_add - data[i].nb_added) + (data[i].nb_remove - data[i].nb_removed); updates += (data[i].nb_add + data[i].nb_remove); collisions += data[i].nb_collisions; add += data[i].nb_add; added += data[i].nb_added; remove += data[i].nb_remove; removed += data[i].nb_removed; effupds += data[i].nb_removed + data[i].nb_added; size += data[i].nb_added - data[i].nb_removed; } printf("Set size : %d (expected: %d)\n", sl_set_size(set), size); printf("nodes processed:%d\n", nb_processed); printf("unreachable : %d\n", unreachable); printf("wasted work : %d\n", nb_processed - (size - unreachable)); printf("Duration : %d (ms)\n", duration); printf("#ops : %lu (%f / s)\n", reads + updates, (reads + updates) * 1000.0 / duration); printf("#read ops : "); printf("%lu (%f / s)\n", reads, reads * 1000.0 / duration); printf("#eff. upd rate: %f \n", 100.0 * effupds / (effupds + effreads)); printf("#update ops : "); printf("%lu (%f / s)\n", updates, updates * 1000.0 / duration); printf("#total_remove : %lu\n", remove); printf("#total_removed: %lu\n", removed); printf("#total_add : %lu\n", add); printf("#total_added : %lu\n", added); printf("#net (rem-add): %lu\n", removed-added); printf("#total_collide: %lu\n", collisions); printf("#norm_collide : %f\n", ((double)collisions)/removed); #ifdef PRINT_END print_skiplist(set); #endif #ifdef PAPI long total_L1_miss = 0; unsigned k = 0; for (k = 0; k < nb_threads; k++) { total_L1_miss += g_values[k][0]; total_L2_miss += g_values[k][1]; //printf("[Thread %d] L1_DCM: %lld\n", i, g_values[i][0]); //printf("[Thread %d] L2_DCM: %lld\n", i, g_values[i][1]); } printf("\n#L1 Cache Misses: %lld\n", total_L1_miss); printf("#Normalized Cache Misses: %f\n", ((double)total_L1_miss)/(reads+updates)); printf("\n#L2 Cache Misses: %lld\n", total_L2_miss); printf("#Normalized Cache Misses: %f\n", ((double)total_L1_miss)/(reads+updates)); #endif // Delete set if (pq || sl) { sl_set_delete(set); } free(threads); free(data); return 0; }
void* sssp(void *data) { thread_data_t *d = (thread_data_t *)data; /* Create transaction */ set_cpu(the_cores[d->id]); /* Wait on barrier */ ssalloc_init(); PF_CORRECTION; seeds = seed_rand(); #ifdef PIN int id = d->id; // int cpu = 40*(id/40) + 4*(id%10) + (id%40)/10; int cpu = 4*(id%20) + id/20; // printf("Pinning %d to %d\n",id,cpu); pin(pthread_self(), cpu); // pin(pthread_self(), id); #endif #ifdef PAPI if (PAPI_OK != PAPI_start_counters(g_events, G_EVENT_COUNT)) { printf("Problem starting counters 1."); } #endif barrier_cross(d->barrier); // Begin SSSP int fail = 0; // int radius = 0; while (1) { val_t node; slkey_t dist_node; // print_skiplist(d->set); while (1) { if (d->sl) { if (spray_delete_min_key(d->set, &dist_node, &node, d)) break; // keep trying until get a node } else if (d->pq) { if (lotan_shavit_delete_min_key(d->set, &dist_node, &node, d)) break; } else if (d->lin) { node = (val_t) deletemin_key(d->linden_set, &dist_node, d); break; } else { printf("error: no queue selected\n"); exit(1); // TODO: grace } if (dist_node == -1) { // flag that list is empty break; } dist_node = 0; } if (dist_node == -1) { // list is empty; TODO make sure threads don't quit early fail++; if (fail > 20*d->nb_threads) { // TODO: really need a better break condition... break; } continue; } fail = 0; if (dist_node != nodes[node].dist) continue; // dead node nodes[node].times_processed++; int i; for (i = 0;i < nodes[node].deg;i++) { int v = nodes[node].adj[i]; int w = nodes[node].weights[i]; slkey_t dist_v = nodes[v].dist; // printf("v=%d dist_v=%d\n", v, dist_v); if (dist_v == -1 || dist_node + w < dist_v) { // found better path to v // printf("attempting cas...\n"); // printf("nodes[v].dist=%d dist_v=%d dist_node=%d\n", nodes[v].dist, dist_v, dist_node); int res = ATOMIC_CAS_MB(&nodes[v].dist, dist_v, dist_node+w); // printf("%d nodes[%d].dist=%d\n", res, v, nodes[v].dist); if (res) { if (d->pq || d->sl) { sl_add_val(d->set, dist_node+w, v, TRANSACTIONAL); // add to queue only if CAS is successful } else if (d->lin) { insert(d->linden_set, dist_node+w, v); } d->nb_add++; // if (dist_node+1 > radius) { // radius = dist_node+1; // printf("radius %d\n", radius); // } } } } } // End SSSP #ifdef PAPI if (PAPI_OK != PAPI_read_counters(g_values[d->id], G_EVENT_COUNT)) { printf("Problem reading counters 2."); } #endif PF_PRINT; return NULL; }
void *test(void *data) { DDPRINT("starting test\n",NULL); //get the per-thread data thread_data_t *d = (thread_data_t *)data; //scale percentages of the various operations to the range 0..255 //this saves us a floating point operation during the benchmark //e.g instead of random()%100 to determine the next operation we will do, we can simply do random()&256 //this saves time on some platfroms uint32_t read_thresh = 256 * finds / 100; //uint32_t write_thresh = 256 * (finds + inserts) / 100; //place the thread on the apropriate cpu set_cpu(d->id); //initialize the custom memeory allocator for this thread (we do not use malloc due to concurrency bottleneck issues) ssalloc_init(); // ssalloc_align(); bst_init_local(d->id); //for fine-grain latency measurements, we need to get the lenght of a getticks() function call, which is also counted //by default when we do getticks(); //code... getticks(); PF_START and PF_STOP use this when fine grain measurements are enabled PF_CORRECTION; uint32_t rand_max; //seed the custom random number generator seeds = seed_rand(); rand_max = max_key; uint32_t op; skey_t key; int i; int last = -1; DDPRINT("staring initial insert\n",NULL); DDPRINT("number of inserts: %u up to %u\n",d->num_add,rand_max); //before starting the test, we insert a number of elements in the data structure //we do this at each thread to avoid the situation where the entire data structure //resides in the same memory node // int num_elem = 0; // if (num_threads == 1){ // num_elem = max_key/2; // } else{ // num_elem = max_key/4; // } // pthread_mutex_lock(d->init_lock); // fprintf(stderr, "Starting critical section %d\n", d->id); for (i=0;i<max_key/4;++i) { key = my_random(&seeds[0],&seeds[1],&seeds[2]) & rand_max; DDPRINT("key is %u\n",key); //we make sure the insert was effective (as opposed to just updating an existing entry) if (d->id < 2) { if (bst_add(key,root, d->id)!=TRUE) { i--; } } } // fprintf(stderr, "Exiting critical section %d\n", d->id); // pthread_mutex_unlock(d->init_lock); DDPRINT("added initial data\n",NULL); bool_t res; /* Init of local data if necessary */ ticks t1,t2; /* Wait on barrier */ // fprintf(stderr, "Waiting on barrier; thread %d\n", d->id); barrier_cross(d->barrier); //start the test while (*running) { //generate a key (node that rand_max is expected to be a power of 2) key = my_random(&seeds[0],&seeds[1],&seeds[2]) & rand_max; //generate the operation op = my_random(&seeds[0],&seeds[1],&seeds[2]) & 0xff; if (op < read_thresh) { //do a find operation //PF_START and PF_STOP can be used to do latency measurements of the operation //to enable them, DO_TIMINGS must be defined at compile time, otherwise they do nothing //PF_START(2); bst_contains(key,root, d->id); //PF_STOP(2); } else if (last == -1) { //do a write operation if (bst_add(key,root, d->id)) { d->num_insert++; last=1; } } else { //do a delete operation if (bst_remove(key,root, d->id)) { d->num_remove++; last=-1; } } d->num_operations++; //memory barrier to ensure no unwanted reporderings are happening //MEM_BARRIER; } //summary of the fine grain measurements if enabled PF_PRINT; return NULL; }
void* test(void *data) { int unext, last = -1; val_t val = 0; pval_t pval = 0; thread_data_t *d = (thread_data_t *)data; /* Create transaction */ TM_THREAD_ENTER(d->id); set_cpu(the_cores[d->id]); /* Wait on barrier */ ssalloc_init(); PF_CORRECTION; seeds = seed_rand(); #ifdef PIN int id = d->id; int cpu = 40*(id/40) + 4*(id%10) + (id%40)/10; // printf("Pinning %d to %d\n",id,cpu); pin(pthread_self(), cpu); // pin(pthread_self(), id); #endif #ifdef PAPI if (PAPI_OK != PAPI_start_counters(g_events, G_EVENT_COUNT)) { printf("Problem starting counters 1."); } #endif barrier_cross(d->barrier); /* Is the first op an update? */ unext = (rand_range_re(&d->seed, 100) - 1 < d->update); #ifdef DISTRIBUTION_EXPERIMENT while (1) #else while (*running) #endif { if (d->es) { // event simulator experiment if (d->lin) { if (!empty(d->linden_set)) { d->nb_remove++; pval_t pval = deletemin(d->linden_set, d); d->nb_removed++; // printf("%d %d\n", pval, deps[pval][0]); int i = 0; val_t dep; while ((dep = deps[pval][i]) != -1 && i < MAX_DEPS) { d->nb_add++; if (insert(d->linden_set, dep, dep)) { d->nb_added++; } i++; } } } else { if (d->set->head->next[0]->next[0] != NULL) {// set not empty d->nb_remove++; if (d->sl) { // spray list if (spray_delete_min(d->set, &val, d)) { d->nb_removed++; } else { continue; } } else if (d->pq) { // lotan_shavit pq if (lotan_shavit_delete_min(d->set, &val, d)) { d->nb_removed++; // continue; // TODO: maybe try remove this to simulate task handling (dependency checks still occur) } else { continue; } } // struct timespec ten_usec; // ten_usec.tv_sec = 0; // ten_usec.tv_nsec = 10000; // nanosleep(&ten_usec, NULL); // dependency handling int i = 0; val_t dep; while ((dep = deps[val][i]) != -1 && i < MAX_DEPS) { if (!sl_contains(d->set, dep, TRANSACTIONAL)) { // dependent has been removed, need to add it again if (sl_add(d->set, dep, TRANSACTIONAL)) { // check if insert actually succeeded (otherwise someone else did it first) d->nb_added++; } d->nb_add++; } i++; } } } } else { // not event simulator if (unext) { // update if (last < 0) { // add val = rand_range_re(&d->seed, d->range); if (d->lin) { pval = val; insert(d->linden_set, pval, pval); d->nb_added++; last = pval; } else { // not linden if (sl_add(d->set, val, TRANSACTIONAL)) { d->nb_added++; last = val; } } d->nb_add++; } else { // remove if (d->pq) { if (lotan_shavit_delete_min(d->set, &val, d)) { d->nb_removed++; if (d->first_remove == -1) { d->first_remove = val; } } last = -1; } else if (d->sl) { if (spray_delete_min(d->set, &val, d)) { d->nb_removed++; if (d->first_remove == -1) { d->first_remove = val; } last = -1; } } else if (d->lin) { if ((pval = deletemin(d->linden_set, d))) { d->nb_removed++; if (d->first_remove == -1) { d->first_remove = pval; } last = -1; } } else if (d->alternate) { // alternate mode (default) if (sl_remove(d->set, last, TRANSACTIONAL)) { d->nb_removed++; if (d->first_remove == -1) { d->first_remove = val; } } last = -1; } else { /* Random computation only in non-alternated cases */ val = rand_range_re(&d->seed, d->range); /* Remove one random value */ if (sl_remove_succ(d->set, val, TRANSACTIONAL)) { d->nb_removed++; if (d->first_remove == -1) { d->first_remove = val; } /* Repeat until successful, to avoid size variations */ last = -1; } } d->nb_remove++; } } else { // read if (d->alternate) { if (d->update == 0) { if (last < 0) { val = d->first; last = val; } else { // last >= 0 val = rand_range_re(&d->seed, d->range); last = -1; } } else { // update != 0 if (last < 0) { val = rand_range_re(&d->seed, d->range); //last = val; } else { val = last; } } } else val = rand_range_re(&d->seed, d->range); PF_START(2); if (sl_contains(d->set, val, TRANSACTIONAL)) d->nb_found++; PF_STOP(2); d->nb_contains++; } /* Is the next op an update? */ if (d->effective) { // a failed remove/add is a read-only tx unext = ((100 * (d->nb_added + d->nb_removed)) < (d->update * (d->nb_add + d->nb_remove + d->nb_contains))); } else { // remove/add (even failed) is considered as an update unext = (rand_range_re(&d->seed, 100) - 1 < d->update); } } #ifdef DISTRIBUTION_EXPERIMENT if (d->first_remove != -1) { break; //only one run } #endif } #ifdef PAPI if (PAPI_OK != PAPI_read_counters(g_values[d->id], G_EVENT_COUNT)) { printf("Problem reading counters 2."); } #endif /* Free transaction */ TM_THREAD_EXIT(); PF_PRINT; return NULL; }