19.11.2014 Views

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

The Fortress Language Specification - CiteSeerX

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

T R, ⊞[ ] body = body<br />

T R, ⊞[ x ← g,gs ]body = g.generateR, ⊞(fn () ⇒ T R, ⊞[gs]body)<br />

T R, ⊞[ p , gs ] body = if p then (T R, ⊞[gs]body) else Identity⊞ end<br />

Figure 32.1: Naive and simple desugaring of generator lists using only the generate method.<br />

value object BlockedRange(lo: Z64,hi: Z64, b: Z64) extends GeneratorZ64<br />

size : Z64 = hi − lo + 1<br />

generateR extends Monoid R, ⊕,opr ⊕(body : Z64 → R) : R =<br />

if size ≤ max(b, 1) then<br />

r : R = Identity⊕<br />

i : Z64 = lo<br />

if i ≤ hi then<br />

label done do<br />

while true do<br />

r := r ⊕ body(i)<br />

if i ≥ hi then exit done with () end<br />

i += 1<br />

end<br />

end<br />

end<br />

r<br />

else<br />

mid = ⌈lo/2⌉ + ⌊hi/2⌋<br />

BlockedRange(lo,mid, b).generate(body)⊕<br />

BlockedRange(mid + 1,hi, b).generate(body)<br />

end<br />

end<br />

This example generates the integers between lo and hi inclusive. It does this using recursive subdivision. Recursive<br />

subdivision is the recommended technique for exposing large amounts of parallelism in a <strong>Fortress</strong> program because<br />

it adjusts easily to varying machine sizes and can be dynamically load balanced. In this example we divide the range<br />

in half if it is larger than the block size b ; these two halves are computed in parallel (recall that the arguments to an<br />

operator are evaluated in parallel). If the range is smaller than b , then it is enumerated serially using a while loop,<br />

accumulating the result r as it goes.<br />

<strong>The</strong> remainder of this section describes in detail the desugaring of generator lists and expressions with generators into<br />

invocations of the generate and join methods of the generators in the generator list. It then outlines how method<br />

overloading may be used to specialize the behavior of particular combinations of generators and reductions.<br />

32.8.1 Simple Desugaring of Expressions with Generators<br />

Each expression with generators is desugared into the following general form:<br />

wrapper(fn () ⇒ T R, ⊞[gs ]body)<br />

where the desugaring must provide appropriate instantiations of wrapper , body , and the reduction static parameters,<br />

R and ⊞ . A simple and easily-understood desugaring “ T R, ⊞[gs ]body ” for generator lists is shown in Figure 32.1.<br />

<strong>The</strong> desugaring of GeneratorList takes three parameters: a block of static parameters, R and ⊞ , the actual generator<br />

list (which we enclose in square brackets), and the body expression which should be used. Here and in subsequent<br />

desugarings, v in v ← g can stand either for a single variable or for a tuple of variables. We convert the<br />

222

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!