Tools for the Polyhedral Model

Preview:

Citation preview

December 15, 2009 1 / 35

Tools for the Polyhedral Model

Sven Verdoolaege

Katholieke Universiteit Leuven, Belgium

December 15, 2009

December 15, 2009 2 / 35

Outline

1 Polyhedral Model

2 isl

Introduction and RepresentationParametric Integer Programming

3 CLooG

4 barvinok

5 bernstein

6 Conclusions

Polyhedral Model December 15, 2009 3 / 35

Outline

1 Polyhedral Model

2 isl

Introduction and RepresentationParametric Integer Programming

3 CLooG

4 barvinok

5 bernstein

6 Conclusions

Polyhedral Model December 15, 2009 4 / 35

Polyhedral Program Analysis and Transformation

Observation:

Most expensive part of a multimedia or signal processing program:manipulation of arrays inside loops

⇒ most interesting part to optimize

Polyhedral Model December 15, 2009 4 / 35

Polyhedral Program Analysis and Transformation

Observation:

Most expensive part of a multimedia or signal processing program:manipulation of arrays inside loops

⇒ most interesting part to optimize

⇒ need for compact representation ofiterations of a loop/elements of an array

⇒ + efficient to manipulate

Polyhedral Model December 15, 2009 4 / 35

Polyhedral Program Analysis and Transformation

Observation:

Most expensive part of a multimedia or signal processing program:manipulation of arrays inside loops

⇒ most interesting part to optimize

⇒ need for compact representation ofiterations of a loop/elements of an array

⇒ + efficient to manipulate

⇒ Integer points in polyhedra (“polyhedral model”)⇒ More generally: sets of integers bounded by affine inequalities

Polyhedral Model December 15, 2009 5 / 35

Polyhedral Representation Example: Iteration Domain

#define N 5

for (i = 1; i <= N; ++i)

for (j = 1; j <= i; ++j)

a[i][j]=

Assumptions on sequential code:

iterators are integers

loops with affine bounds

affine conditions

affine index expressions

Polyhedral Model December 15, 2009 5 / 35

Polyhedral Representation Example: Iteration Domain

#define N 5

for (i = 1; i <= N; ++i)

for (j = 1; j <= i; ++j)

a[i][j]=

j

iIteration domain: P = { [i , j ] | i ≥ 1 }

Assumptions on sequential code:

iterators are integers

loops with affine bounds

affine conditions

affine index expressions

Polyhedral Model December 15, 2009 5 / 35

Polyhedral Representation Example: Iteration Domain

#define N 5

for (i = 1; i <= N; ++i)

for (j = 1; j <= i; ++j)

a[i][j]=

j

iIteration domain: P = { [i , j ] | i ≥ 1 ∧ i ≤ N }

Assumptions on sequential code:

iterators are integers

loops with affine bounds

affine conditions

affine index expressions

Polyhedral Model December 15, 2009 5 / 35

Polyhedral Representation Example: Iteration Domain

#define N 5

for (i = 1; i <= N; ++i)

for (j = 1; j <= i; ++j)

a[i][j]=

j

iIteration domain: P = { [i , j ] | i ≥ 1 ∧ i ≤ N ∧ j ≥ 1 }

Assumptions on sequential code:

iterators are integers

loops with affine bounds

affine conditions

affine index expressions

Polyhedral Model December 15, 2009 5 / 35

Polyhedral Representation Example: Iteration Domain

#define N 5

for (i = 1; i <= N; ++i)

for (j = 1; j <= i; ++j)

a[i][j]=

j

iIteration domain: P = { [i , j ] | i ≥ 1 ∧ i ≤ N ∧ j ≥ 1 ∧ j ≤ i }

Assumptions on sequential code:

iterators are integers

loops with affine bounds

affine conditions

affine index expressions

Polyhedral Model December 15, 2009 5 / 35

Polyhedral Representation Example: Iteration Domain

#define N 5

for (i = 1; i <= N; ++i)

for (j = 1; j <= i; ++j)

a[i][j]=

j

i

• •

• • •

• • • •

• • • • •

Iteration domain: P = { [i , j ] | i ≥ 1 ∧ i ≤ N ∧ j ≥ 1 ∧ j ≤ i }

Assumptions on sequential code:

iterators are integers

loops with affine bounds

affine conditions

affine index expressions

Polyhedral Model December 15, 2009 5 / 35

Polyhedral Representation Example: Iteration Domain

#define N 5

for (i = 1; i <= N; ++i)

for (j = 1; j <= i; ++j)

a[i][j]=

j

i

• •

• • •

• • • •

• • • • •

• •

Iteration domain: P = { [i , j ] | i ≥ 1 ∧ i ≤ N ∧ j ≥ 1 ∧ j ≤ i }

Alternative representation: P = conv.hull { (1, 1), (N, 1), (N, N) }

Assumptions on sequential code:

iterators are integers

loops with affine bounds

affine conditions

affine index expressions

Polyhedral Model December 15, 2009 6 / 35

Polyhedral Program Transformation Example

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[i] = f(a[N-i])

Execution order: top-down, left-right

•, ◦ Statement iterationData flow dependence

