int main() { int i; double k; i=0; k=0; int* a; int b; int operations; scanf("%d",&operations); srand(time(NULL)*time(NULL)); while(i<operations) { a=rpermute(5); b=checker(a); if(b==1) { k++; } free(a); i++; } printf("The result is %lf.",k/operations); return 0; }
int main(int argc, char* argv[]) { printf ("usage: test utility for CSE221 project.\n"); /* do something do something big do something extraordinary do something for the entire humanity */ char device[] = "/dev/sdb"; /* char device[] = "/dev/sdb"; */ tick ts, te; int i = 0; char* buffer = (char *) malloc (sizeof(char) * BUFFER_SIZE); ts = gettick(); /* read twice, this time for course reasons cannot use system commands */ int pf = open(device, O_RDONLY); // printf ("pf = %d\n", pf); long long size_in_bytes; ioctl(pf, BLKGETSIZE64, &size_in_bytes); // printf ("sz = %ld\n", size_in_bytes); long long n = size_in_bytes / BUFFER_SIZE; // printf ("n = %d\n", n); long long *seq = rpermute(n); unsigned long long timelapse = 0; while (i < 1000) { ts = gettick(); lseek (pf, seq[i] * BUFFER_SIZE, SEEK_SET); long long cnt = read (pf, buffer, BUFFER_SIZE); te = gettick(); // when large enough, record the read speed. printf ("offset = %f MB\ttime = %llu cycles\n", (double)seq[i] * 4096.0 / 1048576.0 , te - ts); i++; } close(pf); free (buffer); free (seq); }
int main(void) { int limit = 6; int *a; int k; // Print 7 permutations for (k = 0; k < 7; k++) { a = rpermute(limit); printarray(limit, a); printf("\n"); } return 0; }
//************************************************************************* void recal(double *image,double **newimage,int mrows, int ncols, int *bounds, double *transform, int ntransform) { int i, j; int *order=NULL; for(i=0;i<ITER;i++) { order=rpermute(ncols); for(j=0;j<ncols;j++) { //mexPrintf("I:%d - Optimisation de %d\n",i, order[j]); findbestcorr(newimage, ncols, bounds, order[j], image+mrows*order[j], mrows,transform, ntransform,i); //mexPrintf("new T : %lf\n",transform[order[j]]); } free(order); } }
/* randomly permutes the elements of the array perm of length n */ void randperm( const long n, long perm[]) { long *ind = xmalloc(n*sizeof(long)); long *tmp = xmalloc(n*sizeof(long)); long k; rpermute(n, ind); for( k = 0 ; k < n ; k++ ){ tmp[k] = perm[ind[k]]; } for( k = 0 ; k < n ; k++ ){ perm[k] = tmp[k]; } free(ind); free(tmp); }
int main(int argc, char **argv) { nptsside = atoi(argv[1]); print_node = atoi(argv[2]); side2 = nptsside / 2.0; side4 = nptsside / 4.0; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &nnodes); MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); mpi_chunksize = nptsside/nnodes; struct timespec bgn,nd; clock_gettime(CLOCK_REALTIME, &bgn); scram = rpermute(nptsside, 0); MPI_Scatter(scram, mpi_chunksize, MPI_INT, scram, mpi_chunksize, MPI_INT, 0, MPI_COMM_WORLD); dowork(); dowork(); dowork(); dowork(); dowork(); clock_gettime(CLOCK_REALTIME, &nd); if (my_rank == print_node) printf("The total count is %d,\t and the average time of 5 runs is %f\n", tot_count,timediff(bgn,nd)/5); MPI_Finalize(); }
int main(int argc, char *argv[]) { nptsside = atoi(argv[1]); print_node = atoi(argv[2]); nodenum = (int) atoi(argv[3]); ppnnum = (int) atoi(argv[4]); tasktype = argv[5][0]; side2 = nptsside / 2.0; side4 = nptsside / 4.0; int provided, claimed; MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided ); MPI_Query_thread( &claimed ); // MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &nnodes); MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); if (my_rank == print_node) { printf( "Query thread level= %d Init_thread level= %d\n", claimed, provided ); printf( "Defined LEVEL= %d (ompi_info | grep -i thread) \n", MPI_THREAD_MULTIPLE); } mpi_chunksize = nptsside/nnodes; struct timespec bgn,nd; clock_gettime(CLOCK_REALTIME, &bgn); #ifdef RC scram = rpermute(nptsside, 0); MPI_Scatter(scram, mpi_chunksize, MPI_INT, scram, mpi_chunksize, MPI_INT, 0, MPI_COMM_WORLD); #else findmyrange(nptsside, nnodes, my_rank, myrange); scram = rpermute(mpi_chunksize, myrange[0]); printf("My range is %d %d \n", myrange[0], myrange[1]); #endif dowork(); //implied barrier clock_gettime(CLOCK_REALTIME, &nd); if (my_rank == print_node) { printf("Random chunk RC is defined\n"); printf("time:%g :: maxiteration:%d :: Pixel:%d :: threadnum:%d :: mpi_chunksize:%d :: tot_count:%d :: count:%d :: nodenum:%d :: ppnnum:%d :: tasktype:%c\n", timediff(bgn,nd),MAXITERS, nptsside,nnodes,mpi_chunksize,tot_count,count,nodenum,ppnnum,tasktype); FILE *fp; fp = fopen("OMP_MPI_mand.txt","a"); fprintf(fp, "time:%g :: maxiteration:%d :: Pixel:%d :: threadnum:%d :: mpi_chunksize:%d :: tot_count:%d :: count:%d :: nodenum:%d :: ppnnum:%d :: tasktype:%c\n", timediff(bgn,nd),MAXITERS,nptsside,nnodes,mpi_chunksize,tot_count,count,nodenum,ppnnum,tasktype); fclose(fp); } MPI_Finalize(); }
double* trainFULL(svm_dataset dataset,properties props){ printf("\n"); int MaxWorkingSize = props.MaxSize; double epsilon=1e6; double epsilonTmp=0.0; double epsilonThreshold=0.001; svm_dataset subdataset; subdataset.sparse = dataset.sparse; subdataset.maxdim = dataset.maxdim; subdataset.y=(double *) calloc(MaxWorkingSize,sizeof(double)); subdataset.quadratic_value=(double *) calloc(MaxWorkingSize,sizeof(double)); subdataset.x = (svm_sample **) calloc(MaxWorkingSize,sizeof(svm_sample *)); int found10,found11, found12, found00, found01, found02; double *e = (double *) calloc(dataset.l,sizeof(double)); double *beta=(double *) calloc((dataset.l+1),sizeof(double)); double *betaNew=(double *) calloc((dataset.l+1),sizeof(double)); double *betaBest=(double *) calloc((dataset.l+1),sizeof(double)); double *betaTmp; int *SW = (int *) calloc(MaxWorkingSize,sizeof(int)); int *SIN = (int *) calloc(dataset.l,sizeof(int)); int *SC = (int *) calloc(dataset.l,sizeof(int)); double *GIN=(double *) calloc((MaxWorkingSize+1),sizeof(double)); double *esub=(double *) calloc((MaxWorkingSize+1),sizeof(double)); double *betasub=(double *) calloc((MaxWorkingSize+1),sizeof(double)); int nSW=0, nSIn=0, nSC=0; int i, o, ind=0, ind2=0; double lambeq, mil, mal; int neq=0; for (i=0;i<dataset.l;i++){ int randomNum=i%10; if (randomNum<1 && nSW<MaxWorkingSize){ SW[ind]=i; ind++; nSW++; }else{ SIN[ind2]=i; ind2++; nSIn++; } e[i]=dataset.y[i]; } int iter=0; int endNorm=0; double bestNorm=1e20; int SinceBest=0; while( (endNorm==0) && (SinceBest<300)){ iter+=1; // CONSTRUCT GIN AND GBIN if(nSIn>0){ memset(GIN,0.0,(nSW+1)*sizeof(double)); #pragma omp parallel default(shared) private(i,o) { #pragma omp for schedule(static) for (i=0;i<(nSW+1);i++){ int o; if(i<nSW){ for (o=0;o<nSIn;o++) if (betaNew[SIN[o]] != 0.0){ GIN[i] += betaNew[SIN[o]]*kernel(dataset,SW[i], SIN[o], props)*dataset.y[SW[i]]; } }else{ for (o=0;o<nSIn;o++) GIN[nSW]+=betaNew[SIN[o]]; } } } } //////////////////// // CREATE SUBDATASET /////////////////// subdataset.l = nSW; for(i=0;i<nSW;i++){ subdataset.y[i]=dataset.y[SW[i]]; subdataset.quadratic_value[i]=dataset.quadratic_value[SW[i]]; subdataset.x[i]=dataset.x[SW[i]]; betasub[i]=beta[SW[i]]; esub[i]=e[SW[i]]; } betasub[nSW]=beta[dataset.l]; ///////////////// // CALL TO IRWLS ///////////////// double *betaTmp = subIRWLS(subdataset,props, GIN, esub, betasub); ///////////////// // UPDATING ERROR ///////////////// memcpy(betaNew,beta,(dataset.l+1)*sizeof(double)); for (i=0;i<nSW;i++){ betaNew[SW[i]]=betaTmp[i]; } betaNew[dataset.l]=betaTmp[subdataset.l]; #pragma omp parallel default(shared) private(i,randomNum) { #pragma omp for schedule(static) for (i=0;i<dataset.l;i++){ int j; for (j=0;j<nSW;j++){ e[i]=e[i]-kernel(dataset,i,SW[j],props)*(betaNew[SW[j]]-beta[SW[j]]); } e[i]=e[i]-(betaNew[dataset.l]-beta[dataset.l]); } } free(betaTmp); double deltaW=0.0; double normW=0.0; for (i=0;i<dataset.l+1;i++){ deltaW=deltaW+pow(beta[i]-betaNew[i],2.0); normW=normW+pow(beta[i],2.0); } //printf("El valor es %f\n",deltaW/normW); if(deltaW/normW<1.0e-3){ endNorm=1; } memcpy(beta,betaNew,(dataset.l+1)*sizeof(double)); if(deltaW/normW<bestNorm){ bestNorm=deltaW/normW; SinceBest=0; memcpy(betaBest,betaNew,(dataset.l+1)*sizeof(double)); }else{ SinceBest+=1; } /////////////////////////////// // UPDATING STOPPING CONDITIONS /////////////////////////////// found00=0,found01=0,found02=0,found10=0,found11=0,found12=0; nSW=0; nSIn=0; nSC=0; for (i=0;i<dataset.l;i++){ if(betaNew[i]*dataset.y[i]==((double)props.C)){ epsilonTmp=e[i]*dataset.y[i]; if(epsilonTmp<-1.0*epsilonThreshold){ if(dataset.y[i]==-1 && found02==0){ SW[nSW]=i; nSW+=1; found02=1; }else if(dataset.y[i]==1 && found12==0){ SW[nSW]=i; nSW+=1; found12=1; }else{ SC[nSC]=i; nSC+=1; } }else{ SIN[nSIn]=i; nSIn+=1; } }else if(betaNew[i]==0.0){ epsilonTmp=e[i]*dataset.y[i]; if(epsilonTmp>epsilonThreshold){ if(dataset.y[i]==-1 && found00==0){ SW[nSW]=i; nSW+=1; found00=1; }else if(dataset.y[i]==1 && found10==0){ SW[nSW]=i; nSW+=1; found10=1; }else{ SC[nSC]=i; nSC+=1; } }else{ SIN[nSIn]=i; nSIn+=1; } }else if((betaNew[i]*dataset.y[i]!=0.0) && (betaNew[i]*dataset.y[i]!=(double)props.C)){ epsilonTmp=fabs(e[i]*dataset.y[i]); if(epsilonTmp>epsilonThreshold){ if(dataset.y[i]==-1 && found01==0){ SW[nSW]=i; nSW+=1; found01=1; }else if(dataset.y[i]==1 && found11==0){ SW[nSW]=i; nSW+=1; found11=1; }else{ SC[nSC]=i; nSC+=1; } }else{ SC[nSC]=i; nSC+=1; } } } //printf("ITERS SINCE BEST, %d %d %d\n",itersSinceBest,fullfill1+fullfill2+fullfill3,bestFulfill); printf("%s", "."); fflush(stdout); ////////////////////// // SELECT WORKING SET ////////////////////// if(nSC<(MaxWorkingSize-nSW)){ for(i=0;i<nSC;i++){ SW[nSW]=SC[i]; nSW+=1; } }else{ int *perm = rpermute(nSC); int space = (MaxWorkingSize-nSW); for(i=0;i<nSC;i++){ if (i<space){ SW[nSW]=SC[perm[i]]; nSW+=1; }else{ SIN[nSIn]=SC[perm[i]]; nSIn+=1; } } free(perm); } //memcpy(beta,betaNew,dataset.l*sizeof(double)); } printf("\n"); return betaNew; }