CppNoddy  0.92
Loading...
Searching...
No Matches
MatrixAccess.cpp
Go to the documentation of this file.
1/// \file MatrixAccess.cpp
2/// \ingroup Tests
3/// \ingroup Matrix
4/// A quick check that the overhead associated with the matrix container
5/// class is less than 5% compared to a native array.
6
7#include <algorithm>
8
9#include <Timer.h>
10#include <Types.h>
11#include <Utility.h>
12#include <TwoD_Node_Mesh.h>
13
14#include "../Utils_Fill.h"
15
16using namespace CppNoddy;
17using namespace std;
18
19int main()
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 // DenseMatrix object
28 DenseMatrix<double> A( L, L, 0.0 );
29 // A TwoD_Node_Mesh object (coordinates of the mesh don't matter)
30 const DenseVector<double> coords( Utility::uniform_node_vector( 0.0, 1.0, L ) );
31 TwoD_Node_Mesh<double> M( coords, coords, 1 );
32 // native array
33 double* B;
34 B = new double[ L * L ];
35 // put some junk into the DenseMatrix
37
38
39 // copy the random matrix to a native array & the mesh object
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";
48 Timer timer;
49 timer.start();
50 for ( std::size_t row = 0; row < L; ++row ) {
51 for ( std::size_t col = 0; col < L; ++col ) {
52 A( row, col ) *= 2.0;
53 }
54 }
55 timer.stop();
56 double timeA = timer.get_time();
57 timer.print();
58 timer.reset();
59
60 cout << "\n TwoD_Node_Mesh<double> scaling on a per-element basis via access operator.\n";
61 timer.start();
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 }
67 timer.stop();
68 double timeM = timer.get_time();
69 timer.print();
70 timer.reset();
71
72 cout << "\n Native array scaling on a per-element basis.\n";
73 timer.start();
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 }
79 timer.stop();
80 double timeB = timer.get_time();
81 timer.print();
82 timer.reset();
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
92 bool failed( false );
93 // fail if there is more than a 5% overhead between DenseMatrix & native
94 if ( ( timeA - timeB ) / ( 0.5 * ( timeA + timeB ) ) > 0.05 ) {
95 failed = true;
96 cout << "The % slow-down for a DenseMatrix was " <<
97 100.0 * ( timeA - timeB ) / ( 0.5 * ( timeA + timeB ) ) << "\n";
98 }
99 // fail if there is more than a 5% overhead between DenseMatrix & TwoD_Node_Mesh
100 if ( std::abs( timeM - timeB ) / ( 0.5 * ( timeM + timeB ) ) > 0.05 ) {
101 failed = true;
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}
int main()
A spec for the CppNoddy Timer object.
A specification for a two dimensional mesh object.
A spec for a collection of utility functions.
A matrix class that constructs a DENSE matrix as a row major std::vector of DenseVectors.
Definition: DenseMatrix.h:25
An DenseVector class – a dense vector object.
Definition: DenseVector.h:34
A simple CPU-clock-tick timer for timing metods.
Definition: Timer.h:19
double get_time() const
Return the time.
Definition: Timer.cpp:34
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
A two dimensional mesh utility object.
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...
Definition: Utility.cpp:113
A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechani...
void fill_random(CppNoddy::SparseVector< double > &V, const unsigned &num_of_elts)
Definition: Utils_Fill.h:53

© 2012

R.E. Hewitt