@@ -16,6 +16,11 @@ void Bucket::init(const DeviceGrid& grid) {
16
16
17
17
heap_head_ = std::numeric_limits<size_t >::max ();
18
18
heap_tail_ = 0 ;
19
+
20
+ conv_factor_ = kDefaultConvFactor ;
21
+
22
+ min_cost_ = std::numeric_limits<float >::max ();
23
+ max_cost_ = std::numeric_limits<float >::min ();
19
24
}
20
25
21
26
void Bucket::free () {
@@ -46,6 +51,9 @@ t_heap** Bucket::heap_ = nullptr;
46
51
size_t Bucket::heap_size_ = 0 ;
47
52
size_t Bucket::heap_head_ = std::numeric_limits<size_t >::max();
48
53
size_t Bucket::heap_tail_ = 0 ;
54
+ float Bucket::min_cost_ = 0 .f;
55
+ float Bucket::max_cost_ = 0 .f;
56
+ float Bucket::conv_factor_ = 0 .f;
49
57
50
58
void Bucket::clear () {
51
59
if (heap_head_ != std::numeric_limits<size_t >::max ()) {
@@ -55,19 +63,74 @@ void Bucket::clear() {
55
63
heap_tail_ = 0 ;
56
64
}
57
65
66
+ void Bucket::check_scaling () {
67
+ float min_cost = min_cost_;
68
+ float max_cost = max_cost_;
69
+ VTR_ASSERT (max_cost != std::numeric_limits<float >::min ());
70
+ if (min_cost == std::numeric_limits<float >::max ()) {
71
+ min_cost = max_cost;
72
+ }
73
+ auto min_bucket = cost_to_int (min_cost);
74
+ auto max_bucket = cost_to_int (max_cost);
75
+
76
+ if (min_bucket < 0 || max_bucket < 0 || max_bucket > 1000000 ) {
77
+ // If min and max are close to each other, assume 3 orders of
78
+ // magnitude between min and max.
79
+ //
80
+ // If min and max are at least 3 orders of magnitude apart, scale
81
+ // soley based on max cost.
82
+ conv_factor_ = 50000 .f / max_cost_ / std::max (1 .f , 1000 .f / (max_cost_ / min_cost_));
83
+
84
+ VTR_ASSERT (cost_to_int (min_cost_) >= 0 );
85
+ VTR_ASSERT (cost_to_int (max_cost_) >= 0 );
86
+ VTR_ASSERT (cost_to_int (max_cost_) < 1000000 );
87
+
88
+ // Reheap after adjusting scaling.
89
+ if (heap_head_ != std::numeric_limits<size_t >::max ()) {
90
+ std::vector<t_heap*> reheap;
91
+ for (size_t bucket = heap_head_; bucket <= heap_tail_; ++bucket) {
92
+ for (t_heap* item = heap_[bucket]; item != nullptr ; item = item->next_bucket ) {
93
+ reheap.push_back (item);
94
+ }
95
+ }
96
+
97
+ std::fill (heap_ + heap_head_, heap_ + heap_tail_ + 1 , nullptr );
98
+ heap_head_ = std::numeric_limits<size_t >::max ();
99
+ heap_tail_ = 0 ;
100
+
101
+ for (t_heap* item : reheap) {
102
+ push (item);
103
+ }
104
+ }
105
+ }
106
+ }
107
+
58
108
void Bucket::push (t_heap* hptr) {
59
109
float cost = hptr->cost ;
60
110
if (!std::isfinite (cost)) {
61
111
return ;
62
112
}
63
113
64
- // heap_::verify_extract_top();
114
+ bool check_scale = false ;
115
+ // Exclude 0 cost from min_cost to provide useful scaling factor.
116
+ if (cost < min_cost_ && cost > 0 ) {
117
+ min_cost_ = cost;
118
+ check_scale = true ;
119
+ }
120
+ if (cost > max_cost_) {
121
+ max_cost_ = cost;
122
+ check_scale = true ;
123
+ }
124
+
125
+ if (check_scale) {
126
+ check_scaling ();
127
+ }
65
128
66
129
// Which bucket should this go into?
67
130
auto int_cost = cost_to_int (cost);
68
131
69
132
if (int_cost < 0 ) {
70
- VTR_LOG_WARN (" Cost is negative? cost = %g\n " , cost);
133
+ VTR_LOG_WARN (" Cost is negative? cost = %g, bucket = %d \n " , cost, int_cost );
71
134
int_cost = 0 ;
72
135
}
73
136
@@ -90,8 +153,6 @@ void Bucket::push(t_heap* hptr) {
90
153
if (uint_cost > heap_tail_) {
91
154
heap_tail_ = uint_cost;
92
155
}
93
-
94
- // heap_::verify_extract_top();
95
156
}
96
157
97
158
t_heap* Bucket::pop () {
0 commit comments