void mainloop(){ //int init[N*N] = {0,3,8,1000,-4, 1000,0,1000,1,7,1000,4,0,1000,1000, //2,1000,-5,0,1000,1000,1000,1000,6,0}; int nlr,nlc,s,t,i,j,k,l,li,lsize,tsize0, tsize1,tempp,tempoff,rpos,cpos, *lpart,*linter,*gindx,*lcol,*lrow,*lsrow, *lscol, *ltrow, *ltcol, *temp; int* init = gen_graph(N, 0.05); bsp_begin(bsp_nprocs()); /**********Initialization SuperStep 0***************/ //Compute global row and column indeces for each element int pm = sqrt(bsp_nprocs()); int pn = (bsp_nprocs())/pm; /* Compute 2D processor numbering from 1D numbering with failsafe if the number of processors are not enough, back to simple 1D cyclic distribution */ if ( pn != pm ){ pn = bsp_nprocs(); pm = 1; t = bsp_pid(); s = 0; }else{ s= bsp_pid()%pm; /* 0 <= s < pm */ t= bsp_pid()/pn; /* 0 <= t < pn */ } nlr= nloc(pm,s,N); /* number of local rows */ nlc= nloc(pn,t,N); /* number of local columns */ lsize = nlr*nlc; //interpret 2D size to array size lpart = vecalloci(lsize); //Initialize local part of processor s linter = vecalloci(lsize); //Intermidiate array used for the matrix "multiplication" gindx = vecalloci(lsize); //Array to store the global indeces of the local elements lcol = vecalloci(lsize); //Array to store the glocal column index lrow = vecalloci(lsize); //Array to store the glocal row index bsp_push_reg(lpart,lsize*SZINT); //Distribute the Data li=0; for ( i= 0; i < N; i++){ for ( j= 0; j < N; j++){ if ((j % pn) == t){ lpart[li] = init[N*i+j]; lrow[li] = i; lcol[li] = j; gindx[li] = N*i+j; li++; } } } /*for ( i= 0; i < N*N; i++) { if(bsp_pid() == (i % bsp_nprocs())){ lpart[li] = init[i]; lrow[li] = i/N; lcol[li] = i % N; gindx[li] = i; li++; } }*/ vecfreei(init);//out of the shared space tsize0 = tsize1 =lsize; temp = lrow; //find unique global rows for processor s for(i=0;i<tsize0;i++){ for(j=0;j<tsize0;j++){ if(i==j){ continue; } else if(*(temp+i)==*(temp+j)){ k=j; tsize0--; while(k < tsize0){ *(temp+k)=*(temp+k+1); k++; } j=0; } } } temp = lcol; //find unique global column for processor s for(i=0;i<tsize1;i++){ for(j=0;j<tsize1;j++){ if(i==j){ continue; } else if(*(temp+i)==*(temp+j)){ k=j; tsize1--; while(k < tsize1){ *(temp+k)=*(temp+k+1); k++; } j=0; } } } //keep unique global rows and columns in arrays //initialize arrays to hold the elements of those rows and columns(ltcol, ltrow) lscol = vecalloci(tsize1); lsrow = vecalloci(tsize0); ltcol = vecalloci(N*tsize1); ltrow = vecalloci(N*tsize0); for(i=0;i < tsize0;i++){ lsrow[i] = lrow[i]; } for(i=0;i < tsize1;i++){ lscol[i] = lcol[i]; } vecfreei(lcol);//not needed from this point on vecfreei(lrow);//we use lscol, lsrow, ltrow, ltcol //sort arrays qsort (lsrow, tsize0, sizeof(int), compare_int); qsort (lscol, tsize1, sizeof(int), compare_int); bsp_sync(); /**********End Initialization SuperStep 0***************/ double time0= bsp_time(); /*********Repeated Squaring loop start*************/ j=1; while ((N-1) > j) { /*************Comm. SuperStep j0*************/ for(i=0;i < tsize1;i++){ for(k=0; k<N;k++){ tempp=((N*k+lscol[i]) % bsp_nprocs()); tempoff = ((double)(N*k+lscol[i])/(double)bsp_nprocs()); bsp_get(tempp, &lpart[0],tempoff*SZINT, <col[N*i+k],SZINT); } } for(i=0;i < tsize0;i++){ for(k=0; k<N;k++){ tempp=((N*lsrow[i]+k) % bsp_nprocs()); tempoff = ((double)(N*lsrow[i]+k)/(double)bsp_nprocs()); bsp_get(tempp, &lpart[0],tempoff*SZINT, <row[N*i+k],SZINT); } } bsp_sync(); /*************End Comm. SuperStep j0*************/ /*************Comp. SuperStep j1*************/ for ( i=0; i<lsize; i++) { int gcol = gindx[i] % N; //get global col indx of current element int grow = gindx[i]/N; //get global row indx of current element linter[i]=1000;//initiliaze array //find appropriate indx of the global rows and columns to perform "multiplication" /*for ( l=0; l < tsize0;l++){ if(grow == lsrow[l]){ rpos =l; break; } }*/ int *rp = bsearch (&grow, lsrow, tsize0, sizeof (lsrow),compare_int); rpos = rp - lsrow; int *cp = bsearch (&gcol, lscol, tsize1, sizeof (lscol),compare_int); cpos = cp - lscol; /*for ( l=0; l < tsize1;l++){ if(gcol == lscol[l]){ cpos =l; break; } }*/ //this is where the update is done for(k=0;k<N;k++){ linter[i] = fmin(linter[i], ltrow[N*rpos + k]+ltcol[N*cpos + k]); } } memcpy(lpart,linter,lsize*SZINT); j = 2*j; bsp_sync(); /*************End Comp. SuperStep j1*************/ } /*********Repeated Squaring loop end*************/ double time1= bsp_time(); bsp_sync(); /*********display matrices and time*********/ if(bsp_pid()==0){ printf( " \n Block Cyclic Distr calculation of APSP took: %f seconds \n", time1-time0 ); } /*printf("\n The array is, proc %d \n ", bsp_pid()); for(i=0;i < lsize;i++){ printf(" %d",lpart[i]); }*/ printf("\n "); //clean up bsp_pop_reg(lpart); vecfreei(lpart); vecfreei(linter); vecfreei(lscol); vecfreei(lsrow); vecfreei(ltcol); vecfreei(ltrow); vecfreei(gindx); bsp_end(); }
void bspfft_test() { void bspfft( double * x, int n, int p, int s, int sign, double * w0, double * w, double * tw, int *rho_np, int *rho_p ); void bspfft_init( int n, int p, int s, double * w0, double * w, double * tw, int *rho_np, int *rho_p ); int k1_init( int n, int p ); int p, s, n, q, np, k1, j, jglob, it, *rho_np, *rho_p; double time0, time1, time2, ffttime, nflops, max_error, error_re, error_im, error, *Error, *x, *w0, *w, *tw; bsp_begin( P ); p = bsp_nprocs(); s = bsp_pid(); bsp_push_reg( &n, SZINT ); Error = vecallocd( p ); bsp_push_reg( Error, p * SZDBL ); bsp_sync(); if ( s == 0 ) { printf( "Please enter length n: \n" ); #ifdef _WIN32 scanf_s( "%d", &n ); #else scanf( "%d", &n ); #endif if ( n < 2 * p ) { bsp_abort( "Error in input: n < 2p" ); } for ( q = 1; q < p; q++ ) { bsp_put( q, &n, &n, 0, SZINT ); } } bsp_sync(); if ( s == 0 ) { printf( "FFT of vector of length %d using %d processors\n", n, p ); printf( "performing %d forward and %d backward transforms\n", NITERS, NITERS ); } /* Allocate, register, and initialize vectors */ np = n / p; x = vecallocd( 2 * np ); bsp_push_reg( x, 2 * np * SZDBL ); k1 = k1_init( n, p ); w0 = vecallocd( k1 ); w = vecallocd( np ); tw = vecallocd( 2 * np + p ); rho_np = vecalloci( np ); rho_p = vecalloci( p ); for ( j = 0; j < np; j++ ) { jglob = j * p + s; x[2 * j] = ( double )jglob; x[2 * j + 1] = 1.0; } bsp_sync(); time0 = bsp_time(); /* Initialize the weight and bit reversal tables */ for ( it = 0; it < NITERS; it++ ) { bspfft_init( n, p, s, w0, w, tw, rho_np, rho_p ); } bsp_sync(); time1 = bsp_time(); /* Perform the FFTs */ for ( it = 0; it < NITERS; it++ ) { bspfft( x, n, p, s, 1, w0, w, tw, rho_np, rho_p ); bspfft( x, n, p, s, -1, w0, w, tw, rho_np, rho_p ); } bsp_sync(); time2 = bsp_time(); /* Compute the accuracy */ max_error = 0.0; for ( j = 0; j < np; j++ ) { jglob = j * p + s; error_re = fabs( x[2 * j] - ( double )jglob ); error_im = fabs( x[2 * j + 1] - 1.0 ); error = sqrt( error_re * error_re + error_im * error_im ); if ( error > max_error ) { max_error = error; } } bsp_put( 0, &max_error, Error, s * SZDBL, SZDBL ); bsp_sync(); if ( s == 0 ) { max_error = 0.0; for ( q = 0; q < p; q++ ) { if ( Error[q] > max_error ) { max_error = Error[q]; } } } for ( j = 0; j < NPRINT && j < np; j++ ) { jglob = j * p + s; printf( "proc=%d j=%d Re= %f Im= %f \n", s, jglob, x[2 * j], x[2 * j + 1] ); } fflush( stdout ); bsp_sync(); if ( s == 0 ) { printf( "Time per initialization = %lf sec \n", ( time1 - time0 ) / NITERS ); ffttime = ( time2 - time1 ) / ( 2.0 * NITERS ); printf( "Time per FFT = %lf sec \n", ffttime ); nflops = 5 * n * log( ( double )n ) / log( 2.0 ) + 2 * n; printf( "Computing rate in FFT = %lf Mflop/s \n", nflops / ( MEGA * ffttime ) ); printf( "Absolute error= %e \n", max_error ); printf( "Relative error= %e \n\n", max_error / n ); } bsp_pop_reg( x ); bsp_pop_reg( Error ); bsp_pop_reg( &n ); bsp_sync(); vecfreei( rho_p ); vecfreei( rho_np ); vecfreed( tw ); vecfreed( w ); vecfreed( w0 ); vecfreed( x ); vecfreed( Error ); bsp_end(); } /* end bspfft_test */
void mainloop(){ //int init[N*N] = {0,3,8,1000,-4, 1000,0,1000,1,7,1000,4,0,1000,1000, //2,1000,-5,0,1000,1000,1000,1000,6,0}; int i,j,k,l,v,t,lsize,*lsize_m,*lrow,*lcol, *linit, *linter,*startrow_m; int li,lj,lk,startrow, endrow,g; int* init = gen_graph(N, 0.05); bsp_begin(bsp_nprocs()); /**********Initialization***************/ /*******Comp. Superstep 0******/ lsize = nloc(bsp_nprocs(),bsp_pid(), N); //Get the number of rows of processor s lrow = vecalloci(lsize*N); //The main storing array of processor s lcol = vecalloci(N); //array to hold the column for the matrix squaring startrow_m = vecalloci(bsp_nprocs()); //array to hold all processors starting global row lsize_m = vecalloci(bsp_nprocs()); //array to hold the number of rows of all processors linter = vecalloci(lsize*N); //Intermidiate array used for the matrix "multiplication" bsp_push_reg(startrow_m,bsp_nprocs()*SZINT); bsp_push_reg(lsize_m,bsp_nprocs()*SZINT); bsp_push_reg(lrow,lsize*N*SZINT); /****Get the first and last global row of processor s***/ if(bsp_pid() == (bsp_nprocs() - 1)){ startrow = (N - lsize); endrow = N; }else{ startrow = bsp_pid()*lsize; endrow = bsp_pid()*lsize + lsize; } //Distribute Data, according row block distribution li=0; for ( i= startrow; i < endrow; i++) { lj=0; for(j=0; j < N; j++) { lrow[N*li+lj] = init[N*i+j]; lj++; } li++; } vecfreei(init); //out of the shared enviroment //initialize arrays for ( i=0; i<bsp_nprocs(); i++) { startrow_m[i] = 0; lsize_m[i] = 0; } bsp_sync(); /*******End Comp. Superstep 0******/ /*********Comm. Superstep 1********/ //Communicate the global starting rows of all processors for(g=0; g<bsp_nprocs();g++){ bsp_put(g,&startrow,&startrow_m[0],bsp_pid()*SZINT,SZINT); bsp_put(g,&lsize,&lsize_m[0],bsp_pid()*SZINT,SZINT); } /*********End Comm. Superstep 1*****/ bsp_sync(); /**********End Initialization***************/ double time0= bsp_time(); /*********Repeated Squaring loop start*************/ j=1; while ((N-1) > j) { /****Comp. Superstep j0****/ //initialize arrays for ( i=0; i<N*lsize; i++) { linter[i] = 1000; } for ( i=0; i<N; i++) { lcol[i] = 0; } bsp_sync(); /****End Comp. Superstep j0****/ for ( lj=0; lj < N; lj++) { /***Comm. SuperStep jlj0*******/ //get global column lj t=0; for(g=0; g < bsp_nprocs();g++){ for(v=0; v<lsize_m[g]; v++){ bsp_get(g,&lrow[0],(lj+v*N)*SZINT,&lcol[t],SZINT); t++; } } bsp_sync(); /***End Comm. SuperStep jlj0***/ /***Comp. SuperStep jlj1*******/ //update the values that use global column lj for ( li = 0; li < lsize; li++){ for ( lk=0; lk < N; lk++) { linter[N*li+lj] = fmin(linter[N*li+lj], lrow[N*li+lk]+lcol[lk]); } } bsp_sync(); /***End Comp. SuperStep jlj1***/ } /****Comp. Superstep j1****/ memcpy(lrow,linter,N*lsize*SZINT); j=2*j; bsp_sync(); /****End Comp. Superstep j1****/ } /*********Repeated Squaring loop end*************/ double time1= bsp_time(); bsp_sync(); /*********display matrices and time*********/ if(bsp_pid()==0){ printf( " \n Block Row Distr (need to know basis) calculation of APSP took: %f seconds \n", time1-time0 ); } /*for(g = 0; g < bsp_nprocs(); g++){ if(bsp_pid()==g){ printf("\n i am proc %d and i have APSP Mat \n",bsp_pid()); for(k=0;k<lsize;k++) { printf("\n"); for(l=0;l<N;l++){ printf("\t %d",lrow[N*k+l]); } printf("\n \n "); } } bsp_sync(); }*/ //Clean up bsp_pop_reg(startrow_m); bsp_pop_reg(lsize_m); bsp_pop_reg(lrow); vecfreei(lrow); vecfreei(lcol); vecfreei(startrow_m); vecfreei(lsize_m); vecfreei(linter); bsp_end(); }