-
Notifications
You must be signed in to change notification settings - Fork 2
/
bbsearch.c
166 lines (156 loc) · 4.46 KB
/
bbsearch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <assert.h>
#include <limits.h>
#include <time.h>
#include <stdio.h>
#include "binheap.h"
#include "bitmap.h"
#include "dag.h"
#include "schedule.h"
#include "bbsearch.h"
static int do_timeout;
static clock_t end_time;
int fujita_bound(schedule *s) {
dag *g = schedule_dag(s);
unsigned delta = 1;
while (1) {
/* printf("loop1\n"); */
schedule_build(s, dag_level(g, dag_source(g)) + delta);
int min_m = schedule_machine_bound(s);
/* printf("delta: %d, min_m: %d, m: %d\n", delta, min_m, schedule_m(s)); */
if (min_m <= schedule_m(s)) {
break;
}
delta = delta * 2;
assert(delta != 0);
}
int low_time = dag_level(g, dag_source(g)) + delta / 2;
int high_time = dag_level(g, dag_source(g)) + delta;
int best_time = high_time;
while (1) {
int cur_time = (high_time - low_time) / 2 + low_time;
if (cur_time == low_time) {
break;
}
schedule_build(s, cur_time);
int min_m = schedule_machine_bound(s);
if (min_m <= schedule_m(s)) {
high_time = cur_time;
best_time = (best_time < cur_time) ? best_time : cur_time;
}
else {
low_time = cur_time;
}
}
return best_time;
}
int bb(schedule *s, bitmap *ready_set, unsigned best_soln) {
assert(s != NULL);
if (do_timeout && clock() >= end_time) {
return -2;
}
dag *g = schedule_dag(s);
if (schedule_build(s, 0) != 0) {
return -1;
}
if (schedule_size(s) == dag_size(g)) {
unsigned sched_len = schedule_length(s);
return (best_soln < sched_len) ? best_soln : sched_len;
}
#ifdef FUJITA
#ifdef FB
unsigned fb = schedule_fernandez_bound(s);
if (fb >= best_soln) {
return best_soln;
}
#else // no FB
unsigned mb = fujita_bound(s);
if (mb >= best_soln) {
return best_soln;
}
#endif // FB
#endif // FUJITA
binheap *sorter = binheap_create();
if (sorter == NULL) {
return -1;
}
for (size_t i = 0; i < dag_size(g); i++) {
if (bitmap_get(ready_set, i) == 1) {
if (binheap_put(sorter, i, dag_level(g, i)) != 0) {
binheap_destroy(sorter);
return -1;
}
}
}
idx_vec new_ready;
idx_vec_init(&new_ready, 0);
while (binheap_size(sorter) > 0) {
unsigned new_idx = binheap_get(sorter);
schedule_add(s, new_idx);
size_t nsuccs = dag_nsuccs(g, new_idx);
unsigned succs[nsuccs];
dag_succs(g, new_idx, succs);
for (size_t i = 0; i < nsuccs; i++) {
size_t npreds = dag_npreds(g, succs[i]);
unsigned preds[npreds];
dag_preds(g, succs[i], preds);
int all_scheduled = 1;
for (size_t i = 0; i < npreds; i++) {
if (!schedule_contains(s, preds[i])) {
all_scheduled = 0;
break;
}
}
if (all_scheduled) {
idx_vec_push(&new_ready, succs[i]);
bitmap_set(ready_set, succs[i], 1);
}
}
bitmap_set(ready_set, new_idx, 0);
int soln = bb(s, ready_set, best_soln);
bitmap_set(ready_set, new_idx, 1);
if (soln < 0) {
binheap_destroy(sorter);
return soln;
}
best_soln = (best_soln < soln) ? best_soln : soln;
while (new_ready.size > 0) {
unsigned r;
idx_vec_pop(&new_ready, &r);
bitmap_set(ready_set, r, 0);
}
schedule_pop(s);
}
idx_vec_destroy(&new_ready);
binheap_destroy(sorter);
return best_soln;
}
int bbsearch(dag *g, unsigned m, int timeout) {
assert(g != NULL);
schedule *s = schedule_create(g, m);
if (s == NULL) {
return -1;
}
schedule_add(s, dag_source(g));
bitmap *ready_set = bitmap_create(0);
if (ready_set == NULL) {
schedule_destroy(s);
return -1;
}
if (timeout < 0) {
do_timeout = 0;
}
else {
do_timeout = 1;
end_time = clock() + timeout * CLOCKS_PER_SEC;
}
size_t nsuccs = dag_nsuccs(g, dag_source(g));
unsigned succs[nsuccs];
dag_succs(g, dag_source(g), succs);
for (size_t i = 0; i < nsuccs; i++) {
bitmap_set(ready_set, succs[i], 1);
}
int result = bb(s, ready_set, UINT_MAX);
bitmap_destroy(ready_set);
schedule_destroy(s);
return result;
}