November 30, 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, Motivation SIMD VLIW SS SIMD VLIW SS ? Parallel Heterogeneous Architecture independent
November 30, Sensor SIMD SS Programming Task 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 gyhess find filt
November 30, Programming Data parallelism (Algorithmic) skeletons += += SkeletonKernelOperation
November 30, Skeleton instantiation XTC 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, 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
November 30, Dynamic C metaprogramming Representation code datatype Construction ` quotation $ antiquotation evaluation int x = 3; code c = `x * $x`; x = 4; printf("x * $x = >> X * $x = 12 code myint = `int`; code myx = 5; printf(“x = %d\n”, x); >> x = 5
November 30, 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 = `arg0[PH1]`, body);
November 30, 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 = (term) < !|[ 1 ]| + !|[ 0 ]| }
November 30, 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); doublen doublex intii intiterations Symbol table InputOutput ?1?1 ?1?3?1?3 ?103?103 ??03??03 ??13??13 ??23??23 ??33??33
November 30, Compilation flow
November 30, 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, Contact details SmartCam Website
November 30, Example skeletons Pixel Neighbourhood Recursive neighbourhood Stack Filter Associative reduction
November 30, SmartCam Design Framework Skeletal App Kernels MainTrace Dissect Instan- tiate Simulate Skele- tons Skele- tons Opera- tions Opera- tions Bench- marks Bench- marks Architec- ture Perfor- mance Simulate Instan- tiate Arch. template