20{
21
22 cout << "\n";
23 cout << "=== Matrix: compare access speeds to native array ===\n";
24 cout << "\n";
25
26 const std::size_t L = 10000;
27
29
32
33 double* B;
34 B = new double[ L * L ];
35
37
38
39
40 for ( std::size_t row = 0; row < L; ++row ) {
41 for ( std::size_t col = 0; col < L; ++col ) {
42 B[ row * L + col ] =
A( row, col );
43 M( row, col, 0 ) =
A( row, col );
44 }
45 }
46
47 cout << " DenseMatrix<double> scaling on a per-element basis via access operator.\n";
50 for ( std::size_t row = 0; row < L; ++row ) {
51 for ( std::size_t col = 0; col < L; ++col ) {
53 }
54 }
59
60 cout << "\n TwoD_Node_Mesh<double> scaling on a per-element basis via access operator.\n";
62 for ( std::size_t row = 0; row < L; ++row ) {
63 for ( std::size_t col = 0; col < L; ++col ) {
64 M( row, col, 0 ) *= 2.0;
65 }
66 }
71
72 cout << "\n Native array scaling on a per-element basis.\n";
74 for ( std::size_t row = 0; row < L; ++row ) {
75 for ( std::size_t col = 0; col < L; ++col ) {
76 B[ row * L + col ] *= 2.0;
77 }
78 }
83
84 delete[] B;
85
86 cout << "The % slow-down for a DenseMatrix was " <<
87 100.0 * ( timeA - timeB ) / ( 0.5 * ( timeA + timeB ) ) << "\n";
88
89 cout << "The % slow-down for a TwoD_Node_Mesh was " <<
90 100.0 * ( timeM - timeB ) / ( 0.5 * ( timeM + timeB ) ) << "\n";
91
93
94 if ( ( timeA - timeB ) / ( 0.5 * ( timeA + timeB ) ) > 0.05 ) {
96 cout << "The % slow-down for a DenseMatrix was " <<
97 100.0 * ( timeA - timeB ) / ( 0.5 * ( timeA + timeB ) ) << "\n";
98 }
99
100 if ( std::abs( timeM - timeB ) / ( 0.5 * ( timeM + timeB ) ) > 0.05 ) {
102 cout << "The % slow-down for a TwoD_Node_Mesh was " <<
103 100.0 * ( timeM - timeB ) / ( 0.5 * ( timeM + timeB ) ) << "\n";
104 }
105
106 if ( failed ) {
107 cout << "\033[1;31;48m * FAILED \033[0m\n";
108 return 1;
109 } else {
110 cout << "\033[1;32;48m * PASSED \033[0m\n";
111 return 0;
112 }
113
114}
A matrix class that constructs a DENSE matrix as a row major std::vector of DenseVectors.
An DenseVector class – a dense vector object.
A simple CPU-clock-tick timer for timing metods.
double get_time() const
Return the time.
void start()
Start the timer & reset stored time to zero.
void print() const
Write a string to cout stating the time taken.
void stop()
Stop the clock & add the current time interval to the previously stored values ready for printing.
void reset()
Pause the clock & add the time interval to the stored cumulative time.
A two dimensional mesh utility object.
double A(1.0)
initial hump amplitude
DenseVector< double > uniform_node_vector(const double &lower, const double &upper, const std::size_t &N)
Return a DENSE vector with the nodal points of a uniform mesh distributed between the upper/lower bou...
void fill_random(CppNoddy::SparseVector< double > &V, const unsigned &num_of_elts)