• Executed statementData in memory

Polyhedral Model December 15, 2009 6 / 35

Polyhedral Program Transformation Example

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[i] = f(a[N-i])

• • • • •

◦◦◦◦◦

Execution order: top-down, left-right

•, ◦ Statement iterationData flow dependence

• Executed statementData in memory

Polyhedral Model December 15, 2009 6 / 35

Polyhedral Program Transformation Example

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[i] = f(a[N-i])

• • • • •

◦◦◦◦◦

Execution order: top-down, left-right

•, ◦ Statement iterationData flow dependence

• Executed statementData in memory

Polyhedral Model December 15, 2009 6 / 35

Polyhedral Program Transformation Example

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[i] = f(a[N-i])

• • • • •

◦◦◦◦◦

• •

Execution order: top-down, left-right

•, ◦ Statement iterationData flow dependence

• Executed statementData in memory

Polyhedral Model December 15, 2009 6 / 35

Polyhedral Program Transformation Example

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[i] = f(a[N-i])

• • • • •

◦◦◦◦◦

• • •

Execution order: top-down, left-right

•, ◦ Statement iterationData flow dependence

• Executed statementData in memory

Polyhedral Model December 15, 2009 6 / 35

Polyhedral Program Transformation Example

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[i] = f(a[N-i])

• • • • •

◦◦◦◦◦

• • • •

Execution order: top-down, left-right

•, ◦ Statement iterationData flow dependence

• Executed statementData in memory

Polyhedral Model December 15, 2009 6 / 35

Polyhedral Program Transformation Example

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[i] = f(a[N-i])

• • • • •

◦◦◦◦◦

• • • • •

Execution order: top-down, left-right

•, ◦ Statement iterationData flow dependence

• Executed statementData in memory

Polyhedral Model December 15, 2009 6 / 35

Polyhedral Program Transformation Example

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[i] = f(a[N-i])

• • • • •

◦◦◦◦◦

• • • • •

Execution order: top-down, left-right

•, ◦ Statement iterationData flow dependence

• Executed statementData in memory

Polyhedral Model December 15, 2009 6 / 35

Polyhedral Program Transformation Example

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[i] = f(a[N-i])

• • • • •

◦◦◦◦◦

• • • • •

• •

Execution order: top-down, left-right

•, ◦ Statement iterationData flow dependence

• Executed statementData in memory

Polyhedral Model December 15, 2009 6 / 35

Polyhedral Program Transformation Example

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[i] = f(a[N-i])

• • • • •

◦◦◦◦◦

• • • • •

• • •

Execution order: top-down, left-right

•, ◦ Statement iterationData flow dependence

• Executed statementData in memory

Polyhedral Model December 15, 2009 6 / 35

Polyhedral Program Transformation Example

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[i] = f(a[N-i])

• • • • •

◦◦◦◦◦

• • • • •

• • • •

Execution order: top-down, left-right

•, ◦ Statement iterationData flow dependence

• Executed statementData in memory

Polyhedral Model December 15, 2009 6 / 35

Polyhedral Program Transformation Example

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[i] = f(a[N-i])

• • • • •

◦◦◦◦◦

• • • • •

• • • • •

Execution order: top-down, left-right

•, ◦ Statement iterationData flow dependence

• Executed statementData in memory

Polyhedral Model December 15, 2009 6 / 35

Polyhedral Program Transformation Example

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[i] = f(a[N-i])

• • • • •

◦◦◦◦◦

Execution order: top-down, left-right

•, ◦ Statement iterationData flow dependence

• Executed statementData in memory

• • • • •

◦ ◦ ◦ ◦ ◦

Polyhedral Model December 15, 2009 6 / 35

Polyhedral Program Transformation Example

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[i] = f(a[N-i])

• • • • •

◦◦◦◦◦

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[N-i] = f(a[i])

• • • • •

◦ ◦ ◦ ◦ ◦

Polyhedral Model December 15, 2009 6 / 35

Polyhedral Program Transformation Example

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[i] = f(a[N-i])

• • • • •

◦◦◦◦◦

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[N-i] = f(a[i])

• • • • •

◦ ◦ ◦ ◦ ◦

Polyhedral Model December 15, 2009 6 / 35

Polyhedral Program Transformation Example

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[i] = f(a[N-i])

• • • • •

◦◦◦◦◦

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[N-i] = f(a[i])

• • • • •

◦ ◦ ◦ ◦ ◦

for (i = 0; i <= N; ++i) {

a[i] = ...

b[N-i] = f(a[i])

}

Polyhedral Model December 15, 2009 7 / 35

Polyhedral Program Analysis and Transformation Overview

program code

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[i] = f(a[N-i])

Polyhedral Model December 15, 2009 7 / 35

Polyhedral Program Analysis and Transformation Overview

program code

parser

polyhedral model

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[i] = f(a[N-i])

• • • • •

◦◦◦◦◦

Polyhedral Model December 15, 2009 7 / 35

Polyhedral Program Analysis and Transformation Overview

program code

parser

polyhedral model analysis andtransformation

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[i] = f(a[N-i])

• • • • •

◦◦◦◦◦

