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
Implementing blocking vmc blocking.cpp main() 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
Implementing blocking vmc blocking.cpp main() / / 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 ++) { ostringstream ost ; ost < < i < < ".dat" ; < "blocks_rank" < 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 output f o r p l o t t i n g : ofstream o u t f i l e ; o u t f i l e . open ( "blockres.dat" , ios : : out ) ; o u t f i l e < < setprecision (10) ; Computational Physics II FYS4410
Implementing blocking vmc blocking.cpp main() 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 output o u t f i l e < < block size < < "\t" < < mean < < "\t" < sqrt ( sigma / ( ( n / block size ) − 1.0) ) < < endl ; < } o u t f i l e . close ( ) ; return 0; } Computational Physics II FYS4410
Implementing blocking vmc blocking.cpp blocking() / / f i n d mean and variance of blocks of 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
Implementing blocking vmc blocking.cpp meanvar() / / 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 of 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
Usage Compiling and runing vmc blocking.cpp Prerequisites: The files blocks rank*.dat from VMC simulation (e.g. from vmc para.cpp ) Compiling: g++ -O3 -o vmc blocking.x vmc blocking.cpp (note: not mpicxx !) Usage: Copy vmc blocking.x to directory that contains blocks rank*.dat and change to that directory Run: ./vmc blocking.x <n procs> <min block size> <max block size> <n block samples> Results are written to blockres.dat with block size in first column, mean in second column and std.dev. in third column n procs is number of processors used in the VMC simulation. min block size , max block size and n block samples defines the range and resolution of the result file Computational Physics II FYS4410
Recommend
More recommend