CppNoddy  0.92
Loading...
Searching...
No Matches
Functions
MatrixMultiply_lapack.cpp File Reference
#include <Timer.h>
#include <Types.h>
#include <Utility.h>

Go to the source code of this file.

Functions

int main ()
 

Function Documentation

◆ main()

int main ( )

Definition at line 19 of file MatrixMultiply_lapack.cpp.

20{
21
22 cout << "\n";
23 cout << "=== Matrix: Native/BLAS multiplication =============\n";
24 cout << "\n";
25
26 DenseMatrix<double> a( 2, 3, 0.0 );
27 DenseMatrix<double> b( 3, 2, 0.0 );
28 DenseMatrix<double> c( 2, 2, 0.0 );
29 a( 0, 0 ) = 1.0;
30 a( 0, 1 ) = 2.0;
31 a( 0, 2 ) = 3.0;
32 a( 1, 0 ) = 4.0;
33 a( 1, 1 ) = 5.0;
34 a( 1, 2 ) = 6.0;
35 b( 0, 0 ) = 1.0;
36 b( 0, 1 ) = 2.0;
37 b( 1, 0 ) = 3.0;
38 b( 1, 1 ) = 4.0;
39 b( 2, 0 ) = 5.0;
40 b( 2, 1 ) = 6.0;
41 DenseMatrix<double> answer( 2, 2, 0.0 );
42 answer( 0, 0 ) = 22.0;
43 answer( 0, 1 ) = 28.0;
44 answer( 1, 0 ) = 49.0;
45 answer( 1, 1 ) = 64.0;
46
47 c = a.multiply( b );
48
49 const double tol = 1.e-10;
50 bool failed = false;
51 c.sub( answer );
52 if ( c.inf_norm() > tol )
53 {
54 std::cout << " Infinity norm of error = " << c.inf_norm() << "\n";
55 std::cout << " Native method : Simple (2x3) * (3x2) matrix mult failed\n";
56 failed = true;
57 }
58 else
59 {
60 std::cout << " Native method : Simple (2x3) * (3x2) matrix mult passed \n";
61 }
62
63 // if LAPACK Libs are present, then do the same test
64#ifdef LAPACK
65 DenseMatrix<double> cblas( 2, 2, 0.0 );
66 cblas = Utility::multiply( a, b );
67 cblas.sub( answer );
68 if ( cblas.inf_norm() > tol )
69 {
70 std::cout << " Infinity norm of error = " << cblas.inf_norm() << "\n";
71 std::cout << " BLAS : Simple (2x3) * (3x2) matrix mult failed \n";
72 failed = true;
73 }
74 else
75 {
76 std::cout << " BLAS : Simple (2x3) * (3x2) matrix mult test passed \n";
77 }
78#endif
79
80#ifdef TIME
81 Timer timer;
82 for ( int N = 128; N <= 2048 ; N *= 2 )
83 {
84 std::cout << "\n --- Filling arrays of size " << N << "x" << N << "\n";
85
86 // random array data
87 DenseMatrix<double> A( N, N, 0.0 );
88 Utility::fill_random( A );
89 DenseMatrix<double> B( N, N, 0.0 );
90 Utility::fill_random( B );
91 DenseMatrix<double> C( N, N, 0.0 );
92
93 timer.start();
94 C = A.multiply( B );
95 timer.stop();
96 std::cout << "\n Native N^3 multiplication method :\n";
97 timer.print();
98 timer.reset();
99
100#ifdef LAPACK
101
102 DenseMatrix<double> Cblas( N, N, 0.0 );
103 timer.start();
104 Cblas = Utility::multiply( A, B );
105 timer.stop();
106 std::cout << "\n BLAS multiplication method :\n";
107 timer.print();
108 timer.reset();
109 // Check the difference between the methods
110 Cblas.sub( C );
111 if ( Cblas.inf_norm() > tol )
112 {
113 std::cout << " Infinity norm of error = " << Cblas.inf_norm() << "\n";
114 std::cout << " BLAS & native matrix multiplication disagree! \n";
115 failed = true;
116 }
117#endif // LAPACK
118
119 }
120#endif // TIME
121
122 if ( failed )
123 {
124 cout << "\033[1;31;48m * FAILED \033[0m\n";
125 }
126 else
127 {
128 cout << "\033[1;32;48m * PASSED \033[0m\n";
129 }
130}
A matrix class that constructs a DENSE matrix as a row major std::vector of DenseVectors.
Definition: DenseMatrix.h:25
A simple CPU-clock-tick timer for timing metods.
Definition: Timer.h:19
void start()
Start the timer & reset stored time to zero.
Definition: Timer.cpp:12
void print() const
Write a string to cout stating the time taken.
Definition: Timer.cpp:59
void stop()
Stop the clock & add the current time interval to the previously stored values ready for printing.
Definition: Timer.cpp:17
void reset()
Pause the clock & add the time interval to the stored cumulative time.
Definition: Timer.cpp:26
double A(1.0)
initial hump amplitude
DenseMatrix< double > multiply(DenseMatrix< double > &A, DenseMatrix< double > &B)
BLAS wrapper to do DOUBLE DENSE A_{MxK} * B_{KxN} = C_{MxN} Since this is a Fortran library,...
Definition: Utility.cpp:225

References CppNoddy::DenseMatrix< _Type >::inf_norm(), CppNoddy::DenseMatrix< _Type >::multiply(), CppNoddy::Utility::multiply(), CppNoddy::Timer::print(), CppNoddy::Timer::reset(), CppNoddy::Timer::start(), CppNoddy::Timer::stop(), and CppNoddy::DenseMatrix< _Type >::sub().

© 2012

R.E. Hewitt