Polyhedral Model December 15, 2009 7 / 35

Polyhedral Program Analysis and Transformation Overview

program code

parser

polyhedral model

polyhedralscanner

analysis andtransformation

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[i] = f(a[N-i])

for (i = 0; i <= N; ++i) {

a[i] = ...

b[N-i] = f(a[i])

}

• • • • •

◦◦◦◦◦

isl December 15, 2009 8 / 35

Outline

1 Polyhedral Model

2 isl

Introduction and RepresentationParametric Integer Programming

3 CLooG

4 barvinok

5 bernstein

6 Conclusions

isl Introduction and Representation December 15, 2009 9 / 35

Polyhedral Program Analysis and Transformation Overview

program code

parser

polyhedral model

polyhedralscanner

analysis andtransformation

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[i] = f(a[N-i])

for (i = 0; i <= N; ++i) {

a[i] = ...

b[N-i] = f(a[i])

}

• • • • •

◦◦◦◦◦

isl Introduction and Representation December 15, 2009 10 / 35

Representation and Manipulation of Sets of IntegersPossible representations:

double description based◮ PolyLib◮ PPL◮ polymake◮ . . .

Some operations can be performed more efficiently on explicitrepresentation, but

◮ Computing the dual can be costly◮ Double description requires more space

⇒ trade-off

isl Introduction and Representation December 15, 2009 10 / 35

Representation and Manipulation of Sets of IntegersPossible representations:

double description based◮ PolyLib◮ PPL◮ polymake◮ . . .

Some operations can be performed more efficiently on explicitrepresentation, but

◮ Computing the dual can be costly◮ Double description requires more space

⇒ trade-offconstraints based

◮ Omega◮ isl

isl Introduction and Representation December 15, 2009 10 / 35

Representation and Manipulation of Sets of IntegersPossible representations:

double description based◮ PolyLib◮ PPL◮ polymake◮ . . .

Some operations can be performed more efficiently on explicitrepresentation, but

◮ Computing the dual can be costly◮ Double description requires more space

⇒ trade-offconstraints based

◮ Omega◮ isl

number decision diagrams◮ LASH◮ . . .

generating functions

isl Introduction and Representation December 15, 2009 10 / 35

Representation and Manipulation of Sets of IntegersPossible representations:

double description based◮ PolyLib◮ PPL◮ polymake◮ . . .

Some operations can be performed more efficiently on explicitrepresentation, but

◮ Computing the dual can be costly◮ Double description requires more space

⇒ trade-offconstraints based

◮ Omega◮ isl

number decision diagrams◮ LASH◮ . . .

generating functions

isl Introduction and Representation December 15, 2009 11 / 35

isl Overviewisl is an

LGPL

thread-safe

C

library for manipulating integer sets and relations

bounded by affine constraints

involving parameters and

existentially quantified variables

Supported operations include

basic operations such as intersection, union and set difference

integer projection

set coalescing

convex hull, integer affine hull

sampling and scanning using generalized basis reduction

computing the lexicographic minimum of a relation,

isl Introduction and Representation December 15, 2009 12 / 35

Why do we need existentially quantified variables?

Modeling some problemsWhich array elements are accessed in this loop?

for (j = 1; j <= s; ++j)

for (i = 1; i <= 8; ++i)

a[6i+9j-7] = a[6i+9j-7] + 5;

S(s) = {l ∈ Z | ∃i , j ∈ Z : l = 6i + 9j − 7 ∧ 1 ≤ j ≤ s ∧ 1 ≤ i ≤ 8}

isl Introduction and Representation December 15, 2009 12 / 35

Why do we need existentially quantified variables?

Modeling some problemsWhich array elements are accessed in this loop?

for (j = 1; j <= s; ++j)

for (i = 1; i <= 8; ++i)

a[6i+9j-7] = a[6i+9j-7] + 5;

S(s) = {l ∈ Z | ∃i , j ∈ Z : l = 6i + 9j − 7 ∧ 1 ≤ j ≤ s ∧ 1 ≤ i ≤ 8}

Especially integer divisions/remaindersE.g., i % 10 <= 6

i − 10

i

10

≤ 6

isl Introduction and Representation December 15, 2009 12 / 35

Why do we need existentially quantified variables?

Modeling some problemsWhich array elements are accessed in this loop?

for (j = 1; j <= s; ++j)

for (i = 1; i <= 8; ++i)

a[6i+9j-7] = a[6i+9j-7] + 5;

S(s) = {l ∈ Z | ∃i , j ∈ Z : l = 6i + 9j − 7 ∧ 1 ≤ j ≤ s ∧ 1 ≤ i ≤ 8}

Especially integer divisions/remaindersE.g., i % 10 <= 6

i − 10

i

10

≤ 6

i − 10α ≤ 6

with i − 9 ≤ 10α ≤ i

isl Introduction and Representation December 15, 2009 12 / 35

Why do we need existentially quantified variables?

Modeling some problemsWhich array elements are accessed in this loop?

for (j = 1; j <= s; ++j)

for (i = 1; i <= 8; ++i)

a[6i+9j-7] = a[6i+9j-7] + 5;

