[b86d14c] | 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 | using namespace caf; |
---|
| 10 | |
---|
| 11 | int Actors = 40'000, Set = 100, Rounds = 400, Processors = 1, Batch = 1, Factor = 40; // ' default values |
---|
| 12 | time_point<steady_clock> starttime; |
---|
| 13 | actor * actors; |
---|
| 14 | int actorCnt = 0; |
---|
| 15 | |
---|
| 16 | class Executor : public event_based_actor { |
---|
| 17 | static int ids; // unique actor id generator |
---|
| 18 | actor * gstart; |
---|
| 19 | int id, rounds, group, recs = 0, sends = 0; |
---|
| 20 | |
---|
| 21 | behavior make_behavior() override { |
---|
| 22 | return { |
---|
| 23 | [=]( int & ) -> void { |
---|
| 24 | if ( recs == rounds ) { |
---|
| 25 | if ( __atomic_add_fetch( &actorCnt, 1, __ATOMIC_SEQ_CST ) == Actors ) { |
---|
| 26 | aout(this) << (steady_clock::now() - starttime).count() * Factor / 1'000'000'000.0 << endl; |
---|
| 27 | } // if |
---|
| 28 | this->quit(); |
---|
| 29 | return; |
---|
| 30 | } // if |
---|
| 31 | if ( recs % Batch == 0 ) { |
---|
| 32 | for ( int i = 0; i < Batch; i += 1 ) { |
---|
| 33 | this->send( gstart[sends % Set], 0 ); // cycle through group |
---|
| 34 | sends += 1; |
---|
| 35 | } |
---|
| 36 | } |
---|
| 37 | //aout(this) << id << " " << cnt << endl; |
---|
| 38 | recs += 1; |
---|
| 39 | } |
---|
| 40 | }; |
---|
| 41 | } |
---|
| 42 | public: |
---|
| 43 | Executor( caf::actor_config & cfg ) : event_based_actor( cfg ) { |
---|
| 44 | id = ids++; // unique actor id, and start point for cycle |
---|
| 45 | gstart = &actors[id / Set * Set]; // remember group-start array-element |
---|
| 46 | rounds = Set * Rounds; // send at least one message to each group member |
---|
| 47 | // aout(this) << "id " << id << " rounds " << rounds << " group " << group << endl; |
---|
| 48 | } |
---|
| 49 | }; // Executor |
---|
| 50 | int Executor::ids = 0; |
---|
| 51 | |
---|
| 52 | void caf_main( actor_system & sys ) { |
---|
| 53 | for ( int i = 0; i < Actors; i += 1 ) { // create actors |
---|
| 54 | actors[i] = sys.spawn<Executor>(); |
---|
| 55 | } // for |
---|
| 56 | starttime = steady_clock::now(); |
---|
| 57 | for ( int i = 0; i < Actors; i += 1 ) { // start actors |
---|
| 58 | caf::anon_send( actors[i], 0 ); |
---|
| 59 | } // for |
---|
| 60 | } // caf_main |
---|
| 61 | |
---|
| 62 | int main( int argc, char * argv[] ) { |
---|
| 63 | switch ( argc ) { |
---|
| 64 | case 7: |
---|
| 65 | if ( strcmp( argv[6], "d" ) != 0 ) { // default ? |
---|
| 66 | Factor = stoi( argv[6] ); |
---|
| 67 | if ( Factor < 1 ) goto Usage; |
---|
| 68 | } // if |
---|
| 69 | case 6: |
---|
| 70 | if ( strcmp( argv[5], "d" ) != 0 ) { // default ? |
---|
| 71 | Batch = stoi( argv[5] ); |
---|
| 72 | if ( Batch < 1 ) goto Usage; |
---|
| 73 | } // if |
---|
| 74 | case 5: |
---|
| 75 | if ( strcmp( argv[4], "d" ) != 0 ) { // default ? |
---|
| 76 | Processors = stoi( argv[4] ); |
---|
| 77 | if ( Processors < 1 ) goto Usage; |
---|
| 78 | } // if |
---|
| 79 | case 4: |
---|
| 80 | if ( strcmp( argv[3], "d" ) != 0 ) { // default ? |
---|
| 81 | Rounds = stoi( argv[3] ); |
---|
| 82 | if ( Rounds < 1 ) goto Usage; |
---|
| 83 | } // if |
---|
| 84 | case 3: |
---|
| 85 | if ( strcmp( argv[2], "d" ) != 0 ) { // default ? |
---|
| 86 | Set = stoi( argv[2] ); |
---|
| 87 | if ( Set < 1 ) goto Usage; |
---|
| 88 | } // if |
---|
| 89 | case 2: |
---|
| 90 | if ( strcmp( argv[1], "d" ) != 0 ) { // default ? |
---|
| 91 | Actors = stoi( argv[1] ); |
---|
| 92 | if ( Actors < 1 || Actors <= Set || Actors % Set != 0 ) goto Usage; |
---|
| 93 | } // if |
---|
| 94 | case 1: // use defaults |
---|
| 95 | break; |
---|
| 96 | default: |
---|
| 97 | Usage: |
---|
| 98 | cerr << "Usage: " << argv[0] |
---|
| 99 | << " [ actors (> 0 && > set && actors % set == 0 ) | 'd' (default " << Actors |
---|
| 100 | << ") ] [ set (> 0) | 'd' (default " << Set |
---|
| 101 | << ") ] [ rounds (> 0) | 'd' (default " << Rounds |
---|
| 102 | << ") ] [ processors (> 0) | 'd' (default " << Processors |
---|
| 103 | << ") ] [ batch (> 0) | 'd' (default " << Batch |
---|
| 104 | << ") ]" << endl; |
---|
| 105 | exit( EXIT_FAILURE ); |
---|
| 106 | } // switch |
---|
| 107 | |
---|
| 108 | Rounds = Rounds / Factor; |
---|
| 109 | |
---|
| 110 | //cout << Actors << " " << Set << " " << Rounds << " " << Processors << endl; |
---|
| 111 | actors = new actor[Actors]; |
---|
| 112 | |
---|
| 113 | caf::core::init_global_meta_objects(); |
---|
| 114 | caf::exec_main_init_meta_objects<>(); |
---|
| 115 | |
---|
| 116 | caf::actor_system_config cfg; |
---|
| 117 | cfg.set( "caf.scheduler.max-threads", Processors ); |
---|
| 118 | caf::actor_system system { cfg }; |
---|
| 119 | |
---|
| 120 | return caf::exec_main<>(caf_main, argc, argv); |
---|
| 121 | } // main |
---|
| 122 | //CAF_MAIN() |
---|
| 123 | |
---|
| 124 | // /usr/bin/time -f "%Uu %Ss %Er %Mkb" a.out |
---|
| 125 | |
---|
| 126 | // Local Variables: // |
---|
| 127 | // 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 CAFExecutor.cpp -lcaf_io -lcaf_core -Wl,-rpath=CAF/actor-framework/build/libcaf_core" // |
---|
| 128 | // End: // |
---|