Parcourir la source

Use index for port instead of name

Eric Ramat il y a 6 ans
Parent
commit
36a2b59f83

+ 11 - 11
src/artis-star/common/ExternalEvent.hpp

@@ -49,18 +49,18 @@ template < class Time >
 class ExternalEvent
 {
 public:
-    ExternalEvent(const std::string& port_name, const Value& data) :
-        _port_name(port_name), _model(nullptr), _data(data)
+    ExternalEvent(unsigned int port_index, const Value& data) :
+        _port_index(port_index), _model(nullptr), _data(data)
     { }
 
     ExternalEvent(const Node < Time >& node, const Value& data) :
-        _port_name(node.get_port_name()),
+        _port_index(node.get_port_index()),
         _model(node.get_model()),
         _data(data)
     { }
 
     ExternalEvent(const ExternalEvent& event) :
-        _port_name(event._port_name), _model(event._model),
+        _port_index(event._port_index), _model(event._model),
         _data(event._data)
     { }
 
@@ -73,8 +73,8 @@ public:
     const Value& data() const
     { return _data; }
 
-    const std::string& get_port_name() const
-    { return _port_name; }
+    unsigned int get_port_index() const
+    { return _port_index; }
 
     void data(const Value& data)
     { _data = data; }
@@ -82,8 +82,8 @@ public:
     Model < Time >* get_model() const
     { return _model; }
 
-    bool on_port(const std::string& port_name) const
-    { return _port_name == port_name; }
+    bool on_port(unsigned int port_index) const
+    { return _port_index == port_index; }
 
     void set_model(Model < Time >* model)
     { _model = model; }
@@ -92,7 +92,7 @@ public:
     {
         std::ostringstream ss;
 
-        ss << "( " << _port_name << " , "
+        ss << "( " << _port_index << " , "
            << (_model ? _model->get_name() : "<>")
            << " , ";
         if (not _data.empty()) {
@@ -112,14 +112,14 @@ private:
     {
         (void) version;
 
-        ar & _port_name;
+        ar & _port_index;
         _model = 0;
         // ar & _model;
         ar & _data;
         // ar & _model->get_name();
     }
 
-    std::string     _port_name;
+    unsigned int    _port_index;
     Model < Time >* _model;
     Value           _data;
 };

+ 13 - 13
src/artis-star/common/Links.hpp

@@ -55,48 +55,48 @@ public:
     { }
 
     void add(Model < Time >* out_model,
-             const std::string& out_port_name,
+             unsigned int out_port_index,
              Model < Time >* in_model,
-             const std::string& in_port_name)
+             unsigned int in_port_index)
     {
         std::multimap < Node < Time >,
                         Node < Time > >::insert(
                             std::pair < Node < Time >,
                                         Node < Time > >(
                                             Node < Time >(
-                                                out_model, out_port_name),
+                                                out_model, out_port_index),
                                             Node < Time >(
-                                                in_model, in_port_name)));
+                                                in_model, in_port_index)));
     }
 
     bool exist(Model < Time >* out_model,
-               const std::string& out_port_name,
+               unsigned int out_port_index,
                Model < Time >* in_model,
-               const std::string& in_port_name) const
+               unsigned int in_port_index) const
     {
       std::pair < typename Links < Time >::const_iterator,
 		  typename Links < Time >::const_iterator > it =
 	std::multimap < Node < Time >,
-			Node < Time > >::equal_range(Node < Time >(out_model,
-								   out_port_name));
+			Node < Time > >::equal_range(
+                            Node < Time >(out_model, out_port_index));
       typename Links < Time >::const_iterator it2 = it.first;
         bool found = false;
 
         while (not found and it2 != it.second) {
             found = it2->second == Node < Time >(
-                in_model, in_port_name);
+                in_model, in_port_index);
             ++it2;
         }
         return found;
     }
 
     Links::Result find(Model < Time >* out_model,
-                       const std::string& out_port_name) const
+                       unsigned int out_port_index) const
     {
         return std::multimap < Node < Time >,
                                Node < Time > >::equal_range(
                                    common::Node < Time >(
-                                       out_model, out_port_name));
+                                       out_model, out_port_index));
     }
 
     std::string to_string(int level = 0) const