S(s) = {l ∈ Z | ∃i , j ∈ Z : l = 6i + 9j − 7 ∧ 1 ≤ j ≤ s ∧ 1 ≤ i ≤ 8}

Especially integer divisions/remaindersE.g., i % 10 <= 6

i − 10

i

10

≤ 6

i − 10α ≤ 6

with i − 9 ≤ 10α ≤ i

◮ May appear in original code◮ May be introduced by (PIP-based) dependence analysis

isl Introduction and Representation December 15, 2009 13 / 35

Internal Representation

Polyhedral sets and relations

S(s) = { x ∈ Zd | ∃z ∈ Ze : Ax + Bs + Dz ≥ c }

R(s) = { (x1, x2) ∈ Zd1 × Zd2 | ∃z ∈ Ze : A1x1 + A2x2 + Bs + Dz ≥ c }

“basic” types: “convex” sets and maps (relations)

◮ equality + inequality constraints◮ parameters s◮ (optional) explicit representation of existentially quantified variables as

integer divisions

⇒ useful for aligning dimensions when performing set operations (e.g., setdifference)

⇒ can be computed using PIP⇒ already available if obtained from PIP-based dependence analysis

union types: sets and maps

⇒ (disjoint) unions of basic sets/maps

isl Parametric Integer Programming December 15, 2009 14 / 35

Polyhedral Program Analysis and Transformation Overview

program code

parser

polyhedral model

polyhedralscanner

analysis andtransformation

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[i] = f(a[N-i])

for (i = 0; i <= N; ++i) {

a[i] = ...

b[N-i] = f(a[i])

}

• • • • •

◦◦◦◦◦

isl Parametric Integer Programming December 15, 2009 15 / 35

Lexicographical Order

#define N 5

for (i = 1; i <= N; ++i)

for (j = 1; j <= i; ++j)

a[i][j]=

S = { (i , j) | i ≥ 1 ∧ i ≤ N ∧ j ≥ 1 ∧ j ≤ i }

Execution order: (1,1), (2,1), (2,2), (3,1), (3,2), (3,3), (4,1), (4,2), (4,3),(4,4) (5,1), (5,2), (5,3), (5,4), (5,5)

isl Parametric Integer Programming December 15, 2009 15 / 35

Lexicographical Order

#define N 5

for (i = 1; i <= N; ++i)

for (j = 1; j <= i; ++j)

a[i][j]=

S = { (i , j) | i ≥ 1 ∧ i ≤ N ∧ j ≥ 1 ∧ j ≤ i }

Execution order: (1,1), (2,1), (2,2), (3,1), (3,2), (3,3), (4,1), (4,2), (4,3),(4,4) (5,1), (5,2), (5,3), (5,4), (5,5)

Lexicographical order:

a ≺ b ≡n

i=1

ai < bi ∧i−1∧

j=1

aj = bj

⇒ smaller in first position where vectors differ

isl Parametric Integer Programming December 15, 2009 16 / 35

Parametric Integer Programming

Given a relation R with parameters s

R(s) = { (x1, x2) ∈ Zd1 × Zd2 | ∃z ∈ Ze : A1x1 + A2x2 + Bs + Dz ≥ c }

compute the lexicographic minimum of R:

lexminR = { (x1, x2) ∈ R | ∀x′2 ∈ R(s, x1) : x2 4 x′2 }

isl Parametric Integer Programming December 15, 2009 16 / 35

Parametric Integer Programming

Given a relation R with parameters s

R(s) = { (x1, x2) ∈ Zd1 × Zd2 | ∃z ∈ Ze : A1x1 + A2x2 + Bs + Dz ≥ c }

compute the lexicographic minimum of R:

lexminR = { (x1, x2) ∈ R | ∀x′2 ∈ R(s, x1) : x2 4 x′2 }

Parametric integer programming is useful for

dependence analysis

(enumeration of) integer projections

isl Parametric Integer Programming December 15, 2009 16 / 35

Parametric Integer Programming

Given a relation R with parameters s

R(s) = { (x1, x2) ∈ Zd1 × Zd2 | ∃z ∈ Ze : A1x1 + A2x2 + Bs + Dz ≥ c }

compute the lexicographic minimum of R:

lexminR = { (x1, x2) ∈ R | ∀x′2 ∈ R(s, x1) : x2 4 x′2 }

Parametric integer programming is useful for

dependence analysis

(enumeration of) integer projections

Technique: dual simplex + Gomory cuts

isl Parametric Integer Programming December 15, 2009 17 / 35

Dataflow Analysis

Given a read from an array element, what was the last write to

the same array element before the read?

Simple case: array written through a single access

for (i = 0; i < N; ++i)

for (j = 0; j < N - i; ++j)

a[i+j] = f(a[i+j]);

for (i = 0; i < N; ++i)

Write(a[i]);

isl Parametric Integer Programming December 15, 2009 17 / 35

Dataflow Analysis

Given a read from an array element, what was the last write to

the same array element before the read?

Simple case: array written through a single access

for (i = 0; i < N; ++i)

for (j = 0; j < N - i; ++j)

a[i+j] = f(a[i+j]);

for (i = 0; i < N; ++i)

