PandA-2024.02
state_transition_graph.hpp
Go to the documentation of this file.
1 /*
2  *
3  * _/_/_/ _/_/ _/ _/ _/_/_/ _/_/
4  * _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/
5  * _/_/_/ _/_/_/_/ _/ _/_/ _/ _/ _/_/_/_/
6  * _/ _/ _/ _/ _/ _/ _/ _/ _/
7  * _/ _/ _/ _/ _/ _/_/_/ _/ _/
8  *
9  * ***********************************************
10  * PandA Project
11  * URL: http://panda.dei.polimi.it
12  * Politecnico di Milano - DEIB
13  * System Architectures Group
14  * ***********************************************
15  * Copyright (C) 2004-2024 Politecnico di Milano
16  *
17  * This file is part of the PandA framework.
18  *
19  * The PandA framework is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
50 #ifndef STATE_TRANSTION_GRAPH_HPP
51 #define STATE_TRANSTION_GRAPH_HPP
52 
54 #include <edge_info.hpp>
55 #include <graph_info.hpp>
56 #include <node_info.hpp>
57 
59 #include <graph.hpp>
60 
62 #include "custom_set.hpp"
63 
65 #include "refcount.hpp"
66 #include "string_manipulation.hpp" // for STR
67 
73 class HLS_manager;
76 
81 struct StateInfo : public NodeInfo
82 {
85 
87  unsigned int funId;
88 
90  std::string name;
91 
93  std::list<vertex> executing_operations;
94 
96  std::list<vertex> starting_operations;
97 
99  std::list<vertex> ending_operations;
100 
102  std::list<vertex> onfly_operations;
103 
106 
108  bool is_dummy;
109 
112 
114  unsigned int sourceBb;
115 
118 
121 
123  bool all_paths;
124 
127 
130 
132  std::list<vertex> moved_exec_op;
133 
135  std::list<vertex> moved_ending_op;
136 
138  unsigned int loopId;
139 
145  void print(std::ostream& os, const int detail_level) const override;
146 
151  : funId(0),
152  is_dummy(false),
153  is_duplicated(false),
154  sourceBb(0),
155  isOriginalState(false),
156  clonedState(NULL_VERTEX),
157  all_paths(false),
158  loopId(0)
159  {
160  }
161 };
165 
166 enum transition_type : int
167 {
174 };
175 
180 class TransitionInfo : public EdgeInfo
181 {
182  private:
185 
188  bool has_default{false};
190  vertex ref_state{NULL_VERTEX};
192  size_t epp_increment{0};
193  bool epp_incrementValid{false};
194 
195  public:
196  explicit TransitionInfo(OpGraphConstRef g) : op_function_graph(g)
197  {
198  }
199 
201 
206  void print(std::ostream& os, const int detail_level) const;
207 
208  bool get_has_default() const
209  {
210  return has_default;
211  }
213  {
214  return t;
215  }
217  {
218  return ops;
219  }
220  vertex get_operation() const;
222  {
223  return labels;
224  }
225  vertex get_ref_state() const;
226 
229  {
231  ST_EDGE_NORMAL = 1 << 0,
233  ST_EDGE_FEEDBACK = 1 << 1,
235  ST_EDGE_EPP = 1 << 2,
236  };
237 
238  void set_epp_increment(size_t n)
239  {
240  epp_increment = n;
241  epp_incrementValid = true;
242  }
243  size_t get_epp_increment() const
244  {
245  return epp_increment;
246  }
247 };
251 
256 {
259 
262 
265 
267  bool is_a_dag;
268 
269  bool bounded;
270 
272  unsigned int min_cycles;
274  unsigned int max_cycles;
275 
276  std::map<unsigned int, vertex> state_id_to_vertex;
277 
278  std::map<vertex, unsigned int> vertex_to_state_id;
279 
283  explicit StateTransitionGraphInfo(const OpGraphConstRef op_function_graph);
284 
286 };
290 
295 {
296  public:
302  StateTransitionGraphsCollection(const StateTransitionGraphInfoRef state_transition_graph_info,
304 
309 
318  inline EdgeDescriptor AddEdge(const vertex source, const vertex target, const int selector,
319  const TransitionInfoRef info)
320  {
321  THROW_ASSERT(not ExistsEdge(source, target), "Trying to create an already existing edge");
322  return InternalAddEdge(source, target, selector, RefcountCast<EdgeInfo>(info));
323  }
324 };
328 
333 {
334  public:
340  StateTransitionGraph(const StateTransitionGraphsCollectionRef state_transition_graphs_collection, int selector);
341 
348  StateTransitionGraph(const StateTransitionGraphsCollectionRef state_transition_graphs_collection, int selector,
350 
354  ~StateTransitionGraph() override;
355 
360  inline StateInfoRef GetStateInfo(const vertex state)
361  {
362  return RefcountCast<StateInfo>(graph::GetNodeInfo(state));
363  }
364 
369  inline const StateInfoConstRef CGetStateInfo(const vertex state) const
370  {
371  return RefcountCast<const StateInfo>(graph::CGetNodeInfo(state));
372  }
373 
378  inline const StateInfoConstRef CGetStateInfo(unsigned int state_id) const
379  {
380  return CGetStateInfo(GetVertex(state_id));
381  }
382 
386  inline vertex GetVertex(unsigned int state_id) const
387  {
388  THROW_ASSERT(CGetStateTransitionGraphInfo()->state_id_to_vertex.find(state_id) !=
389  CGetStateTransitionGraphInfo()->state_id_to_vertex.end(),
390  "State ID " + STR(state_id) + " was not stored");
391  return CGetStateTransitionGraphInfo()->state_id_to_vertex.at(state_id);
392  }
393 
399  {
400  return RefcountCast<TransitionInfo>(graph::GetEdgeInfo(transition));
401  }
402 
407  inline const TransitionInfoConstRef CGetTransitionInfo(const EdgeDescriptor transition) const
408  {
409  return RefcountCast<const TransitionInfo>(graph::CGetEdgeInfo(transition));
410  }
411 
417  {
418  return RefcountCast<StateTransitionGraphInfo>(graph::GetGraphInfo());
419  }
420 
426  {
427  return RefcountCast<const StateTransitionGraphInfo>(graph::CGetGraphInfo());
428  }
429 
435  void WriteDot(const std::string& file_name, const int detail_level = 0) const;
436 };
440 
444 class StateWriter : public VertexWriter
445 {
446  private:
449 
452 
455 
458 
459  public:
463  StateWriter(const graph* _stg, const OpGraphConstRef _op_function_graph, int _detail_level);
464 
468  void operator()(std::ostream& out, const vertex& v) const override;
469 };
470 
476 {
477  private:
480 
483 
484  public:
490  TransitionWriter(const graph* stg, const OpGraphConstRef op_function_graph, int _detail_level);
491 
495  void operator()(std::ostream& out, const EdgeDescriptor& e) const override;
496 };
497 
499 {
500  public:
502  : state_graph(input_state_graph), pipeline(enable)
503  {
504  }
505 
507  {
508  if(not pipeline)
509  {
510  return top;
511  }
512  graph::in_edge_iterator in_edge, in_edge_end;
513 #if HAVE_ASSERTS
514  bool multiple_in_edges = false;
515 #endif
516  vertex ret_v = NULL_VERTEX;
517  for(boost::tie(in_edge, in_edge_end) = boost::in_edges(bottom, *state_graph); in_edge != in_edge_end; ++in_edge)
518  {
519  ret_v = boost::source(*in_edge, *state_graph);
520  THROW_ASSERT(not multiple_in_edges, "A pipeline should not contain phi operations");
521 #if HAVE_ASSERTS
522  multiple_in_edges = true;
523 #endif
524  }
525  THROW_ASSERT(multiple_in_edges, "No input edge found");
526  return ret_v;
527  }
528 
529  private:
531  bool pipeline;
532 };
533 
535 {
536  public:
537  explicit next_unique_state(StateTransitionGraphConstRef input_state_graph) : state_graph(input_state_graph)
538  {
539  }
540 
542  {
543  graph::out_edge_iterator out_edge, out_edge_end;
544 #if HAVE_ASSERTS
545  bool multiple_out_edges = false;
546 #endif
547  vertex next_state = NULL_VERTEX;
548  for(boost::tie(out_edge, out_edge_end) = boost::out_edges(state, *state_graph); out_edge != out_edge_end;
549  ++out_edge)
550  {
551  next_state = boost::target(*out_edge, *state_graph);
552  THROW_ASSERT(not multiple_out_edges, "First state has multiple out edges");
553 #if HAVE_ASSERTS
554  multiple_out_edges = true;
555 #endif
556  }
557  THROW_ASSERT(multiple_out_edges, "No output edge found");
558  return next_state;
559  }
560 
561  private:
563 };
564 #endif
std::map< vertex, unsigned int > vertex_to_state_id
CustomOrderedSet< unsigned int > BB_ids
set of BB ids associated with the state
EdgeDescriptor AddEdge(const vertex source, const vertex target, const int selector, const TransitionInfoRef info)
Add an edge with information associated.
void * top(node_stack *head)
Definition: tree.c:75
bool is_a_dag
true when the FSM has cycles
bool isOriginalState
flag to check if the state is duplicated and the original one
unsigned int loopId
ID of the loop this state belongs to.
std::list< vertex > starting_operations
set of operation vertices which starts in this state
unsigned int max_cycles
maximum number of cycles
StateTransitionGraphInfoRef GetStateTransitionGraphInfo()
Return the info associated with the graph.
Base class storing user data information to the whole graph.
Definition: graph_info.hpp:60
vertex exit_node
vertex that contains exit node
string target
Definition: lenet_tvm.py:16
vertex exit_node
this vertex represents the exit state of the state transition graph
void print(std::ostream &os, const int detail_level) const override
Implementation of print method for this kind of node.
REF_FORWARD_DECL(Parameter)
last_intermediate_state(StateTransitionGraphConstRef input_state_graph, bool enable)
vertex entry_node
vertex that contains entry node
CustomOrderedSet< vertex > ops
bool is_dummy
flag to check if the state is dummy or not
EdgeInfoRef GetEdgeInfo(typename boost::graph_traits< graphs_collection >::vertex_descriptor source, typename boost::graph_traits< graphs_collection >::vertex_descriptor target)
Get the edge property.
Definition: graph.hpp:794
Class specification of the graph structures.
TransitionInfoRef GetTransitionInfo(const EdgeDescriptor transition)
Return the info associated with a transition.
std::list< vertex > moved_ending_op
set of moved operations ending in this state
StateInfo()
Constructor.
const StateTransitionGraphConstRef state_graph
This structure defines the bulk for the state transition graph.
const EdgeInfoConstRef CGetEdgeInfo(typename boost::graph_traits< graphs_collection >::vertex_descriptor source, typename boost::graph_traits< graphs_collection >::vertex_descriptor target) const
Get the edge property.
Definition: graph.hpp:811
CustomOrderedSet< unsigned int > moved_op_use_set
set of ssa vars used in the state by the moved operations
const OpGraphConstRef op_function_graph
reference to operation graph
NodeInfoRef GetNodeInfo(typename boost::graph_traits< graphs_collection >::vertex_descriptor node)
FIXME: this method should become protected and called by equivalent method in subclasses Get the node...
Definition: graph.hpp:1039
const BehavioralHelperConstRef BH
this is the helper associated to the actual module
transition_type get_type() const
vertex operator()(vertex state)
vertex operator()(vertex top, vertex bottom)
std::list< vertex > executing_operations
set of operation vertices which require inputs in this state
#define STR(s)
Macro which performs a lexical_cast to a string.
unsigned int funId
function identifier of the function from which the state has been derived
Auxiliary methods for manipulating string.
void * pipeline[MAX_PIPELINE_DEPTH]
Definition: filter.c:6
std::list< vertex > onfly_operations
set of operation vertices already in execution at the beginning of this state
Class used to describe a state transition graph.
unsigned int min_cycles
in case of a dag it is possible to compute the minimum number of cycles
Functor used to write the content of a vertex to dotty file.
Definition: graph.hpp:1406
const StateTransitionGraphInfoConstRef CGetStateTransitionGraphInfo() const
Return the info associated with the graph.
const NodeInfoConstRef CGetNodeInfo(typename boost::graph_traits< graphs_collection >::vertex_descriptor node) const
Get the node property.
Definition: graph.hpp:781
next_unique_state(StateTransitionGraphConstRef input_state_graph)
Base class description of data information associated with each node of a graph.
OpGraphConstRef op_function_graph
pointer to graph storing information about operations
TransitionInfo(OpGraphConstRef g)
redefinition of set to manage ordered/unordered structures
boost::graph_traits< graph >::vertex_descriptor vertex
vertex definition.
Definition: graph.hpp:1303
const OpGraphConstRef op_function_graph
reference to operation graph
General class used to describe a graph in PandA.
Definition: graph.hpp:771
Functor used to write the content of the edges to a dotty file and it is used to write specific edge ...
Base class storing user data information.
Definition: edge_info.hpp:55
bool all_paths
this state is duplicated but manage of path incoming in the phis
const StateTransitionGraphConstRef state_graph
Base class for graph property.
GraphInfoRef GetGraphInfo()
FIXME: this method should become protected and called by equivalent method in subclasses Get the grap...
Definition: graph.hpp:1051
CustomOrderedSet< unsigned > labels
Template definition of refcount.
Base class description of data information associated with each edge of a graph.
bulk graph.
Definition: graph.hpp:287
std::string name
string used to give a label to the state
vertex GetVertex(unsigned int state_id) const
Return the state vertex corresponding to the given id.
StateInfoRef GetStateInfo(const vertex state)
Return the info associated with a state.
Functor used to write the content of the edges to a dotty file.
Definition: graph.hpp:1441
const GraphInfoConstRef CGetGraphInfo() const
FIXME: this method should become protected and called by equivalent method in subclasses Get the grap...
Definition: graph.hpp:1062
Structure holding information about a node into graph.
StateTransitionType
Types associated with the edges of the graph.
std::list< vertex > ending_operations
set of operation vertices completing their execution in this state
size_t get_epp_increment() const
vertex clonedState
pointer to the cloned state
std::list< vertex > moved_exec_op
set of moved operations in execution
Template borrowed from the ANTLR library by Terence Parr (http://www.jGuru.com - Software rights: htt...
Definition: refcount.hpp:94
const CustomOrderedSet< vertex > & get_operations() const
const CustomOrderedSet< unsigned > & get_labels() const
bool is_duplicated
flag to check if the state is duplicated or not
Wrefcount< const HLS_manager > HLSMgr
Weak Reference to function behavior.
const OpGraphConstRef op_function_graph
reference to operation graph
const TransitionInfoConstRef CGetTransitionInfo(const EdgeDescriptor transition) const
Return the info associated with a transition.
unsigned int sourceBb
ID of the bb edge associated with the phi operation.
vertex entry_node
this vertex represents the entry state of the state transition graph
Class used to describe a particular graph with operations as nodes.
Definition: op_graph.hpp:783
std::map< unsigned int, vertex > state_id_to_vertex
CustomOrderedSet< unsigned int > moved_op_def_set
set of ssa vars defined in the state by the moved operations
void set_epp_increment(size_t n)
const BehavioralHelperConstRef BH
this is the helper associated to the actual module
CONSTREF_FORWARD_DECL(BehavioralHelper)
Structure holding information about the whole graph.
const StateInfoConstRef CGetStateInfo(const vertex state) const
Return the info associated with a state.
#define NULL_VERTEX
null vertex definition
Definition: graph.hpp:1305
Structure holding the information about an edge into the graph.
Functor template used to write the content of the nodes to a dotty file.
const StateInfoConstRef CGetStateInfo(unsigned int state_id) const
Return the info associated with a state, given its ID is the ID of the state to be considered...
boost::graph_traits< graph >::edge_descriptor EdgeDescriptor
edge definition.
Definition: graph.hpp:1316
#define THROW_ASSERT(cond, str_expr)
helper function used to check an assert and if needed to throw an error in a standard way ...
Definition: exceptions.hpp:289

Generated on Mon Feb 12 2024 13:02:54 for PandA-2024.02 by doxygen 1.8.13