PandA-2024.02
state_transition_graph_manager.cpp
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  */
49 
51 #include "behavioral_helper.hpp"
52 #include "op_graph.hpp"
53 
54 #include "generic_device.hpp"
55 #include "structural_manager.hpp"
56 #include "technology_manager.hpp"
57 #include "technology_node.hpp"
58 
60 #include "fu_binding.hpp"
61 #include "function_behavior.hpp"
62 #include "hls.hpp"
63 #include "hls_device.hpp"
64 #include "hls_manager.hpp"
65 
67 #include "Parameter.hpp"
68 
72 
73 #include "funit_obj.hpp"
74 #include "multi_unbounded_obj.hpp"
75 
77 #include "var_pp_functor.hpp"
78 
80 #include "dbgPrintHelper.hpp" // for DEBUG_LEVEL_
81 #include <boost/graph/topological_sort.hpp>
82 
83 #define PP_MAX_CYCLES_BOUNDED "max-cycles-bounded"
84 #define DEFAULT_MAX_CYCLES_BOUNDED (8)
85 
87  const ParameterConstRef _Param)
88  : state_transition_graphs_collection(StateTransitionGraphsCollectionRef(new StateTransitionGraphsCollection(
90  _HLSMgr->CGetFunctionBehavior(_HLS->functionId)->CGetOpGraph(FunctionBehavior::CFG))),
91  _Param))),
92  ACYCLIC_STG_graph(StateTransitionGraphRef(new StateTransitionGraph(
93  state_transition_graphs_collection, TransitionInfo::StateTransitionType::ST_EDGE_NORMAL))),
95  state_transition_graphs_collection, TransitionInfo::StateTransitionType::ST_EDGE_NORMAL |
96  TransitionInfo::StateTransitionType::ST_EDGE_FEEDBACK))),
98  state_transition_graphs_collection,
99  TransitionInfo::StateTransitionType::ST_EDGE_NORMAL | TransitionInfo::StateTransitionType::ST_EDGE_EPP))),
100  op_function_graph(_HLSMgr->CGetFunctionBehavior(_HLS->functionId)->CGetOpGraph(FunctionBehavior::CFG)),
101  Param(_Param),
102  output_level(_Param->getOption<int>(OPT_output_level)),
103  debug_level(_Param->getOption<int>(OPT_debug_level)),
104  _max_cycles_bounded(_Param->IsParameter(PP_MAX_CYCLES_BOUNDED) ?
105  _Param->GetParameter<unsigned int>(PP_MAX_CYCLES_BOUNDED) :
107  HLS(_HLS),
109  new StateTransitionGraph_constructor(state_transition_graphs_collection, _HLSMgr, _HLS->functionId)))
110 {
111  STG_builder->create_entry_state();
112  STG_builder->create_exit_state();
113 }
114 
116 
118 {
119  return ACYCLIC_STG_graph;
120 }
121 
123 {
124  return STG_graph;
125 }
126 
128 {
129  return EPP_STG_graph;
130 }
131 
133 {
134  auto info = STG_graph->GetStateTransitionGraphInfo();
135  if(info->is_a_dag && !Param->getOption<bool>(OPT_disable_bounded_function))
136  {
137  std::list<vertex> sorted_vertices;
138  ACYCLIC_STG_graph->TopologicalSort(sorted_vertices);
139  CustomUnorderedMap<vertex, unsigned int> CSteps_min, CSteps_max;
140  bool has_dummy_state = false;
141  for(const auto v : sorted_vertices)
142  {
143  CSteps_min[v] = 0;
144  CSteps_max[v] = 0;
145  has_dummy_state |= ACYCLIC_STG_graph->GetStateInfo(v)->is_dummy;
146  InEdgeIterator ie, iend;
147  boost::tie(ie, iend) = boost::in_edges(v, *ACYCLIC_STG_graph);
148  const auto ie_first = ie;
149  for(; ie != iend; ie++)
150  {
151  const auto src = boost::source(*ie, *ACYCLIC_STG_graph);
152  CSteps_max[v] = std::max(CSteps_max[v], 1 + CSteps_max[src]);
153  if(ie == ie_first)
154  {
155  CSteps_min[v] = 1 + CSteps_min[src];
156  }
157  else
158  {
159  CSteps_min[v] = std::min(CSteps_min[v], 1 + CSteps_max[src]);
160  }
161  }
162  }
163  THROW_ASSERT(CSteps_min.find(info->exit_node) != CSteps_min.end(), "Exit node not reachable");
164  THROW_ASSERT(CSteps_max.find(info->exit_node) != CSteps_max.end(), "Exit node not reachable");
165  info->min_cycles = CSteps_min.find(info->exit_node)->second - 1;
166  info->max_cycles = CSteps_max.find(info->exit_node)->second - 1;
167  info->bounded =
168  is_pipelined || (info->min_cycles == info->max_cycles && info->max_cycles <= _max_cycles_bounded &&
169  info->min_cycles > 0 && !has_dummy_state);
170  }
171  else
172  {
173  THROW_ASSERT(Param->getOption<bool>(OPT_disable_bounded_function) || !is_pipelined,
174  "A pipelined function should always generate a DAG");
175  }
176 }
177 
179 {
180  return STG_graph->CGetStateTransitionGraphInfo()->entry_node;
181 }
182 
184 {
185  CustomOrderedSet<vertex> vertex_set;
186  VertexIterator v, vend;
187  for(boost::tie(v, vend) = boost::vertices(*STG_graph); v != vend; v++)
188  {
189  const std::list<vertex>* operations;
190  switch(statetypes)
191  {
192  case EXECUTING:
193  operations = &(STG_graph->CGetStateInfo(*v)->executing_operations);
194  break;
195  case STARTING:
196  operations = &(STG_graph->CGetStateInfo(*v)->starting_operations);
197  break;
198  case ENDING:
199  operations = &(STG_graph->CGetStateInfo(*v)->ending_operations);
200  break;
201  default:
202  THROW_UNREACHABLE("Unknown state type. Which one are you lookig for?");
203  }
204  auto op_it_end = operations->end();
205  bool stop_flag;
206  stop_flag = false;
207  for(auto op_it = operations->begin(); op_it != op_it_end && !stop_flag; ++op_it)
208  {
209  if(*op_it == op)
210  {
211  stop_flag = true;
212  vertex_set.insert(*v);
213  }
214  }
215  }
216  THROW_ASSERT(vertex_set.size() > 0,
217  "Something wrong! Operation " + GET_NAME(op_function_graph, op) + " is executed " + "into no states");
218  return vertex_set;
219 }
220 
222 {
223  return get_states(op, StateTransitionGraphManager::StateTypes::ENDING);
224 }
225 
227 {
228  return get_states(op, StateTransitionGraphManager::StateTypes::STARTING);
229 }
230 
232 {
233  return get_states(op, StateTypes::EXECUTING);
234 }
235 
237 {
238  return STG_graph->CGetStateTransitionGraphInfo()->exit_node;
239 }
240 
242 {
243  return STG_graph->CGetStateInfo(state)->name;
244 }
245 
247 {
248  return ACYCLIC_STG_graph;
249 }
250 
252 {
253  return STG_graph;
254 }
255 
257 {
258  return EPP_STG_graph;
259 }
260 
262 {
263  return static_cast<unsigned int>(boost::num_vertices(*state_transition_graphs_collection) - 2);
264 }
265 
267 {
270  "---" + std::string("Is a DAG: ") +
271  (STG_graph->CGetStateTransitionGraphInfo()->is_a_dag ? "T" : "F"));
272  if(STG_graph->CGetStateTransitionGraphInfo()->min_cycles != 0 &&
273  STG_graph->CGetStateTransitionGraphInfo()->max_cycles != 0)
274  {
276  "---Minimum number of cycles: " + STR(STG_graph->CGetStateTransitionGraphInfo()->min_cycles));
278  "---Maximum number of cycles " + STR(STG_graph->CGetStateTransitionGraphInfo()->max_cycles));
279  }
280 }
281 
283 {
284  if(multi_unbounded_table.find(s) == multi_unbounded_table.end())
285  {
287  generic_objRef(new multi_unbounded_obj(s, ops, std::string("mu_") + STG_graph->CGetStateInfo(s)->name));
288  }
289 }
290 
292 {
293  INDENT_DBG_MEX(DEBUG_LEVEL_VERBOSE, HLS->debug_level, "---Specializing " + mu_mod->get_path());
294  auto mut = GetPointer<multi_unbounded_obj>(mu);
295  THROW_ASSERT(mut, "unexpected condition");
296  structural_objectRef inOps = mu_mod->find_member("ops", port_vector_o_K, mu_mod);
297  auto* port = GetPointer<port_o>(inOps);
298  const auto& ops = mut->get_ops();
299  auto n_in_ports = static_cast<unsigned int>(ops.size());
300  port->add_n_ports(n_in_ports, inOps);
301 }
302 
304 {
305  const auto& SM = HLS->datapath;
306  const auto& circuit = SM->get_circ();
307  INDENT_DBG_MEX(DEBUG_LEVEL_VERBOSE, debug_level, "-->Adding :multi-unbounded controllers");
308  for(const auto& state2mu : multi_unbounded_table)
309  {
310  auto mu = state2mu.second;
311  std::string name = mu->get_string();
312  std::string library = HLS->HLS_D->get_technology_manager()->get_library(SIMPLEJOIN_STD);
313  structural_objectRef mu_mod = SM->add_module_from_technology_library(name, SIMPLEJOIN_STD, library, circuit,
314  HLS->HLS_D->get_technology_manager());
315  specialise_mu(mu_mod, mu);
316 
317  structural_objectRef port_ck = mu_mod->find_member(CLOCK_PORT_NAME, port_o_K, mu_mod);
318  SM->add_connection(clock_port, port_ck);
319  structural_objectRef port_rst = mu_mod->find_member(RESET_PORT_NAME, port_o_K, mu_mod);
320  SM->add_connection(reset_port, port_rst);
321  mu->set_structural_obj(mu_mod);
322  auto p_obj = mu_mod->find_member("sop", port_o_K, mu_mod);
323  mu->set_out_sign(p_obj);
324  }
325  INDENT_DBG_MEX(DEBUG_LEVEL_VERBOSE, debug_level, "<--Adding :multi-unbounded controllers");
326 }
Data structure representing the entire HLS information.
#define INDENT_DBG_MEX(dbgLevel, curDbgLevel, mex)
We are producing a debug version of the program, so the message is printed;.
StateTransitionGraphManager(const HLS_managerConstRef HLSMgr, hlsRef HLS, const ParameterConstRef _Param)
Constructor of the class.
File containing functions and utilities to support the printing of debug messagges.
Base class for all unbounded objects added to datapath.
const StateTransitionGraph_constructorRef STG_builder
reference to the class for building the graph
structural_managerRef datapath
Store the datapath description.
Definition: hls.hpp:155
This file contains the structures needed to manage a graph that will represent the state transition g...
CustomOrderedSet< vertex > get_ending_states(const vertex &op) const
const StateTransitionGraphConstRef CGetEPPStg() const
Returns pointer to acyclic STG with additional edges for Efficient Path Profiling.
const structural_objectRef get_circ() const
Get a reference to circ field.
const StateTransitionGraphsCollectionRef state_transition_graphs_collection
The bulk graph.
vertex get_entry_state() const
Gets vertex that represents state that contains entry node.
#define INDENT_OUT_MEX(outLevel, curOutLevel, mex)
StateTransitionGraphRef GetAstg()
Returns pointer to state transition graph created.
#define GET_NAME(data, vertex_index)
Helper macro returning the name associated with a node.
const HLS_deviceRef HLS_D
reference to the information representing the target for the synthesis
Definition: hls.hpp:107
boost::graph_traits< graph >::in_edge_iterator InEdgeIterator
in_edge_iterator definition.
Definition: graph.hpp:1310
This structure defines the bulk for the state transition graph.
void ComputeCyclesCount(bool is_pipelined)
Compute minimum and maximum number of cycles for bounded scheduling.
vertex get_exit_state() const
Gets vertex that represents state that contains exit node.
virtual structural_objectRef find_member(const std::string &id, so_kind type, const structural_objectRef owner) const =0
Return the object named id of a given type which belongs to or it is associated with the object...
#define min(x, y)
Class specification of the manager of the technology library data structures.
const OpGraphConstRef op_function_graph
reference to operation graph
#define OUTPUT_LEVEL_MINIMUM
minimum debugging print is performed.
StateTransitionGraphRef GetEPPStg()
Returns pointer to acyclic STG with additional edges for Efficient Path Profiling.
const ParameterConstRef Param
class containing all the parameters
#define STR(s)
Macro which performs a lexical_cast to a string.
std::map< vertex, generic_objRef > multi_unbounded_table
map between state and multi-unbounded controllers
#define max
Definition: backprop.h:17
#define CLOCK_PORT_NAME
standard name for ports
#define THROW_UNREACHABLE(str_expr)
helper function used to specify that some points should never be reached
Definition: exceptions.hpp:292
#define DEFAULT_MAX_CYCLES_BOUNDED
class modeling a register object
absl::flat_hash_map< T, U, Hash, Eq, Alloc > CustomUnorderedMap
Definition: custom_map.hpp:148
CustomOrderedSet< vertex > get_states(const vertex &op, StateTypes statetypes) const
Class used to describe a state transition graph.
File contanining the structures necessary to manage a graph that will represent a state transition gr...
#define SIMPLEJOIN_STD
Class specification of the data structures used to manage technology information. ...
const std::string get_path() const
Return a unique identifier of the structural object.
boost::graph_traits< graph >::vertex_descriptor vertex
vertex definition.
Definition: graph.hpp:1303
This file contains the structures needed to manage a graph that will represent the state transition g...
CustomOrderedSet< vertex > get_starting_states(const vertex &op) const
boost::graph_traits< graph >::vertex_iterator VertexIterator
vertex_iterator definition.
Definition: graph.hpp:1307
#define PP_MAX_CYCLES_BOUNDED
Header include.
const StateTransitionGraphRef STG_graph
The complete version of the STG.
const StateTransitionGraphConstRef CGetAstg() const
Returns pointer to state transition graph created.
const StateTransitionGraphRef EPP_STG_graph
The acyclic version of stg with additional edges necessary for Efficient Path Profiling.
void add_to_SM(structural_objectRef clock_port, structural_objectRef reset_port)
Add components to the datapath required by the FSM.
void specialise_mu(structural_objectRef &mu_mod, generic_objRef mu) const
#define OUTPUT_LEVEL_VERY_PEDANTIC
verbose debugging print is performed.
Data structure used to store the functional-unit binding of the vertexes.
const StateTransitionGraphRef ACYCLIC_STG_graph
The acyclic version of the STG.
Data structures used in operations graph.
Template borrowed from the ANTLR library by Terence Parr (http://www.jGuru.com - Software rights: htt...
Definition: refcount.hpp:94
this class is used to manage the command-line or XML options.
StateTransitionGraphRef GetStg()
Returns pointer to state transition graph created.
#define RESET_PORT_NAME
~StateTransitionGraphManager()
Destructor.
Class implementation of the structural_manager.
Generic device description.
Base class for all register into datapath.
#define DEBUG_LEVEL_VERBOSE
verbose debugging print is performed.
Data structure definition for high-level synthesis flow.
const StateTransitionGraphConstRef CGetStg() const
Returns pointer to state transition graph created.
Structure holding information about the whole graph.
CustomOrderedSet< vertex > get_execution_states(const vertex &op) const
int debug_level
debugging level of the class
Definition: hls.hpp:172
Structure holding the information about an edge into the graph.
void add_multi_unbounded_obj(vertex s, const CustomOrderedSet< vertex > &ops)
refcount< generic_obj > generic_objRef
RefCount definition for generic_obj class.
HLS specialization of generic_device.
A brief description of the C++ Header File.
std::string get_state_name(vertex state) const
Get the name of a state.
#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