@@ -47,25 +47,42 @@ void HungarianMatching::findAssignment()
4747void HungarianMatching::createMatrix ()
4848{
4949 hungarian_matrix_.resize (non_blocked_slots_);
50- int slot_index = 0 ;
51- for (int i = begin_slot_; i <= end_slot_; ++i) {
52- int pinIndex = 0 ;
53- const Point& slot_pos = slots_[i].pos ;
54- if (slots_[i].blocked ) {
55- continue ;
56- }
57- hungarian_matrix_[slot_index].resize (num_io_pins_,
58- std::numeric_limits<int >::max ());
59- for (int idx : pin_indices_) {
60- IOPin& io_pin = netlist_->getIoPin (idx);
61- if (!io_pin.isInGroup ()) {
62- int hpwl = netlist_->computeIONetHPWL (idx, slot_pos)
63- + getMirroredPinCost (io_pin, slot_pos);
64- hungarian_matrix_[slot_index][pinIndex] = hpwl;
65- pinIndex++;
50+ int pin_index = 0 ;
51+
52+ for (int idx : pin_indices_) {
53+ IOPin& io_pin = netlist_->getIoPin (idx);
54+ if (!io_pin.isInGroup ()) {
55+ bool is_mirrored = false ;
56+ std::vector<int > larger_costs;
57+ int slot_index = 0 ;
58+ for (int i = begin_slot_; i <= end_slot_; ++i) {
59+ const Point& slot_pos = slots_[i].pos ;
60+ if (slots_[i].blocked ) {
61+ continue ;
62+ }
63+ hungarian_matrix_[slot_index].resize (num_io_pins_,
64+ std::numeric_limits<int >::max ());
65+ const int io_net_hpwl = netlist_->computeIONetHPWL (idx, slot_pos);
66+ const int mirrored_cost = getMirroredPinCost (io_pin, slot_pos);
67+ const int hpwl = io_net_hpwl + mirrored_cost;
68+ larger_costs.push_back (std::max (io_net_hpwl, mirrored_cost));
69+ hungarian_matrix_[slot_index][pin_index] = hpwl;
70+ is_mirrored = is_mirrored || mirrored_cost != 0 ;
71+ slot_index++;
6672 }
73+
74+ if (is_mirrored) {
75+ std::vector<uint8_t > rank = getTieBreakRank (larger_costs);
76+ for (int idx = 0 ; idx < slot_index; idx++) {
77+ const int hpwl = hungarian_matrix_[idx][pin_index];
78+ if ((hpwl >> 24 ) != 0 ) {
79+ logger_->critical (utl::PPL , 210 , " Cost for pin exceeds 24 bits." );
80+ }
81+ hungarian_matrix_[idx][pin_index] = (hpwl << 8 ) | rank[idx];
82+ }
83+ }
84+ pin_index++;
6785 }
68- slot_index++;
6986 }
7087}
7188
@@ -208,31 +225,47 @@ void HungarianMatching::createMatrixForGroups()
208225 }
209226
210227 hungarian_matrix_.resize (group_slots_);
211- int slot_index = 0 ;
212- for (int i : valid_starting_slots_) {
213- int groupIndex = 0 ;
214- const Point& slot_pos = slots_[i].pos ;
215-
216- hungarian_matrix_[slot_index].resize (num_pin_groups_,
217- std::numeric_limits<int >::max ());
218- for (const auto & [pins, order] : pin_groups_) {
228+ int group_index = 0 ;
229+ for (const auto & [pins, order] : pin_groups_) {
230+ int slot_index = 0 ;
231+ bool is_mirrored = false ;
232+ std::vector<int > larger_costs (valid_starting_slots_.size (), 0 );
233+ for (int i : valid_starting_slots_) {
219234 int group_hpwl = 0 ;
220235 for (const int io_idx : pins) {
236+ const Point& slot_pos = slots_[i].pos ;
237+
238+ hungarian_matrix_[slot_index].resize (num_pin_groups_,
239+ std::numeric_limits<int >::max ());
221240 IOPin& io_pin = netlist_->getIoPin (io_idx);
222241 int pin_hpwl = netlist_->computeIONetHPWL (io_idx, slot_pos);
223242 if (pin_hpwl == hungarian_fail_) {
224243 group_hpwl = hungarian_fail_;
225244 break ;
226245 }
227- group_hpwl += pin_hpwl + getMirroredPinCost (io_pin, slot_pos);
246+ const int mirrored_cost = getMirroredPinCost (io_pin, slot_pos);
247+ group_hpwl += pin_hpwl + mirrored_cost;
248+ larger_costs[slot_index] += std::max (pin_hpwl, mirrored_cost);
249+ is_mirrored = is_mirrored || mirrored_cost != 0 ;
228250 }
229251 if (pins.size () > group_slot_capacity[slot_index]) {
230252 group_hpwl = std::numeric_limits<int >::max ();
231253 }
232- hungarian_matrix_[slot_index][groupIndex] = group_hpwl;
233- groupIndex++;
254+ hungarian_matrix_[slot_index][group_index] = group_hpwl;
255+ slot_index++;
256+ }
257+
258+ if (is_mirrored) {
259+ std::vector<uint8_t > rank = getTieBreakRank (larger_costs);
260+ for (int idx = 0 ; idx < slot_index; idx++) {
261+ const int hpwl = hungarian_matrix_[idx][group_index];
262+ if ((hpwl >> 24 ) != 0 ) {
263+ logger_->critical (utl::PPL , 211 , " Cost for pin exceeds 24 bits." );
264+ }
265+ hungarian_matrix_[idx][group_index] = (hpwl << 8 ) | rank[idx];
266+ }
234267 }
235- slot_index ++;
268+ group_index ++;
236269 }
237270
238271 if (hungarian_matrix_.empty ()) {
@@ -359,4 +392,17 @@ Edge HungarianMatching::getMirroredEdge(const Edge& edge)
359392 return mirrored_edge;
360393}
361394
395+ std::vector<uint8_t > HungarianMatching::getTieBreakRank (
396+ const std::vector<int >& costs)
397+ {
398+ std::vector<uint8_t > rank (costs.size ());
399+ uint8_t ranking = 1 ;
400+ for (int i : sortIndexes (costs, costs)) {
401+ rank[i] = ranking;
402+ ranking++;
403+ }
404+
405+ return rank;
406+ }
407+
362408} // namespace ppl
0 commit comments