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 IntMsg; |
---|
13 | struct CharMsg; |
---|
14 | |
---|
15 | CAF_BEGIN_TYPE_ID_BLOCK(custom_types_1, first_custom_type_id) |
---|
16 | CAF_ADD_TYPE_ID(custom_types_1, (IntMsg)) |
---|
17 | CAF_ADD_TYPE_ID(custom_types_1, (CharMsg)) |
---|
18 | CAF_END_TYPE_ID_BLOCK(custom_types_1) |
---|
19 | |
---|
20 | // --(rst-foo-begin)-- |
---|
21 | struct IntMsg { |
---|
22 | int val; |
---|
23 | }; |
---|
24 | struct CharMsg { |
---|
25 | char val; |
---|
26 | }; |
---|
27 | |
---|
28 | template <class Inspector> |
---|
29 | bool inspect(Inspector& f, IntMsg& x) { |
---|
30 | return f.object(x).fields(f.field("val", x.val)); |
---|
31 | } |
---|
32 | |
---|
33 | template <class Inspector> |
---|
34 | bool inspect(Inspector& f, CharMsg& x) { |
---|
35 | return f.object(x).fields(f.field("val", x.val)); |
---|
36 | } |
---|
37 | |
---|
38 | size_t Messages = 100000, Processors = 4, Times = 100, Factor = 20; |
---|
39 | |
---|
40 | time_point<steady_clock> starttime; |
---|
41 | actor * client; |
---|
42 | actor * servers; |
---|
43 | unsigned int actorCnt = 0; |
---|
44 | |
---|
45 | class Client : public event_based_actor { |
---|
46 | IntMsg * intmsg; |
---|
47 | CharMsg * charmsg; |
---|
48 | size_t results = 0, times = 0; |
---|
49 | |
---|
50 | void reset() { |
---|
51 | times += 1; |
---|
52 | if ( times == Times ) { |
---|
53 | for ( unsigned int i = 0; i < Messages; i += 1 ) { |
---|
54 | this->send( servers[i], 0 ); |
---|
55 | } // for |
---|
56 | if ( __atomic_add_fetch( &actorCnt, 1, __ATOMIC_SEQ_CST ) == Messages + 1 ) { |
---|
57 | aout(this) << (steady_clock::now() - starttime).count() * Factor / 1'000'000'000.0 << endl; |
---|
58 | } // if |
---|
59 | this->quit(); |
---|
60 | return; |
---|
61 | } |
---|
62 | results = 0; |
---|
63 | this->send( this, 0 ); |
---|
64 | } |
---|
65 | |
---|
66 | behavior make_behavior() override { |
---|
67 | return { |
---|
68 | [=]( IntMsg & msg ) -> void { |
---|
69 | results++; |
---|
70 | if ( results == 2 * Messages ) reset(); |
---|
71 | }, |
---|
72 | [=]( CharMsg & msg ) -> void { |
---|
73 | results++; |
---|
74 | if ( results == 2 * Messages ) reset(); |
---|
75 | }, |
---|
76 | [=]( int & ) -> void { |
---|
77 | for ( size_t i = 0; i < Messages; i += 1 ) { // send out work |
---|
78 | this->send( servers[i], intmsg[i] ); |
---|
79 | this->send( servers[i], charmsg[i] ); |
---|
80 | } |
---|
81 | } |
---|
82 | }; |
---|
83 | } |
---|
84 | public: |
---|
85 | Client( caf::actor_config & cfg ) : event_based_actor( cfg ) { |
---|
86 | intmsg = new IntMsg[Messages]; |
---|
87 | charmsg = new CharMsg[Messages]; |
---|
88 | } |
---|
89 | ~Client() { |
---|
90 | delete [] charmsg; |
---|
91 | delete [] intmsg; |
---|
92 | } |
---|
93 | }; // Client |
---|
94 | |
---|
95 | class Server : public event_based_actor { |
---|
96 | behavior make_behavior() override { |
---|
97 | return { |
---|
98 | [=]( IntMsg & msg ) -> void { |
---|
99 | msg.val = 7; |
---|
100 | send( *client, msg ); |
---|
101 | }, |
---|
102 | [=]( CharMsg & msg ) -> void { |
---|
103 | msg.val = 'x'; |
---|
104 | this->send( *client, msg ); |
---|
105 | }, |
---|
106 | [=]( int & ) -> void { |
---|
107 | if ( __atomic_add_fetch( &actorCnt, 1, __ATOMIC_SEQ_CST ) == Messages + 1 ) { |
---|
108 | aout(this) << (steady_clock::now() - starttime).count() / 1'000'000'000.0 << endl; |
---|
109 | } // if |
---|
110 | this->quit(); |
---|
111 | return; |
---|
112 | } |
---|
113 | }; |
---|
114 | } |
---|
115 | public: |
---|
116 | Server( caf::actor_config & cfg ) : event_based_actor( cfg ) {} |
---|
117 | }; // Server |
---|
118 | |
---|
119 | void caf_main( actor_system & sys ) { |
---|
120 | starttime = steady_clock::now(); |
---|
121 | |
---|
122 | *client = sys.spawn<Client>(); |
---|
123 | |
---|
124 | for ( unsigned int i = 0; i < Messages; i += 1 ) { // create actors |
---|
125 | servers[i] = sys.spawn<Server>(); |
---|
126 | } // for |
---|
127 | |
---|
128 | caf::scoped_actor self{sys}; |
---|
129 | self->send( *client, 0 ); |
---|
130 | } // caf_main |
---|
131 | |
---|
132 | int main( int argc, char * argv[] ) { |
---|
133 | switch ( argc ) { |
---|
134 | case 5: |
---|
135 | if ( strcmp( argv[4], "d" ) != 0 ) { // default ? |
---|
136 | Factor = stoi( argv[4] ); |
---|
137 | if ( Factor < 1 ) goto Usage; |
---|
138 | } // if |
---|
139 | case 4: |
---|
140 | if ( strcmp( argv[3], "d" ) != 0 ) { // default ? |
---|
141 | Times = stoi( argv[3] ); |
---|
142 | if ( Times < 1 ) goto Usage; |
---|
143 | } // if |
---|
144 | case 3: |
---|
145 | if ( strcmp( argv[2], "d" ) != 0 ) { // default ? |
---|
146 | Processors = stoi( argv[2] ); |
---|
147 | if ( Processors < 1 ) goto Usage; |
---|
148 | } // if |
---|
149 | case 2: |
---|
150 | if ( strcmp( argv[1], "d" ) != 0 ) { // default ? |
---|
151 | Messages = stoi( argv[1] ); |
---|
152 | if ( Messages < 1 ) goto Usage; |
---|
153 | } // if |
---|
154 | case 1: // use defaults |
---|
155 | break; |
---|
156 | default: |
---|
157 | Usage: |
---|
158 | cerr << "Usage: " << argv[0] |
---|
159 | << ") ] [ messages (> 0) | 'd' (default " << Messages |
---|
160 | << ") ] [ processors (> 0) | 'd' (default " << Processors |
---|
161 | << ") ] [ Times (> 0) | 'd' (default " << Times |
---|
162 | << ") ]" << endl; |
---|
163 | exit( EXIT_FAILURE ); |
---|
164 | } // switch |
---|
165 | |
---|
166 | Times = Times / Factor; |
---|
167 | |
---|
168 | //cout << Actors << " " << Set << " " << Rounds << " " << Processors << endl; |
---|
169 | servers = new actor[Messages]; |
---|
170 | client = new actor(); |
---|
171 | |
---|
172 | caf::core::init_global_meta_objects(); |
---|
173 | caf::exec_main_init_meta_objects<id_block::custom_types_1>(); |
---|
174 | // caf::exec_main_init_meta_objects<>(); |
---|
175 | |
---|
176 | caf::actor_system_config cfg; |
---|
177 | cfg.set( "caf.scheduler.max-threads", Processors ); |
---|
178 | caf::actor_system system { cfg }; |
---|
179 | |
---|
180 | caf::exec_main<>(caf_main, argc, argv); |
---|
181 | } // main |
---|
182 | |
---|
183 | |
---|
184 | // /usr/bin/time -f "%Uu %Ss %Er %Mkb" a.out |
---|
185 | |
---|
186 | // Local Variables: // |
---|
187 | // 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" // |
---|
188 | // End: // |
---|