Data Blocking
Jon K. Nilsen
Department of Physics and Scientific Computing Group University of Oslo, N-0316 Oslo, Norway
Spring 2008
Computational Physics II FYS4410
Data Blocking Jon K. Nilsen Department of Physics and Scientific - - PowerPoint PPT Presentation
Data Blocking Jon K. Nilsen Department of Physics and Scientific Computing Group University of Oslo, N-0316 Oslo, Norway Spring 2008 Computational Physics II FYS4410 Outline Data Blocking Implementing blocking Using vmc blocking
Computational Physics II FYS4410
Computational Physics II FYS4410
int main ( int nargs , char∗ args [ ] ) { int n procs , min block size , max block size , n block samples ; / / Read from screen a possible new vaue of n i f ( nargs > 4) { n procs = a t o i ( args [ 1 ] ) ; min block size = a t o i ( args [ 2 ] ) ; max block size = a t o i ( args [ 3 ] ) ; n block samples = a t o i ( args [ 4 ] ) ; } else{ cerr < < "usage: ./vmc_blocking.x <n_procs> <min_bloc_size> " < < "<max_block_size> <n_block_samples>" < < endl ; e x i t (1) ; } / / get f i l e size using s t a t struct s t a t r e s u l t ; int local n , n ; i f ( s t a t ("blocks_rank0.dat" , &r e s u l t ) == 0){ l o c a l n = r e s u l t . s t s i z e / sizeof ( double ) ; n = l o c a l n∗n procs ; } else{ cerr < < "error in getting file size" < < endl ; e x i t (1) ; } Computational Physics II FYS4410
/ / get a l l mc r e s u l t s from f i l e s double∗ mc results = new double [ n ] ; for ( int i =0; i<n procs ; i ++){
< "blocks_rank" < < i < < ".dat" ; ifstream i n f i l e ; i n f i l e . open ( ost . s t r ( ) . c s t r ( ) , ios : : in | ios : : binary ) ; i n f i l e . read ( ( char∗)&( mc results [ i ∗l o c a l n ] ) , r e s u l t . s t s i z e ) ; i n f i l e . close ( ) ; } / / and summarize double mean, sigma ; double res [ 2 ] ; meanvar ( mc results , n , res ) ; mean = res [ 0 ] ; sigma= res [ 1 ] ; / / Open f i l e f o r writing , w r i t i n g r e s u l t s in formated
f o r p l o t t i n g :
ios : : out ) ;
< setprecision (10) ; Computational Physics II FYS4410
double∗ b l o c k r e s u l t s = new double [ n block samples ] ; int block size , block step length ; block step length = ( max block size−min block size ) / n block samples ; / / loop over block sizes for ( int i =0; i<n block samples ; i ++){ block size = min block size+ i ∗block step length ; blocking ( mc results , n , block size , res ) ; mean = res [ 0 ] ; sigma = res [ 1 ] ; / / formated
< block size < < "\t" < < mean < < "\t" < < sqrt ( sigma / ( ( n / block size ) −1.0) ) < < endl ; }
return 0; } Computational Physics II FYS4410
/ / f i n d mean and variance
blocks
size block size . / / mean and variance are stored in res void blocking ( double ∗vals , int n vals , int block size , double ∗res ){ / / note : integer d i v i s i o n w i l l waste some values int n blocks = n vals / block size ; double∗ block vals = new double [ n blocks ] ; for ( int i =0; i<n blocks ; i ++){ block vals [ i ] = mean( vals+ i ∗block size , block size ) ; } meanvar ( block vals , n blocks , res ) ; delete block vals ; } Computational Physics II FYS4410
/ / f i n d mean of values in vals double mean( double ∗vals , int n vals ){ double m=0; for ( int i =0; i<n vals ; i ++){ m+=vals [ i ] ; } return m/ double ( n vals ) ; } / / calculate mean and variance
vals , r e s u l t s stored in res void meanvar ( double ∗vals , int n vals , double ∗res ){ double m2=0 , m=0 , val ; for ( int i =0; i<n vals ; i ++){ val=vals [ i ] ; m+=val ; m2+=val∗val ; } m /= double ( n vals ) ; m2 /= double ( n vals ) ; res [ 0 ] = m; res [ 1 ] = m2−(m∗m) ; } Computational Physics II FYS4410
Computational Physics II FYS4410