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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
#include <iostream>
#include <set>
#include <algorithm>
#include <cmath>
#include "lp_common.h"
#include "blocking.h"
#include "stl-hashmap.h"
#include "cpu_time.h"
typedef hashmap<unsigned int, unsigned int> BlockingLimits;
// Constraint 14
static void add_fifo_cluster_constraints(
VarMapper& vars,
const ResourceSharingInfo& info,
const TaskInfo& ti,
LinearProgram& lp)
{
foreach_remote_task(info.get_tasks(), ti, tx)
{
unsigned int t = tx->get_id();
// compute direct blocking opportunities, not counting Tx
BlockingLimits per_resource_remote;
foreach(ti.get_requests(), req)
per_resource_remote[req->get_resource_id()] = 0;
foreach_local_task_except(info.get_tasks(), *tx, ty)
{
foreach(ty->get_requests(), req)
{
unsigned int u = req->get_resource_id();
if (per_resource_remote.find(u) !=
per_resource_remote.end())
per_resource_remote[u] += req->get_max_num_requests(ti.get_response());
}
}
unsigned int total_limit = 0;
foreach(ti.get_requests(), req)
{
unsigned int u = req->get_resource_id();
total_limit += std::min(req->get_num_requests(),
per_resource_remote[u]);
}
LinearExpression *exp = new LinearExpression();
foreach(tx->get_requests(), request)
{
unsigned int q = request->get_resource_id();
foreach_request_instance(*request, ti, v)
{
unsigned int var_id;
var_id = vars.lookup(t, q, v, BLOCKING_INDIRECT);
exp->add_var(var_id);
}
}
lp.add_inequality(exp, total_limit);
}
}
// Constraint 13
static void add_total_fifo_constraints(
VarMapper& vars,
const ResourceSharingInfo& info,
const TaskInfo& ti,
LinearProgram& lp,
BlockingLimits &per_cluster_counts)
{
unsigned int total_num_requests = 0;
foreach(ti.get_requests(), req)
total_num_requests += req->get_num_requests();
foreach_task_except(info.get_tasks(), ti, tx)
{
unsigned int t = tx->get_id();
LinearExpression *exp = new LinearExpression();
//for all requests accessed by tx
foreach(tx->get_requests(), request)
{
unsigned int q = request->get_resource_id();
foreach_request_instance(*request, ti, v)
{
unsigned int var_id;
var_id = vars.lookup(t, q, v, BLOCKING_DIRECT);
exp->add_var(var_id);
var_id = vars.lookup(t, q, v, BLOCKING_INDIRECT);
exp->add_var(var_id);
}
}
lp.add_inequality(exp, per_cluster_counts[tx->get_cluster()]);
}
}
// Constraint 12
static void add_fifo_resource_constraints(
VarMapper& vars,
const ResourceSharingInfo& info,
const TaskInfo& ti,
LinearProgram& lp)
{
BlockingLimits per_resource_counts;
foreach(ti.get_requests(), req)
per_resource_counts[req->get_resource_id()] = req->get_num_requests();
foreach_task_except(info.get_tasks(), ti, tx)
{
unsigned int t = tx->get_id();
//for all requests accessed by tx
foreach(tx->get_requests(), request)
{
unsigned int q = request->get_resource_id();
LinearExpression *exp = new LinearExpression();
foreach_request_instance(*request, ti, v)
{
unsigned int var_id;
var_id = vars.lookup(t, q, v, BLOCKING_DIRECT);
exp->add_var(var_id);
}
lp.add_inequality(exp, per_resource_counts[q]);
}
}
}
static BlockingLimits count_blocking_opportunities(
const ResourceSharingInfo &info,
const TaskInfo& ti)
{
BlockingLimits per_cluster_counts;
foreach_task_except(info.get_tasks(), ti, tx)
{
unsigned int c = tx->get_cluster();
if (per_cluster_counts.find(c) == per_cluster_counts.end())
{
// compute direct blocking opportunities on Tx's cluster
BlockingLimits per_resource_remote;
foreach(ti.get_requests(), req)
per_resource_remote[req->get_resource_id()] = 0;
foreach_local_task(info.get_tasks(), *tx, ty)
{
foreach(ty->get_requests(), req)
{
unsigned int u = req->get_resource_id();
if (per_resource_remote.find(u) !=
per_resource_remote.end())
per_resource_remote[u] += req->get_max_num_requests(ti.get_response());
}
}
per_cluster_counts[c] = 0;
foreach(ti.get_requests(), req)
{
unsigned int u = req->get_resource_id();
per_cluster_counts[c] += std::min(req->get_num_requests(),
per_resource_remote[u]);
}
}
}
return per_cluster_counts;
}
static void add_fmlp_constraints(
VarMapper& vars,
const ResourceSharingInfo& info,
const TaskInfo& ti,
LinearProgram& lp)
{
// Constraint 1
add_mutex_constraints(vars, info, ti, lp);
// Constraint 9
add_local_higher_priority_constraints_shm(vars, info, ti, lp);
// Constraint 10
add_topology_constraints_shm(vars, info, ti, lp);
// Constraint 11
add_local_lower_priority_constraints_shm(vars, info, ti, lp);
BlockingLimits per_cluster_counts;
per_cluster_counts = count_blocking_opportunities(info, ti);
// Constraint 12
add_fifo_resource_constraints(vars, info, ti, lp);
// Constraint 13
add_total_fifo_constraints(vars, info, ti, lp, per_cluster_counts);
// Constraint 14
add_fifo_cluster_constraints(vars, info, ti, lp);
}
static void apply_fmlp_bounds_for_task(
unsigned int i,
BlockingBounds& bounds,
const ResourceSharingInfo& info)
{
LinearProgram lp;
VarMapper vars;
const TaskInfo& ti = info.get_tasks()[i];
LinearExpression *local_obj = new LinearExpression();
#if DEBUG_LP_OVERHEADS >= 2
static DEFINE_CPU_CLOCK(model_gen_cost);
static DEFINE_CPU_CLOCK(solver_cost);
std::cout << "---- " << __FUNCTION__ << " ----" << std::endl;
model_gen_cost.start();
#endif
set_blocking_objective_part_shm(vars, info, ti, lp, local_obj);
vars.seal();
add_fmlp_constraints(vars, info, ti, lp);
#if DEBUG_LP_OVERHEADS >= 2
model_gen_cost.stop();
std::cout << model_gen_cost << std::endl;
solver_cost.start();
#endif
Solution *sol = linprog_solve(lp, vars.get_num_vars());
#if DEBUG_LP_OVERHEADS >= 2
solver_cost.stop();
std::cout << solver_cost << std::endl;
#endif
assert(sol != NULL);
Interference total, remote, local;
total.total_length = lrint(sol->evaluate(*lp.get_objective()));
local.total_length = lrint(sol->evaluate(*local_obj));
remote.total_length = total.total_length - local.total_length;
bounds[i] = total;
bounds.set_remote_blocking(i, remote);
bounds.set_local_blocking(i, local);
delete local_obj;
delete sol;
}
static BlockingBounds* _lp_fmlp_bounds(const ResourceSharingInfo& info)
{
BlockingBounds* results = new BlockingBounds(info);
for (unsigned int i = 0; i < info.get_tasks().size(); i++)
apply_fmlp_bounds_for_task(i, *results, info);
return results;
}
BlockingBounds* lp_part_fmlp_bounds(const ResourceSharingInfo& info)
{
#if DEBUG_LP_OVERHEADS >= 1
static DEFINE_CPU_CLOCK(cpu_costs);
cpu_costs.start();
#endif
BlockingBounds *results = _lp_fmlp_bounds(info);
#if DEBUG_LP_OVERHEADS >= 1
cpu_costs.stop();
std::cout << cpu_costs << std::endl;
#endif
return results;
}
|