[126] | 1 | #include <itpp/itbase.h> |
---|
| 2 | |
---|
| 3 | using namespace itpp; |
---|
| 4 | |
---|
| 5 | //These lines are needed for use of cout and endl |
---|
| 6 | using std::cout; |
---|
| 7 | using std::endl; |
---|
| 8 | |
---|
| 9 | mat matmul ( mat &A, mat &B ) { |
---|
| 10 | mat C ( A.rows(),B.cols() ); |
---|
| 11 | int i,j,k; |
---|
| 12 | double sum; |
---|
| 13 | |
---|
| 14 | for ( i=0;i<A.rows();i++ ) { |
---|
| 15 | for ( j=0;j<A.cols();j++ ) { |
---|
| 16 | sum = 0.0; |
---|
| 17 | for ( k=0;k<A.cols();k++ ) { |
---|
| 18 | sum+=A._elem ( i,k ) *B._elem ( k,j ); |
---|
| 19 | } |
---|
| 20 | C ( i,j ) = sum; |
---|
| 21 | } |
---|
| 22 | } |
---|
| 23 | return C; |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | void matmul2 ( int n, double *A, double *B, double *C ) { |
---|
| 27 | int i,j,k; |
---|
| 28 | double sum; |
---|
| 29 | |
---|
| 30 | for ( i=0;i<n;i++ ) { |
---|
| 31 | for ( j=0;j<n;j++ ) { |
---|
| 32 | sum = 0.0; |
---|
| 33 | for ( k=0;k<n;k++ ) { |
---|
| 34 | sum+=A [ i*n+k ] * B [ k*n+j ]; |
---|
| 35 | } |
---|
| 36 | C[ i*n+j] = sum; |
---|
| 37 | } |
---|
| 38 | } |
---|
| 39 | // return C; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | int main() { |
---|
| 43 | Real_Timer tt; |
---|
| 44 | vec exec_times ( 4 ); |
---|
| 45 | vec exec_times_b ( 4 ); |
---|
| 46 | vec exec_times_c ( 4 ); |
---|
| 47 | |
---|
| 48 | mat A; |
---|
| 49 | mat B; |
---|
| 50 | mat C; |
---|
| 51 | |
---|
| 52 | vec vn="5 50 200 500"; |
---|
| 53 | int n; |
---|
| 54 | |
---|
| 55 | for ( int i=0;i<vn.length();i++ ) { |
---|
| 56 | n = vn ( i ); |
---|
| 57 | A = randu ( n,n ); |
---|
| 58 | B = randu ( n,n ); |
---|
| 59 | |
---|
| 60 | tt.tic(); |
---|
| 61 | for ( int i=0;i<10;i++ ) {C = matmul ( A,B );} |
---|
| 62 | exec_times ( i ) =tt.toc(); |
---|
| 63 | |
---|
| 64 | tt.tic(); |
---|
| 65 | for ( int i=0;i<10;i++ ) {C = A*B;} |
---|
| 66 | exec_times_b ( i ) =tt.toc(); |
---|
| 67 | |
---|
| 68 | C = zeros(n,n); |
---|
| 69 | tt.tic(); |
---|
| 70 | for ( int i=0;i<10;i++ ) { matmul2(n,A._data(),B._data(),C._data());} |
---|
| 71 | exec_times_c ( i ) =tt.toc(); |
---|
| 72 | } |
---|
| 73 | cout << exec_times <<endl; |
---|
| 74 | cout << exec_times_b <<endl; |
---|
| 75 | cout << exec_times_c <<endl; |
---|
| 76 | |
---|
| 77 | it_file itf ( "blas_test.it" ); |
---|
| 78 | itf << Name ( "exec_times" ) <<exec_times; |
---|
| 79 | itf << Name ( "exec_times_b" ) <<exec_times_b; |
---|
| 80 | itf << Name ( "exec_times_c" ) <<exec_times_c; |
---|
| 81 | |
---|
| 82 | return 0; |
---|
| 83 | } |
---|