13
November 30, 2006 1 Pseudo-dynamic C metaprogramming Using strategic term rewriting and partial evaluation 7 th Stratego User Days Wouter Caarls Quantitative Imaging Group, Department of Imaging Science and Technology, Faculty of Applied Sciences

November 30, 2006 1 Pseudo-dynamic C metaprogramming Using strategic term rewriting and partial evaluation 7 th Stratego User Days Wouter Caarls Quantitative

Embed Size (px)

Citation preview

November 30, 2006

1

Pseudo-dynamic C metaprogrammingUsing strategic term rewriting and partial evaluation

7th Stratego User Days

Wouter Caarls

Quantitative Imaging Group,Department of Imaging Science and Technology, Faculty of Applied Sciences

November 30, 2006 2

Motivation SIMDVLIW

SS

SIMDVLIW

SS

?

• Parallel• Heterogeneous• Architecture independent

November 30, 2006 3

Sensor

SIMD

SS

ProgrammingTask parallelism

• Stream computing

STREAM image, gx, g, hess, extrema;double sigma = 1;

capture(image);gaussx(image, &sigma, gx);gaussy(gx, &sigma, g);hessian(image, hess);findextrema(g, extrema);filterextrema(extrema, hess, extrema);

cap

gx

gy hess

find

filt

November 30, 2006 4

ProgrammingData parallelism

• (Algorithmic) skeletons

+ =

+ =

Skeleton Kernel Operation

November 30, 2006 5

Skeleton instantiationXTC target language (for SIMD)

NeighbourhoodToPixelOp()erodex(in stream int i[0][-2..2], out stream int *o){ int x, val=0;

for (x=-2; x <= 2; x++) if (i[0][x] < val) val = i[0][x];

*o = val;}

if (task_type==ERODEX){ lmem arg0, arg1;

while (ReadLine(&arg0)) { reg x; lmem val=0;

for (x=-2; x <= 2; x++) (arg0[x] < val)?arg0[x]:val;

arg1 = val; WriteLine(&arg1); }}

• Program generation• Pattern substitution• Analysis & transformation

November 30, 2006 6

Skeleton instantiation language

• Extend C to a dynamic metaprogramming language• Call library transformation functions for simple pattern

substitutions• Embed Stratego for more involved transformations

? Target does not support dynamic metaprogramming! Require all metaprogramming to depend only on compile-

time values, and partially evaluate

TargetMetaTargetCC evaluatePartially eInstantiat

November 30, 2006 7

Dynamic C metaprogramming

• Representation• code datatype

• Construction• ` quotation• $ antiquotation

• Running• @ evaluation

int x = 3;code c = `x * $x`;x = 4;printf("x * $x = %d\n", @c);>> X * $x = 12

code myint = `int`;code myx = `x`;@myint @myx = 5;printf(“x = %d\n”, x);>> x = 5

November 30, 2006 8

Calling transformations

• The only operations defined on code are assignment (includes parameter passing) and evaluation

• All code manipulation is done by calling transformations

int equals(code a, code b);if (equals(arguments[0].datatype, arguments[1].datatype)) …

code replace(code pattern, code replacement, code term);body = replace(`*o`, `arg1`, body);body = replace(`$@(arguments[0].id)[0][PH1]`, `arg0[PH1]`, body);

November 30, 2006 9

Defining transformations

• All transformations are done using Stratego• Stratego transformations can be embedded

stratego int contains(code subterm, code term){ imports simple-traversal

strategies main = <oncetd(?subterm)>(term) < !|[ 1 ]| + !|[ 0 ]|}

November 30, 2006 10

Partial evaluation by interpretation

double n, x=1;int ii, iterations=3;

scanf(“%lf”, &n);

for (ii=0; ii < iterations; ii++) x = (x + n/x)/2;

printf(“sqrt(%f) = %f\n”, n, x);

double n;double x;int ii;int iterations;x = 1;iterations = 3;scanf(“%lf”, &n);ii = 0;x = (1 + n/1)/2;ii = 1;x = (x + n/x)/2;ii = 2;x = (x + n/x)/2;ii = 3;printf(“sqrt(%f) = %f\n”, n, x);

double n

double x

int ii

int iterations

Symbol table

Input Output

?

1

?

1

?

3

?

1

0

3

?

?

0

3

?

?

1

3

?

?

2

3

?

?

3

3

November 30, 2006 11

Compilation flow

November 30, 2006 12

Implementation issues

• “static” code is defined by the fidelity of the partial evaluator

• Code fragments are dynamically scoped (use lambda library function to capture variables)

• Single translation unit (first preprocessed)• Grammar is based on c-tools• Strategies and rules in Stratego functions may

clash• Identifying metavariables by name is

unsatisfactory

November 30, 2006 13

Contact details

SmartCam Website

http://www.qi.tnw.tudelft.nl/~wcaarls/smartcam

E-mail

mailto:[email protected]