Browse Source

Add lookahead in multithreading coordinator

Eric Ramat 1 year ago
parent
commit
cb81616110

+ 3 - 0
src/artis-star/common/Model.hpp

@@ -261,6 +261,9 @@ namespace artis {
 
             virtual typename Time::type transition(const typename Time::type& t) = 0;
 
+            virtual typename Time::type lookahead(const typename Time::type& t) const
+            { return t; }
+
             // scheduler
             void handle(SchedulerHandle handle) { _handle.handle(handle); }
 

+ 26 - 2
src/artis-star/common/scheduler/HeapScheduler.hpp

@@ -65,7 +65,7 @@ namespace artis {
                     return type::top().get_model();
                 }
 
-                models_type get_current_models(typename Time::type time) const
+                models_type get_current_models(const typename Time::type& time) const
                 {
                     models_type models;
                     typename models_type::iterator it;
@@ -85,6 +85,29 @@ namespace artis {
                     return models;
                 }
 
+                models_type get_current_models(const typename Time::type& begin,
+                        const typename Time::type& end) const
+                {
+                    models_type models;
+                    typename models_type::iterator it;
+
+                    for (typename type::ordered_iterator it = type::ordered_begin();
+                         it != type::ordered_end() and (std::abs(it->get_time() - begin) < 1e-6
+                                 or (it->get_time() > begin
+                                         and it->get_time() < end)); ++it) {
+                        std::string str = it->get_model()->get_name();
+                        auto it2 = find_if(models.begin(), models.end(),
+                                [&str](const model_type* obj) {
+                                    return obj->get_name() == str;
+                                });
+
+                        if (it2 == models.end()) {
+                            models.push_back(it->get_model());
+                        }
+                    }
+                    return models;
+                }
+
                 typename Time::type get_current_time() const
                 {
                     return type::top().get_time();
@@ -110,7 +133,8 @@ namespace artis {
                     }
                 }
 
-                void remove_model(model_type* model) {
+                void remove_model(model_type* model)
+                {
                     type::erase(model->handle()._handle);
                 }
 

+ 3 - 0
src/artis-star/kernel/devs/Coordinator.hpp

@@ -328,6 +328,9 @@ namespace artis {
                 return common::Value();
             }
 
+            typename Time::type lookahead(const typename Time::type& t) const
+            { return _graph_manager.lookahead(t); }
+
             common::Models<Time> get_imm_without_receivers(const common::Models<Time>& IMM,
                     const common::Models<Time>& receivers) const
             {

+ 3 - 0
src/artis-star/kernel/devs/Dynamics.hpp

@@ -70,6 +70,9 @@ namespace artis {
             virtual common::Value observe(const typename Time::type& /* t */,
                     unsigned int /* index */) const { return common::Value(); }
 
+            virtual typename Time::type lookahead(const typename Time::type& t) const
+            { return t; }
+
             const std::string& get_name() const { return _name; }
 
             void observable(Observable observable)

+ 3 - 0
src/artis-star/kernel/devs/GraphManager.hpp

@@ -89,6 +89,9 @@ namespace artis {
                 return _links.find(std::make_pair(src_model, dst_model));
             }
 
+            virtual typename Time::type lookahead(const typename Time::type& t) const
+            { return t; }
+
             void post_event(typename Time::type t, const common::ExternalEvent<Time>& event)
             {
                 auto it = std::find_if(_links.begin(), _links.end(),

+ 3 - 0
src/artis-star/kernel/devs/Simulator.hpp

@@ -278,6 +278,9 @@ namespace artis {
                 return type::_tn;
             }
 
+            typename Time::type lookahead(const typename Time::type& t) const
+            { return _dynamics.lookahead(t); }
+
         private :
             Dynamics _dynamics;
         };

+ 3 - 0
src/artis-star/kernel/dtss/Coordinator.hpp

@@ -155,6 +155,9 @@ namespace artis {
                 return common::Value();
             }
 
+            typename Time::type lookahead(const typename Time::type& t) const
+            { return _graph_manager.lookahead(t); }
+
             void output(const typename Time::type& t)
             {
 

+ 3 - 0
src/artis-star/kernel/dtss/Dynamics.hpp

@@ -66,6 +66,9 @@ namespace artis {
             virtual common::Value observe(const typename Time::type& /* t */,
                     unsigned int /* index */) const { return common::Value(); }
 
+            virtual typename Time::type lookahead(const typename Time::type& t) const
+            { return t; }
+
             const std::string& get_name() const { return _name; }
 
             void input_port(common::Port p)

+ 3 - 0
src/artis-star/kernel/dtss/GraphManager.hpp

@@ -113,6 +113,9 @@ namespace artis {
                 }
             }
 
+            virtual typename Time::type lookahead(const typename Time::type& t) const
+            { return t; }
+
             void post_event(typename Time::type t, const common::ExternalEvent<Time>& event)
             {
                 typename common::Links<Time>::Result result =

+ 3 - 0
src/artis-star/kernel/dtss/Simulator.hpp

@@ -134,6 +134,9 @@ namespace artis {
                 return type::_tn;
             }
 
+            typename Time::type lookahead(const typename Time::type& t) const
+            { return _dynamics.lookahead(t); }
+
             common::Value observe(const typename Time::type& t,
                     unsigned int index) const { return _dynamics.observe(t, index); }
 

+ 3 - 0
src/artis-star/kernel/fddevs/Dynamics.hpp

@@ -72,6 +72,9 @@ namespace artis {
             virtual typename Time::type
             tau(typename Time::type /* time */) const { return Time::infinity; }
 
+            virtual typename Time::type lookahead(const typename Time::type& t) const
+            { return t; }
+
             // observation
             virtual common::Value observe(const typename Time::type& /* t */,
                     unsigned int /* index */) const { return common::Value(); }

+ 3 - 0
src/artis-star/kernel/fddevs/Simulator.hpp

@@ -300,6 +300,9 @@ namespace artis {
                 return type::_tn;
             }
 
+            typename Time::type lookahead(const typename Time::type& t) const
+            { return _dynamics.lookahead(t); }
+
         private :
             Dynamics _dynamics;
         };

+ 3 - 0
src/artis-star/kernel/pdevs/Coordinator.hpp

@@ -329,6 +329,9 @@ namespace artis {
                 return common::Value();
             }
 
+            typename Time::type lookahead(const typename Time::type& t) const
+            { return _graph_manager.lookahead(t); }
+
             common::Models<Time> get_receivers() const
             {
                 common::Models<Time> receivers;

+ 3 - 0
src/artis-star/kernel/pdevs/Dynamics.hpp

@@ -78,6 +78,9 @@ namespace artis {
             virtual common::Value observe(const typename Time::type& /* t */,
                     unsigned int /* index */) const { return common::Value(); }
 
+            virtual typename Time::type lookahead(const typename Time::type& t) const
+            { return t; }
+
             const std::string& get_name() const { return _name; }
 
             void input_port(common::Port p)

+ 3 - 0
src/artis-star/kernel/pdevs/GraphManager.hpp

@@ -122,6 +122,9 @@ namespace artis {
                 return _link_list.exist(src_model, src_port_index, dst_model, dst_port_index);
             }
 
+            virtual typename Time::type lookahead(const typename Time::type& t) const
+            { return t; }
+
             void post_event(typename Time::type t, const common::ExternalEvent<Time>& event)
             {
                 typename common::Links<Time>::Result result =

+ 3 - 0
src/artis-star/kernel/pdevs/Simulator.hpp

@@ -293,6 +293,9 @@ namespace artis {
                 return type::_tn;
             }
 
+            typename Time::type lookahead(const typename Time::type& t) const
+            { return _dynamics.lookahead(t); }
+
         private :
             Dynamics _dynamics;
         };

+ 6 - 3
src/artis-star/kernel/pdevs/multithreading/Coordinator.hpp

@@ -78,7 +78,8 @@ namespace artis {
                     class GraphManager,
                     class Parameters = common::NoParameters,
                     class GraphParameters = common::NoParameters>
-            class Coordinator : public pdevs::Coordinator<Time, GraphManager, Parameters, GraphParameters> {
+            class Coordinator
+                    : public pdevs::Coordinator<Time, GraphManager, Parameters, GraphParameters> {
                 typedef pdevs::Coordinator<Time, GraphManager,
                         Parameters, GraphParameters> parent_type;
                 typedef Coordinator<Time, GraphManager,
@@ -119,7 +120,7 @@ namespace artis {
                         for (;;) {
                             _incoming.wait()
                                     .template handle<start_message_type>(
-                                            [&](start_message_type const& msg) {
+                                             [&](start_message_type const& msg) {
                                                 typename Time::type tn = start(msg._t);
                                                 _sender.send(done_start_message_type(tn, this));
                                             })
@@ -187,7 +188,9 @@ namespace artis {
                     assert(t >= type::_tl and t <= type::_tn);
 
                     common::Models<Time> receivers = type::get_receivers();
-                    common::Models<Time> IMM = type::_event_table.get_current_models(t);
+//                    common::Models<Time> IMM = type::_event_table.get_current_models(t);
+                    common::Models<Time> IMM = type::_event_table.get_current_models(t,
+                            type::_graph_manager.lookahead(t));
 
                     _received = 0;
                     for (auto& model : receivers) {