1 Parametric Heap Usage Analysis for Functional Programs Leena Unnikrishnan Scott D. Stoller.

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

Path-Sensitive Analysis for Linear Arithmetic and Uninterpreted Functions SAS 2004 Sumit Gulwani George Necula EECS Department University of California,
2. Getting Started Heejin Park College of Information and Communications Hanyang University.
Warm up Translate following expression a. three less than x. b. Product of a number and 4 2. a. -24+(-13) b. 34 – 42 3.a. -3/7 + 2/3 b. 2/3.
BY Lecturer: Aisha Dawood. The notations we use to describe the asymptotic running time of an algorithm are defined in terms of functions whose domains.
Analysis of Computer Algorithms
Slide 1 Insert your own content. Slide 2 Insert your own content.
By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 6 Prof. Erik Demaine.
Multiplying binomials You will have 20 seconds to answer each of the following multiplication problems. If you get hung up, go to the next problem when.
0 - 0.
ALGEBRAIC EXPRESSIONS
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
ADDING INTEGERS 1. POS. + POS. = POS. 2. NEG. + NEG. = NEG. 3. POS. + NEG. OR NEG. + POS. SUBTRACT TAKE SIGN OF BIGGER ABSOLUTE VALUE.
MULTIPLICATION EQUATIONS 1. SOLVE FOR X 3. WHAT EVER YOU DO TO ONE SIDE YOU HAVE TO DO TO THE OTHER 2. DIVIDE BY THE NUMBER IN FRONT OF THE VARIABLE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
1 Student-Project Allocation with Preferences over Projects David Manlove Gregg OMalley University of Glasgow Department of Computing Science Supported.
1 Automating the Generation of Mutation Tests Mike Papadakis and Nicos Malevris Department of Informatics Athens University of Economics and Business.
Reductions Complexity ©D.Moshkovitz.
Introduction A function is called higher-order if it takes a function as an argument or returns a function as a result. twice :: (a  a)  a  a twice.
Comp 122, Spring 2004 Order Statistics. order - 2 Lin / Devi Comp 122 Order Statistic i th order statistic: i th smallest element of a set of n elements.
Measuring Time Complexity
Solve two-step equations.
Meta Predicate Abstraction for Hierarchical Symbolic Heaps Josh Berdine Microsoft Research, Cambridge joint with Mike Emmi University of California, Los.
Ack: Several slides from Prof. Jim Anderson’s COMP 202 notes.
1 Parallel Algorithms (chap. 30, 1 st edition) Parallel: perform more than one operation at a time. PRAM model: Parallel Random Access Model. p0p0 p1p1.
Data Structures ADT List
Modern Programming Languages, 2nd ed.
Introduction Recall that the imaginary unit i is equal to. A fraction with i in the denominator does not have a rational denominator, since is not a rational.
Solving Equations How to Solve Them
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
Squares and Square Root WALK. Solve each problem REVIEW:
1 © 1999 Citrix Systems Inc Using type information in garbage collection Tim Harris.
Past Tense Probe. Past Tense Probe Past Tense Probe – Practice 1.
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
Properties of Exponents
Purpose : To convert this string to a new character array. Return Type : char[ ] Parameters : none Declaration : public char[ ] toCharArray() Returns.
Chapter 5 Test Review Sections 5-1 through 5-4.
Addition 1’s to 20.
25 seconds left…...
Test B, 100 Subtraction Facts
11 = This is the fact family. You say: 8+3=11 and 3+8=11
Week 1.
Use the substitution method
Chapter 11 Limitations of Algorithm Power Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
9.2 Absolute Value Equations and Inequalities
Building Java Programs Chapter 13
Solve an equation by multiplying by a reciprocal
Copyright © Cengage Learning. All rights reserved.
Extensible Shape Analysis by Designing with the User in Mind Bor-Yuh Evan Chang Bor-Yuh Evan Chang, Xavier Rival, and George Necula University of California,
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
Foundations of Data Structures Practical Session #11 Sort properties, Quicksort algorithm.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
MATH 224 – Discrete Mathematics
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages – It’s elegant, minimal, can.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 Programming Languages and Paradigms Lisp Programming.
Computability Start complexity. Motivation by thinking about sorting. Homework: Finish examples.
CS Main Questions Given that the computer is the Great Symbol Manipulator, there are three main questions in the field of computer science: What kinds.
1 Lecture 16: Lists and vectors Binary search, Sorting.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Logic Circuits Chapter 2. Overview  Many important functions computed with straight-line programs No loops nor branches Conveniently described with circuits.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
CS314 – Section 5 Recitation 9
CS314 – Section 5 Recitation 10
CS 550 Programming Languages Jeremy Johnson
CS 270 Math Foundations of CS Jeremy Johnson
More Scheme CS 331.
Presentation transcript:

