21.06.2014 Views

Numerical Methods Contents - SAM

Numerical Methods Contents - SAM

Numerical Methods Contents - SAM

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

4 {<br />

5 a ssert ( this−>n==B. n && this−>m==B .m) ; / / only support square matrices<br />

6 ColumnMajorMatrix C( n ,m) ; / / important: must be zero: (done in constructor)<br />

7 double alpha ( 1 . 0 ) , beta ( 1 . 0 ) ;<br />

8 cblas_dgemm ( CblasColMajor , CblasNoTrans , CblasNoTrans , n , m,<br />

B .m, alpha , data , n , B . data , B . n , beta , C. data , C. n ) ;<br />

9 return C;<br />

10 }<br />

Code 1.4.10: Timings of different Matrix Multiplications in C++, Ex. 1.4.1<br />

1 #include <br />

2 #include < cstdio ><br />

3 #include < c s t d l i b ><br />

4 #include <br />

5 #include " simpleTimer . h "<br />

6 #include " unixTimer . h "<br />

7 #include " ColumnMajorMatrix . h "<br />

8 /∗ Main Routine f o r the t i m i n g o f d i f f e r e n t<br />

9 ∗ M a trix M a trix M u l t i p l i c a t i o n implementations ∗/<br />

10 i n t main ( i n t argc , char ∗ const argv [ ] ) {<br />

Ôº ½º<br />

13 simpleTimer watch ;<br />

11<br />

12 double T0(1 e20 ) ,T1(1 e20 ) ,T2(1 e20 ) ,T3(1 e20 ) ;<br />

33 p r i n t f ( "N: %i d o t M u l t i p l y : %g , e r r o r : %g \ n " ,n , T1 ,D. CalcErr (C) ) ;<br />

34 p r i n t f ( "N: %i gemvMultiply : %g , e r r o r : %g \ n " ,n , T2 , E . CalcErr (C) ) ;<br />

35 p r i n t f ( "N: %i gemmMultiply : %g , e r r o r : %g \ n " ,n , T3 , F . CalcErr (C) ) ;<br />

36 }<br />

CPU−Time: [s]<br />

trivial<br />

dot<br />

gemv<br />

gemm<br />

Slope Order<br />

10 3<br />

10 2<br />

10 1<br />

10 0<br />

10 −1<br />

10 −2<br />

10 −3<br />

10 −4<br />

10 −5<br />

10 −6<br />

10 0 3<br />

10 1 10 2<br />

Matrix Size: N<br />

10 3 10 4<br />

✁ timings for different implementations of matrix<br />

multiplication (see C++-codes above)<br />

OS: Mac OS X<br />

Processor: Intel Core 2 Duo 2GB 667 MHz DDR2<br />

SDRAM<br />

Compiler: intel v.11.0 (-O3 option)<br />

✸<br />

Ôº ½º<br />

14 i n t rep ( 1 ) , n ( 5 ) ;<br />

15 i f ( argc >1) n= a t o i ( argv [ 1 ] ) ;<br />

16 i f ( argc >2) rep= a t o i ( argv [ 2 ] ) ;<br />

17 / / Declare Input Data<br />

18 ColumnMajorMatrix A( n , n ) ;<br />

19 A . i n i tRand ( ) ; / / A.initGrow();<br />

20 ColumnMajorMatrix B(A) ;<br />

21 / / The Results:<br />

22 ColumnMajorMatrix C( n , n ) ,D( n , n ) ,E( n , n ) ,F ( n , n ) ;<br />

23 / / loop for repetitions (always take timing results over several measurements!)<br />

24 for ( i n t r =0; r

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

Saved successfully!

Ooh no, something went wrong!