Limbo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GreedySearch.h
Go to the documentation of this file.
1 
16 #ifndef _LIMBO_ALGORITHMS_PLACEMENT_GREEDYSEARCH_H
17 #define _LIMBO_ALGORITHMS_PLACEMENT_GREEDYSEARCH_H
18 
19 #include <iostream>
20 #include <iterator>
21 
23 namespace limbo
24 {
26 namespace algorithms
27 {
29 namespace placement
30 {
31 
32 using std::cout;
33 using std::endl;
34 using std::pair;
35 
38 #if 0
39 struct GSCallback
41 {
43  typedef Component node_type;
44  typedef Segment row_type;
45  typedef int site_coordinate_type;
46  typedef list<node_type*> node_vector_type;
47  typedef list<node_type*> node_fail_vector_type;
48  typedef vector<row_type*> row_vector_type;
49  struct cost_type
50  {
51  long cost;
52  int seg_id;
53  int site_id;
54  int color_id;
55  vector<list<node_type*>::iterator> vItNode;
56  cost_type()
58  {
60  seg_id = -1;
61  site_id = -1;
62  color_id = -1;
63  }
64  cost_type(cost_type const& rhs)
65  {
66  cost = rhs.cost;
67  seg_id = rhs.seg_id;
68  site_id = rhs.site_id;
69  color_id = rhs.color_id;
70  vItNode = rhs.vItNode;
71  }
74  friend bool operator<(cost_type const& c1, cost_type const& c2) {return c1.cost < c2.cost;}
75  };
76 
77  SegLegalizerCF* pslmr;
78 
79  GSCallback(SegLegalizerCF* psl) : pslmr(psl) {}
80  GSCallback(GSCallback const& rhs) : pslmr(rhs.pslmr) {}
81 
85  site_coordinate_type site_xl(const node_type* pn) const {}
86  site_coordinate_type site_xh(const node_type* pn) const {}
87 
89  pair<size_t, size_t> row_range(const node_type* pn) const {}
90 
92  node_vector_type& nodes_in_row(size_t row_idx) const {}
93 
95  bool check_displace(const node_type* pn, site_coordinate_type x, site_coordinate_type y) const {}
96 
104  cost_type calc_cost(const node_type* pcomp2, site_coordinate_type seg_id2,
105  site_coordinate_type site_id2, vector<list<node_type*>::iterator> const& vItNode, unsigned int swap_cnt) const{}
106 
108  bool check_valid(cost_type const& c) const {}
110  void apply(node_type* pcomp, cost_type const& c, node_fail_vector_type& vFailNode, int swap_cnt) const {}
111 
113  row_type* row(size_t row_idx) const {}
114  site_coordinate_type site_xl(const row_type* pr) const {}
115  site_coordinate_type site_xh(const row_type* pr) const {}
116 };
117 #endif
118 
121 template <typename T>
123 {
124  typedef T& value_type;
125  typedef T const& const_value_type;
126 };
129 template <typename T>
130 struct gs_choose_type<T*>
131 {
132  typedef T* value_type;
133  typedef const T* const_value_type;
134 };
135 
138 template <typename CallbackType>
140 {
141  public:
143  typedef CallbackType callback_type;
144  typedef typename callback_type::node_type node_type;
145  typedef typename callback_type::cost_type cost_type;
146  typedef typename callback_type::row_type row_type;
147  typedef typename callback_type::site_coordinate_type site_coordinate_type;
148  typedef typename callback_type::node_vector_type node_vector_type;
149  typedef typename callback_type::node_fail_vector_type node_fail_vector_type;
150  typedef typename callback_type::row_vector_type row_vector_type;
151 
152  // it can be row_type& or row_type*
155  // it can be node_type& or node_type*
159 
162  GreedySearch(callback_type cbk = callback_type()) : m_cbk(cbk) {}
163 
167  void operator()(node_fail_vector_type& vFailNode, int max_swap_cnt) {this->run(vFailNode, max_swap_cnt);}
171  void run(node_fail_vector_type& vFailNode, int max_swap_cnt)
172  {
173  for (typename node_fail_vector_type::iterator it = vFailNode.begin();
174  it != vFailNode.end(); )
175  {
176  node_value_type n = *it;
177  bool success_flag = false;
178  for (int i = 0; i <= max_swap_cnt; ++i)
179  {
180  if (search_swap(n, vFailNode, i))
181  {
182  success_flag = true;
183  break;
184  }
185  }
186  if (success_flag)
187  {
188  typename node_fail_vector_type::iterator itPrev = it;
189  ++it;
190  vFailNode.erase(itPrev);
191  }
192  else ++it;
193  }
194  }
200  bool search_swap(node_value_type n, node_fail_vector_type& vFailNode, int swap_cnt)
201  {
202  typedef typename node_vector_type::iterator node_iterator_type;
203  pair<size_t, size_t> row_range = m_cbk.row_range(n);
204 
205  size_t row_idx1 = row_range.first;
206  size_t row_idx2 = row_range.second;
207  assert(row_idx1 < row_idx2);
208 
209  // it should be initialized to invalid
210  cost_type best_cost;
211  assert(!m_cbk.check_valid(best_cost));
212  for (size_t row_idx = row_idx1; row_idx != row_idx2; ++row_idx)
213  {
214  node_vector_type& vNode = m_cbk.nodes_in_row(row_idx);
215  // row can be T& or T*
216  row_const_value_type row = m_cbk.row(row_idx);
217  // prepare a set of consecutive iterator2
218  vector<node_iterator_type> vIt2 (swap_cnt+1, vNode.begin());
219  bool next_flag1 = false; // if true, perform continue
220  for (int i = 0; i < swap_cnt; ++i)
221  {
222  if (vIt2.back() == vNode.end())
223  {
224  next_flag1 = true;
225  break;
226  }
227  ++(vIt2.back());
228  }
229  if (next_flag1) continue;
230  for (; vIt2.back() != vNode.end(); ++vIt2.back())
231  {
232  // prepare the set of consecutive iterator2
233  for (int i = vIt2.size()-1; i > 0; --i)
234  {
235  vIt2[i-1] = vIt2[i];
236  --vIt2[i-1];
237  }
238 
239  site_coordinate_type site_size_x = this->node_site_size_x(n);
240  bool next_flag2 = false; // if true, perform continue
241  // check node size
242  for (int i = 0; i < vIt2.size()-1; ++i)
243  {
244  // n should not be included in the node map
245  assert(n != *vIt2[i]);
246  if (this->node_site_size_x(*vIt2[i]) >= site_size_x)
247  {
248  next_flag2 = true;
249  break;
250  }
251  }
252  if (next_flag2) continue;
253 
254  // check whitespace size
255  site_coordinate_type ws_site_xl;
256  site_coordinate_type ws_site_xh = m_cbk.site_xl(*vIt2.back());
257  if (vIt2.front() != vNode.begin())
258  {
259  node_iterator_type it2_0 = vIt2.front();
260  --it2_0;
261  ws_site_xl = this->site_xh(*it2_0);
262  }
263  else ws_site_xl = m_cbk.site_xl(row);
264  // if whitespace is too small
265  if (ws_site_xh-ws_site_xl < site_size_x) continue;
266 
267  // enumerate all possible positions in the whitespace
268  for (site_coordinate_type tgt_site = ws_site_xl;
269  tgt_site <= ws_site_xh-site_size_x; ++tgt_site)
270  {
271  // check displacment constraint
272  if (!m_cbk.check_displace(n, tgt_site, row_idx)) continue;
273  cost_type cur_cost = m_cbk.calc_cost(n, row_idx, tgt_site, vIt2, swap_cnt);
274  if (!m_cbk.check_valid(best_cost) || cur_cost < best_cost)
275  {
276  best_cost = cur_cost;
277  }
278  }
279  }
280  }
281  if (m_cbk.check_valid(best_cost))
282  {
283  // I assume apply() will remove replaced nodes and insert current node
284  m_cbk.apply(n, best_cost, vFailNode, swap_cnt);
285  return true;
286  }
287  return false;
288  }
289  protected:
292  site_coordinate_type node_site_size_x(node_const_value_type n) const
293  {
294  return m_cbk.site_xh(n)-m_cbk.site_xl(n);
295  }
299  site_coordinate_type node_site_gap_x(node_const_value_type n1, node_const_value_type n2) const
300  {
301  return m_cbk.site_xl(n2)-m_cbk.site_xh(n1);
302  }
305  site_coordinate_type site_xl(node_const_value_type n) const
306  {
307  return m_cbk.site_xl(n);
308  }
311  site_coordinate_type site_xh(node_const_value_type n) const
312  {
313  return m_cbk.site_xh(n);
314  }
315 
316  callback_type m_cbk;
317 };
318 
319 } // namespace placement
320 } // namespace algorithms
321 } // namespace limbo
322 
323 #endif
callback_type m_cbk
a copiable callback_type is required
Definition: GreedySearch.h:316
void apply(GdsCellReference const &cellRef, ObjectType *object)
apply cell reference
site_coordinate_type node_site_gap_x(node_const_value_type n1, node_const_value_type n2) const
Definition: GreedySearch.h:299
const T * const_value_type
constant value type
Definition: GreedySearch.h:133
site_coordinate_type site_xh(node_const_value_type n) const
Definition: GreedySearch.h:311
T const & const_value_type
constant value type
Definition: GreedySearch.h:125
void operator()(node_fail_vector_type &vFailNode, int max_swap_cnt)
Definition: GreedySearch.h:167
namespace for Limbo
Definition: GraphUtility.h:22
std::iterator_traits< Iterator >::value_type max(Iterator first, Iterator last)
get max of an array
Definition: Math.h:61
site_coordinate_type node_site_size_x(node_const_value_type n) const
Definition: GreedySearch.h:292
void run(node_fail_vector_type &vFailNode, int max_swap_cnt)
Definition: GreedySearch.h:171
bool search_swap(node_value_type n, node_fail_vector_type &vFailNode, int swap_cnt)
Definition: GreedySearch.h:200
site_coordinate_type site_xl(node_const_value_type n) const
Definition: GreedySearch.h:305
callback_type::node_vector_type node_vector_type
node container for a single row
Definition: GreedySearch.h:148
GreedySearch(callback_type cbk=callback_type())
Definition: GreedySearch.h:162