1 Parametric Heap Usage Analysis for Functional Programs Leena Unnikrishnan Scott D. Stoller

2 Motivation Goal: upper bound on live heap usage. Understanding memory usage of programs. Guiding automatic methods, e.g., help program transformations select space-efficient programs. Required for time analysis: running times depend on memory allocation, garbage collection, etc. Determining/verifying space usage of embedded applications.

3 Challenges Determine live heap usage over entire program execution, and over multiple execution paths. Unlike running times, live heap space increases and decreases during execution. Static determination of liveness of objects.

4 Overview Input: Program P in a functional language with lists. Output: worst-case live heap usage of P expressed in terms of sizes of P s inputs. Example Input: program reverse(ls) to reverse a list Output: Max live heap usage = 2|ls| - 1, if |ls| > 0 Method –Transform functions in P into bound functions that describe their heap space usage and related metrics. –Simplify and rewrite bound functions into recurrences. –Solve recurrences into closed forms.

5 Outline Bound functions Recurrences containing the max operator Soundness checks for R functions Examples Related work Conclusion

6 Bound Functions Construct three bound functions for each function f in program P. Arguments of each bound function for f(v 1,…,v n ) are the sizes v 1 s,…,v n s, of v 1,…,v n. Size of a boolean or number: its value Size of a flat list: its length

7 Bound Functions For f(v 1,…,v n ) in the input program, construct Live heap space bound function, S f (v 1 s,…,v n s ) : upper bound on live heap use of f, for all possible arguments v 1,…,v n, of sizes v 1 s,…,v n s. New result-space bound function, N f (v 1 s,…,v n s,v 1 w,…,v n w ) : upper bound on amount of newly allocated space in the result of f, for all possible arguments v 1,…,v n, of sizes v 1 s,…,v n s. v i w is the amount of new space in v i. –New is w.r.t. start of evaluation of call to f. Size bound function, R f (v 1 s,…,v n s ) : upper bound on size of result of f, for all possible arguments v 1,…,v n, of sizes v 1 s,…,v n s.

8 Live Heap Space Bound Functions For f(v 1,…,v n ) = e, S f (v 1 s,…,v n s ) = S[e]. Example clauses in the definition of S[.]. S[v] = 0, S[cons(e 1, e 2 )] = 1 + max(S[e 1 ], S[e 2 ]) S[f(e 1, e 2 )] = max(S[e 1 ], N[e 1 ] + S[e 2 ], N[e 1 ] + N[e 2 ] + S f (R[e 1 ], R[e 2 ])) reverse(ls) = if null?(ls) then nil else append(reverse(cdr(ls)), cons(car(ls), nil)) S rev (ls s ) = if ls s =0 then 0 else max(S rev (ls s -1), N rev (ls s -1)+1, N rev (ls s -1)+1+S app (R rev (ls s -1)))

