amortized resource analysis
play

Amortized Resource Analysis Martin Hofmann - PowerPoint PPT Presentation

Amortized Resource Analysis Martin Hofmann Ludwig-Maximilians-Universit at M unchen EWSCS Winter School 2011, Palmse, Estonia mh (lmumun) Amortized Resource Analysis 28.02.-04.03.2011 1 / 72 Why resource analysis Computing under


  1. Amortized Resource Analysis Martin Hofmann Ludwig-Maximilians-Universit¨ at M¨ unchen EWSCS Winter School 2011, Palmse, Estonia mh (lmumun) Amortized Resource Analysis 28.02.-04.03.2011 1 / 72

  2. Why resource analysis Computing under severe resource restrictions: mobile phones, wireless sensors, embedded systems, smart cards. Issues of trust related to resources: grid & cloud computing (local computing power, bandwidth), active networking, mobile agents. Forge-proof certification of resource needs. Static analysis of resource needs to guarantee survival of sandboxing. Terminating a program due to resource bound violation may not be feasible mh (lmumun) Amortized Resource Analysis 28.02.-04.03.2011 2 / 72

  3. Type systems Verification of arbitrary programs is infeasible and undecidable in general. Verification of simple properties of sensibly written programs can be quite easy. Type systems highlight simple properties of sensibly written programs. Examples: Java type system prevents uncaught exceptions, certain segmentation faults, etc. ML type system often prevents one from supplying arguments to a function in the wrong order etc. (“If it typechecks it works”) Abstract types promote modularity by preventing breaches of abstraction mh (lmumun) Amortized Resource Analysis 28.02.-04.03.2011 3 / 72

  4. Approaches to resource analysis Testing and extrapolating (Unnikrishnan et al) Inserting counter variables, analysing their values (Gulwani, SPEED) Extraction of recurrences (Grobauer, Hermenegildo, COSTA) Abstract interpretation for concrete fixed WCET bounds (Wilhelm, Ferdinand, ABSINT) Amortized analysis: this work mh (lmumun) Amortized Resource Analysis 28.02.-04.03.2011 4 / 72

  5. These lectures Automatic type-based inference of resource bounds for programs with ◮ recursive data structures (lists, trees) ◮ recursive functions ◮ composition of helper functions via intermediate data structures We do not improve the precise analysis of basic blocks or programs with a simple loop structure; Method is parametric in cost model, can interact with complementary approaches for basic blocks. mh (lmumun) Amortized Resource Analysis 28.02.-04.03.2011 5 / 72

  6. Examples Heap-, stack, time, general resource bounds for Functional programs on inductive data: Insertion sort, quicksort (destructive and non-destructive), tree sort, Huffman OO-versions of these programs Larger programs: filters from signal processing, block booking text messages. Programs with polynomial resource usage: matrix operations, dynamic programming, e.g. longest common subsequence mh (lmumun) Amortized Resource Analysis 28.02.-04.03.2011 6 / 72

  7. Summary Resource analysis Amortized complexity according to Tarjan Inference of linear resource bounds according to [H-Jost ’03]. A glance on objects Polynomial resource annotations with binomial coefficients Multivariate polynomial resource annotations Conclusion & wrap up Joint work with Klaus Aehlig, Jan Hoffmann, Steffen Jost, Dulma Rodriguez. mh (lmumun) Amortized Resource Analysis 28.02.-04.03.2011 7 / 72

  8. Amortized Cost Introduced by R. Tarjan in the 70s to facilitate cost analysis of algorithms that repeatedly access a data structure. assign (nonnegative) potential to states of the data structure define amortized cost of a single operation as actual cost + potential difference. sum of amortized costs + potential of initial data structure bounds actual cost of sequence of operations. Usually, one arranges things so that amortised cost of operations are constant for then no sizes of intermediate data structures need to be maintained. mh (lmumun) Amortized Resource Analysis 28.02.-04.03.2011 8 / 72

  9. Queue as two stacks A FIFO queue Q can be implemented with two stacks S in and S out : Put , Get Put ( Q , x ) Push ( S in , x ) Get ( Q ) if Empty ( S out ) then while not Empty ( S in ) do Push ( S out , Pop ( S in )) return Pop ( S out ) mh (lmumun) Amortized Resource Analysis 28.02.-04.03.2011 9 / 72

  10. Cost of Put and Get How many stack operations do the queue operations need in the worst-case? Put ( Q , x ): 1 Get ( Q ): 2 n + 1 5 times mh (lmumun) Amortized Resource Analysis 28.02.-04.03.2011 10 / 72

  11. Actual cost . . . of a sequence of m Put/Get-operations is O ( m ) and not O ( m 2 ), because every element is moved at most 3 × Justification: assign potential 2 to each element of entry stack. Amortized cost of Put : 3 (overcharge by 2) Amortized cost of Get : 1 (pay expensive case from saved capital) mh (lmumun) Amortized Resource Analysis 28.02.-04.03.2011 11 / 72

  12. Automatic inference Our approach: work with potential that is an unknown nonnegative linear combination of nonnegative basis functions use type-based analysis to infer linear bounds on the coefficients. use standard linear programming package to solve these constraints. mh (lmumun) Amortized Resource Analysis 28.02.-04.03.2011 12 / 72

  13. H-Jost ’03: linear potentials Refined type system for first-order functional programs with lists (and other inductive data), A refined type A determines a function assigning to each value of that type a nonnegative potential Φ A ( v ). For example, for the refined list type L (7) ( int ) refining the type L ( int ) of integer lists one has Φ L (7) ( int ) ([3; 4; 5; 6] = 28 I.e. 7 units per list entry. More interestingly, for the refined type A = L (7) ( L (3) ( int ) , L (2) ( int )) one has � � | l i | + 2 | k i | Φ A ([( l 1 , k 1 ); ( l 2 , k 2 ); . . . ; ( l j , k j )]) = 7 j + 3 i i mh (lmumun) Amortized Resource Analysis 28.02.-04.03.2011 13 / 72

  14. Cost models Cost is defined by an instrumented operational semantics: k k ′ e � v , h ′ S , h means that given environment S = x 1 = v 1 , . . . , x n = v n and heap h then the evaluation of e results in value v and result heap h ′ and k , k ′ ≥ 0 describe resource usage in the following way: mh (lmumun) Amortized Resource Analysis 28.02.-04.03.2011 14 / 72

  15. Imagine a resource counter that counts free resources (freelist, egg timer, . . . ). If prior to execution the counter’s value is ≥ k then the counter will not run dry (become negative) during execution and at the end of the execution its value will be ≥ k ′ . Alternative reading: temporary resource (space) usage will be ≤ k (high watermark). Net resource usage upon termination will be ≤ k − k ′ . For timelike resources we can always assume k ′ = 0; for space-like resources we can have k = k ′ = 1: net resource usage zero, high watermark 1 ( allocation of 1 cell followed by deallocation before termination). mh (lmumun) Amortized Resource Analysis 28.02.-04.03.2011 15 / 72

  16. Typing judgement The typing judgement takes the form q x 1 : A 1 , . . . , x n : A n q ′ e : B and the typing rules are set up in such a way that if x 1 = v 1 , . . . , x n = v n , h e � v i Φ A i ( v i , h ) + q there exists k ′ ≥ q ′ + Φ B ( v , h ′ ) such then for each k ≥ � k that x 1 = v 1 , . . . , x n = v n , h k ′ e � v . Thus, temporary resource usage ≤ � i Φ A i ( v i , h ) + q and total resource i Φ A i ( v i , h ′ ) + q − q ′ − Φ B ( v , h ′ ). usage ≤ � Total resource usage may be negative (deallocation). mh (lmumun) Amortized Resource Analysis 28.02.-04.03.2011 16 / 72

  17. Typing rules r + 1 Allocation must be paid for: x : A , y : L ( r ) ( A ) x :: y : L ( r ) ( A ); 0 q + r Pattern matching frees potential: If Γ , x : A , y : L ( r ) ( A ) e : C q ′ q then Γ , l : L ( r ) ( A ) q ′ match l with [] → · · · | x :: y → e : C Destructive pattern matching even allows one to reclaim resource: If q + r + 1 Γ , x : A , y : L ( r ) ( A ) e : C then q ′ q Γ , l : L ( r ) ( A ) q ′ match l with [] → · · · | x :: y @ → e : C Duplicating a variable requires splitting of potential: can use a variable x : L (7) ( int ) twice: once with type L (3) ( int ) and once with type L (4) ( int ). But not twice with type L (7) ( int )! Otherwise, typing rules are pretty standard. Remark: instead of +1 one can charge other quantities. Charging for operations other than allocation � general resource analysis. mh (lmumun) Amortized Resource Analysis 28.02.-04.03.2011 17 / 72

  18. Typing rules: more detail q / q ′ B . We then Function symbols are declared with types like A 1 , . . . , A ℓ − − − → expect the judgement q q ′ e : B x 1 : A 1 , . . . , x l : A l to hold for the body e of f . Slogan: Assume any typing for function symbols. Justify it for the body. Use the assumed typing judgement for recursive calls. mh (lmumun) Amortized Resource Analysis 28.02.-04.03.2011 18 / 72

  19. Some (expected) typing rules q q 0 q ≥ 1 + k + q ′ Γ 1 q 0 e 1 : A Γ 2 , x : A q ′ e 2 : C q q Γ , x h : A , x t : L ( k ) ( A ) , n q ′ x h :: x t : L ( k ) ( A ) Γ 1 , Γ 2 q ′ let x = e 1 in e 2 : C k / k ′ f : A 1 , . . . , A p − − − → C q − k + k ′ ≥ q ′ q ≥ k q q ′ f ( x 1 , . . . , x p ): C Γ , x 1 : A 1 , . . . , x p : A p mh (lmumun) Amortized Resource Analysis 28.02.-04.03.2011 19 / 72

Recommend


More recommend