21.06.2014 Views

Numerical Methods Contents - SAM

Numerical Methods Contents - SAM

Numerical Methods Contents - SAM

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

• Windows: for the exercises it is recommend to use a linux emulator like cygwin or VirtualBox:<br />

http://www.virtualbox.org/ http://www.cygwin.com/<br />

Time Measurement:<br />

In order to compare the efficiency of different implementations we need to be able to measure the<br />

time spent on a computation. The following definitions are commonly used in this context:<br />

• the wall time or real time denotes the time an observer would measure between the program<br />

start and end. (c.f. wall clock)<br />

• the user time denotes the cpu-time spent in executing the user’s code.<br />

• the system time denotes the cpu-time that the system spent on behalf of the user’s code (e.g.<br />

memory allocation, i/o handling etc.)<br />

Unix-based systems provide the time command for measuring the time of a whole runnable, e.g.: time<br />

./runnable. For the measurement of the runtimes in c++, the clock()-command provided in the time.h<br />

can be used. These methods will not provide correct results for the time-measurement of parallelized<br />

code, where the routines from the parallelization framework should be used. (e.g. MPI_WTIME for<br />

MPI-programs)<br />

22 void r e s e t ( )<br />

23 {<br />

24 time =0;<br />

25 bStarted = false ;<br />

26 }<br />

27 private :<br />

28 double time ;<br />

29 bool bStarted ;<br />

30 } ;<br />

1 /∗<br />

Code 1.4.12: Measuring real, user and system time from C++ on unix based systems<br />

2 ∗ unixTimer Class using times ( )−command from the unixbased times . h<br />

3 ∗ t h i s class w i l l r e p o r t the user , system and r e a l time .<br />

4 ∗/<br />

5 #include <br />

6 #include <br />

7 #include <br />

8 class u n i x t i m e r {<br />

9 public :<br />

10 u n i x t i m e r ( ) : utime ( 0 ) , stime ( 0 ) , r t i m e ( 0 ) , bStarted ( false )<br />

11 { }<br />

Ôº½ ½º<br />

Ôº¿ ½º<br />

12<br />

Code 1.4.11: Measuring CPU time from C++ using clock command<br />

1 #include <br />

2 #include / / header for clock()<br />

3<br />

4 /∗<br />

5 ∗ simple Timer Class , using clock ( )−command from the time . h ( should work<br />

on a l l p l a t f o r m s )<br />

6 ∗ t h i s class w i l l only r e p o r t the cputime ( not w a l l t i m e )<br />

7 ∗/<br />

8 class simpleTimer {<br />

9 public :<br />

10 simpleTimer ( ) : time ( 0 ) , bStarted ( false )<br />

11 { }<br />

12 void s t a r t ( )<br />

13 {<br />

14 time= clock ( ) ;<br />

15 bStarted =true ;<br />

16 }<br />

17 double getTime ( )<br />

18 {<br />

19 a ssert ( bStarted ) ;<br />

20 return ( clock ( )−time ) / ( double )CLOCKS_PER_SEC; ;<br />

13 void s t a r t ( ) { r t 0 =times (& t0 ) ; bStarted =true ; }<br />

14<br />

15 double stop ( ) {<br />

16 tms t1 ;<br />

17 long r t 1 ;<br />

18 a ssert ( bStarted ) ;<br />

19 r t 1 =times (& t1 ) ;<br />

20 utime = ( ( double ) ( t1 . tms_utime−t0 . tms_utime ) ) /<br />

CLOCKS_PER_SEC∗10000;<br />

21 stime = ( ( double ) ( t1 . tms_stime−t0 . tms_stime ) ) /<br />

CLOCKS_PER_SEC∗10000;<br />

22 r t i m e = ( ( double ) ( r t1−r t 0 ) ) / CLOCKS_PER_SEC∗10000;<br />

23 bStarted = false ;<br />

24 return r t i m e ;<br />

25 }<br />

26<br />

27 double user ( ) { a ssert ( ! bStarted ) ; return utime ; }<br />

28 double system ( ) { a ssert ( ! bStarted ) ; return stime ; }<br />

29 double r e a l ( ) { a ssert ( ! bStarted ) ; return r t i m e ; }<br />

Ôº¾ ½º<br />

Ôº ½º<br />

21 } 33 tms t0 ;<br />

30<br />

31 private :<br />

32 double utime , stime , r t i m e ;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!