Write(a[i]);

isl Parametric Integer Programming December 15, 2009 17 / 35

Dataflow Analysis

Given a read from an array element, what was the last write to

the same array element before the read?

Simple case: array written through a single access

for (i = 0; i < N; ++i)

for (j = 0; j < N - i; ++j)

a[i+j] = f(a[i+j]);

for (i = 0; i < N; ++i)

Write(a[i]);

Access relations:A1 = {(i , j) → (i + j) | 0 ≤ i < N ∧ 0 ≤ j < N − i}A2 = {(i) → (i) | 0 ≤ i < N}

isl Parametric Integer Programming December 15, 2009 17 / 35

Dataflow Analysis

Given a read from an array element, what was the last write to

the same array element before the read?

Simple case: array written through a single access

for (i = 0; i < N; ++i)

for (j = 0; j < N - i; ++j)

a[i+j] = f(a[i+j]);

for (i = 0; i < N; ++i)

Write(a[i]);

Access relations:A1 = {(i , j) → (i + j) | 0 ≤ i < N ∧ 0 ≤ j < N − i}A2 = {(i) → (i) | 0 ≤ i < N}

Map to all writes: R ′ = A1−1 ◦ A2 = {(i) → (i ′, i − i ′) | 0 ≤ i ′ ≤ i < N}

isl Parametric Integer Programming December 15, 2009 17 / 35

Dataflow Analysis

Given a read from an array element, what was the last write to

the same array element before the read?

Simple case: array written through a single access

for (i = 0; i < N; ++i)

for (j = 0; j < N - i; ++j)

a[i+j] = f(a[i+j]);

for (i = 0; i < N; ++i)

Write(a[i]);

Access relations:A1 = {(i , j) → (i + j) | 0 ≤ i < N ∧ 0 ≤ j < N − i}A2 = {(i) → (i) | 0 ≤ i < N}

Map to all writes: R ′ = A1−1 ◦ A2 = {(i) → (i ′, i − i ′) | 0 ≤ i ′ ≤ i < N}

Last write: R = lexmaxR ′ = {(i) → (i , 0) | 0 ≤ i < N}

isl Parametric Integer Programming December 15, 2009 17 / 35

Dataflow Analysis

Given a read from an array element, what was the last write to

the same array element before the read?

Simple case: array written through a single access

for (i = 0; i < N; ++i)

for (j = 0; j < N - i; ++j)

a[i+j] = f(a[i+j]);

for (i = 0; i < N; ++i)

Write(a[i]);

Access relations:A1 = {(i , j) → (i + j) | 0 ≤ i < N ∧ 0 ≤ j < N − i}A2 = {(i) → (i) | 0 ≤ i < N}

Map to all writes: R ′ = A1−1 ◦ A2 = {(i) → (i ′, i − i ′) | 0 ≤ i ′ ≤ i < N}

Last write: R = lexmaxR ′ = {(i) → (i , 0) | 0 ≤ i < N}

In general: impose lexicographical order on shared iterators

CLooG December 15, 2009 18 / 35

Outline

1 Polyhedral Model

2 isl

Introduction and RepresentationParametric Integer Programming

3 CLooG

4 barvinok

5 bernstein

6 Conclusions

CLooG December 15, 2009 19 / 35

Polyhedral Program Analysis and Transformation Overview

program code

parser

polyhedral model

polyhedralscanner

analysis andtransformation

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[i] = f(a[N-i])

for (i = 0; i <= N; ++i) {

a[i] = ...

b[N-i] = f(a[i])

}

• • • • •

◦◦◦◦◦

CLooG December 15, 2009 20 / 35

CLooG ExampleCLooG generates code for scanning integer points in polyhedra (iterationdomains)

S1: {(i , j) | 1 ≤ i ≤ n ≤ m ∧ j = i}S2: {(i , j) | 1 ≤ i ≤ n ≤ m ∧ i ≤ j ≤ n}S3: {(i , j) | 1 ≤ i ≤ m ∧ j = n ≤ m}

i

j

CLooG December 15, 2009 20 / 35

CLooG ExampleCLooG generates code for scanning integer points in polyhedra (iterationdomains)

S1: {(i , j) | 1 ≤ i ≤ n ≤ m ∧ j = i}S2: {(i , j) | 1 ≤ i ≤ n ≤ m ∧ i ≤ j ≤ n}S3: {(i , j) | 1 ≤ i ≤ m ∧ j = n ≤ m}

i

j

CLooG December 15, 2009 20 / 35

CLooG ExampleCLooG generates code for scanning integer points in polyhedra (iterationdomains)

S1: {(i , j) | 1 ≤ i ≤ n ≤ m ∧ j = i}S2: {(i , j) | 1 ≤ i ≤ n ≤ m ∧ i ≤ j ≤ n}S3: {(i , j) | 1 ≤ i ≤ m ∧ j = n ≤ m}

i

j

CLooG December 15, 2009 20 / 35

CLooG ExampleCLooG generates code for scanning integer points in polyhedra (iterationdomains)

