Parcourir la source

pdevs/multithreading: fix

Eric Ramat il y a 3 ans
Parent
commit
579c98a475

+ 49 - 45
src/tests/multithreading/simple/graph_manager.hpp

@@ -105,13 +105,6 @@ public:
   void init()
   {}
 
-  void start(common::DoubleTime::type /* t */)
-  {}
-
-  void transition(const common::Models<common::DoubleTime> & /* receivers */,
-                  artis::common::DoubleTime::type /* t */)
-  {}
-
 private:
   artis::pdevs::Simulator<artis::common::DoubleTime, Link> _link_1;
   artis::pdevs::Simulator<artis::common::DoubleTime, Link> _link_2;
@@ -202,49 +195,60 @@ public:
     return *std::min(lookaheads.begin(), lookaheads.end());
   }
 
-  void init()
-  {
-    dynamic_cast< coordinator_type *>(this->coordinator())
-        ->attach_child(&_link_1_2, _link_1_2.get_queue());
-    dynamic_cast< coordinator_type *>(this->coordinator())
-        ->attach_child(&_link_3_4, _link_3_4.get_queue());
-    dynamic_cast< coordinator_type *>(this->coordinator())
-        ->attach_child(&_link_5_6, _link_5_6.get_queue());
-    dynamic_cast< coordinator_type *>(this->coordinator())
-        ->attach_child(&_link_7_8, _link_7_8.get_queue());
-
-    _link_1_2.attach_parent(dynamic_cast< coordinator_type *>(this->coordinator())->get_queue());
-    _link_3_4.attach_parent(dynamic_cast< coordinator_type *>(this->coordinator())->get_queue());
-    _link_5_6.attach_parent(dynamic_cast< coordinator_type *>(this->coordinator())->get_queue());
-    _link_7_8.attach_parent(dynamic_cast< coordinator_type *>(this->coordinator())->get_queue());
-  }
+//  void init()
+//  {
+//    dynamic_cast< coordinator_type *>(this->coordinator())
+//        ->attach_child(&_link_1_2, _link_1_2.get_queue());
+//    dynamic_cast< coordinator_type *>(this->coordinator())
+//        ->attach_child(&_link_3_4, _link_3_4.get_queue());
+//    dynamic_cast< coordinator_type *>(this->coordinator())
+//        ->attach_child(&_link_5_6, _link_5_6.get_queue());
+//    dynamic_cast< coordinator_type *>(this->coordinator())
+//        ->attach_child(&_link_7_8, _link_7_8.get_queue());
+//
+//    _link_1_2.attach_parent(dynamic_cast< coordinator_type *>(this->coordinator())->get_queue());
+//    _link_3_4.attach_parent(dynamic_cast< coordinator_type *>(this->coordinator())->get_queue());
+//    _link_5_6.attach_parent(dynamic_cast< coordinator_type *>(this->coordinator())->get_queue());
+//    _link_7_8.attach_parent(dynamic_cast< coordinator_type *>(this->coordinator())->get_queue());
+//  }
 
 private:
   artis::pdevs::Simulator<artis::common::DoubleTime, Generator, GeneratorParameters> _generator;
 