@@ -109,10 +109,10 @@ public:
              it != Links < Time >::end(); ++it) {
             ss << common::String::make_spaces((level + 1) * 2)
                << it->first.get_model()->get_name() << "::"
-               << it->first.get_port_name()
+               << it->first.get_port_index()
                << " -> "
                << it->second.get_model()->get_name() << "::"
-               << it->second.get_port_name() << std::endl;
+               << it->second.get_port_index() << std::endl;
         }
         return ss.str();
     }

+ 30 - 20
src/artis-star/common/Model.hpp

@@ -50,8 +50,14 @@ class InternalEvent;
 template < class Time >
 class Bag;
 
-typedef std::string Port;
-typedef std::vector < Port > Ports;
+struct Port
+{
+    unsigned int index;
+    std::string name;
+};
+
+typedef std::vector < unsigned int > Ports;
+typedef std::map < unsigned int, std::string > PortMap;
 
 template < class Time >
 class Model
@@ -69,46 +75,48 @@ public:
     }
 
     // structure
-    void add_in_port(const std::string& port_name)
+    void add_in_port(const Port& port)
     {
-        assert(not exist_in_port(port_name));
+        assert(not exist_in_port(port.index));
 
-        _in_ports.push_back(port_name);
+        _in_ports.push_back(port.index);
+        _in_port_map[port.index] = port.name;
     }
 
-    void add_out_port(const std::string& port_name)
+    void add_out_port(const Port& port)
     {
-        assert(not exist_out_port(port_name));
+        assert(not exist_out_port(port.index));
 
-        _out_ports.push_back(port_name);
+        _out_ports.push_back(port.index);
+        _out_port_map[port.index] = port.name;
     }
 
-    void delete_in_port(const std::string& port_name)
+    void delete_in_port(const Port& port)
     {
-        assert(not exist_in_port(port_name));
+        assert(not exist_in_port(port.index));
 
         _in_ports.erase(std::find(_in_ports.begin(), _in_ports.end(),
-                                  port_name));
+                                  port.index));
+        _in_port_map.erase(port.index);
     }
 
-    void delete_out_port(const std::string& port_name)
+    void delete_out_port(const Port& port)
     {
-        assert(not exist_out_port(port_name));
+        assert(not exist_out_port(port.index));
 
         _out_ports.erase(std::find(_out_ports.begin(), _out_ports.end(),
-                                   port_name));
+                                   port.index));
+        _out_port_map.erase(port.index);
     }
 
-    bool exist_in_port(const std::string& port_name)
+    bool exist_in_port(unsigned int port_index)
     {
-        return std::find(_in_ports.begin(), _in_ports.end(),
-                         port_name) != _in_ports.end();
+        return _in_port_map.find(port_index) != _in_port_map.end();
     }
 
-    bool exist_out_port(const std::string& port_name)
+    bool exist_out_port(unsigned int port_index)
     {
-        return std::find(_out_ports.begin(), _out_ports.end(),
-                         port_name) != _out_ports.end();
+        return _out_port_map.find(port_index) != _out_port_map.end();
     }
 
     const std::string& get_name() const
@@ -209,7 +217,9 @@ private :
     Model < Time >* _parent;
     std::string     _name;
     Ports           _in_ports;
+    PortMap         _in_port_map;
     Ports           _out_ports;
+    PortMap         _out_port_map;
 
     Bag < Time >*   _inputs;
     SchedulerHandle _handle;

+ 9 - 8
src/artis-star/common/Node.hpp