S1: {(i , j) | 1 ≤ i ≤ n ≤ m ∧ j = i}S2: {(i , j) | 1 ≤ i ≤ n ≤ m ∧ i ≤ j ≤ n}S3: {(i , j) | 1 ≤ i ≤ m ∧ j = n ≤ m}

i

jfor (i=1;i<=m;i++) {

if (i <= n) {

S1(i,i);

}

for (j=i;j<=n;j++) {

S2(i);

}

S3(i,n);

}

CLooG December 15, 2009 20 / 35

CLooG ExampleCLooG generates code for scanning integer points in polyhedra (iterationdomains)

S1: {(i , j) | 1 ≤ i ≤ n ≤ m ∧ j = i}S2: {(i , j) | 1 ≤ i ≤ n ≤ m ∧ i ≤ j ≤ n}S3: {(i , j) | 1 ≤ i ≤ m ∧ j = n ≤ m}

i

jfor (i=1;i<=m;i++) {

if (i <= n) {

S1(i,i);

}

for (j=i;j<=n;j++) {

S2(i);

}

S3(i,n);

}

barvinok December 15, 2009 21 / 35

Outline

1 Polyhedral Model

2 isl

Introduction and RepresentationParametric Integer Programming

3 CLooG

4 barvinok

5 bernstein

6 Conclusions

barvinok December 15, 2009 22 / 35

Polyhedral Program Analysis and Transformation Overview

program code

parser

polyhedral model

polyhedralscanner

analysis andtransformation

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[i] = f(a[N-i])

for (i = 0; i <= N; ++i) {

a[i] = ...

b[N-i] = f(a[i])

}

• • • • •

◦◦◦◦◦

barvinok December 15, 2009 23 / 35

Counting Example 1

How many times is S1 executed ?

for (i=1; i<=N; i++)

for (j=1; j<=i; j++)

S1;

barvinok December 15, 2009 23 / 35

Counting Example 1

How many times is S1 executed ?

for (i=1; i<=N; i++)

for (j=1; j<=i; j++)

S1;

N = 4

i

j(0, 0)

••••

•••

•• •

#{ (i , j) ∈ Z2 | 1 ≤ i ≤ N ∧ 1 ≤ j ≤ i } =N(N + 1)

2

barvinok December 15, 2009 23 / 35

Counting Example 1

How many times is S1 executed ?

for (i=1; i<=N; i++)

for (j=1; j<=i; j++)

S1;

N = 4

i

j(0, 0)

••••

•••

•• •

#{ (i , j) ∈ Z2 | 1 ≤ i ≤ N ∧ 1 ≤ j ≤ i } =N(N + 1)

2

= #

[

i

j

]

∈ Z2 |

1 0−1 00 11 −1

[

i

j

]

0−100

[

N]

+

1010

barvinok December 15, 2009 24 / 35

Counting Example 2

How many times is S1 executed ?

for (i=max(0,N-M); i<=N-M+3; i++)

for (j=0; j<=N-2*i; j++)

S1;

barvinok December 15, 2009 24 / 35

Counting Example 2

How many times is S1 executed ?

for (i=max(0,N-M); i<=N-M+3; i++)

for (j=0; j<=N-2*i; j++)

S1;

Equal to the number of elements in

P( NM ) =

[

i

j

]

∈ Z2 |

1 01 0−1 00 1−2 −1

[

i

j

]

0 01 −1−1 10 0−1 0

[

N

M

]

+

00−300

barvinok December 15, 2009 25 / 35

Counting Example 2

#P( NM ) =

−4N + 8M − 8 if M ≤ N ≤ 2M − 6

MN − 2N − M2 + 6M − 8

if N ≤ M ≤ N + 3 ∧ N ≤ 2M − 6

N2

4 + 34N + 1

2

N2

+ 1 if 0 ≤ N ≤ M ∧ 2M ≤ N + 6

N2

4 − MN − 54N + M2 + 2M + 1

2

N2

+ 1

if M ≤ N ≤ 2M ≤ N + 6

barvinok December 15, 2009 25 / 35

Counting Example 2

#P( NM ) =

−4N + 8M − 8 if M ≤ N ≤ 2M − 6

MN − 2N − M2 + 6M − 8

if N ≤ M ≤ N + 3 ∧ N ≤ 2M − 6

N2

4 + 34N + 1

2

N2

+ 1 if 0 ≤ N ≤ M ∧ 2M ≤ N + 6

N2

4 − MN − 54N + M2 + 2M + 1

2

N2

+ 1

if M ≤ N ≤ 2M ≤ N + 6

Chambers:

N

M

barvinok December 15, 2009 26 / 35

Counting Example 3

How many elements of array a are accessed ?

for (j = 1; j <= p; ++j)

for (i = 1; i <= 8; ++i)

a[6i+9j-7] = a[6i+9j-7] + 5;

barvinok December 15, 2009 26 / 35

Counting Example 3

How many elements of array a are accessed ?

for (j = 1; j <= p; ++j)

for (i = 1; i <= 8; ++i)

a[6i+9j-7] = a[6i+9j-7] + 5;

Equal to the number of elements in

Sp = { l ∈ Z | ∃i , j ∈ Z : l = 6i + 9j − 7 ∧ 1 ≤ j ≤ p ∧ 1 ≤ i ≤ 8 }

