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.

Several common mathematical operators are defined to be reduction operators in the <strong>Fortress</strong> standard libraries. <strong>The</strong>se<br />

include + , ·, ∧ , ∨ , and ∨ . If a type T extends GroupT, +, −,id (see Section 37.4 for details about the Group<br />

trait) then reduction expressions of the form:<br />

x −= y<br />

are transformed into:<br />

x += x.id − y<br />

Note that since there are no guarantees on the order of execution of loop iterations, there are also no guarantees on the<br />

order of reduction.<br />

<strong>The</strong> semantics of reductions enables implementation strategies such as OpenMP [22]: A reduction variable l is assigned<br />

l.id at the beginning of each iteration. <strong>The</strong> original variable value may be read ahead of time, resulting in the<br />

loss of parallel updates to the variable which occur in other threads while the loop is running. Note that because this<br />

implementation strategy does not read reduction variables in the loop body, the actual implementation of reduction<br />

may vary substantially from the execution.<br />

In the following example, sum is a reduction variable:<br />

arraySumnat x(a : R64[x]) : R64 = do<br />

sum : R64 := 0<br />

for i ← a.indices do<br />

sum := sum + a i<br />

end<br />

sum<br />

end<br />

13.16 Ranges<br />

Syntax:<br />

Range ::= [Expr] : [Expr][ : [Expr]]<br />

| Expr # Expr<br />

A range expression is used to create a special kind of Generator for a set of integers, called a Range, useful for<br />

indexing an array or controlling a for loop. Generators in general are discussed further in Section 13.17.<br />

An explicit range is self-contained and completely describes a set of integers. Assume that a, b, and c are expressions<br />

that produce integer values.<br />

• <strong>The</strong> range a :b is the set of n = max(0, b − a + 1) integers {a, a + 1, a + 2, . . .,b − 2, b − 1, b}. This is a<br />

nonstrided range. If a and b are both static expressions (described in Section 13.27), then it is a static range of<br />

type StaticRangea, n, 1 and therefore also a range of static size) of type RangeOfStaticSizen .<br />

• <strong>The</strong> range a :b:c is the set of n = max ( 0, ⌊ ⌋) ⌊<br />

b−a+c<br />

c integers {a, a + c, a + 2c, . . .,a + b−a<br />

⌋<br />

c c}, unless c is<br />

zero, in which case it throws an exception. (If c is a static expression, then it is a static error if c is zero.) This is<br />

a strided range. If a , b , and c are all static expressions, then it is a static range of type StaticRangea, n, c<br />

and therefore also a range of static size) of type RangeOfStaticSizen .<br />

• <strong>The</strong> range a #n is the set of n integers {a, a+1, a+2, . . ., a+n−3, a+n−2, a+n−1}, unless n is negative,<br />

in which case it throws an exception. (If n is a static expression, then it is a static error if n is negative.) This is<br />

a nonstrided range. If a and n are both static expressions, then it is a static range of type StaticRangea, n, 1<br />

If b is a static expression, then it is a range of static size of type RangeOfStaticSizen , even if a is not a<br />

static expression.<br />

101

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

Saved successfully!

Ooh no, something went wrong!