-  /*
-                    artis::pdevs::Coordinator<artis::common::DoubleTime, LinksGraphManager, GeneratorParameters, artis::common::NoParameters> _link_1_2;
-                    artis::pdevs::Coordinator<artis::common::DoubleTime, LinksGraphManager, GeneratorParameters, artis::common::NoParameters> _link_3_4;
-                    artis::pdevs::Coordinator<artis::common::DoubleTime, LinksGraphManager, GeneratorParameters, artis::common::NoParameters> _link_5_6;
-                    artis::pdevs::Coordinator<artis::common::DoubleTime, LinksGraphManager, GeneratorParameters, artis::common::NoParameters> _link_7_8;
-*/
+  artis::pdevs::Coordinator<artis::common::DoubleTime,
+                            LinksGraphManager,
+                            GeneratorParameters,
+                            artis::common::NoParameters> _link_1_2;
+  artis::pdevs::Coordinator<artis::common::DoubleTime,
+                            LinksGraphManager,
+                            GeneratorParameters,
+                            artis::common::NoParameters> _link_3_4;
+  artis::pdevs::Coordinator<artis::common::DoubleTime,
+                            LinksGraphManager,
+                            GeneratorParameters,
+                            artis::common::NoParameters> _link_5_6;
+  artis::pdevs::Coordinator<artis::common::DoubleTime,
+                            LinksGraphManager,
+                            GeneratorParameters,
+                            artis::common::NoParameters> _link_7_8;
+
+//  artis::pdevs::multithreading::Coordinator<artis::common::DoubleTime,
+//                                            LinksGraphManager,
+//                                            GeneratorParameters,
+//                                            artis::common::NoParameters> _link_1_2;
+//  artis::pdevs::multithreading::Coordinator<artis::common::DoubleTime,
+//                                            LinksGraphManager,
+//                                            GeneratorParameters,
+//                                            artis::common::NoParameters> _link_3_4;
+//  artis::pdevs::multithreading::Coordinator<artis::common::DoubleTime,
+//                                            LinksGraphManager,
+//                                            GeneratorParameters,
+//                                            artis::common::NoParameters> _link_5_6;
+//  artis::pdevs::multithreading::Coordinator<artis::common::DoubleTime,
+//                                            LinksGraphManager,
+//                                            GeneratorParameters,
+//                                            artis::common::NoParameters> _link_7_8;
 
-  artis::pdevs::multithreading::Coordinator<artis::common::DoubleTime,
-                                            LinksGraphManager,
-                                            GeneratorParameters,
-                                            artis::common::NoParameters> _link_1_2;
-  artis::pdevs::multithreading::Coordinator<artis::common::DoubleTime,
-                                            LinksGraphManager,
-                                            GeneratorParameters,
-                                            artis::common::NoParameters> _link_3_4;
-  artis::pdevs::multithreading::Coordinator<artis::common::DoubleTime,
-                                            LinksGraphManager,
-                                            GeneratorParameters,
-                                            artis::common::NoParameters> _link_5_6;
-  artis::pdevs::multithreading::Coordinator<artis::common::DoubleTime,
-                                            LinksGraphManager,
-                                            GeneratorParameters,
-                                            artis::common::NoParameters> _link_7_8;
   artis::pdevs::Simulator<artis::common::DoubleTime, Counter> _counter;
 };
 

+ 28 - 28
src/tests/multithreading/simple/main.cpp

@@ -37,35 +37,12 @@ using namespace artis::common;
 using namespace std::chrono;
 using namespace artis::tests::multithreading::simple;
 
-//void simple_monothreading()
-//{
-//  GeneratorParameters parameters = {10, 2, 5, 763752};
-//  artis::common::context::Context<artis::common::DoubleTime> context(0, 2000);
-//  artis::common::RootCoordinator<
-//      DoubleTime, artis::pdevs::Coordinator<
-//          DoubleTime,
-//          SimpleGraphManager,
-//          GeneratorParameters,
-//          NoParameters>
-//  > rc(context, "root", parameters, NoParameters());
-//
-//  steady_clock::time_point t1 = steady_clock::now();
-//
-//  rc.run(context);
-//
-//  steady_clock::time_point t2 = steady_clock::now();
-//
-//  duration<double> time_span = duration_cast<duration<double> >(t2 - t1);
-//
-//  std::cout << "Time = " << time_span.count() << std::endl;
-//}
-
-void simple_multithreading()
+void simple_monothreading()
 {
   GeneratorParameters parameters = {10, 2, 5, 763752};
-  artis::common::context::Context<artis::common::DoubleTime> context(0, 2000);
+  artis::common::context::Context<artis::common::DoubleTime> context(0, 200000);
   artis::common::RootCoordinator<
-      DoubleTime, artis::pdevs::multithreading::Coordinator<
+      DoubleTime, artis::pdevs::Coordinator<
           DoubleTime,
           SimpleGraphManager,
           GeneratorParameters,
@@ -83,9 +60,32 @@ void simple_multithreading()
   std::cout << "Time = " << time_span.count() << std::endl;
 }
 