barvinok December 15, 2009 26 / 35

Counting Example 3

How many elements of array a are accessed ?

for (j = 1; j <= p; ++j)

for (i = 1; i <= 8; ++i)

a[6i+9j-7] = a[6i+9j-7] + 5;

Equal to the number of elements in

Sp = { l ∈ Z | ∃i , j ∈ Z : l = 6i + 9j − 7 ∧ 1 ≤ j ≤ p ∧ 1 ≤ i ≤ 8 }

#Sp =

{

8 if p = 1

3p + 10 if p ≥ 2

barvinok December 15, 2009 27 / 35

Weighted Counting Example

p = a;

for (i = 0; i < N; ++i)

for (j = i; j < N; ++j) {

p += j * ((j-i)/4);

*p = 0;

}

Can we parallelize this code?

barvinok December 15, 2009 27 / 35

Weighted Counting Example

p = a;

for (i = 0; i < N; ++i)

for (j = i; j < N; ++j) {

p += j * ((j-i)/4);

*p = 0;

}

Can we parallelize this code?

⇒ No, (false) dependency through p

⇒ Compute closed formula for p

p = a+∑

(i ′,j ′)∈S

(i ′,j ′)4(i ,j)

j ′⌊

j ′ − i ′

4

with S = { (i ′, j ′) ∈ Z2 | 0 ≤ i ′ < N ∧ i ′ ≤ j ′ < N }

barvinok December 15, 2009 27 / 35

Weighted Counting Example

p = a;

for (i = 0; i < N; ++i)

for (j = i; j < N; ++j) {

p += j * ((j-i)/4);

*p = 0;

}

Can we parallelize this code?

⇒ No, (false) dependency through p

⇒ Compute closed formula for p

p = a+∑

(i ′,j ′)∈S

(i ′,j ′)4(i ,j)

j ′⌊

j ′ − i ′

4

= a+∑

(i ′,j ′)∈S

i ′<i

j ′⌊

j ′ − i ′

4

+∑

(i ′,j ′)∈S

i ′=i ,j ′≤j

j ′⌊

j ′ − i ′

4

with S = { (i ′, j ′) ∈ Z2 | 0 ≤ i ′ < N ∧ i ′ ≤ j ′ < N }

barvinok December 15, 2009 27 / 35

Weighted Counting Example

p = a;

for (i = 0; i < N; ++i)

for (j = i; j < N; ++j) {

p += j * ((j-i)/4);

*p = 0;

}

Can we parallelize this code?

⇒ No, (false) dependency through p

⇒ Compute closed formula for p

p = a+∑

(i ′,j ′)∈S

(i ′,j ′)4(i ,j)

j ′⌊

j ′ − i ′

4

= a+∑

(i ′,j ′)∈S

i ′<i

j ′⌊

j ′ − i ′

4

+∑

(i ′,j ′)∈S

i ′=i ,j ′≤j

j ′⌊

j ′ − i ′

4

with S = { (i ′, j ′) ∈ Z2 | 0 ≤ i ′ < N ∧ i ′ ≤ j ′ < N }

⇒ weighted counting

barvinok December 15, 2009 27 / 35

Weighted Counting Example

p = a;

for (i = 0; i < N; ++i)

for (j = i; j < N; ++j) {

p += j * ((j-i)/4);

*p = 0;

}

N − 1

i − 1

j ′

i ′

•••

••••

•••••

••••••

Can we parallelize this code?

⇒ No, (false) dependency through p

⇒ Compute closed formula for p

p = a+∑

(i ′,j ′)∈S

(i ′,j ′)4(i ,j)

j ′⌊

j ′ − i ′

4

= a+∑

(i ′,j ′)∈S

i ′<i

j ′⌊

j ′ − i ′

4

+∑

(i ′,j ′)∈S

i ′=i ,j ′≤j

j ′⌊

j ′ − i ′

4

with S = { (i ′, j ′) ∈ Z2 | 0 ≤ i ′ < N ∧ i ′ ≤ j ′ < N }

⇒ weighted counting

barvinok December 15, 2009 28 / 35

Parametric Polytopes and Piecewise Step-polynomials

Parametric polytope:

P(s) : s 7→ {t ∈ Qd | As + Bt + c ≥ 0}

(N, i , j) → { (i ′, j ′) | 0 ≤ i ′ < i∧i ′ ≤ j ′ < N }

N − 1

i − 1

j ′

i ′

•••

••••

•••••

••••••

barvinok December 15, 2009 28 / 35

Parametric Polytopes and Piecewise Step-polynomials

Parametric polytope:

P(s) : s 7→ {t ∈ Qd | As + Bt + c ≥ 0}

(N, i , j) → { (i ′, j ′) | 0 ≤ i ′ < i∧i ′ ≤ j ′ < N }

N − 1

i − 1

j ′

i ′

•••

••••

•••••

••••••

Step-polynomial: q(s, t) ∈ Q [⌊Q[s, t]≤1⌋] j ′⌊

j ′−i ′

4

Piecewise step-polynomial: (s, t) 7→

q1(s, t) if t ∈ P1(s)

. . .

qk(s, t) if t ∈ Pk(s)

barvinok December 15, 2009 28 / 35

Parametric Polytopes and Piecewise Step-polynomials

Parametric polytope:

P(s) : s 7→ {t ∈ Qd | As + Bt + c ≥ 0}

(N, i , j) → { (i ′, j ′) | 0 ≤ i ′ < i∧i ′ ≤ j ′ < N }

N − 1

i − 1

j ′

i ′

•••

••••

•••••

••••••

Step-polynomial: q(s, t) ∈ Q [⌊Q[s, t]≤1⌋] j ′⌊

j ′−i ′

4

Piecewise step-polynomial: (s, t) 7→

q1(s, t) if t ∈ P1(s)

. . .

qk(s, t) if t ∈ Pk(s)

Weighted counting:

Zn → Q : s 7→∑

i

t∈Zd

[t ∈ Pi (s)] qi (s, t)

barvinok December 15, 2009 29 / 35

barvinok Overview

Main functionality: (weighted) counting of elements in polyhedral set

Input:

polyhedral set, or

piecewise step-polynomial

Output:

piecewise step-polynomial c(s)

⇒ number of elements in set⇒ weighted number of elements in each cell

or

generating function C (x) =∑

s c(s) xs

bernstein December 15, 2009 30 / 35

Outline

1 Polyhedral Model

2 isl

Introduction and RepresentationParametric Integer Programming

3 CLooG

4 barvinok

5 bernstein

6 Conclusions

bernstein December 15, 2009 31 / 35

Polyhedral Program Analysis and Transformation Overview

program code

parser

polyhedral model

polyhedralscanner

analysis andtransformation

for (i = 0; i <= N; ++i)

a[i] = ...

for (i = 0; i <= N; ++i)

b[i] = f(a[N-i])

for (i = 0; i <= N; ++i) {

a[i] = ...

b[N-i] = f(a[i])

}

• • • • •

◦◦◦◦◦

bernstein December 15, 2009 32 / 35

bernstein example

for (i = 0; i < N; ++i)

for (j = i; j < N; ++j) {

p = malloc(i * j + i - N + 1);

/* ... */

free(p);

}

How much memory is needed?

bernstein December 15, 2009 32 / 35

bernstein example

for (i = 0; i < N; ++i)

for (j = i; j < N; ++j) {

p = malloc(i * j + i - N + 1);

/* ... */

free(p);

}

How much memory is needed?

m(N) = max(i ,j)∈S

(ij + i − N + 1)

with S = { (i , j) ∈ Z2 | 0 ≤ i < N ∧ i ≤ j < N }

bernstein December 15, 2009 32 / 35

bernstein example

for (i = 0; i < N; ++i)

for (j = i; j < N; ++j) {

p = malloc(i * j + i - N + 1);

/* ... */

free(p);

}

How much memory is needed?

m(N) = max(i ,j)∈S

(ij + i − N + 1)

with S = { (i , j) ∈ Z2 | 0 ≤ i < N ∧ i ≤ j < N }

Typical application: maximal number of live elements

⇒ count the number of live elements at each execution point

⇒ compute upper bound over all execution points

bernstein December 15, 2009 33 / 35

Computing Parametric Upper Bounds

Maximal memory requirements:

u(N) ≥ m(N) = max0≤i<N∧i≤j<N

(ij + i − N + 1)

m(N) can be approximated using Bernstein expansion ⇒ u(N)

Ideally:◮ maximum over◮ integer points

Bernstein expansion gives◮ upper bound over◮ rational points

In practice very accurate

Conclusions December 15, 2009 34 / 35

Outline

1 Polyhedral Model

2 isl

Introduction and RepresentationParametric Integer Programming

3 CLooG

4 barvinok

5 bernstein

6 Conclusions

Conclusions December 15, 2009 35 / 35

ConclusionSeveral tools have been developed within the context of the polyhedralmodel, including,

isl (http://freshmeat.net/projects/isl/)Represents and manipulates sets and relations of integersSupports several operations including parametric integer programmingCLooG (http://www.cloog.org/)Generates code for scanning the elements of polytopesbarvinok (http://freshmeat.net/projects/barvinok/)Counts the number of elements in a polyhedral set, possibly weightedby a step-polynomialbernstein (included in barvinok)Computes bounds on polynomials over parametric polytopes

Conclusions December 15, 2009 35 / 35

ConclusionSeveral tools have been developed within the context of the polyhedralmodel, including,

isl (http://freshmeat.net/projects/isl/)Represents and manipulates sets and relations of integersSupports several operations including parametric integer programmingCLooG (http://www.cloog.org/)Generates code for scanning the elements of polytopesbarvinok (http://freshmeat.net/projects/barvinok/)Counts the number of elements in a polyhedral set, possibly weightedby a step-polynomialbernstein (included in barvinok)Computes bounds on polynomials over parametric polytopes

Future Work: port barvinok to isl; now uses

PolyLib: GPLv2 only, . . .⇒ isl needs chamber decomposition and parametric verticesNTL: not thread-safe, C++⇒ isl needs LLL

Recommended