aboutsummaryrefslogtreecommitdiffstats
path: root/native/src/blocking/linprog/lp_dpcp.cpp
blob: 50d38c545648e5dfdf5030c6ebbe81d1a8d840e9 (plain) (blame)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#include <iostream>
#include <set>
#include <algorithm>
#include <cmath>

#include "lp_common.h"
#include "blocking.h"
#include "stl-hashmap.h"

#include "cpu_time.h"

#define NO_WAIT_TIME_BOUND (-1)

class MaxWaitTimes
{
private:
	// mapping of resource id to previously computed maximum wait time
	hashmap<unsigned int, long> max_wait_time;

	// parameters for wait time calculation
	const ResourceSharingInfo& info;
	const ResourceLocality& locality;
	const TaskInfo& ti;
	const PriorityCeilings& prio_ceiling;

	void bound_wait_time(unsigned int res_id);

public:
	MaxWaitTimes(const ResourceSharingInfo& _info, const ResourceLocality& _loc,
		     const TaskInfo& _ti, const std::vector<unsigned int>& _pc)
		: info(_info), locality(_loc), ti(_ti), prio_ceiling(_pc)
		{};

	long operator[](unsigned int res_id)
	{
		if (!max_wait_time.count(res_id))
			bound_wait_time(res_id);
		return max_wait_time[res_id];
	}
};

void MaxWaitTimes::bound_wait_time(unsigned int res_id)
{
	unsigned int own_length = 0, delay_by_lower = 0, delay_by_higher = 0;

	unsigned int c = locality[res_id];

	// find ti's maximum request length
	foreach(ti.get_requests(), ti_req)
		if (ti_req->get_resource_id() == res_id)
			own_length = std::max(own_length,
					      ti_req->get_request_length());

	// find maximum lower-priority request length that blocks
	foreach_lowereq_priority_task(info.get_tasks(), ti, tx)
	{
		// on the cluster in which res_id is located
		foreach_request_in_cluster(tx->get_requests(), locality,
					   c, request)
		{
			unsigned int q = request->get_resource_id();
			// can block?
			if (prio_ceiling[q] <= ti.get_priority())
				delay_by_lower = std::max(delay_by_lower,
							  request->get_request_length());
		}
	}

	// response-time analysis to find final maximum wait time

	unsigned long next_estimate = own_length + delay_by_lower;
	unsigned long estimate = 0;

	while (next_estimate <= ti.get_response() && next_estimate != estimate)
	{
		delay_by_higher = 0;
		estimate = next_estimate;

		foreach_higher_priority_task(info.get_tasks(), ti, tx)
		{
			foreach_request_in_cluster(tx->get_requests(), locality,
						   c, request)
			{
				unsigned int nreqs = request->get_max_num_requests(estimate);
				unsigned long rlen = request->get_request_length();

				delay_by_higher += nreqs * rlen;
			}
		}

		next_estimate = own_length + delay_by_lower + delay_by_higher;
	}

	if (estimate <= ti.get_response())
		max_wait_time[res_id] = estimate;
	else
		max_wait_time[res_id] = NO_WAIT_TIME_BOUND;
}

// Constraint 8
// Ti's maximum wait times limit the maximum number of times
// that higher-priority tasks can directly and indirectly delay Ti.
static void add_max_wait_time_constraints(
	VarMapper& vars,
	const ResourceSharingInfo& info,
	const ResourceLocality& locality,
	const TaskInfo& ti,
	const PriorityCeilings& prio_ceilings,
	LinearProgram& lp)
{
	MaxWaitTimes max_wait_time = MaxWaitTimes(info, locality, ti, prio_ceilings);

	foreach_higher_priority_task(info.get_tasks(), ti, tx)
	{
		unsigned int t = tx->get_id();
		foreach(tx->get_requests(), request)
		{
			unsigned int q = request->get_resource_id();
			unsigned int c = locality[q];

			unsigned int max_num_reqs = 0;
			bool bounded = true;

			// Figure out how often and how long ti is waiting in
			// total in cluster c.  To do so, look at each of ti's
			// requests for a resource located in cluster c.

			foreach_request_in_cluster(ti.get_requests(), locality,
						   c, ti_req)
			{
				unsigned int y = ti_req->get_resource_id();
				long wait = max_wait_time[y];

				if (wait == NO_WAIT_TIME_BOUND)
				{
					bounded = false;
					break;
				}

				unsigned int nreqs = request->get_max_num_requests(wait);
				max_num_reqs += nreqs * ti_req->get_num_requests();
			}


			if (bounded)
			{
				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);
					var_id = vars.lookup(t, q, v, BLOCKING_INDIRECT);
					exp->add_var(var_id);
				}
				lp.add_inequality(exp, max_num_reqs);
			}
		}
	}
}