+//void simple_multithreading()
+//{
+//  GeneratorParameters parameters = {10, 2, 5, 763752};
+//  artis::common::context::Context<artis::common::DoubleTime> context(0, 200000);
+//  artis::common::RootCoordinator<
+//      DoubleTime, artis::pdevs::multithreading::Coordinator<
+//          DoubleTime,
+//          SimpleGraphManager,
+//          GeneratorParameters,
+//          NoParameters>
+//  > rc(context, "root", parameters, NoParameters());
+//
+//  steady_clock::time_point t1 = steady_clock::now();
+//
+//  rc.run(context);
+//
+//  steady_clock::time_point t2 = steady_clock::now();
+//
+//  duration<double> time_span = duration_cast<duration<double> >(t2 - t1);
+//
+//  std::cout << "Time = " << time_span.count() << std::endl;
+//}
+
 int main()
 {
-//  simple_monothreading();
-  simple_multithreading();
+  simple_monothreading();
+//  simple_multithreading();
   return 0;
 }

+ 38 - 26
src/tests/multithreading/simple/models.hpp

@@ -114,10 +114,12 @@ public:
 
   void dint(const artis::common::DoubleTime::type &t) override
   {
+
+//    std::cout << get_full_name() << " at " << t << " - dint" << std::endl;
+
     _last_time = t;
     _sigma = _distribution(_generator);
     _sigma = _sigma <= 0 ? 0.1 : _sigma;
-
     _next_v_max = _v_max * _v_max_distribution(_generator);
     _next_port = _port_distribution(_generator);
     ++_index;
@@ -125,6 +127,9 @@ public:
 
   void start(const artis::common::DoubleTime::type &t) override
   {
+
+//    std::cout << get_full_name() << " at " << t << " - start" << std::endl;
+
     _last_time = t;
     _sigma = _distribution(_generator);
     _sigma = _sigma <= 0 ? 0.1 : _sigma;
@@ -142,6 +147,8 @@ public:
   {
     artis::common::Bag<artis::common::DoubleTime> bag;
 
+//    std::cout << get_full_name() << " at " << t << " - lambda" << std::endl;
+
     if (t > 0) {
       Vehicle vehicle = {_index, _next_v_max, 0.5, State::STOP, t};
 
@@ -206,19 +213,21 @@ public:
 
   ~Counter() override = default;
 
-  void
-  dext(const artis::common::DoubleTime::type & /* t */,
-       const artis::common::DoubleTime::type & /* e */,
-       const artis::common::Bag<artis::common::DoubleTime> &bag) override
+  void dext(const artis::common::DoubleTime::type & /* t */,
+            const artis::common::DoubleTime::type & /* e */,
+            const artis::common::Bag<artis::common::DoubleTime> &bag) override
   {
 
-//                        std::cout << t << ": " << _counter << std::endl;
+//    std::cout << get_full_name() << " at " << t << " - dext" << std::endl;
 
     _counter += bag.size();
   }
 
   void start(const artis::common::DoubleTime::type & /* t */) override
   {
+
+//    std::cout << get_full_name() << " at " << t << " - start" << std::endl;
+
     _counter = 0;
   }
 
@@ -228,8 +237,7 @@ public:
     return artis::common::DoubleTime::infinity;
   }
 
-  artis::common::Value
-  observe(const artis::common::DoubleTime::type & /* t */,
+  artis::common::Value observe(const artis::common::DoubleTime::type & /* t */,
           unsigned int index) const override
   {
     if (index == vars::COUNTER) {
@@ -239,8 +247,7 @@ public:
     }
   }
 
-  common::DoubleTime::type
-  lookahead(const common::DoubleTime::type & /* t */) const override
+  common::DoubleTime::type lookahead(const common::DoubleTime::type & /* t */) const override
   {
     return common::DoubleTime::infinity;
   }
@@ -281,7 +288,9 @@ public :
 
   void dint(const artis::common::DoubleTime::type &t) override
   {
-    delay();
+//    delay();
+
+//    std::cout << get_full_name() << " at " << t << " - dint" << std::endl;
 
     auto it = _vehicles.begin();
 
@@ -298,12 +307,10 @@ public :
         switch (vehicle.state) {
         case State::UP: {
           double duration = vehicle.v_max / vehicle.acceleration;
-          double acceleration_distance =
-              0.5 * vehicle.acceleration * duration * duration;
+          double acceleration_distance = 0.5 * vehicle.acceleration * duration * duration;
 
           vehicle.state = State::MAX;
-          vehicle.next_time = t +
-              (_length - 2 * acceleration_distance) / vehicle.v_max;
+          vehicle.next_time = t + (_length - 2 * acceleration_distance) / vehicle.v_max;
           break;
         }
         case State::MAX: {
@@ -330,14 +337,16 @@ public :
             const artis::common::DoubleTime::type & /* e */,
             const artis::common::Bag<artis::common::DoubleTime> &bag) override
   {
+
+//    std::cout << get_full_name() << " at " << t << " - dext" << std::endl;
+
     std::for_each(bag.begin(), bag.end(),
                   [this, t](const common::ExternalEvent<common::DoubleTime> &event) {
                     if (event.on_port(inputs::IN)) {
                       Vehicle vehicle;
 
                       event.data()(vehicle);
-                      vehicle.next_time =
-                          t + vehicle.v_max / vehicle.acceleration;
+                      vehicle.next_time = t + vehicle.v_max / vehicle.acceleration;
                       vehicle.state = State::UP;
                       _vehicles.push_back(vehicle);
                     }
@@ -345,8 +354,7 @@ public :
     update_sigma(t);
   }
 
-  artis::common::DoubleTime::type
-  ta(const artis::common::DoubleTime::type & /* t */) const override
+  artis::common::DoubleTime::type ta(const artis::common::DoubleTime::type & /* t */) const override
   { return _sigma; }
 
   artis::common::Bag<artis::common::DoubleTime>
@@ -354,6 +362,8 @@ public :
   {
     artis::common::Bag<artis::common::DoubleTime> bag;
 
+//    std::cout << get_full_name() << " at " << t << " - lambda" << std::endl;
+
     for (auto vehicle: _vehicles) {
       if (vehicle.next_time == t and vehicle.state == State::STOP) {
         bag.push_back(
@@ -364,12 +374,15 @@ public :
     return bag;
   }
 
-  void
-  start(const artis::common::DoubleTime::type & /* t */) override
-  {}
+  void start(const artis::common::DoubleTime::type & /* t */) override
+  {
 
-  common::DoubleTime::type
-  lookahead(const common::DoubleTime::type &t) const override
+//    std::cout << get_full_name() << " at " << t << " - start" << std::endl;
+
+    _sigma = artis::common::DoubleTime::infinity;
+  }
+
+  common::DoubleTime::type lookahead(const common::DoubleTime::type &t) const override
   {
     double eot = artis::common::DoubleTime::infinity;
 
@@ -384,8 +397,7 @@ public :
         eot_i = vehicle.next_time + vehicle.v_max / vehicle.acceleration;
       } else if (vehicle.state == State::UP) {
         double duration = vehicle.v_max / vehicle.acceleration;
-        double acceleration_distance =
-            0.5 * vehicle.acceleration * duration * duration;
+        double acceleration_distance = 0.5 * vehicle.acceleration * duration * duration;
 
         eot_i = vehicle.next_time
             + (_length - 2 * acceleration_distance) / vehicle.v_max