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

farkas lemma made easy tool demo
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Farkas Lemma made easy Tool Demo

Christophe Alias

Inria, LIP/ENS-Lyon, CNRS, UCBL

IMPACT’20 – January 22, 2020

1 / 19

slide-2
SLIDE 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

slide-3
SLIDE 3

Application: Pluto style loop tiling

for t := 1 to T for i := 1 to N S: b[i] := a[i-1] + a[i] + a[i+1]; for i := 1 to N T: a[i] := b[i];

t i 1 2 3 N = 4 1 2 3 t1 t2 1 2 3 4 5 6 7 8 9 10 11 1 2 3

  • e1
  • e2

Orthogonal tiling after the affine transformation: φS : (t, i) → (t, 2t + i) φT : (t, i) → (t, 2t + i + 1)

3 / 19

slide-4
SLIDE 4

Application: Pluto style loop tiling

for t := 1 to T for i := 1 to N S: b[i] := a[i-1] + a[i] + a[i+1]; for i := 1 to N T: a[i] := b[i];

t i 1 2 3 N = 4 1 2 3 t1 t2 1 2 3 4 5 6 7 8 9 10 11 1 2 3

  • e1
  • e2

Constraints ∀(t, i) ∈ DS : φ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

slide-5
SLIDE 5

Outline

1

Farkas lemma and corollaries

2

fkcc

3

Demo

4

Conclusion

4 / 19

slide-6
SLIDE 6

Farkas lemma (affine form)

Lemma 1 (Farkas Lemma, affine form) Let: P = { x, A x + b ≥ 0} ⊆ Rn, P = ∅ φ : Rn → R an affine form φ(x) ≥ 0 ∀x ∈ P Then: ∃ λ ≥ 0, λ0 ≥ 0 such that: φ( x) =

t

λ(A x + b) + λ0 ∀ x Notation F(λ0, λ, A, b)( x) =

t

λ(A x + b) + λ0

5 / 19

slide-7
SLIDE 7

Two practical corollaries

Corollary 2 (solve) Consider a summation S( x) = u · x + v +

i F(λi 0,

λi, Ai, bi)( x)

  • f affine forms, including Farkas terms. Then:

∀ x : S( x) = 0 iff

  • u +

i tAi

λi = 0 ∧ v +

i

  • λi ·

bi + λ0i

  • = 0

Corollary 3 (define) F(λ0, λ, A, b)( x) = ( t λA) x + ( λ · b + λ0)

6 / 19

slide-8
SLIDE 8

Application to affine loop tiling (1/3)

Positivity φS( x) ≥ 0 ∀ x ∈ DS with: DS = { x, AS x + bS ≥ 0} Apply Farkas ∃λS

0 ≥ 0,

λS ≥ 0: φS( x) = F(λS

0 ,

λS, AS, bS)( x)

7 / 19

slide-9
SLIDE 9

Application to affine loop tiling (2/3)

Causality φT( y) − φS( x) ≥ 0 ∀( x, y) ∈ ∆ST with: ∆ST = {( x, y), AST( x, y) + bST ≥ 0} Apply Farkas ∃λST ≥ 0, λST ≥ 0: φT( y) − φS( x) = F(λST

0 ,

λST, AST, bST)( x, y)

8 / 19

slide-10
SLIDE 10

Application to affine loop tiling (2/3)

Causality φT( y) − φS( x) ≥ 0 ∀( x, y) ∈ ∆ST with: ∆ST = {( x, y), AST( x, y) + bST ≥ 0} Apply Farkas ∃λST ≥ 0, λST ≥ 0: φT( y) − φS( x) = F(λST

0 ,

λST, AST, bST)( x, y) Putting it all together F(λT

0 ,

λT, [0 AT], bT) − F(λS

0 ,

λS, [AS 0], bS) = F(λST

0 ,

λST, AST, bST) → By Corollary 2, we obtain ∃ affine constraints!

8 / 19

slide-11
SLIDE 11

Application to affine loop tiling (3/3)

Laziness: ∀( x, y) ∈ ∆ST : φT( y) − φS( x) ≤ δ(N) δ( N) ≥ 0 ∀ N ∈ C = { N, AC N + bC ≥ 0} Apply Farkas ∃µ0 ≥ 0, µ ≥ 0: δ( N) = F(µ0, µ, AC, bC)( N)

9 / 19

slide-12
SLIDE 12

Application to affine loop tiling (3/3)

Laziness: ∀( x, y) ∈ ∆ST : φT( y) − φS( x) ≤ δ(N) δ( N) ≥ 0 ∀ N ∈ C = { N, AC N + bC ≥ 0} Apply Farkas ∃µ0 ≥ 0, µ ≥ 0: δ( N) = F(µ0, µ, AC, bC)( N) Putting it all together ∀( x, y) ∈ ∆ST : δ( N) − φT( y) + φS( x) ≥ 0 gives: F(λT

0 ,

λT, [0 AC], bC) −F(λT

0 ,

λT, [0 AT], bT) + F(λS

0 ,

λS, [AS 0], bS) = F(λST

0 ,

λST, AST, bST) → By Corollary 2, we obtain ∃ affine constraints!

9 / 19

slide-13
SLIDE 13

Back to the example...

Demo

10 / 19

slide-14
SLIDE 14

Outline

1

Farkas lemma and corollaries

2

fkcc

3

Demo

4

Conclusion

11 / 19

slide-15
SLIDE 15

fkcc: positive on, solve

for i := 0 to N for j := 0 to N B: a[i] := a[i] + 1;

i j

1 2 N = 3 1 2 3

B φ1 φ2

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

slide-16
SLIDE 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

slide-17
SLIDE 17

fkcc: find, lexmin

find find v1, ..., vn s.t. <farkas> = 0 is a macro for: 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

slide-18
SLIDE 18

Outline

1

Farkas lemma and corollaries

2

fkcc

3

Demo

4

Conclusion

15 / 19

slide-19
SLIDE 19

Demo: complete example

for i := 0 to N for j := 0 to N B: a[i] := a[i] + 1;

i j

1 2 N = 3 1 2 3

B φ1 φ2

Expected: φ(i, j) = (i, j) δ(N) = (0, 1)

16 / 19

slide-20
SLIDE 20

Back to Jacobi-1D...

for t := 1 to T for i := 1 to N S: b[i] := a[i-1] + a[i] + a[i+1]; for i := 1 to N T: a[i] := b[i];

t i 1 2 3 N = 4 1 2 3 t1 t2 1 2 3 4 5 6 7 8 9 10 11 1 2 3

  • e1
  • e2

Expected: φS : (t, i) → (t, 2t + i) φT : (t, i) → (t, 2t + i + 1) δ(T, N) = (T, 2T)

17 / 19

slide-21
SLIDE 21

Outline

1

Farkas lemma and corollaries

2

fkcc

3

Demo

4

Conclusion

18 / 19

slide-22
SLIDE 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