@@ -25,7 +25,7 @@
  */
 
 #ifndef COMMON_NODE
-#define COMMON_NODE 1
+#define COMMON_NODE
 
 #include <artis-star/common/Model.hpp>
 
@@ -40,14 +40,15 @@ template < class Time >
 class Node
 {
 public :
-    Node(Model < Time >* model, const std::string& port_name)
-      : _model(model), _port_name(port_name)
+    Node(Model < Time >* model, unsigned int port_index)
+      : _model(model), _port_index(port_index)
     {
-      _id = std::hash<std::string>()(model->get_name() + port_name);
+        _id = std::hash<std::string>()(model->get_name() +
+                                       std::to_string(port_index));
     }
 
     Node(const Node < Time >& other)
-      : _model(other._model), _port_name(other._port_name), _id(other._id)
+        : _model(other._model), _port_index(other._port_index), _id(other._id)
     { }
 
     virtual ~Node()
@@ -69,15 +70,15 @@ public :
       return _id == o._id;
     }
 
-    const std::string& get_port_name() const
-    { return _port_name; }
+    unsigned int get_port_index() const
+    { return _port_index; }
 
     Model < Time >* get_model() const
     { return _model; }
 
 private :
     Model < Time >* _model;
-    std::string     _port_name;
+    unsigned int    _port_index;
     int             _id;
 };
 

+ 10 - 16
src/artis-star/kernel/pdevs/Dynamics.hpp

@@ -43,12 +43,6 @@ class Dynamics
     typedef pdevs::Simulator < Time, Dyn, Parameters > Simulator;
 
 public:
-    struct port
-    {
-        unsigned int index;
-        std::string  name;
-    };
-
     Dynamics(const std::string& name,
              const Context < Time, Dyn, Parameters >& context) :
         _name(name), _simulator(context.simulator())
@@ -87,29 +81,29 @@ public:
     const std::string& get_name() const
     { return _name; }
 
-    void input_port(port p)
+    void input_port(common::Port p)
     {
-        _simulator->add_in_port(p.name);
+        _simulator->add_in_port(p);
     }
 
