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.

10 for n=N<br />

11 % Initialize dense vectors a, b, x (column vectors!)<br />

12 a = ( 1 : n ) ’ ; b = ( n: −1:1) ’ ; x = rand ( n , 1 ) ;<br />

13<br />

14 % Measuring times using MATLAB tic-toc commands<br />

15 t f o o l = 1000; for i =1: nruns , t i c ; y = ( a∗b ’ ) ∗x ; t f o o l =<br />

min ( t f o o l , toc ) ; end ;<br />

16 tsmart = 1000; for i =1: nruns , t i c ; y = a∗dot ( b ’ , x ) ; tsmart =<br />

min ( tsmart , toc ) ; end ;<br />

17 times = [ times ; n , t f o o l , tsmart ] ;<br />

18 end<br />

19<br />

20 % log-scale plot for investigation of asymptotic complexity<br />

21 figure ( ’name ’ , ’ d o t t e n s t i m i n g ’ ) ;<br />

22 loglog ( times ( : , 1 ) , times ( : , 2 ) , ’m+ ’ , . . .<br />

23 times ( : , 1 ) , times ( : , 3 ) , ’ r ∗ ’ , . . .<br />

24 times ( : , 1 ) , times ( : , 1 ) ∗times ( 1 , 3 ) / times ( 1 , 1 ) , ’ k−’ , . . .<br />

25 times ( : , 1 ) , ( times ( : , 1 ) . ^ 2 ) ∗times ( 2 , 2 ) / ( times ( 2 , 1 ) ^2) , ’ b−−’ ) ;<br />

26 xlabel ( ’ { \ b f problem size n } ’ , ’ f o n t s i z e ’ ,14) ;<br />

27 ylabel ( ’ { \ b f average runtime ( s ) } ’ , ’ f o n t s i z e ’ ,14) ;<br />

28 t i t l e ( ’ t i c −toc timing , mininum over 10 runs ’ ) ;<br />

29 legend ( ’ slow e v a l u a t i o n ’ , ’ e f f i c i e n t e v a l u a t i o n ’ , . . .<br />

30 ’O( n ) ’ , ’O( n ^2) ’ , ’ l o c a t i o n ’ , ’ northwest ’ ) ;<br />

Ôº ½º¿<br />

31 p r i n t −depsc2 ’ . . / PICTURES/ d o t t e n s t i m i n g . eps ’ ;<br />

Remark 1.3.5 (Relevance of asymptotic complexity).<br />

Runtimes in Ex. 1.3.2 illustrate that the<br />

asymptotic complexity of an algorithm need not be closely correlated with its overall runtime on<br />

a particular platform,<br />

because on modern computer architectures with multi-level memory hierarchies the memory access<br />

pattern may be more important for efficiency than the mere number of floating point operations, see<br />

[25].<br />

Then, why do we pay so much attention to asymptotic complexity in this course ?<br />

☛ To a certain extent, the asymptotic complexity allows to predict the dependence of the runtime<br />

of a particular implementation of an algorithm on the problem size (for large problems). For<br />

instance, an algorithm with asymptotic complexity O(n 2 ) is likely to take 4× as much time when<br />

the problem size is doubled.<br />

△<br />

1.4 BLAS<br />

Ôº ½º<br />

BLAS = basic linear algebra subroutines<br />

Remark 1.3.4 (Reading off complexity).<br />

Available: “Measurements” t i = t i (n i ) for different n 1 ,n 2 ,...,n N , n i ∈ N<br />

✸<br />

BLAS provides a library of routines with standardized (FORTRAN 77 style) interfaces. These routines<br />

have been implemented efficiently on various platforms and operating systems.<br />

Grouping of BLAS routines (“levels”) according to asymptotic complexity, see [18, Sect. 1.1.12]:<br />

Conjectured: Algebraic dependence t i = Cn α i , α ∈ R<br />

t i = Cn α i ⇒ log(t i ) = log C + α log(n i ) , i = 1,...,N .<br />

If the conjecture holds true, then the points (n i ,t i ) will lie on a straight line with slope α in a<br />

doubly logarithmic plot.<br />

➣ quick “visual test” of conjectured asymptotic complexity<br />

More rigorous: Perform linear regression on (log n i , log t i ), i = 1, ...,N (→ Ch. 6)<br />

△<br />

Ôº ½º¿<br />

• Level 1: vector operations such as scalar products and vector norms.<br />

asymptotic complexity O(n), (with n ˆ= vector length),<br />

e.g.: dot product: ρ = x T y<br />

• Level 2: vector-matrix operations such as matrix-vector multiplications.<br />

asymptotic complexity O(mn),(with (m, n) ˆ= matrix size),<br />

e.g.: matrix×vector multiplication: y = αAx + βy<br />

• Level 3: matrix operations such as matrix additions or multiplications.<br />

asymptotic complexity O(nmk),(with (n,m, k) ˆ= matrix sizes),<br />

e.g.: matrix product: C = AB<br />

Ôº ½º

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

Saved successfully!

Ooh no, something went wrong!