1 | #include <iostream> |
---|
2 | using namespace std; |
---|
3 | #include <chrono> |
---|
4 | using namespace chrono; |
---|
5 | |
---|
6 | #include "caf/actor_ostream.hpp" |
---|
7 | #include "caf/caf_main.hpp" |
---|
8 | #include "caf/event_based_actor.hpp" |
---|
9 | #include "caf/all.hpp" |
---|
10 | using namespace caf; |
---|
11 | |
---|
12 | struct WorkMsg; |
---|
13 | |
---|
14 | CAF_BEGIN_TYPE_ID_BLOCK(custom_types_1, first_custom_type_id) |
---|
15 | CAF_ADD_TYPE_ID(custom_types_1, (WorkMsg)) |
---|
16 | CAF_END_TYPE_ID_BLOCK(custom_types_1) |
---|
17 | |
---|
18 | // --(rst-foo-begin)-- |
---|
19 | struct WorkMsg { |
---|
20 | unsigned int b; |
---|
21 | }; |
---|
22 | |
---|
23 | template <class Inspector> |
---|
24 | bool inspect(Inspector& f, WorkMsg& x) { |
---|
25 | return f.object(x).fields(f.field("b", x.b)); |
---|
26 | } |
---|
27 | |
---|
28 | unsigned int xr = 3'072, xc = 3'072, yc = 3'072, Processors = 1, MaxProcs = 48; // default values |
---|
29 | |
---|
30 | time_point<steady_clock> starttime; |
---|
31 | actor * actors; |
---|
32 | WorkMsg ** work_msgs; |
---|
33 | unsigned int actorCnt = 0; |
---|
34 | |
---|
35 | int wCount = 0; |
---|
36 | |
---|
37 | int ** w_Z, ** w_X, ** w_Y; |
---|
38 | |
---|
39 | class MatrixMult : public event_based_actor { |
---|
40 | behavior make_behavior() override { |
---|
41 | return { |
---|
42 | [=]( const WorkMsg & val ) -> void { |
---|
43 | unsigned int w = val.b; |
---|
44 | for ( unsigned int i = 0; i < yc; i += 1 ) { // multiply X_row by Y_col and sum products |
---|
45 | w_Z[w][i] = 0; |
---|
46 | for ( unsigned int j = 0; j < xc; j += 1 ) { |
---|
47 | w_Z[w][i] += w_X[w][j] * w_Y[j][i]; |
---|
48 | } // for |
---|
49 | } // for |
---|
50 | |
---|
51 | if ( __atomic_add_fetch( &actorCnt, 1, __ATOMIC_SEQ_CST ) == xr ) { |
---|
52 | aout(this) << (steady_clock::now() - starttime).count() * ( MaxProcs / Processors ) / 1'000'000'000.0 << endl; |
---|
53 | } // if |
---|
54 | this->quit(); |
---|
55 | return; |
---|
56 | } |
---|
57 | }; |
---|
58 | } |
---|
59 | public: |
---|
60 | MatrixMult( caf::actor_config & cfg ) : event_based_actor( cfg ) {} |
---|
61 | }; // MatrixMult |
---|
62 | |
---|
63 | void caf_main( actor_system & sys ) { |
---|
64 | starttime = steady_clock::now(); |
---|
65 | |
---|
66 | for ( unsigned int i = 0; i < xr; i += 1 ) { // create actors |
---|
67 | actors[i] = sys.spawn<MatrixMult>(); |
---|
68 | } // for |
---|
69 | |
---|
70 | caf::scoped_actor self{sys}; |
---|
71 | for ( unsigned int i = 0; i < xr; i += 1 ) { // start actors |
---|
72 | self->send( actors[i], WorkMsg{i} ); |
---|
73 | } // for |
---|
74 | } // caf_main |
---|
75 | |
---|
76 | int main( int argc, char * argv[] ) { |
---|
77 | switch ( argc ) { |
---|
78 | case 6: |
---|
79 | if ( strcmp( argv[5], "d" ) != 0 ) { // default ? |
---|
80 | MaxProcs = stoi( argv[5] ); |
---|
81 | if ( MaxProcs < 1 ) goto Usage; |
---|
82 | } // if |
---|
83 | case 5: |
---|
84 | if ( strcmp( argv[4], "d" ) != 0 ) { // default ? |
---|
85 | Processors = stoi( argv[4] ); |
---|
86 | if ( Processors < 1 ) goto Usage; |
---|
87 | } // if |
---|
88 | case 4: |
---|
89 | if ( strcmp( argv[3], "d" ) != 0 ) { // default ? |
---|
90 | xr = stoi( argv[3] ); |
---|
91 | if ( xr < 1 ) goto Usage; |
---|
92 | } // if |
---|
93 | case 3: |
---|
94 | if ( strcmp( argv[2], "d" ) != 0 ) { // default ? |
---|
95 | xc = stoi( argv[2] ); |
---|
96 | if ( xc < 1 ) goto Usage; |
---|
97 | } // if |
---|
98 | case 2: |
---|
99 | if ( strcmp( argv[1], "d" ) != 0 ) { // default ? |
---|
100 | yc = stoi( argv[1] ); |
---|
101 | if ( yc < 1 ) goto Usage; |
---|
102 | } // if |
---|
103 | case 1: // use defaults |
---|
104 | break; |
---|
105 | default: |
---|
106 | Usage: |
---|
107 | cerr << "Usage: " << argv[0] |
---|
108 | << " [ yc (> 0) | 'd' (default " << yc |
---|
109 | << ") ] [ xc (> 0) | 'd' (default " << xc |
---|
110 | << ") ] [ xr (> 0) | 'd' (default " << xr |
---|
111 | << ") ] [ processors (> 0) | 'd' (default " << Processors |
---|
112 | << ") ]" << endl; |
---|
113 | exit( EXIT_FAILURE ); |
---|
114 | } // switch |
---|
115 | |
---|
116 | xr = xr / ( MaxProcs / Processors ); |
---|
117 | |
---|
118 | //cout << Actors << " " << Set << " " << Rounds << " " << Processors << endl; |
---|
119 | actors = new actor[xr]; |
---|
120 | |
---|
121 | caf::core::init_global_meta_objects(); |
---|
122 | caf::exec_main_init_meta_objects<id_block::custom_types_1>(); |
---|
123 | |
---|
124 | caf::actor_system_config cfg; |
---|
125 | cfg.set( "caf.scheduler.max-threads", Processors ); |
---|
126 | caf::actor_system system { cfg }; |
---|
127 | |
---|
128 | unsigned int r, c; |
---|
129 | int * Z[xr], * X[xr], * Y[xc]; |
---|
130 | |
---|
131 | w_Z = Z; |
---|
132 | w_Y = Y; |
---|
133 | w_X = X; |
---|
134 | |
---|
135 | for ( r = 0; r < xr; r += 1 ) { // create/initialize X matrix |
---|
136 | X[r] = new int[xc]; |
---|
137 | for ( c = 0; c < xc; c += 1 ) { |
---|
138 | X[r][c] = r * c % 37; // for timing |
---|
139 | } // for |
---|
140 | } // for |
---|
141 | for ( r = 0; r < xc; r += 1 ) { // create/initialize Y matrix |
---|
142 | Y[r] = new int[yc]; |
---|
143 | for ( c = 0; c < yc; c += 1 ) { |
---|
144 | Y[r][c] = r * c % 37; // for timing |
---|
145 | } // for |
---|
146 | } // for |
---|
147 | for ( r = 0; r < xr; r += 1 ) { // create Z matrix |
---|
148 | Z[r] = new int[yc]; |
---|
149 | } // for |
---|
150 | |
---|
151 | caf::exec_main<>(caf_main, argc, argv); |
---|
152 | |
---|
153 | for ( r = 0; r < xr; r += 1 ) { // deallocate X and Z matrices |
---|
154 | delete [] X[r]; |
---|
155 | delete [] Z[r]; |
---|
156 | } // for |
---|
157 | for ( r = 0; r < xc; r += 1 ) { // deallocate Y matrix |
---|
158 | delete [] Y[r]; |
---|
159 | } // for |
---|
160 | } // main |
---|
161 | //CAF_MAIN() |
---|
162 | |
---|
163 | // /usr/bin/time -f "%Uu %Ss %Er %Mkb" a.out |
---|
164 | |
---|
165 | // Local Variables: // |
---|
166 | // compile-command: "g++-10 -Wall -O3 -std=c++17 -ICAF/actor-framework/libcaf_core -ICAF/actor-framework/libcaf_core/caf -ICAF/actor-framework/build/libcaf_core -LCAF/actor-framework/build/libcaf_core -LCAF/actor-framework/build/libcaf_io CAFMatrix.cpp -lcaf_io -lcaf_core -Wl,-rpath=CAF/actor-framework/build/libcaf_core" // |
---|
167 | // End: // |
---|