1 | #include <iostream> |
---|
2 | #include <algorithm> |
---|
3 | using namespace std; |
---|
4 | #include <chrono> |
---|
5 | using namespace chrono; |
---|
6 | #include <uActor.h> |
---|
7 | |
---|
8 | unsigned int xr = 3'072, xc = 3'072, yc = 3'072, Processors = 1; // default values |
---|
9 | |
---|
10 | struct WorkMsg : public uActor::Message { // derived message |
---|
11 | int * Z; |
---|
12 | const int * const X, * const * Y; |
---|
13 | WorkMsg( int Z[], const int X[], const int * const Y[] ) : |
---|
14 | Message( uActor::Finished ), Z( Z ), X( X ), Y( Y ) {} // one-shot |
---|
15 | }; // WorkMsg |
---|
16 | |
---|
17 | time_point<steady_clock> starttime; |
---|
18 | |
---|
19 | _Actor MatrixMult { |
---|
20 | Allocation receive( Message & msg ) { |
---|
21 | Case ( WorkMsg, msg ) { |
---|
22 | int * z = msg_d->Z; // optimizations |
---|
23 | const int * const x = msg_d->X, * const * y = msg_d->Y; |
---|
24 | for ( unsigned int i = 0; i < yc; i += 1 ) { // multiply X_row by Y_col and sum products |
---|
25 | z[i] = 0; |
---|
26 | for ( unsigned int j = 0; j < xc; j += 1 ) { |
---|
27 | z[i] += x[j] * y[j][i]; |
---|
28 | } // for |
---|
29 | } // for |
---|
30 | } // Case |
---|
31 | |
---|
32 | return Finished; |
---|
33 | } // MatrixMult:::receive |
---|
34 | }; // MatrixMult |
---|
35 | |
---|
36 | int main( int argc, char * argv[] ) { |
---|
37 | locale loc( getenv("LANG") ); |
---|
38 | cout.imbue( loc ); |
---|
39 | |
---|
40 | switch ( argc ) { |
---|
41 | case 5: |
---|
42 | if ( strcmp( argv[4], "d" ) != 0 ) { // default ? |
---|
43 | Processors = stoi( argv[4] ); |
---|
44 | if ( Processors < 1 ) goto Usage; |
---|
45 | } // if |
---|
46 | case 4: |
---|
47 | if ( strcmp( argv[3], "d" ) != 0 ) { // default ? |
---|
48 | xr = stoi( argv[3] ); |
---|
49 | if ( xr < 1 ) goto Usage; |
---|
50 | } // if |
---|
51 | case 3: |
---|
52 | if ( strcmp( argv[2], "d" ) != 0 ) { // default ? |
---|
53 | xc = stoi( argv[2] ); |
---|
54 | if ( xc < 1 ) goto Usage; |
---|
55 | } // if |
---|
56 | case 2: |
---|
57 | if ( strcmp( argv[1], "d" ) != 0 ) { // default ? |
---|
58 | yc = stoi( argv[1] ); |
---|
59 | if ( yc < 1 ) goto Usage; |
---|
60 | } // if |
---|
61 | case 1: // use defaults |
---|
62 | break; |
---|
63 | default: |
---|
64 | Usage: |
---|
65 | cerr << "Usage: " << argv[0] |
---|
66 | << " [ yc (> 0) | 'd' (default " << yc |
---|
67 | << ") ] [ xc (> 0) | 'd' (default " << xc |
---|
68 | << ") ] [ xr (> 0) | 'd' (default " << xr |
---|
69 | << ") ] [ processors (> 0) | 'd' (default " << Processors |
---|
70 | << ") ]" << endl; |
---|
71 | exit( EXIT_FAILURE ); |
---|
72 | } // switch |
---|
73 | |
---|
74 | unsigned int r, c; |
---|
75 | int * Z[xr], * X[xr], * Y[xc]; |
---|
76 | |
---|
77 | for ( r = 0; r < xr; r += 1 ) { // create/initialize X matrix |
---|
78 | X[r] = new int[xc]; |
---|
79 | for ( c = 0; c < xc; c += 1 ) { |
---|
80 | X[r][c] = r * c % 37; // for timing |
---|
81 | } // for |
---|
82 | } // for |
---|
83 | for ( r = 0; r < xc; r += 1 ) { // create/initialize Y matrix |
---|
84 | Y[r] = new int[yc]; |
---|
85 | for ( c = 0; c < yc; c += 1 ) { |
---|
86 | Y[r][c] = r * c % 37; // for timing |
---|
87 | } // for |
---|
88 | } // for |
---|
89 | for ( r = 0; r < xr; r += 1 ) { // create Z matrix |
---|
90 | Z[r] = new int[yc]; |
---|
91 | } // for |
---|
92 | |
---|
93 | uExecutor * executor = new uExecutor( Processors, Processors, Processors == 1 ? 1 : Processors * 32, true, 0 ); |
---|
94 | uActor::start( executor ); // start actor system |
---|
95 | uNoCtor<MatrixMult> * multiply = new uNoCtor<MatrixMult>[xr]; |
---|
96 | uNoCtor<WorkMsg> * workMsg = new uNoCtor<WorkMsg>[xr]; |
---|
97 | |
---|
98 | for ( unsigned int r = 0; r < xr; r += 1 ) { |
---|
99 | multiply[r].ctor(); |
---|
100 | workMsg[r].ctor( Z[r], X[r], (const int * const *)Y ); |
---|
101 | } // for |
---|
102 | |
---|
103 | starttime = steady_clock::now(); |
---|
104 | for ( unsigned int r = 0; r < xr; r += 1 ) { |
---|
105 | *multiply[r] | *workMsg[r]; |
---|
106 | } // for |
---|
107 | |
---|
108 | uActor::stop(); // wait for all actors to terminate |
---|
109 | |
---|
110 | cout << (steady_clock::now() - starttime).count() / 1'000'000'000.0 << endl; |
---|
111 | |
---|
112 | for ( r = 0; r < xr; r += 1 ) { // deallocate X and Z matrices |
---|
113 | delete [] X[r]; |
---|
114 | delete [] Z[r]; |
---|
115 | } // for |
---|
116 | for ( r = 0; r < xc; r += 1 ) { // deallocate Y matrix |
---|
117 | delete [] Y[r]; |
---|
118 | } // for |
---|
119 | |
---|
120 | // malloc_stats(); |
---|
121 | } // main |
---|
122 | |
---|
123 | // /usr/bin/time -f "%Uu %Ss %Er %Mkb" a.out |
---|
124 | |
---|
125 | // Local Variables: // |
---|
126 | // compile-command: "u++-work -g -Wall -Wextra -O3 -nodebug -DNDEBUG -multi uC++Matrix.cc" // |
---|
127 | // End: // |
---|