// Substitute for Constraint 8
// no direct or indirect blocking due to resources
// on clusters that ti doesn't even access.
static void add_independent_cluster_constraints(
	VarMapper& vars,
	const ResourceSharingInfo& info,
	const ResourceLocality& locality,
	const TaskInfo& ti,
	LinearProgram& lp)
{

	//find all clusters that ti accesses
	std::set<int> accessed_clusters;
	foreach(ti.get_requests(), req)
	{
		int c = locality[req->get_resource_id()];
		accessed_clusters.insert(c);
	}

	LinearExpression *exp = new LinearExpression();

	foreach_task_except(info.get_tasks(), ti, tx)
	{
		unsigned int t = tx->get_id();
		foreach(tx->get_requests(), request)
		{
			unsigned int q = request->get_resource_id();
			if (accessed_clusters.count(locality[q]) == 0)
			{
				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_equality(exp, 0);
}

// Constraint 6
static void add_conflict_set_constraints(
	VarMapper& vars,
	const ResourceSharingInfo& info,
	const ResourceLocality& locality,
	const TaskInfo& ti,
	const PriorityCeilings& prio_ceiling,
	LinearProgram& lp)
{
	LinearExpression *exp = new LinearExpression();

	foreach_task_except(info.get_tasks(), ti, tx)
	{
		unsigned int t = tx->get_id();
		foreach(tx->get_requests(), request)
		{
			unsigned int q = request->get_resource_id();
			if (prio_ceiling[q] > ti.get_priority())
			{
				// smaller ID <=> higher priority
				// Priority ceiling is lower than ti's priority,
				// so it cannot block ti.
				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);
				}
			}
		}
	}

	// force blocking fractions to zero
	lp.add_equality(exp, 0);
}

// Constraint 7
// Each request can be directly delayed at most
// once by a lower-priority task.
static void add_atmostonce_lower_prio_constraints(
	VarMapper& vars,
	const ResourceSharingInfo& info,
	const ResourceLocality& locality,
	const TaskInfo& ti,
	const PriorityCeilings& priority_ceiling,
	LinearProgram& lp)
{
	// Count the number of times that each cluster is accessed by ti.
	hashmap<unsigned int, unsigned int> per_cluster_counts;
	foreach(ti.get_requests(), req)
		per_cluster_counts[locality[req->get_resource_id()]] += req->get_num_requests();

	// build one constraint for each cluster
	hashmap<unsigned int, LinearExpression *> constraints;

	foreach_lowereq_priority_task_except(info.get_tasks(), ti, tx)
	{
		unsigned int t = tx->get_id();

		foreach(tx->get_requests(), request)
		{
			unsigned int q = request->get_resource_id();

			if (priority_ceiling[q] <= ti.get_priority())
			{
				// yes, can block us
				unsigned int c = locality[q];
				LinearExpression *exp;

				if (constraints.find(c) == constraints.end())
					constraints[c] = new LinearExpression();

				exp = constraints[c];

				foreach_request_instance(*request, ti, v)
				{
					unsigned int var_id;
					var_id = vars.lookup(t, q, v, BLOCKING_DIRECT);
					exp->add_var(var_id);
				}
			}
		}
	}

	// add each per-cluster constraint
	foreach(constraints, it)
		lp.add_inequality(it->second, per_cluster_counts[it->first]);
}

void add_dpcp_constraints(
	VarMapper& vars,
	const ResourceSharingInfo& info,
	const ResourceLocality& locality,
	const TaskInfo& ti,
	const PriorityCeilings& prio_ceilings,
	LinearProgram& lp,
	bool use_rta)
{
	// Constraint 1
	add_mutex_constraints(vars, info, ti, lp);
	// Constraint 2
	add_topology_constraints(vars, info, locality, ti, lp);
	// Constraint 3
	add_local_lower_priority_constraints(vars, info, locality, ti, lp);
	// Constraint 7
	add_atmostonce_lower_prio_constraints(vars, info, locality,
					      ti, prio_ceilings, lp);
	// Constraint 6
	add_conflict_set_constraints(vars, info, locality, ti,
				     prio_ceilings, lp);

	if (use_rta)
		// Constraint 8
		add_max_wait_time_constraints(vars, info, locality, ti, prio_ceilings,
					      lp);
	else
		add_independent_cluster_constraints(vars, info, locality, ti, lp);
}

#ifdef CONFIG_MERGED_LINPROGS

static BlockingBounds* _lp_dpcp_bounds(const ResourceSharingInfo& info,
				       const ResourceLocality& locality,
				       bool use_rta)
{
	BlockingBounds *results = new BlockingBounds(info);
	const unsigned int num_tasks = info.get_tasks().size();
	LinearExpression *local_obj = new LinearExpression[num_tasks];
	LinearExpression *remote_obj = new LinearExpression[num_tasks];

	PriorityCeilings prio_ceilings = get_priority_ceilings(info);

	LinearProgram lp;
	unsigned int var_idx = 0;

#if DEBUG_LP_OVERHEADS >= 2
	static DEFINE_CPU_CLOCK(model_gen_cost);
	static DEFINE_CPU_CLOCK(solver_cost);
	static DEFINE_CPU_CLOCK(extract_cost);
	static DEFINE_CPU_CLOCK(total_cost);

	std::cout << "---- " << __FUNCTION__ << " ----" << std::endl;

	model_gen_cost.start();
	total_cost.start();
#endif

	// Generate a "merged" LP.
	for (unsigned int i = 0; i < num_tasks; i++)
	{
		const TaskInfo &ti = info.get_tasks()[i];
		VarMapper vars = VarMapper(var_idx);

		set_blocking_objective(vars, info, locality, ti, lp,
				       local_obj + i, remote_obj + i);
		add_dpcp_constraints(vars, info, locality, ti,
				     prio_ceilings, lp, use_rta);

		var_idx = vars.get_next_var();
	}

#if DEBUG_LP_OVERHEADS >= 2
	model_gen_cost.stop();
	solver_cost.start();
#endif

	// Solve the big, combined LP.
	Solution *sol = linprog_solve(lp, var_idx);

	assert(sol != NULL);

#if DEBUG_LP_OVERHEADS >= 2
	solver_cost.stop();
	extract_cost.start();
#endif

	// Extract each task's solution.
	for (unsigned int i = 0; i < num_tasks; i++)
	{
		Interference total, remote, local;

		local.total_length = lrint(sol->evaluate(local_obj[i]));
		remote.total_length = lrint(sol->evaluate(remote_obj[i]));
		total.total_length = local.total_length + remote.total_length;

		(*results)[i] = total;
		results->set_remote_blocking(i, remote);
		results->set_local_blocking(i, local);
	}

#if DEBUG_LP_OVERHEADS >= 2
	extract_cost.stop();
	total_cost.stop();
	std::cout << model_gen_cost << std::endl;
	std::cout << solver_cost << std::endl;
	std::cout << extract_cost << std::endl;
	std::cout << total_cost << std::endl;
#endif

	delete sol;
	delete[] local_obj;
	delete[] remote_obj;

	return results;
}

#else // per-task LPs

static void apply_dpcp_bounds_for_task(
	unsigned int i,
	BlockingBounds& bounds,
	const ResourceSharingInfo& info,
	const ResourceLocality& locality,
	const PriorityCeilings& prio_ceilings,
	bool use_rta)
{
	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(vars, info, locality, ti, lp, local_obj);

	add_dpcp_constraints(vars, info, locality, ti,
			     prio_ceilings, lp, use_rta);

#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_dpcp_bounds(const ResourceSharingInfo& info,
				       const ResourceLocality& locality,
				       bool use_rta)
{
	BlockingBounds* results = new BlockingBounds(info);

	PriorityCeilings prio_ceilings = get_priority_ceilings(info);

	for (unsigned int i = 0; i < info.get_tasks().size(); i++)
		apply_dpcp_bounds_for_task(i, *results, info,
					   locality, prio_ceilings, use_rta);

	return results;
}

#endif

BlockingBounds* lp_dpcp_bounds(const ResourceSharingInfo& info,
			       const ResourceLocality& locality,
			       bool use_rta)
{
#if DEBUG_LP_OVERHEADS >= 1
	static DEFINE_CPU_CLOCK(cpu_costs);

	cpu_costs.start();
#endif

	BlockingBounds *results = _lp_dpcp_bounds(info, locality, use_rta);

#if DEBUG_LP_OVERHEADS >= 1
	cpu_costs.stop();
	std::cout << cpu_costs << std::endl;
#endif

	return results;
}