9 Size Bound Functions For f( v 1,…,v n ) = e, R f ( v 1 s,…,v n s ) = R[e]. Example clauses in the definition of R[.]. R[v] = v s, R[cons(e 1, e 2 )] = 1 + R[e 2 ] R[if p then e 1 else e 2 ] max(R[e 1 ], R[e 2 ]), if p contains car if R[p] then R[e 1 ] else R[e 2 ] R[f(e 1,…,e n )] = R f (R[e 1 ],…, R[e n ]) = { reverse(ls) = if null?(ls) then nil else append(reverse(cdr(ls)), cons(car(ls), nil)) R rev (ls s ) = if ls s =0 then 0 else R app (R rev (ls s -1), 1)

10 New Result-Space Bound Functions For f(v) in the input, the N function is N f (v s, v w ). f(e ) in input program yields N f (R[e ], N[e ]) in bound functions. – New is w.r.t. the start of evaluation of f(e ). – v w is the part of v created in e. – N f (…) is the part of the result created in e or the body of f. For f(v) = e, N f (v s, v w ) = N[e]. Example clauses in the definition of N[.]. N[v] = v w, N[cons(e 1, e 2 )] = 1 + N[e 2 ] N[f(e 1,…,e n )] = N f (R[e 1 ],…,R[e n ], N[e 1 ],…,N[e n ])

11 New Result-Space Bound Functions reverse(ls) = if null?(ls) then nil else append(reverse(cdr(ls)), cons(car(ls), nil)) N rev (ls s, ls w ) = if ls s =0 then 0 else N app (R rev (ls s -1), 1, N rev (ls s -1, 0), 1)

12 Outline Bound functions Recurrences containing the max operator Soundness checks for R functions Examples Related work Conclusion

13 Composite Recurrences Bound functions are simplified into recurrences that may contain max. We call these composite recurrences. Example: For insert in insertion sort, R ins (n) = max(1+n, 1+R ins (n-1)), if n>0 Solve composite recurrences using a library of solution templates.

14 Solving Composite Recurrences: Example For a recurrence of the form where, the arguments of max are equal for (a) base cases, i.e., e 1 (n) = e 2 (n) + a·c (n-b-i+1), for n in [i+j+1, i+j+b] (b) all other values of n, i.e. e 1 (n) = e 2 (n) + a·e 1 (n-b), for n>(i+j+b) the solution is T(n) = e 1 (n), for n>(i+j). T(n) = { c 1,…,c j+1, if n = i,…,(i+j), respectively max(e 1 (n), e 2 (n) + aT(n-b)), otherwise

15 Outline Bound functions Recurrences containing the max operator Soundness checks for R functions Examples Related work Conclusion

16 Soundness Checks on Uses of R Functions Intuition: It is not necessarily the object with the largest size that leads to worst-case live heap usage. R function results are arguments to S and N functions. Consider the R function, R f (n), of greaterlist in quicksort. R f (n) is the maximum of a set of values. R f is a complex R function. R f (n) = { 0, if n=0 max(1+R f (n-1), R f (n-1)), o.w. } = max(n, …, 0)

17 Soundness Checks on Uses of R functions Using the result of R f (n) as an argument to a bound function, say S g, yields a true upper bound down the line, only if the following hold: –Case 1: If S g (R f (n)) is a recursive call in the body of S g, then using the result of R f (n) in the definition of S g, yields the maximum result. E.g., this holds for recurrences of the form where base values of T are monotonically increasing largest base value < value at smallest non-base input e(n) is monotonically increasing w.r.t. n –Case 2: check if the solution of S g is monotonically increasing w.r.t. its single argument. T(n) = { c 1,…,c j+1, if n = i,…,(i+j), respectively e(n) + aT(R(n)), otherwise

18 Improving Solvability of Recurrences Eliminate unused arguments from bound functions. –Simplifies recurrences. May eliminate dependence on other recurrences. Use invariants to simplify max expressions. –May simplify composite recurrences or make them regular. –Example: S f (e 1,…,e n ) N f (e 1,…,e n,0,…,0). So, max(S f (e 1,…,e n ), N f (e 1,…,e n,0,…,0)) = S f (e 1,…,e n ). Use simpler, approximate definitions of N functions. precise: N f (v 1 s,…,v n s, v 1 w,…,v n w ) = N[e] approx: N f (v 1 s,…,v n s ) = R f (v 1 s,…,v n s ) In our examples, even with this simplification, we get tight bounds on worst-case space usage.

19 Examples ProgramS Function ( v s > c) Tight? reverse(ls)S rev (ls s ) = 2ls s – 1 Yes insertion-sort(ls)S is (ls s ) = 2ls s – 1 Yes selection-sort(ls)S ss (ls s ) = ls s (ls s +1)/2 Yes merge-sort(ls)S ms (ls s ) = 2ls s – 1 Yes quicksort(ls) S qs (ls s ) = 3·2 ls s -1 – 1 No longest-common-subseq(ls 1, ls 2 )S lcs (ls 1 s, ls 2 s ) = 2ls 2 s + 2 Yes string-edit-distance(ls 1, ls 2 )S se (ls 1 s, ls 2 s ) = 2ls 1 + 2ls Yes binomial-coefficient(n, m)S bc (n s,m s ) = 2m s +2, if m s <n s Yes Derives linear, quadratic, and exponential bounds. Bounds are tight (exact) for all examples except quicksort.

20 Related Work Timing analysis using recurrence relations [Wegbreit 75, LeMetayer 88]. Static prediction of heap space [Hofmann & Jost 03] –Applies to a linearly-typed functional language. –Derives linear heap bounds. Parametric prediction of heap memory requirements [Braberman et al 08] –Java-like language. –Region-based memory management models heap recovery. –Derives polynomial bounds.

21 Related Work Memory resource bounds for low-level programs [Chin et al 08]: –Assembly-like language. –Explicit deallocation to model heap recovery. –Derives linear bounds. Cost analysis [Albert et al 07]: –Java bytecode. –Escape analysis to model heap recovery. –Analysis for live heap at ISMM 09!

22 Conclusion Automatic, accurate analysis of live heap usage of functional programs. –Framework of bound functions that describe live heap usage of input programs. –Methods to simplify and solve composite recurrences. –Soundness checks involving monotonicity and monotonicity–like properties of recurrences. Results are at source-code level and are easy to understand. Results are not restricted to any complexity class.

23 Thank You!

24 Extensions to Handle Other Data Types Specialize R functions and size arguments to each data type. Binary Trees –Possible size measures: height, total. –R functions: R height, R total Extend S, N, R transformations to new data type. R height [ tree(e, e l, e r ) ] = 1 + max(R height [ e l ], R height [ e r ]) R total [ tree(e, e l, e r ) ] = 1 + R total [ e l ] + R total [ e r ] Live heap usage of a given program may be better represented in terms of one specific size measure, than others.

25 Example insertion-sort(ls) = if null?(ls) then nil else insert(car(ls), insertion-sort(cdr(ls))) insert(x, ls) = if null?(ls) then cons(x, nil) else if lesseroreq?(x, car(ls)) then cons(x, ls) else cons(car(ls), insert(x,cdr(ls))) Input Program S is (ls s ) = if ls s =0 then 0 else max(S is (ls s -1), N is (ls s -1) + S ins (R is (ls s -1))) N is (ls s ) = if ls s =0 then 0 else N ins (R is (ls s -1)) R is (ls s ) = if ls s =0 then 0 else R ins (R is (ls s -1)) S ins (ls s ) = if ls s =0 then 1 else max(1, 1+S ins (ls s -1)) N ins (ls s ) = R ins (ls s ) R ins (ls s ) = if ls s =0 then 1+0 else max(1+ls s, 1+R ins (ls s -1)) Bound Functions

26 Example insertion-sort(ls) = if null?(ls) then nil else insert(car(ls), insertion-sort(cdr(ls))) insert(x, ls) = if null?(ls) then cons(x, nil) else if lesseroreq?(x, car(ls)) then cons(x, ls) else cons(car(ls), insert(x,cdr(ls))) Input Program N ins (ls s ) = 1+ls s S is (ls s ) = { 0, if ls s =0 max(S is (ls s -1), 2ls s -1), otherwise S ins (ls s ) = { 1, if ls s =0 max(1, 1+S ins (ls s -1)), otherwise R ins (ls s ) = { 1, if ls s =0 max(1+ls s, 1+R ins (ls s -1)), otherwise R is (ls s ) = 0, if ls s =0 1+ R is (ls s -1), otherwise { N is (ls s ) = ls s Recurrences/Equations

27 Example insertion-sort(ls) = if null?(ls) then nil else insert(car(ls), insertion-sort(cdr(ls))) insert(x, ls) = if null?(ls) then cons(x, nil) else if lesseroreq?(x, car(ls)) then cons(x, ls) else cons(car(ls), insert(x,cdr(ls))) Input Program S is (ls s ) = { 0, if ls s =0 2ls s -1, otherwise N is (ls s ) = ls s N ins (ls s ) = 1+ls s R is (ls s ) = ls s S ins (ls s ) = 1+ls s R ins (ls s ) = 1+ls s Closed Forms