PandA-2024.02
chordal_coloring_register.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  */
44 
45 #include "Parameter.hpp"
46 #include "behavioral_helper.hpp"
47 #include "cpu_time.hpp"
48 #include "dbgPrintHelper.hpp"
49 #include "hls.hpp"
50 #include "hls_manager.hpp"
51 #include "liveness.hpp"
52 #include "reg_binding.hpp"
54 #include "utility.hpp"
55 
56 #include <boost/graph/sequential_vertex_coloring.hpp>
57 
58 #include <vector>
59 
61  unsigned int _funId,
62  const DesignFlowManagerConstRef _design_flow_manager)
63  : conflict_based_register(_Param, _HLSMgr, _funId, _design_flow_manager,
65 {
66 }
67 
69 
70 bool chordal_coloring_register::lex_compare_gt(const std::vector<unsigned int>& v1,
71  const std::vector<unsigned int>& v2) const
72 {
73  /*
74  std::cout << "v1 ";
75  std::copy(v1.begin(), v1.end(), std::ostream_iterator<unsigned int>(std::cout, " "));
76  std::cout << "\nv2 ";
77  std::copy(v2.begin(), v2.end(), std::ostream_iterator<unsigned int>(std::cout, " "));
78  std::cout << "\n";
79  */
80  size_t v1_size = v1.size();
81  if(v1_size == 0)
82  {
83  return false;
84  }
85  else
86  {
87  size_t v2_size = v2.size();
88  if(v2_size == 0)
89  {
90  return true;
91  }
92  else
93  {
94  for(unsigned int index = 0; index < v1_size && index < v2_size; ++index)
95  {
96  if(v1[index] > v2[index])
97  {
98  return true;
99  }
100  else if(v1[index] < v2[index])
101  {
102  return false;
103  }
104  }
106  if(v1_size > v2_size)
107  {
108  return true;
109  }
110  else
111  {
112  return false;
113  }
114  }
115  }
116 }
117 
119 {
120  long step_time = 0;
122  {
123  START_TIME(step_time);
124  }
126  unsigned int cg_num_vertices = HLS->storage_value_information->get_number_of_storage_values();
127  const unsigned int NO_ORDER = std::numeric_limits<unsigned int>::max();
128  std::vector<cg_vertex_descriptor> vertex_order(cg_num_vertices);
129 
130  std::vector<std::vector<unsigned int>> label(cg_num_vertices);
131  std::vector<unsigned int> seq(cg_num_vertices, NO_ORDER);
132 
133  for(unsigned int irev = 0; irev < cg_num_vertices; ++irev)
134  {
135  unsigned int i = cg_num_vertices - irev - 1;
137  unsigned int vx_index = 0;
138  bool found;
139  found = false;
140  for(unsigned int vindex = 0; vindex < cg_num_vertices; ++vindex)
141  {
142  if(seq[vindex] == NO_ORDER)
143  {
144  if(!found)
145  {
146  vx_index = vindex;
147  found = true;
148  }
149  else if(lex_compare_gt(label[vindex], label[vx_index]))
150  {
151  vx_index = vindex;
152  }
153  }
154  }
155  THROW_ASSERT(found, "maximal not found");
156  seq[vx_index] = i;
157  cg_vertex_descriptor vx = boost::vertex(vx_index, *cg);
158  vertex_order[i] = vx;
159  // for each unnumbered vertex v adjacent to vx
160  // label(v)=label(v) + i
161  boost::graph_traits<conflict_graph>::adjacency_iterator adj_i, adj_e;
162  for(boost::tie(adj_i, adj_e) = boost::adjacent_vertices(vx, *cg); adj_i != adj_e; ++adj_i)
163  {
164  long unsigned int vindex = get(boost::vertex_index, *cg, *adj_i);
165  if(seq[vindex] == NO_ORDER)
166  {
167  bool add;
168  add = true;
169  auto it_end = label[vindex].end();
170  for(auto it = label[vindex].begin(); it != it_end && add; ++it)
171  {
172  if(*it == i)
173  {
174  add = false;
175  }
176  }
177  if(add)
178  {
179  label[vindex].push_back(i); // append the label
180  }
181  }
182  }
183  }
184 
186  cg_vertices_size_type num_colors = boost::sequential_vertex_coloring(
187  *cg,
188  boost::make_iterator_property_map(vertex_order.begin(), boost::identity_property_map(),
189  boost::graph_traits<conflict_graph>::null_vertex()),
190  color);
191 
194  const std::list<vertex>& support = HLS->Rliv->get_support();
195 
196  const auto vEnd = support.end();
197  for(auto vIt = support.begin(); vIt != vEnd; ++vIt)
198  {
199  const CustomOrderedSet<unsigned int>& live = HLS->Rliv->get_live_in(*vIt);
200  auto k_end = live.end();
201  for(auto k = live.begin(); k != k_end; ++k)
202  {
203  unsigned int storage_value_index = HLS->storage_value_information->get_storage_value_index(*vIt, *k);
204  HLS->Rreg->bind(storage_value_index,
205  static_cast<unsigned int>(color[boost::vertex(storage_value_index, *cg)]));
206  }
207  }
208  delete cg;
209  HLS->Rreg->set_used_regs(static_cast<unsigned int>(num_colors));
211  {
212  STOP_TIME(step_time);
213  }
215  {
217  }
219  "-->Register binding information for function " +
220  HLSMgr->CGetFunctionBehavior(funId)->CGetBehavioralHelper()->get_function_name() + ":");
222  std::string("---Register allocation algorithm obtains ") +
223  (num_colors == register_lower_bound ? "an optimal" : "a sub-optimal") +
224  " result: " + STR(num_colors) + " registers" +
225  (num_colors == register_lower_bound ? "" : ("(LB:" + STR(register_lower_bound) + ")")));
227  {
228  HLS->Rreg->print();
229  }
231  {
233  "Time to perform register binding: " + print_cpu_time(step_time) + " seconds");
234  }
237  {
239  }
241 }
Class specification to contain liveness information.
boost::iterator_property_map< cg_vertices_size_type *, cg_vertex_index_map, cg_vertices_size_type, cg_vertices_size_type & > color
const HLS_managerRef HLSMgr
information about all the HLS synthesis
Definition: hls_step.hpp:205
Data structure representing the entire HLS information.
File containing functions and utilities to support the printing of debug messagges.
refcount< reg_binding > reg_bindingRef
RefCount type definition of the reg_binding class structure.
~chordal_coloring_register() override
Destructor of the class.
const int output_level
The output level.
Class managing the register binding.
Definition: reg_binding.hpp:70
boost::graph_traits< conflict_graph >::vertex_descriptor cg_vertex_descriptor
#define INDENT_OUT_MEX(outLevel, curOutLevel, mex)
void set_used_regs(unsigned int regs)
sets number of used register
const unsigned int funId
identifier of the function to be processed (0 means that it is a global step)
base seq[MAX_SIZE]
void bind(unsigned int sv, unsigned int index)
conflict_graph * cg
conflict graph
#define OUTPUT_LEVEL_MINIMUM
minimum debugging print is performed.
Include a set of utilities used to manage CPU time measures.
#define STR(s)
Macro which performs a lexical_cast to a string.
#define max
Definition: backprop.h:17
boost::graph_traits< conflict_graph >::vertices_size_type cg_vertices_size_type
const std::list< vertex > & get_support() const
return the support set of the live in/out
Definition: liveness.hpp:212
#define START_TIME(time_var)
Macro used to store the start time into time_var.
Definition: cpu_time.hpp:133
Data structure used to store the register binding of variables.
StorageValueInformationRef storage_value_information
data-structure for storage values
Definition: hls.hpp:130
Class specification of the register allocation algorithms based on chordal algorithm.
static const uint32_t k[]
Definition: sha-256.c:22
HLSFlowStep_Type
Definition: hls_step.hpp:95
chordal_coloring_register(const ParameterConstRef Param, const HLS_managerRef HLSMgr, unsigned int funId, const DesignFlowManagerConstRef design_flow_manager)
Constructor of the class.
#define index(x, y)
Definition: Keccak.c:74
#define STOP_TIME(time_var)
Macro used to store the elapsed time into time_var.
Definition: cpu_time.hpp:136
boost::graph_traits< graph >::vertex_descriptor vertex
vertex definition.
Definition: graph.hpp:1303
DesignFlowStep_Status
The status of a step.
This file collects some utility functions and macros.
reg_bindingRef Rreg
Store the refcounted register binding of the variables.
Definition: hls.hpp:133
unsigned int register_lower_bound
lower bound
This package is used to define the storage value scheme adopted by the register allocation algorithms...
livenessRef Rliv
data-structure containing the variable liveness
Definition: hls.hpp:127
std::string print_cpu_time(long int t)
massage a long which represents a time interval in milliseconds, into a string suitable for output ...
Definition: cpu_time.hpp:110
virtual void print() const
Function that prints the class variable2obj.
Definition: Variable.hpp:82
bool lex_compare_gt(const std::vector< unsigned int > &v1, const std::vector< unsigned int > &v2) const
compare lexically two vectors
void add(int accelnum, int startidx, int endidx)
Definition: add.c:11
#define OUTPUT_LEVEL_PEDANTIC
verbose debugging print is performed.
#define OUTPUT_LEVEL_VERY_PEDANTIC
verbose debugging print is performed.
hlsRef HLS
HLS data structure of the function to be analyzed.
virtual unsigned int get_storage_value_index(vertex curr_vertex, unsigned int var_index)=0
Returns the index of the storage value associated with the variable in a given vertex.
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.
DesignFlowStep_Status RegisterBinding() final
Chordal coloring algorithm algorithm.
unsigned int get_number_of_storage_values() const
Returns the number of storage values inserted.
Data structure definition for high-level synthesis flow.
const CustomOrderedSet< unsigned int > & get_live_in(const vertex &v) const
Get the set of variables live at the input of a vertex.
Definition: liveness.cpp:109
#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:53 for PandA-2024.02 by doxygen 1.8.13