-    void input_ports(std::initializer_list < port > list)
+    void input_ports(std::initializer_list < common::Port > list)
     {
-        for (typename std::initializer_list < port >::iterator it =
+        for (typename std::initializer_list < common::Port >::iterator it =
                  list.begin(); it != list.end(); ++it) {
-            _simulator->add_in_port(it->name);
+            _simulator->add_in_port(*it);
         }
     }
 
-    void output_port(port p)
+    void output_port(common::Port p)
     {
-        _simulator->add_out_port(p.name);
+        _simulator->add_out_port(p);
     }
 
-    void output_ports(std::initializer_list < port > list)
+    void output_ports(std::initializer_list < common::Port > list)
     {
-        for (typename std::initializer_list < port >::iterator it =
+        for (typename std::initializer_list < common::Port >::iterator it =
                  list.begin(); it != list.end(); ++it) {
-            _simulator->add_out_port(it->name);
+            _simulator->add_out_port(*it);
         }
     }
 

+ 52 - 25
src/artis-star/kernel/pdevs/GraphManager.hpp

@@ -43,6 +43,27 @@ template < class Time,
 class GraphManager : public common::GraphManager < Time >
 {
 public:
+    typedef GraphManager < Time, GraphParameters > type;
+
+    struct ModelPort
+    {
+        common::Model < Time >* model;
+        unsigned int port_index;
+        type* graph_manager;
+
+        ModelPort(common::Model < Time >* model,
+                  unsigned int port_index) : model(model),
+                                             port_index(port_index),
+                                             graph_manager(nullptr)
+        {}
+
+        void operator>>(const ModelPort& dst)
+        {
+            graph_manager->add_link(model, port_index,
+                                    dst.model, dst.port_index);
+        }
+    };
+
     GraphManager(common::Coordinator < Time >* coordinator,
                  const GraphParameters& /* parameters */) :
         common::GraphManager < Time >(coordinator)
@@ -51,26 +72,11 @@ public:
     virtual ~GraphManager()
     { }
 
-    void add_link(common::Model < Time >* src_model,
-                  const std::string& src_port_name,
-                  common::Model < Time >* dst_model,
-                  const std::string& dst_port_name)
-    {
-        assert((src_model != common::GraphManager < Time >::_coordinator and
-                dst_model != common::GraphManager < Time >::_coordinator and
-                src_model->exist_out_port(src_port_name) and
-                dst_model->exist_in_port(dst_port_name)) or
-               (src_model == common::GraphManager < Time >::_coordinator and
-                dst_model != common::GraphManager < Time >::_coordinator and
-                src_model->exist_in_port(src_port_name) and
-                dst_model->exist_in_port(dst_port_name)) or
-               (src_model != common::GraphManager < Time >::_coordinator and
-                dst_model == common::GraphManager < Time >::_coordinator and
-                src_model->exist_out_port(src_port_name) and
-                dst_model->exist_out_port(dst_port_name)));
+    ModelPort in(ModelPort p)
+    { p.graph_manager = this; return p; }
 
-        _link_list.add(src_model, src_port_name, dst_model, dst_port_name);
-    }
+    ModelPort out(ModelPort p)
+    { p.graph_manager = this; return p; }
 
     void dispatch_events(common::Bag < Time > bag,
                          typename Time::type t)
@@ -78,7 +84,7 @@ public:
         for (auto & ymsg : bag) {
             typename common::Links < Time >::Result result_model =
                 _link_list.find(ymsg.get_model(),
-                                ymsg.get_port_name());
+                                ymsg.get_port_index());
 
             for (typename common::Links < Time >::
                      const_iterator it = result_model.first;
@@ -112,12 +118,12 @@ public:
     }
 
     bool exist_link(common::Model < Time >* src_model,
-                    const std::string& src_port_name,
+                    unsigned int src_port_index,
                     common::Model < Time >* dst_model,
-                    const std::string& dst_port_name) const
+                    unsigned int dst_port_index) const
     {
-        return _link_list.exist(src_model, src_port_name, dst_model,
-                                dst_port_name);
+        return _link_list.exist(src_model, src_port_index, dst_model,
+                                dst_port_index);
     }
 
     void post_event(typename Time::type t,
@@ -125,7 +131,7 @@ public:
     {
         typename common::Links < Time >::Result result =
             _link_list.find(common::GraphManager < Time >::_coordinator,
-                            event.get_port_name());
+                            event.get_port_index());
 
         for (typename common::Links < Time >::const_iterator it_r =
                  result.first; it_r != result.second; ++it_r) {
@@ -148,6 +154,27 @@ public:
     }
 
 private:
+    void add_link(common::Model < Time >* src_model,
+                  unsigned int src_port_index,
+                  common::Model < Time >* dst_model,
+                  unsigned int dst_port_index)
+    {
+        assert((src_model != common::GraphManager < Time >::_coordinator and
+                dst_model != common::GraphManager < Time >::_coordinator and
+                src_model->exist_out_port(src_port_index) and
+                dst_model->exist_in_port(dst_port_index)) or
+               (src_model == common::GraphManager < Time >::_coordinator and
+                dst_model != common::GraphManager < Time >::_coordinator and
+                src_model->exist_in_port(src_port_index) and
+                dst_model->exist_in_port(dst_port_index)) or
+               (src_model != common::GraphManager < Time >::_coordinator and
+                dst_model == common::GraphManager < Time >::_coordinator and
+                src_model->exist_out_port(src_port_index) and
+                dst_model->exist_out_port(dst_port_index)));
+
+        _link_list.add(src_model, src_port_index, dst_model, dst_port_index);
+    }
+
     common::Links < Time > _link_list;
 };