-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel.py
507 lines (396 loc) · 16.3 KB
/
kernel.py
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
"""Solve the kernel.
The kernel is an optimization problem:
minimize t
s.t. t ∈ [a, b] ∩ ℤ
sum(ceil((t + alpha) / tsk.period) * tsk.wcet
for tsk, alpha in zip(tsks, alphas)) + beta <= t
a, b, alphas, beta are integral constants, and tsks is a constant list of tasks
with total utilization at most 1. Both FP and EDF schedulability reduce to the
kernel in polynomial time.
The kernel can be solved by any of the following three methods:
• fixed-point iteration
• integer programming
• a specialized cutting-plane algorithm
All three methods are implemented in this module.
More details about the kernel may be found in https://arxiv.org/abs/2210.11185.
"""
import ctypes
import math
import os
from fractions import Fraction
from typing import List, Optional
from docplex.mp.model import Model
from rtsched.system.task import Task
def fixed_point(tsks: List[Task],
alphas: List[int],
beta: int,
a: int,
b: int,
perf=None) -> Optional[int]:
"""Solve a kernel instance using fixed-point iteration.
Args:
tsks: list of tasks
alphas: list of integers of same length as tsks
beta: an integer
a, b: integer endpoints of interval [a, b]. tsks, alphas, beta, a, b
are the constants used to specify the kernel instance.
perf: if perf is not None, then the number of iterations used by the
method is added to perf.num_iterations
Returns:
the optimal objective value if the instance is feasible; None, otherwise.
>>> fixed_point(tsks = [Task(20, 40), Task(10, 50), Task(33, 150)], alphas = [0, 0, 0], beta= 0, a = 20, b = 150)
143
"""
def phi(t):
return sum([
math.ceil(Fraction(t + alpha, tsk.period)) * tsk.wcet
for tsk, alpha in zip(tsks, alphas)
]) + beta
# check trivial cases
if not tsks and a <= b:
return a
if a > b:
return None
if phi(a) <= a:
return a
# fixed point iteration
t_ = a
while t_ <= b:
v = phi(t_)
if perf is not None:
perf.num_iterations += 1
if v == t_: # fixed point is found
return t_
t_ = v
# problem is infeasible
return None
def integer_program(tsks: List[Task],
alphas: List[int],
beta: int,
a: int,
b: int,
perf=None) -> Optional[int]:
"""Solve a kernel instance using integer programming.
Args:
tsks: list of tasks
alphas: list of integers of same length as tsks
beta: an integer
a, b: integer endpoints of interval [a, b]
tsks, alphas, beta, a, b are the constants used to specify the
kernel instance.
perf: if perf is not None, then the number of iterations used by the
method is added to perf.num_iterations
Returns: the optimal objective value if the instance is feasible; None,
otherwise.
Note that cplex uses double-precision (64-bit) arithmetic in its
computations but the arguments provided to the function are python integers
that are potentially unbounded.The integers in cplex are not perfect but
are subject to an integrality tolerance; the constraints in cplex are also
imperfect and subject to a feasibility tolerance. We tune these tolerances
using the numbers in the problem instance but for instances with very large
numbers the cutting-plane implementation is more suitable.
Remember to add the path to your cplex installation to the environment
variable PYTHONPATH, and to use a python version that is compatible with
the installed cplex. I use python 3.8.
Some tips for building cplex models efficiently are provided here:
https://github.com/IBMDecisionOptimization/docplex-examples/blob/master/examples/mp/jupyter/efficient.ipynb
>>> integer_program(tsks=[Task(20, 40), Task(10, 50), Task(33, 150)], alphas=[0, 0, 0], beta=0, a=20, b=150)
143
>>> integer_program(tsks=[Task(6, 17, 10), Task(5, 13, 10)], alphas=[-7, -3], beta=1, a=-12, b=-10)
-10
TODO: Experiment with cut callbacks as described here:
https://github.com/IBMDecisionOptimization/docplex-examples/blob/master/examples/mp/callbacks/cut_callback.py
"""
if not tsks and a <= b:
return a
if a > b:
return None
# create model
# change log_output to True to see cplex output on stdout
mdl = Model(ignore_names=True, log_output=False, checker='off')
# create variables
t = mdl.integer_var(lb=a, ub=b)
n = len(tsks)
xs = mdl.integer_var_list(n, lb=-mdl.infinity, ub=mdl.infinity)
# add constraints
wcets = [tsk.wcet for tsk in tsks]
periods = [tsk.period for tsk in tsks]
mdl.add_constraint(t - mdl.scal_prod(terms=xs, coefs=wcets) >= beta)
mdl.add_constraints(periods[i] * xs[i] - t >= alphas[i] for i in range(n))
# set objective
mdl.minimize(t)
# specify cplex tolerances for integrality and feasibility
tol = min(1.0 / (max(periods) + 3.0), 1.0 / (sum(wcets) + 3.0))
tol = min(tol, 0.1)
mdl.parameters.mip.tolerances.integrality = tol
mdl.parameters.simplex.tolerances.feasibility = tol
# turn off bound strengthening during presolve.if bound strengthening is
# on, the problem often gets solved during presolve but we wish to measure
# the number of iterations used by the branch& cut algorithm.if you are not
# interested in this measurement, you may want to toggle this switch and
# see if it reduces solve times.
mdl.parameters.preprocessing.boundstrength = 0
#solve integer program
s = mdl.solve()
if perf is not None:
perf.num_iterations += mdl.solve_details.nb_iterations
if s is None:
return None
return round(s.objective_value)
def cutting_plane(tsks: List[Task],
alphas: List[int],
beta: int,
a: int,
b: int,
perf=None) -> Optional[int]:
"""Solve a kernel instance using a specialized cutting-plane algorithm.
Args:
tsks: list of tasks
alphas: list of integers of same length as tsks
beta: an integer
a, b: integer endpoints of interval [a, b]
tsks, alphas, beta, a, b are the constants used to specify the
kernel instance.
perf: if perf is not None, then the number of iterations used by the
method is added to perf.num_iterations
Returns:
the optimal objective value if the instance is feasible; None, otherwise.
Unlike integer_program(...), this function does not have double-precision
arithmetic issues because we use python integers and fractions.
>>> cutting_plane(tsks = [Task(20, 40), Task(10, 50), Task(33, 150)], alphas = [0, 0, 0], beta= 0, a = 20, b = 150)
143
cutting_plane(tsks=[Task(25, 1181, 377, 8), Task(83, 1261, 893, 10), Task(6, 44, 15, 6), Task(4, 9, 13, 9)], alphas= [-812, -378, -35, -5], beta=1, a=-6000, b=-4)
-13
>>> print(cutting_plane([Task(15, 112, 26, 3)], [-89], 1, -212, -23))
None
>>> print(cutting_plane([Task(2, 4)], [0], 3, 3, 6))
None
"""
if a > b:
return None
n = len(tsks)
if n == 0:
return a
p, q, r, xs, ys, pi = beta, 1, beta, [], [], []
for i in range(n):
period = tsks[i].period
wcet = tsks[i].wcet
util = tsks[i].utilization
alpha = alphas[i]
xs.append(math.ceil(Fraction(a + alpha, period)))
ys.append(period * xs[-1] - alpha)
pi.append(i)
p, q, r = p + util * alpha, q - util, r + wcet * xs[-1]
assert q >= 0, "utilization greater than one is not allowed"
if p > 0 and q == 0:
return None
if r <= a:
return a
i0 = 1 if q == 0 else 0
pi.sort(key=ys.__getitem__, reverse=True)
while True:
p, q, i = r, 1, n - 1
t = r
while i >= i0:
k = pi[i]
if p <= q * ys[k]:
t = math.ceil(Fraction(p, q))
break
wcet = tsks[k].wcet
util = tsks[k].utilization
alpha = alphas[k]
p, q, i = p - wcet * xs[k] + util * alpha, q - util, i - 1
if i == i0 - 1:
t = Fraction(p, q)
if perf is not None:
perf.num_iterations += 1
# instance is infeasible because relaxation is infeasible
if t > b:
return None
# solution is integral and feasible, and hence optimal
if i == n - 1:
return math.ceil(t)
for j in range(i + 1, n):
k = pi[j]
period = tsks[k].period
wcet = tsks[k].wcet
alpha = alphas[k]
d = math.ceil(Fraction(t + alpha, period)) - xs[k]
xs[k], ys[k], r = xs[k] + d, ys[k] + period * d, r + wcet * d
i += 1
l1 = pi[:i].copy()
l2 = sorted(pi[i:n], key=ys.__getitem__, reverse=True)
i1, i2, k = 0, 0, 0
while i1 < i and i2 < n - i:
if ys[l1[i1]] >= ys[l2[i2]]:
pi[k] = l1[i1]
i1 += 1
else:
pi[k] = l2[i2]
i2 += 1
k += 1
if i1 == i:
l1 = l2
i1 = i2
i = n - i
while i1 < i:
pi[k] = l1[i1]
i1 += 1
k += 1
def fixed_point_cpp(tsks: List[Task],
alphas: List[int],
beta: int,
a: int,
b: int,
perf=None) -> Optional[int]:
"""Solve a kernel instance using the c++ implementation of fixed-point
iteration; the only purpose of this function is to measure CPU running time
of the algorithm.
Args:
tsks: list of tasks
alphas: list of integers of same length as tsks
beta: an integer
a, b: integer endpoints of interval [a, b]. tsks, alphas, beta, a, b
are the constants used to specify the kernel instance.
perf: if perf is not None, then the number of iterations used by the
method is added to perf.num_iterations
Returns:
the optimal objective value if the instance is feasible; None, otherwise.
There may be overflow issues when converting from python integers to C++
integers! The C++ implementation itself may contain integer overflow
errors! DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING!
>>> fixed_point_cpp(tsks = [Task(20, 40), Task(10, 50), Task(33, 150)], alphas = [0, 0, 0], beta= 0, a = 20, b = 150)
143
"""
dir = os.path.dirname(__file__)
lib = ctypes.CDLL(os.path.join(dir, "build/libkernel.dylib"))
lib.fixed_point.argtypes = [
ctypes.POINTER(ctypes.c_uint64),
ctypes.POINTER(ctypes.c_uint64),
ctypes.POINTER(ctypes.c_int64), ctypes.c_uint64, ctypes.c_int64,
ctypes.c_int64, ctypes.c_int64,
ctypes.POINTER(ctypes.c_bool),
ctypes.POINTER(ctypes.c_double)
]
lib.fixed_point.restype = ctypes.c_int64
wcets = [tsk.wcet for tsk in tsks]
periods = [tsk.period for tsk in tsks]
n = len(tsks)
cs = (ctypes.c_uint64 * n)(*wcets)
ps = (ctypes.c_uint64 * n)(*periods)
als = (ctypes.c_int64 * n)(*alphas)
feasible = ctypes.c_bool(True)
time = ctypes.c_double(0)
res = lib.fixed_point(cs, ps, als, ctypes.c_uint64(n),
ctypes.c_int64(beta), ctypes.c_int64(a),
ctypes.c_int64(b), ctypes.byref(feasible),
ctypes.byref(time))
if perf is not None:
perf.time += time.value
if feasible:
return res
return None
def cutting_plane_cpp(tsks: List[Task],
alphas: List[int],
beta: int,
a: int,
b: int,
perf=None) -> Optional[int]:
"""Solve a kernel instance using the c++ implementation of the specialized
cutting-plane algorithm; the only purpose of this function is to measure
CPU running time of the algorithm.
Args:
tsks: list of tasks (the length of the list must not exceed 100, and
the utilization must be strictly less than one)
alphas: list of integers of same length as tsks
beta: an integer
a, b: integer endpoints of interval [a, b]. tsks, alphas, beta, a, b
are the constants used to specify the kernel instance.
perf: if perf is not None, then the time used by the method is added to
perf.time
Returns:
the optimal objective value if the instance is feasible; None, otherwise.
There may be overflow issues when converting from python integers to C++
integers! The C++ implementation itself may contain integer overflow and
double-precision errors! The function only works for bounded-utilization
systems! DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING!
>>> cutting_plane_cpp(tsks = [Task(20, 40), Task(10, 50), Task(33, 150)], alphas = [0, 0, 0], beta= 0, a = 20, b = 150)
143
>>> cutting_plane_cpp(tsks=[Task(25, 1181, 377, 8), Task(83, 1261, 893, 10), Task(6, 44, 15, 6), Task(4, 9, 13, 9)], alphas= [-812, -378, -35, -5], beta=1, a=-6000, b=-4)
-13
>>> print(cutting_plane_cpp([Task(15, 112, 26, 3)], [-89], 1, -212, -23))
None
>>> print(cutting_plane_cpp([Task(2, 4)], [0], 3, 3, 6))
None
"""
dir = os.path.dirname(__file__)
lib = ctypes.CDLL(os.path.join(dir, "build/libkernel.dylib"))
lib.cutting_plane.argtypes = [
ctypes.POINTER(ctypes.c_uint64),
ctypes.POINTER(ctypes.c_uint64),
ctypes.POINTER(ctypes.c_double),
ctypes.POINTER(ctypes.c_int64), ctypes.c_uint64, ctypes.c_int64,
ctypes.c_int64, ctypes.c_int64,
ctypes.POINTER(ctypes.c_bool),
ctypes.POINTER(ctypes.c_double)
]
lib.cutting_plane.restype = ctypes.c_int64
wcets = [tsk.wcet for tsk in tsks]
periods = [tsk.period for tsk in tsks]
utils = [tsk.utilization for tsk in tsks]
assert sum(utils) < 1
utils = [float(u) for u in utils]
n = len(tsks)
cs = (ctypes.c_uint64 * n)(*wcets)
ps = (ctypes.c_uint64 * n)(*periods)
us = (ctypes.c_double * n)(*utils)
als = (ctypes.c_int64 * n)(*alphas)
feasible = ctypes.c_bool(True)
time = ctypes.c_double(0)
res = lib.cutting_plane(cs, ps, us, als, ctypes.c_uint64(n),
ctypes.c_int64(beta), ctypes.c_int64(a),
ctypes.c_int64(b), ctypes.byref(feasible),
ctypes.byref(time))
if perf is not None:
perf.time += time.value
if feasible:
return res
return None
METHODS = {
'fp': fixed_point, # fixed-point iteration
'ip': integer_program, # integer programming
'cp': cutting_plane, # specialized cutting-plane algorithm
'fp_cpp': fixed_point_cpp, # fixed-point iteration (C++ wrapper)
'cp_cpp':
cutting_plane_cpp # specialized cutting-plane algorithm (C++ wrapper)
}
def solve(tsks: List[Task],
alphas: List[int],
beta: int,
a: int,
b: int,
perf=None,
method: str = 'cp') -> Optional[int]:
"""Solve a kernel instance.
Args:
tsks: list of tasks
alphas: list of integers of same length as tsks
beta: an integer
a, b: integer endpoints of interval [a, b]. tsks, alphas, beta, a, b
are the constants used to specify the kernel instance.
perf: if perf is not None, then the number of iterations used by the
method is added to perf.num_iterations
method: if method is 'fp' (resp, 'ip', 'cp') then the kernel instance
is solved using fixed-point iteration (resp., integer
programming, specialized cutting plane)
Returns:
the optimal objective value if the instance is feasible; None, otherwise.
>>> solve(tsks = [Task(20, 40), Task(10, 50), Task(33, 150)], alphas = [0, 0, 0], beta= 0, a = 20, b = 150, method='cp')
143
"""
us = [tsk.utilization for tsk in tsks]
assert sum(us) <= 1, 'the utilization of the system must be at most 1'
assert len(tsks) == len(alphas)
assert method in METHODS
f = METHODS[method]
return f(tsks, alphas, beta, a, b, perf)