farkas lemma made easy tool demo
play

Farkas Lemma made easy Tool Demo Christophe Alias Inria, - PowerPoint PPT Presentation

Farkas Lemma made easy Tool Demo Christophe Alias Inria, LIP/ENS-Lyon, CNRS, UCBL IMPACT20 January 22, 2020 1 / 19 Introduction Many program analysis and transformations requires to handle constraints x P : ( x ) 0


  1. Farkas Lemma made easy Tool Demo Christophe Alias Inria, LIP/ENS-Lyon, CNRS, UCBL IMPACT’20 – January 22, 2020 1 / 19

  2. Introduction Many program analysis and transformations requires to handle constraints ∀ x ∈ P : φ ( x ) ≥ 0 Examples: generation of invariants, termination analysis, loop scheduling, loop tiling Trick: Farkas lemma (affine form) eliminates universal quantification and (allows to) produce ∃ affine constraints Challenge: tricky algebraic manipulations, not easy to apply by hand, neither to implement. http://foobar.ens-lyon.fr/fkcc/ 2 / 19

  3. Application: Pluto style loop tiling t 2 11 10 9 i 8 for t := 1 to T N = 4 7 for i := 1 to N 3 6 S: b[i] := a[i-1] + a[i] 2 5 + a[i+1]; 1 e 2 � 4 for i := 1 to N t 0 3 e 1 � 0 1 2 3 T: a[i] := b[i]; 2 1 t 1 0 0 1 2 3 Orthogonal tiling after the affine transformation: φ S : ( t , i ) �→ ( t , 2 t + i ) φ T : ( t , i ) �→ ( t , 2 t + i + 1) 3 / 19

  4. Application: Pluto style loop tiling t 2 11 10 9 i 8 for t := 1 to T N = 4 7 for i := 1 to N 3 6 S: b[i] := a[i-1] + a[i] 2 5 + a[i+1]; 1 � e 2 4 for i := 1 to N t 0 3 � e 1 0 1 2 3 T: a[i] := b[i]; 2 1 t 1 0 0 1 2 3 Constraints ∀ ( t , i ) ∈ D S : φ S ( t , i ) ≥ 0 (Positivity) ∀ ( t , i , t ′ , i ′ ) ∈ ∆ ST : φ T ( t ′ , i ′ ) ≥ φ S ( t , i ) (Causality) ∀ ( t , i , t ′ , i ′ ) ∈ ∆ ST : φ T ( t ′ , i ′ ) − φ S ( t , i ) ≤ δ ( N ) (Laziness) Quadratic constraints & ∀ quantifiers � Farkas lemma 3 / 19

  5. Outline Farkas lemma and corollaries 1 fkcc 2 Demo 3 Conclusion 4 4 / 19

  6. Farkas lemma (affine form) Lemma 1 (Farkas Lemma, affine form) Let: x + � b ≥ 0 } ⊆ R n , P � = ∅ P = { � x , A � φ : R n → R an affine form φ ( x ) ≥ 0 ∀ x ∈ P Then: ∃ � λ ≥ � 0 , λ 0 ≥ 0 such that: t � x + � φ ( � x ) = λ ( A � b ) + λ 0 ∀ � x Notation F ( λ 0 ,� λ, A ,� t � x + � b )( � x ) = λ ( A � b ) + λ 0 5 / 19

  7. Two practical corollaries Corollary 2 ( solve) i F ( λ i 0 , � λ i , A i ,� Consider a summation S ( � x ) = � u · � x + v + � b i )( � x ) of affine forms, including Farkas terms. Then: t A i � λ i = � � u + � 0 ∧ � i ∀ � x : S ( � x ) = 0 iff � � λ i · � � v + � b i + λ 0 i = 0 i Corollary 3 ( define) F ( λ 0 ,� λ, A ,� x ) = ( t � x + ( � λ · � b )( � λ A ) � b + λ 0 ) 6 / 19

  8. Application to affine loop tiling (1/3) Positivity x ) ≥ 0 ∀ � x ∈ D S φ S ( � x + � with: D S = { � b S ≥ 0 } x , A S � Apply Farkas λ S ≥ � 0 ≥ 0 ,� ∃ λ S 0: x ) = F ( λ S 0 ,� λ S , A S ,� φ S ( � b S )( � x ) 7 / 19

  9. Application to affine loop tiling (2/3) Causality φ T ( � y ) − φ S ( � x ) ≥ 0 ∀ ( � y ) ∈ ∆ ST x ,� y ) + � with: ∆ ST = { ( � y ) , A ST ( � b ST ≥ 0 } x ,� x ,� Apply Farkas λ ST ≥ � ≥ 0 ,� ∃ λ ST 0: 0 x ) = F ( λ ST 0 ,� λ ST , A ST ,� φ T ( � y ) − φ S ( � b ST )( � x ,� y ) 8 / 19

  10. Application to affine loop tiling (2/3) Causality φ T ( � y ) − φ S ( � x ) ≥ 0 ∀ ( � y ) ∈ ∆ ST x ,� y ) + � with: ∆ ST = { ( � y ) , A ST ( � b ST ≥ 0 } x ,� x ,� Apply Farkas λ ST ≥ � ≥ 0 ,� ∃ λ ST 0: 0 x ) = F ( λ ST 0 ,� λ ST , A ST ,� φ T ( � y ) − φ S ( � b ST )( � x ,� y ) Putting it all together 0 ,� λ T , [0 A T ] ,� 0 ,� λ S , [ A S 0] ,� F ( λ T b T ) − F ( λ S b S ) 0 ,� λ ST , A ST ,� = F ( λ ST b ST ) → By Corollary 2, we obtain ∃ affine constraints! 8 / 19

  11. Application to affine loop tiling (3/3) Laziness: ∀ ( � y ) ∈ ∆ ST : φ T ( � y ) − φ S ( � x ) ≤ δ ( N ) x ,� δ ( � ∀ � N ∈ C = { � N , A C � N + � N ) ≥ 0 b C ≥ 0 } Apply Farkas µ ≥ � ∃ µ 0 ≥ 0 , � 0: δ ( � µ, A C ,� b C )( � N ) = F ( µ 0 , � N ) 9 / 19

  12. Application to affine loop tiling (3/3) Laziness: ∀ ( � y ) ∈ ∆ ST : φ T ( � y ) − φ S ( � x ) ≤ δ ( N ) x ,� δ ( � ∀ � N ∈ C = { � N , A C � N + � N ) ≥ 0 b C ≥ 0 } Apply Farkas µ ≥ � ∃ µ 0 ≥ 0 , � 0: δ ( � µ, A C ,� b C )( � N ) = F ( µ 0 , � N ) Putting it all together y ) ∈ ∆ ST : δ ( � ∀ ( � x ,� N ) − φ T ( � y ) + φ S ( � x ) ≥ 0 gives: 0 ,� λ T , [0 A C ] ,� F ( λ T b C ) 0 ,� λ T , [0 A T ] ,� 0 ,� λ S , [ A S 0] ,� − F ( λ T b T ) + F ( λ S b S ) 0 ,� λ ST , A ST ,� = F ( λ ST b ST ) → By Corollary 2, we obtain ∃ affine constraints! 9 / 19

  13. Back to the example... Demo 10 / 19

  14. Outline Farkas lemma and corollaries 1 fkcc 2 Demo 3 Conclusion 4 11 / 19

  15. fkcc : positive on , solve j φ 1 N = 3 for i := 0 to N φ 2 2 for j := 0 to N B B: a[i] := a[i] + 1; 1 0 i 0 1 2 3 D := [] -> { [i,j,N]: 0 <= i and i < N ...}; phi := positive_on D; Delta := [] -> { [i,j,i’,j’,N]: ...}; to_target := {[i,j,i’,j’,N] -> [i’,j’,N]}; to_source := {[i,j,i’,j’,N] -> [i,j,N]}; solve (phi . to_target) - (phi . to_source) - positive_on Delta = 0; 12 / 19

  16. fkcc : define , keep ... phi_correct := (solve (phi . to_target) - (phi . to_source) - positive_on Delta = 0) * (define phi with phi); phi_correct; keep phi_0,phi_1,phi_2,phi_3 in phi_correct; console $ fkcc < test.fk [] -> {[lambda_0,lambda_1,lambda_2,lambda_3,lambda_4,lambda_5,lambda_6,lambda_7,lambda_8, lambda_9,lambda_10,lambda_11,lambda_12,lambda_13,phi_0,phi_1,phi_2,phi_3] : (((((-1*lambda_0)+lambda_1)+lambda_5)+(-1*lambda_6))+(-1*lambda_9))+lambda_10 >= 0 and ((((lambda_0+(-1*lambda_1))+(-1*lambda_5))+lambda_6)+lambda_9)+(-1*lambda_10) >= 0 and [...] ((-1*lambda_0)+lambda_1)+phi_0 >= 0 and (lambda_0+(-1*lambda_1))+(-1*phi_0) >= 0 and ((-1*lambda_2)+lambda_3)+phi_1 >= 0 and (lambda_2+(-1*lambda_3))+(-1*phi_1) >= 0 and [...]}; [] -> {[phi_0,phi_1,phi_2,phi_3] : phi_2+phi_3 >= 0 and phi_0+phi_2 >= 0 and phi_1 >= 0 and phi_2 >= 0 and 1 >= 0}; 13 / 19

  17. fkcc : find , lexmin find <farkas> = 0 is a macro for: find v1, ..., vn s.t. keep v1, ..., vn in (solve <farkas> = 0) * (define v1 with v1) * ... ... phi_correct := find phi s.t. (phi . to_target) - (phi . to_source) - positive_on Delta = 0 phi_correct; lexmin phi_correct; 14 / 19

  18. Outline Farkas lemma and corollaries 1 fkcc 2 Demo 3 Conclusion 4 15 / 19

  19. Demo: complete example j φ 1 N = 3 for i := 0 to N φ 2 2 for j := 0 to N B B: a[i] := a[i] + 1; 1 i 0 0 1 2 3 Expected: φ ( i , j ) = ( i , j ) δ ( N ) = (0 , 1) 16 / 19

  20. Back to Jacobi-1D... t 2 11 10 9 i 8 for t := 1 to T N = 4 7 for i := 1 to N 3 6 S: b[i] := a[i-1] + a[i] 2 5 + a[i+1]; 1 e 2 � 4 for i := 1 to N t 0 3 e 1 � 0 1 2 3 T: a[i] := b[i]; 2 1 t 1 0 0 1 2 3 Expected: φ S : ( t , i ) �→ ( t , 2 t + i ) φ T : ( t , i ) �→ ( t , 2 t + i + 1) δ ( T , N ) = ( T , 2 T ) 17 / 19

  21. Outline Farkas lemma and corollaries 1 fkcc 2 Demo 3 Conclusion 4 18 / 19

  22. Conclusion fkcc , a scripting tool to prototype program analysis and transformations using the affine form of Farkas lemma fkcc is powerful enough to write in a few lines tricky scheduling algorithms and termination analysis Object representation (polyhedron, affine functions) is compatible with iscc http://foobar.ens-lyon.fr/fkcc/ 19 / 19

Recommend


More recommend