09.07.2015 Views

Fortress Programming Language Tutorial.pdf - Free

Fortress Programming Language Tutorial.pdf - Free

Fortress Programming Language Tutorial.pdf - Free

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006<strong>Fortress</strong>: “To Do for FortranWhat Java TM Did for C”Great ideas from the Java TM programming language:• Catch “stupid mistakes”> Array bounds and null pointer checking> Automatic storage management• Platform independence• Platform-independent multithreading• Dynamic compilation• Make programmers more productive© 2006 Sun Microsystems, Inc. All rights reserved.4


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Goal: Science-Centered Computation• Program structureshould reflect thescience• Not FLOPS• Not communicationstructure© 2006 Sun Microsystems, Inc. All rights reserved.5


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Introduction<strong>Language</strong> OverviewBasics of ParallelismComponents and APIsDefining Mathematical OperatorsPolymorphism and Type InferenceParallelism: Generators and ReducersContracts, Properties, and TestingSummary© 2006 Sun Microsystems, Inc. All rights reserved.11


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Unicode and Twiki Identifier Notation• Regular: a zip trickOrTreat foobara zip trickOrTreat foobar• Formatted:a3 _a a_ a_vec _a_hat a_max foo_bara 3 a a a a a max foo• Greek letters: alpha beta GAMMA DELTAα β Γ Δ• Unicode names: HEBREW_ALEFא • Blackboard font:RR QQ NN ZZ ZZ64 RR_starR Q N Z Z64R *© 2006 Sun Microsystems, Inc. All rights reserved.14


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Unicode and Twiki Literal Notation• Boolean: true false• String:"Hello, world!"“Hello, world!”• Numbers: 1234 ffff0000_161234 ffff0000 166.02 TIMES 10^236.02×10 2312345678901234567890645236436352© 2006 Sun Microsystems, Inc. All rights reserved.15


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Numeric Data Types• Integers Z: 23 0 -152453629162521266(signed “big integers” of any size)• Naturals N: 23 17 15245362162521266(unsigned “big integers” of any size)• Rational Q: 13 5/7 -999/1001• Real R, complex C (these include Z and N and Q)• Fixed-size integers: Z8 Z16 Z32 Z64 Z128Z256 Z 512 ... and N8 N16 N 32 ...• Floating-point: R32 R64 R128 R256R 512 ... and C64 C128 C256 ...© 2006 Sun Microsystems, Inc. All rights reserved.16


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Units and Dimensions• Units: m_ kg_ s_ micro_s_ MW_ ns_m kg s s MW ns• Dimensions: Length Mass Time Forcem: RR64 Mass = 3 kg__v: RR64[3] Velocity_p: RR64[3] Momentum_p := m _v(* Project v onto p *)_v := _v (_v DOT _p)/(_p DOT _p)_v := _p (_v DOT _p)/(_p DOT _p)_v := _p (_v DOT _p)/(_v DOT _v)© 2006 Sun Microsystems, Inc. All rights reserved.m: R64Mass = 3 kgv : R 64[3] Velocityp: R 64[3] Momentump := m v(* Project v onto p *)v := v v⋅pp⋅pv := p v⋅pp⋅pv := p v⋅pv⋅v17


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Expressions and Statements• Everything is an expression• () is the void value• Statements are void-typed expressions:while, for, assignment, and binding• Some “statements” may have non-() values:if, do, atomic, try, case,typecase, dispatch, spawn© 2006 Sun Microsystems, Inc. All rights reserved.18


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Examples of “Statements”• if x ≥ 0 then x else -x end• for k←1#10 do a[k] := k! end• while n


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Aggregate Expressions• Set, array, map, and list constants{ 2, 3, 5, 7 }[ “cat” ↦ “dog”, “mouse” ↦ “cat” ]⟨ 0, 1, 1, 2, 3, 5, 8, 13 ⟩• Set, array, map, and list comprehensions{ x 2 | x ← primes }[ x 2 ↦ x 3 | x ← fibs, x < 1000 ]⟨ x(x+1)/2 | x ← 1#100 ⟩• Array pasting[ 1 00 A ][ 1 00 A]© 2006 Sun Microsystems, Inc. All rights reserved.21


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Binding, Assignment, Generation• Binding: v = eMust be a non-final statement within a block• Assignment:v := e• Generation: v ← eUsed in loops, comprehensions, reductions• The form v = e is also used for equality tests and forkeyword arguments; context mattersif n = 3 thenp = pixel(red=ff 16,green=33 16,blue=cc 16)drawPixel(p, (x=y), x = 27, y = 19)end© 2006 Sun Microsystems, Inc. All rights reserved.23


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Limited Whitespace Sensitivity• Subscripting: a[m n]• Scalar times vector: a [m n]• Fractions:v = 1 / 2 cos xs = 1/2 g t 2• Vertical bars: { |x| | x←1:20 }• Conflicting cues are forbidden:a+b / c+d (* error *)a + b / c + d (* okay *)a + b/c + d (* best *)© 2006 Sun Microsystems, Inc. All rights reserved.24


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Type System: Objects and Traits• Traits: like interfaces, but may contain code> Based on work by Schärli, Ducasse, Nierstrasz, Black, et al.• Multiple inheritance of code (but not fields)> Objects with fields are the leaves of the hierarchy• Multiple inheritance of contracts and tests> Automated unit testing• Traits and methods may be parameterized> Parameters may be types or compile-time constants• Primitive types are first-class> Booleans, integers, floats, characters are all objects© 2006 Sun Microsystems, Inc. All rights reserved.25


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006trait Booleanextends BooleanAlgebra⟦Boolean,,,,⊻,false,true⟧comprises { true, false }opr (self, other: Boolean): Booleanopr (self, other: Boolean): Booleanopr (self): Booleanopr ⊻(self, other: Boolean): Booleanendobject true extends Booleanopr (self, other: Boolean) = otheropr (self, other: Boolean) = selfopr (self) = falseopr ⊻(self, other: Boolean) = otherendobject false extends Booleanopr (self, other: Boolean) = selfopr (self, other: Boolean) = otheropr (self) = trueopr ⊻(self, other: Boolean) = otherend© 2006 Sun Microsystems, Inc. All rights reserved.26


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Parametric Objectsobject Cart(re: R, im: R) extends Copr (self, other: Cart): Cart =Cart(self.re + other.re, self.im + other.im)opr (self): Cart =Cart(-self.re, -self.im)opr (self, other: Cart): Cart =Cart(self.re - other.re, self.im – other.im)opr ·(self, other: Cart): Cart =Cart(self.re · other.re – self.im · other.im,self.re · other.im + self.im · other.re)opr |self| : R = √((self.re) 2 + (self.im) 2 )...end© 2006 Sun Microsystems, Inc. All rights reserved.27


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Methods and Fields• Methods are defined within traits or objects; fields in objectsobject BankAccount(var balance:N)deposit(amount:N) = doself.balance += amountgenerateReceipt(amount, balance)endendmyAccount: BankAccount = BankAccount(43)myReceipt = myAccount.deposit(19)print myAccount.balance© 2006 Sun Microsystems, Inc. All rights reserved.28


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Functions• Functions are defined at top level or within blockstriple(x:R):R = 3 xbogglify(n:R): = Rif n > 3 thenboggle(x:R) = triple(x+1)boggle(47 n + 1) – boggle(n)elsetriple nend© 2006 Sun Microsystems, Inc. All rights reserved.29


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Simple Example: NAS CG Kernel (ASCII)conjGrad(A: Matrix[\Float\], x: Vector[\Float\]):(Vector[\Float\], Float) = docgit_max = 25z: Vector[\Float\] = 0r: Vector[\Float\] = xp: Vector[\Float\] = rrho: Float = r^T rfor j


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Simple Example: NAS CG Kernel (ASCII)conjGrad[\Elt extends Number, nat N,Mat extends Matrix[\Elt,N BY N\],Vec extends Vector[\Elt,N\]\](A: Mat, x: Vec): (Vec, Elt) = docgit_max = 25z: Vec = 0r: Vec = xp: Vec = rrho: Elt = r^T rfor j


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Simple Example: NAS CG Kernel (Unicode)conjGrad⟦Elt extends Number, nat N,Mat extends Matrix⟦Elt,N×N⟧,Vec extends Vector⟦Elt,N⟧⟧(A: Mat, x: Vec): (Vec, Elt) = docgit_max = 25z: Vec = 0r: Vec = xp: Vec = rρ: Elt = r^T rfor j ← seq(1:cgit_max) doq = A pα = ρ / p^T qz := z + α pr := r - α qρ₀ = ρρ := r^T rβ = ρ / ρ₀p := r + β pend(z, ‖x - A z‖)endThis would be considered entirelyequivalent to the previousversion. You might think of this asan abbreviated form of the ASCIIversion, or you might think of theASCII version as a way toconveniently enter this version ona standard keyboard.© 2006 Sun Microsystems, Inc. All rights reserved.32


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Simple Example: NAS CG KernelconjGrad 〚Elt extends Number, nat N, 〛Mat extends Matrix〚Elt, N ×N 〛 ,Vec extends Vector 〚Elt, N〛〚 〛 A: Mat, x : Vec:Vec, Eltcgit max= 25z : Vec = 0r : Vec = xp:Vec = r:Elt = r T rfor j seq1:cgit max doq = A p =p T qz := z pr := r− q 0= := r T r = 0p := r pend z , ⋳x−A z⋳It's not new or surprising that codewritten in a programming languagemight be displayed in a conventionalmath-like format. The point of thisexample is how similar the code is tothe math notation: the gap betweenthe two syntaxes is relatively small.We want to see what will happen ifa principal goal of a new languagedesign is to minimize this gap.© 2006 Sun Microsystems, Inc. All rights reserved.33


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Comparison: NAS NPB 1 Specificationz = 0r = x = r T rp = rDO i= 1, 25q = A p = / p T qz = z p 0= r = r− q = r T r = / 0p = r pENDDOcompute residual norm explicitly: ⋳r⋳=⋳x−A z⋳z : Vec = 0r : Vec = xp: Vec = r: Elt = r T rfor j seq1:cgit max doq = A p =p T qz := z pr := r− q 0= := r T r = 0p := r pend z , ⋳x−A z⋳© 2006 Sun Microsystems, Inc. All rights reserved.34


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Comparison: NAS NPB 2.3 Serial Codedo j=1,naa+1do j=1,lastcol-firstcol+1q(j) = 0.0d0w(j) = 0.0d0z(j) = 0.0d0enddor(j) = x(j)sum = 0.0d0p(j) = r(j)do j=1,lastcol-firstcol+1w(j) = 0.0d0sum = sum + p(j)*q(j)enddoenddosum = 0.0d0d = sumdo j=1,lastcol-firstcol+1alpha = rho / dsum = sum + r(j)*r(j)rho0 = rhoenddodo j=1,lastcol-firstcol+1rho = sumz(j) = z(j) + alpha*p(j)do cgit = 1,cgitmaxr(j) = r(j) - alpha*q(j)do j=1,lastrow-firstrow+1enddosum = 0.d0sum = 0.0d0do k=rowstr(j),rowstr(j+1)-1 do j=1,lastcol-firstcol+1sum = sum + a(k)*p(colidx(k)) sum = sum + r(j)*r(j)enddoenddow(j) = sumrho = sumenddobeta = rho / rho0do j=1,lastcol-firstcol+1do j=1,lastcol-firstcol+1q(j) = w(j)p(j) = r(j) + beta*p(j)enddoenddoenddodo j=1,lastrow-firstrow+1sum = 0.d0do k=rowstr(j),rowstr(j+1)-1sum = sum + a(k)*z(colidx(k))enddow(j) = sumenddodo j=1,lastcol-firstcol+1r(j) = w(j)enddosum = 0.0d0do j=1,lastcol-firstcol+1d = x(j) - r(j)sum = sum + d*denddod = sumrnorm = sqrt( d )© 2006 Sun Microsystems, Inc. All rights reserved.35


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Introduction<strong>Language</strong> OverviewBasics of ParallelismComponents and APIsDefining Mathematical OperatorsPolymorphism and Type InferenceParallelism: Generators and ReducersContracts, Properties, and TestingSummary© 2006 Sun Microsystems, Inc. All rights reserved.36


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Parallelism Is Not a Feature!• Parallel programming is not a goal,but a pragmatic compromise.• It would be a lot easier to program a singleprocessor chip running at 1 PHz than a millionprocessors running at 20 GHz.> We don't know how to build a 1 Phz processor.> Even if we did, someone would still want to strapa bunch of them together!• Parallel programming is difficult and error-prone.(This is not a property of machines, but of people.)© 2006 Sun Microsystems, Inc. All rights reserved.37


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Should Parallelism Be the Default?• “Loop” can be a misleading term> A set of executions of a parameterized block of code> Whether to order or parallelize those executionsshould be a separate question> Maybe you should have to ask for sequential execution!• <strong>Fortress</strong> “loops” are parallel by default> This is actually a library convention about generators© 2006 Sun Microsystems, Inc. All rights reserved.38


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006In <strong>Fortress</strong>, Parallelism Is the Defaultfor i←1:m, j←1:n doa[i,j] := b[i] c[j]endfor i←seq(1:m) dofor j←seq(1:n) doprint a[i,j]endendfor (i,j)←a.indices doa[i,j] := b[i] c[j]end1:n is a generatorfor (i,j)←a.indices.rowMajor doprint a[i,j]endseq(1:m) is a sequential generatora.indices is a generator forthe indices of the array aa.indices.rowMajor is asequential generator of indices© 2006 Sun Microsystems, Inc. All rights reserved.39


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Primitive Constructs for Parallelism• Iterations of a for loopfor x ← 1#1000 doa[x] := xend• Tuples(a1, a2, a3) = (e1, e2, e3)f(e1, e2)• Spawned threadst1 = spawn do e1 endt2 = spawn do e2 enda1 = t1.value()a2 = t2.value()Give rise toimplicit threadsGive rise toexplicit spawnedthreads© 2006 Sun Microsystems, Inc. All rights reserved.40


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Array Types• May include bounds, or leave them optionala : RR64[xSize, ySize, zSize]M : Complex⟦RR64⟧[ 32, 32 ]space : Vector⟦RR64,6⟧[:,:,:]• Bounds are specified using nat type parameters:conjGrad⟦Elt extends Number, nat N,Mat extends Matrix⟦Elt,N×N⟧,Vec extends Vector⟦Elt,N⟧⟧(A: Mat, x: Vec): (Vec, Elt)• Both Matrix and Vector implement Array© 2006 Sun Microsystems, Inc. All rights reserved.41


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Constructing Arrays• Construct using an aggregate constant:identity = [ 1 00 1 ]• Or a comprehension:a = [(x, y, 1) ↦ 0.0 | x ← 1 : xSize ,y ← 1 : ySize(1, y, z) ↦ 0.0 | y ← 1 : ySize ,z ← 2 : zSize(x, 1, z) ↦ 0.0 | x ← 2 : xSize ,z ← 2 : zSize(x, y, z) ↦ x + y · z | x ← 2 : xSize ,y ← 2 : ySize ,z ← 2 : zSize ]© 2006 Sun Microsystems, Inc. All rights reserved.42


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Indexing and Assignment• Specified by the trait Indexable:trait Indexable⟦Self extends Indexable⟦Self,E,I⟧,E extends Object, I extends Object⟧extends Objectopr [ i : I ] : Eopr [ i : I ]:=( e : E ) : ()endtrait Array⟦E extends Object, I extends ArrayIndex⟧extends Indexable⟦ Array⟦E, I⟧, E, I ⟧...end• The type notation T[a,b] is simply shorthand forArray⟦T,(a,b)⟧© 2006 Sun Microsystems, Inc. All rights reserved.43


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Generators• Generators (defined by libraries) manage parallelismand the assignment of threads to processors• Aggregates> Lists ⟨1,2,4,3,4⟩ and vectors [1 2 4 3 4]> Sets {1,2,3,4} and multisets {|1,2,3,4,4|}> Arrays (including multidimensional)• Ranges 1:10 and 1:99:2 and 0#50• Index sets a.indices and a.indices.rowMajor• Index-value sets ht.keyValuePairs© 2006 Sun Microsystems, Inc. All rights reserved.44


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Local Variables, Reduction Variables• Variables unassigned in a loop body are local• Variables accumulated in a loop body but not readare reduction variablesmeanVar⟦E extends Number, I extends ArrayIndex⟧(a : E[I]): (E,E) = don : E := 0sum : E := 0sumsq : E := 0for i ← a.indices don += 1t = a[i]sum += tsumsq += t tend(sum/n, (sumsq – sum sum)/n )end© 2006 Sun Microsystems, Inc. All rights reserved.45


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Atomic Blocks• Variables mutated in a loop body, but not reduced,must be accessed within an atomic block.histogram⟦nat lo, nat sz⟧(a: A[#,#]): Int[lo#sz] =do hist : Int[lo#sz] := 0for i,j ← a.indices doatomic dohist[a[i,j]] += 1endendhistend© 2006 Sun Microsystems, Inc. All rights reserved.46


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Introduction<strong>Language</strong> OverviewBasics of ParallelismComponents and APIsDefining Mathematical OperatorsPolymorphism and Type InferenceParallelism: Generators and ReducersContracts, Properties, and TestingSummary© 2006 Sun Microsystems, Inc. All rights reserved.47


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Replaceable Components• Avoid a monolithic “Standard Library”• Replaceable components with version control• Encourage alternate implementations> Performance choices> Test them against each other• Encourage experimentation> Framework for alternate language designs© 2006 Sun Microsystems, Inc. All rights reserved.48


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Encapsulated Upgradable Components• The stability of static linking• The sharing and upgradability of dynamic linking© 2006 Sun Microsystems, Inc. All rights reserved.49


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Desired Properties• Installation never blocked by existing components• Execution without signaling a component error• Upgrade without affecting other applications• No unnecessary copies© 2006 Sun Microsystems, Inc. All rights reserved.50


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Hello WorldTypes checkedagainst APIcomponent Helloimport print from IOexport Executablerun(args: String...) = print “Hello world”endapi IOprint: String → ()endapi Executablerun(args: String...) → ()endHello imports thisHello exports this© 2006 Sun Microsystems, Inc. All rights reserved.51


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006APIs• APIs are the “interfaces” of components.• APIs consist only of declarations, not definitions.• An API imports other APIs it uses.• Each API in the world has a distinct name.© 2006 Sun Microsystems, Inc. All rights reserved.52


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Components• Components are immutable• Simple components are units of compilation> Typically the size of small Java packages• Compound components are produced bycombining components> Through linking> Through upgrade• Components import and export APIs© 2006 Sun Microsystems, Inc. All rights reserved.53


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Simple Components<strong>Fortress</strong>.IO<strong>Fortress</strong>.SparseIronCrypto IOSparseMatrix IronCrypto<strong>Fortress</strong>.Matrix<strong>Fortress</strong>.IO© 2006 Sun Microsystems, Inc. All rights reserved.54


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Compound Components<strong>Fortress</strong>.IO<strong>Fortress</strong>.SparseSparse<strong>Fortress</strong>.IO<strong>Fortress</strong>.SparseIOSparseMatrix<strong>Fortress</strong>.Matrix<strong>Fortress</strong>.IO© 2006 Sun Microsystems, Inc. All rights reserved.<strong>Fortress</strong>.Matrix55


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006ExecutablePhysicalSimulationSolveSystemSparse<strong>Fortress</strong>.IO<strong>Fortress</strong>.Sparse<strong>Fortress</strong>.IO<strong>Fortress</strong>.SparseMatrixSolverIOSparseMatrix<strong>Fortress</strong>.IO<strong>Fortress</strong>.Matrix<strong>Fortress</strong>.IO<strong>Fortress</strong>.Matrix© 2006 Sun Microsystems, Inc. All rights reserved.56


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006ExecutablePhysicalSimulationSolveSystemSparse<strong>Fortress</strong>.IO<strong>Fortress</strong>.Sparse<strong>Fortress</strong>.IO<strong>Fortress</strong>.SparseCoolSolverIOSparseMatrix<strong>Fortress</strong>.Matrix<strong>Fortress</strong>.IO<strong>Fortress</strong>.Matrix© 2006 Sun Microsystems, Inc. All rights reserved.57


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Sharing: <strong>Fortress</strong>es• Components are not manipulated directly; theyare stored in fortresses.• <strong>Fortress</strong>es are persistent databases mappingnames to components and APIs.• Typically, a single machine includes a singlefortress.© 2006 Sun Microsystems, Inc. All rights reserved.58


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Efficient Implementation• Because components are immutable, they can beshared at will.• Components not directly reachable can bereferred to by other compound components.• Reclamation of unused components can behandled via conventional garbage collection.© 2006 Sun Microsystems, Inc. All rights reserved.59


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Introduction<strong>Language</strong> OverviewBasics of ParallelismComponents and APIsDefining Mathematical OperatorsPolymorphism and Type InferenceParallelism: Generators and ReducersContracts, Properties, and TestingSummary© 2006 Sun Microsystems, Inc. All rights reserved.60


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006What Syntax Is Actually Wired in?• Parentheses ( ) for grouping• Comma , to separate expressions in tuples• Semicolon ; to separate statements on a line• Dot . for field and method selection• Juxtaposition is a binary operator• Any other operator can be infix, prefix, and/or postfix• Many sets of brackets• Conservative, traditional rules of precedence> A dag, not always transitive (examples: A+B>C is okay;so is B>CD>E; but A+BC needs parentheses)© 2006 Sun Microsystems, Inc. All rights reserved.61


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Libraries Define . . .• Which operators have infix, prefix, postfix definitions,and what types they apply to:opr -(m: Z64,n: Z64) = m.subtract(n)opr -(m: Z64) = m.negate()opr (n: Z64)! =© 2006 Sun Microsystems, Inc. All rights reserved.if n=0 then 1 else n·(n-1)! end• Whether a juxtaposition is meaningful:opr juxtaposition(m: Z64,n: Z64) = m.times(n)• What bracketing operators actually mean:opr x:Number = ceiling(x)opr |x:Number| = if x


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006But Wasn’t Operator Overloadinga Disaster in C++ ?• Yes, it was> Not enough operators to go around> Failure to stick to traditional meanings• We have also been tempted and had to resist• We see benefits in using notations for programmingthat are also used for specification© 2006 Sun Microsystems, Inc. All rights reserved.63


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Matrix-Vector Multiplication• We want to define operators within traits.> Good for providing multiple implementations for a datatype> Good for enforcing contracts on subtypes (and thereforeenforcing contracts on the multiple implementations)• We want nice notation, not x.multiply(y)• We want to define both Vector-times-Matrix andMatrix-times-Vector in the Matrix trait.© 2006 Sun Microsystems, Inc. All rights reserved.64


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Functional Methods• A functional method declaration has an explicitself parameter in the parameter list, rather thanan implicit self parameter before the method name• A functional method invocation uses the samesyntax as function calls• Example:trait Vectoropr +(self, other:Vector):Vectordouble(self): Vector = self + self...endx = v1 + double(v2)© 2006 Sun Microsystems, Inc. All rights reserved.65


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Juxtaposition Operator• Juxtaposition is an infix operator in <strong>Fortress</strong>.> When the left operand is a function,juxtaposition performs function application.> When the left operand is a number,juxtaposition performs multiplication.> When the left operand is a string,juxtaposition performs string concatenation.• Example:y = 3 x sin x cos 2 x log log x© 2006 Sun Microsystems, Inc. All rights reserved.66


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Operator Overloading• Operator overloading allows multiple operatordeclarations with the same operator name.• Operator applications are equivalent in behavior tofunction calls.• Example:opr ⦇x:Number ⦈: Number = x 2opr ⦇x:Number,y:Number⦈: Number = x 2 + y 2⦇3 ⦈ (* reduces to 9 *)⦇3, 4⦈ (* reduces to 25 *)© 2006 Sun Microsystems, Inc. All rights reserved.67


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Restrictions on OverloadingNo undefined or ambiguous calls at run time• No statically ambiguous function calls• No dynamically ambiguous function calls> <strong>Fortress</strong> performs multi-argument dispatch> But a special rule forbids even potential ambiguity• Theorem: If there is a statically most specificapplicable declaration, then there is a dynamicallymost specific applicable declaration.© 2006 Sun Microsystems, Inc. All rights reserved.68


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Overloading and Subtyping• Assuming that Z64 is a subtype of Number,the following two declarations are ambiguous:foo(x:Number, y: Z64)foo(x: Z64, y:Number)The following new declaration would resolve theambiguity:foo(x: Z64, y: Z64)© 2006 Sun Microsystems, Inc. All rights reserved.69


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Matrix-Vector Multiplication in <strong>Fortress</strong>trait Matrix⟦T⟧ excludes { Vector⟦T⟧ }...opr juxtaposition(self, other:Vector⟦T⟧)opr juxtaposition(other:Vector⟦T⟧, self)endx = v M + M v© 2006 Sun Microsystems, Inc. All rights reserved.70


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Introduction<strong>Language</strong> OverviewBasics of ParallelismComponents and APIsDefining Mathematical OperatorsPolymorphism and Type InferenceParallelism: Generators and ReducersContracts, Properties, and TestingSummary© 2006 Sun Microsystems, Inc. All rights reserved.71


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Subtype Polymorphism• Subtype polymorphism allows code reuse.object Container(var element:Object)setElement(e:Object):() = element := egetElement():Object = elementend> Storing: safe upcastsc = Container(0)c.setElement(2)> Retrieving: potentially unsafe downcastsx: Z64 = cast⟦ Z 64⟧(c.getElement())© 2006 Sun Microsystems, Inc. All rights reserved.72


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Parametric Polymorphism• Parametric polymorphism also allows code reuse.object Container⟦T extends Equality⟧(var element:T)setElement(e:T):() = element := egetElement():T = elementendc = Container⟦Z64 ⟧(0)c.setElement(2)x: Z64= c.getElement()© 2006 Sun Microsystems, Inc. All rights reserved.73


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Static ParametersParameters may be types or compile-time constants.• Type parameters> types such as traits, tuple types, and arrow types• int and nat parameters> integer values (nat parameters are non-negative)• bool parameters> Boolean values• dim and unit parameters> dimensions and units• opr and nam parameters> operator symbols and method names© 2006 Sun Microsystems, Inc. All rights reserved.74


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Parameterized Functions and Traits• Functions, methods, traits, and objects are allowedto be parametric with respect to static parameters.makeList⟦T, nat length⟧(rest:T[length]) =if length = 0 then Emptyelse Cons(rest[0],makeList(rest[1#(length-1)]))end© 2006 Sun Microsystems, Inc. All rights reserved.75


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Nat and Bool Parameters• Nat parametersf⟦nat n⟧(x:R64 Length 2n ):R64 Length n = √x• Bool parameterstrait RationalQuantity⟦bool negativeInf,bool positiveInf,bool nan⟧...end© 2006 Sun Microsystems, Inc. All rights reserved.76


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Dimension/Unit/Operator/Name Parameters• Dimension and unit parametersopr √⟦unit U⟧(x: R64 U 2 ):R64 U =numericalsqrt(x/U 2 ) U• Operator and name parameterstrait CommutativeMonoid⟦T,opr ๏,nam id⟧...end© 2006 Sun Microsystems, Inc. All rights reserved.77


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006The “Self Types” Tricktrait Equality⟦T extends Equality⟦T⟧ ⟧opr =(self, T):Booleanendtrait Ordering⟦T extends Ordering⟦T⟧ ⟧extends Equality⟦T⟧opr ≤(self, other: T):Booleanopr ≥(self, other: T) = other ≤ selfopr (self, other: T) = not (self ≤ other)opr CMP(self, other: T) =if self > other then GreaterThanelif self < other then LessThanelse EqualTo endendIdiomfor selftyping© 2006 Sun Microsystems, Inc. All rights reserved.78


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Interesting Uses of Types at Runtime• Operations dependent on type parameterscast⟦T⟧(x : Object): T =typecase x inHere x : TT xelse throw CastExceptionend© 2006 Sun Microsystems, Inc. All rights reserved.79


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Interesting Type Relationships• Elimination of redundant parameters• Infinitely broad extensions> Monomorphic extension of polymorphic types• Variant subtyping> Covariant subtyping> Contravariant subtyping• Unifying concept: where clauses© 2006 Sun Microsystems, Inc. All rights reserved.80


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Elimination of Redundant Parameters• Instead of:trait Unit⟦D extends Dimension⟧trait Measurement⟦D extends Dimension,U extends Unit⟦D⟧ ⟧• We can write:trait Unit⟦D extends Dimension⟧trait Measurement⟦U extends Unit⟦D⟧ ⟧where {D extends Dimension}© 2006 Sun Microsystems, Inc. All rights reserved.81


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Infinitely Broad Extensions• We can define a single type Empty that is asubtype of all lists:trait List⟦T⟧object Cons⟦T⟧(first:T, rest:List⟦T⟧)extends List⟦T⟧object Empty extends List⟦T⟧where {T extends Object}© 2006 Sun Microsystems, Inc. All rights reserved.82


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Variant Subtyping• We can define covariant lists without additionallanguage constructs:trait List⟦X extends Y⟧ extends List⟦Y⟧where {Y extends Object}cons(y:Y):List⟦Y⟧ = Cons⟦Y⟧(y, self)...endType inferencecan fill this type inx : List⟦Number⟧ = Empty.cons(3).cons(5.7)© 2006 Sun Microsystems, Inc. All rights reserved.83


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Introduction<strong>Language</strong> OverviewBasics of ParallelismComponents and APIsDefining Mathematical OperatorsPolymorphism and Type InferenceParallelism: Generators and ReducersContracts, Properties, and TestingSummary© 2006 Sun Microsystems, Inc. All rights reserved.84


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Data and Control Models• Data model: shared global address space• Control model: multithreaded> Basic primitives are tuples and spawn> We hope application code seldom uses spawn• Declared distribution of data and threads> Managing aggregates integrated into type system> Policies programmed as libraries, not wired in• Transactional access to shared variables> Atomic blocks> Explicit testing and signaling of failure/retry> Deadlock-free, minimize blocking© 2006 Sun Microsystems, Inc. All rights reserved.85


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Data and Control Localityfor i ← 1#1000 doa[i] := a[i] + b[i]endfor i ← 1#1000 doa[i] := a[i] / c[i]end• Opportunities for locality:> Co-locate chunks ofarrays a, b, and c> Co-locate iterations ofthe loops (bothmanipulate the samearray a)© 2006 Sun Microsystems, Inc. All rights reserved.86


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Distributions: Allocating DataAllocation: d.array(l, u)a := d.array([0,0], [8,8])b := d.array([2,3], [9,10])CPU3CPU1CPU6CPU3CPU1CPU5CPU1CPU1CPU4CPU6CPU3CPU5CPU5CPU1CPU4CPU2CPU3CPU4CPU5CPU5© 2006 Sun Microsystems, Inc. All rights reserved.87


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Placing Computations• We can:> Co-locate data by using a common distribution> Find the region of an object by using its regionmethod• But how do we place a computation on a specificregion of the machine?> Augment the spawn expression:spawn x.region do f(x) end© 2006 Sun Microsystems, Inc. All rights reserved.88


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Revisiting Our Examplea = d.array([1],[1000])b = d.array([1],[1000])c = d.array([1],[1000])for i ← a.indices() doa[i] := a[i] + b[i]endfor i ← a.indices() doa[i] := a[i] / c[i]end• Opportunities for locality:> Co-locate chunks ofarrays a, b, and c> Co-locate iterations ofthe loops (bothmanipulate the samearray a)© 2006 Sun Microsystems, Inc. All rights reserved.89


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Distributions• Describe how to map a data structure onto a region> Block, block-cyclic, etc., and user-definable!> Map an array into a chip? Use a local heap.> Map an array onto a cluster? Break it up.1234 75 86 9101112© 2006 Sun Microsystems, Inc. All rights reserved.90


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Some Example DistributionsdefaultUsed when no other distribution givenseq(d)Data distributed, computation sequentiallocalData local, computation sequentialparChunks of size 1, no particular layoutruler Hierarchical division at powers of 2mortonMorton order, Z-layoutblockCyclic(n) Block cyclic, block size nblocked(n)Blocked, block size multiple of nrowMajor(d) Uninterleave dimensionscolumnMajor(d)© 2006 Sun Microsystems, Inc. All rights reserved.91


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Regions• Hierarchical data structure describes CPU andmemory resources and their properties> Allocation heaps> ParallelismCluster> Memory coherence• A running thread canfind out its resourcesNode Node Node• Spawn takes an optionalregion argument• Distribution assigns regionsChipCoreChipCoreChipNode© 2006 Sun Microsystems, Inc. All rights reserved.92


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Abstract CollectionsAggregateRangeIndex setGeneratorprotocolAbstractcollectionReductionprotocolResultAggregateRangeIndex setOptimized generator-reductionResult© 2006 Sun Microsystems, Inc. All rights reserved.93


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Representation of Abstract Collections◊1 εBinary operator ◊Leaf operator (“unit”) □Optional empty collection (“zero”) εthat is the identity for ◊2◊◊◊341 ◊2◊◊34© 2006 Sun Microsystems, Inc. All rights reserved.94


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Associativity1 ◊◊2ε◊◊3◊14◊2◊31 ◊4◊23◊41These are all consideredto be equivalent.◊◊21 ◊23◊◊34◊◊4ε© 2006 Sun Microsystems, Inc. All rights reserved.95


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Possible Algebraic Properties of ◊Associative Commutative Idempotentno no no leaf treesno no yes BDD-likeno yes no mobilesno yes yes weirdyes no no listsyes no yes weirdyes yes no multisetsyes yes yes setsThe “Boom hierarchy”© 2006 Sun Microsystems, Inc. All rights reserved.96


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Catamorphism: SummationReplace ◊ □ ε with + identity 0ε◊◊1◊2◊340++1+2+3410© 2006 Sun Microsystems, Inc. All rights reserved.97


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Catamorphism: ListsReplace ◊ □ ε with append ⟨–⟩ ⟨⟩1◊◊◊2 ⟨1⟩34appendappend append⟨2⟩ ⟨3⟩ ⟨4⟩⟨1,2,3,4⟩© 2006 Sun Microsystems, Inc. All rights reserved.98


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Catamorphism: Splicing Linked ListsReplace ◊ □ ε with conc unitList nilunitList:xx(At the end, use the left-handpointer of the final pair.)conc:a . . . d e f . . .ha . . . d e f . . .h© 2006 Sun Microsystems, Inc. All rights reserved.99


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Desugaring∑[i←a,j←b,p,k←c] ebecomes ∑(f)⟨ e | i←a,j←b,p,k←c ⟩ becomes makeList(f)for i←a,j←b,p,k←c do e endbecomes forLoop(f)where f =(fn (r)(a).generate(r, fn (i)(b).generate(r, fn (j)(p).generate(r, fn ()(c).generate(r, fn (k)r.single(e))))))Note: generate can be overloaded to exploit properties of r!© 2006 Sun Microsystems, Inc. All rights reserved.100


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Implementationopr ∑⟦T⟧(f: Generator⟦T⟧): Twhere { T extends Monoid⟦T,+,zero⟧ } =f.run(Catamorphism(fn(x,y) x+y, id, 0))makeList⟦T⟧(f: Generator⟦T⟧): List⟦T⟧ =f.run(Catamorphism(append, fn(x) ⟨x⟩, ⟨⟩))makeList⟦T⟧(f: Generator⟦T⟧): List⟦T⟧ =f.run(Catamorphism(conc, unitList, nil)).firstforLoop(f: Generator⟦()⟧): () =f.run(Catamorphism(par, id, ()))© 2006 Sun Microsystems, Inc. All rights reserved.101


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Implementationvalue object Catamorphism⟦T,R⟧(join : (R,R)→ R,empty: R,single : T → R)map⟦U ⟧(f : U → T) : Catamorphism⟦U,R⟧ =Catamorphism(join, empty, fn x single(f(x)) )endtrait Generator⟦T⟧ extends Objectrun⟦R⟧(c : Catamorphism⟦T,R⟧) : R =generate(c, fn x x)generate⟦R⟧(c : Catamorphism⟦T,R⟧, f : T → R): R =run(c.map(f))size: Z64end© 2006 Sun Microsystems, Inc. All rights reserved.102


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Generator Examplevalue object BlockedRange(lo: Z64, hi: Z64, b: Z64)extends Generator⟦ Z 64⟧size = hi – lo + 1run⟦R⟧(c : Catamorphism⟦ Z 64,R⟧) : R =if size ≤ max(b,1) thenr : R = c.emptyi : Z64 = lowhile i ≤ hi dor := c.join(r,c.single(i))i += 1endrelsemid = (lo + hi) 2c.join(BlockedRange(lo,mid,b).run(c),BlockedRange(mid+1,hi,b).run(c))endend© 2006 Sun Microsystems, Inc. All rights reserved.103


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Generators Drive Parallelism1234 75 86 9101112parparparseq seq seq seqseq 3 seq 6 seq 9 seq 12seq 2 seq 5 seq 8 seq 11ε1ε4ε7ε10© 2006 Sun Microsystems, Inc. All rights reserved.104


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Generators Modify Reducers:Parallelism123A4 75 86 9∑A101112 +par+par+par+seq +seq +seq +seq+seq 3 +seq 6 +seq 9 +seq 12+seq 2 +seq 5 +seq 8 +seq 11010407010© 2006 Sun Microsystems, Inc. All rights reserved.105


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Generators Modify Reducers:DistributionA[ x(x+1)/2 | x←A ]1234 75 86 9101112There are also ways(not shown here) for theprogrammer to specifya distribution explicitly.136101521283645556678© 2006 Sun Microsystems, Inc. All rights reserved.106


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Generalizing Comprehensions• We can generalize the comprehension notation:[ x ↦ y | x ← a.indices ][b.distribution]⟨ f(x) | x ← xs ⟩ ^consume• In full generality (using both features), we write:⟨ e | g ⟩ ^consume[args]• Comprehension yields generator G, called like so:consume( G , args)• Default subscript constructs list / array / set / map asappropriate© 2006 Sun Microsystems, Inc. All rights reserved.107


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Summary: Parallelism in <strong>Fortress</strong>• Regions describe machine resources.• Distributions map aggregates onto regions.• Aggregates used as generators drive parallelism.• Algebraic properties drive implementation strategies.• Algebraic properties are described by traits.• Properties are verified by automated unit testing.• Traits allow sharing of code, properties, and test data.• Reducers and generators negotiate throughoverloaded method dispatch keyed by traitsto achieve mix-and-match code selection.© 2006 Sun Microsystems, Inc. All rights reserved.108


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Introduction<strong>Language</strong> OverviewBasics of ParallelismComponents and APIsDefining Mathematical OperatorsPolymorphism and Type InferenceParallelism: Generators and ReducersContracts, Properties, and TestingSummary© 2006 Sun Microsystems, Inc. All rights reserved.109


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Contracts• Function contracts consist of three optional parts:> a requires part> an ensures part> an invariant part• Example: requiresfactorial(n: Z64)requires n≥0 =if n = 0 then 1else n factorial(n - 1)end© 2006 Sun Microsystems, Inc. All rights reserved.110


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Contract Example• Example: ensures and invariantmangle(input:List) ensures sorted(result)provided sorted(input)invariant size(input) =if input ≠ Emptythen mangle(first(input))mangle(rest(input))end© 2006 Sun Microsystems, Inc. All rights reserved.111


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Properties• Properties can be declared in trait declarations.• Such properties are expected to hold for allinstances of the trait and for all bindings of theproperty's parameters.• Example:trait Symmetric⟦T extends Symmetric⟦T,~⟧,opr ~⟧extends BinaryPredicate⟦T,~⟧property (a:T,b:T)(a~b) ↔ (b~a)end© 2006 Sun Microsystems, Inc. All rights reserved.112


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Tests• Test functions are evaluated with every permutationof test data.• If a non-test code refers to any part of test code, astatic error is signaled.• Example:test s:Set⟦ Z 64⟧ ={-2000,0,1,7,42,59,1000,5697}test fIsMonotonic[x←s,y←s] =assert(x ≤ y f x ≤ f y)© 2006 Sun Microsystems, Inc. All rights reserved.113


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Properties and Tests• Properties are verified by automated unit testing.• Properties can be named as property functions andcan be referred to in a program's test code.• If the result of a property function call is not true, atest failure is signaled.© 2006 Sun Microsystems, Inc. All rights reserved.114


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Inheritance of Properties and Tests• Traits allow sharing of code, properties, and testdata.• Algebraic constraints are described by traits.• Multiple inheritance of contracts, properties, andtests of algebraic constraints are provided.© 2006 Sun Microsystems, Inc. All rights reserved.115


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Algebraic Constraints Example(This is actual <strong>Fortress</strong> library code.)© 2006 Sun Microsystems, Inc. All rights reserved.116


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Example: Lexicographic Comparison• Assume a binary CMP operator that returns one ofLess, Equal, or Greater• Now consider the binary operator LEXICO:LEXICO Less Equal GreaterLess Less Less LessEqual Less Equal GreaterGreater Greater Greater Greater> Associative (but not commutative)> Equal is the identity> Less and Greater are left zeroes© 2006 Sun Microsystems, Inc. All rights reserved.117


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Algebraic Properties of LEXICOtrait Comparison extends {}...IdentityEquality⟦Comparison⟧,Associative⟦Comparison,LEXICO⟧,HasIdentity⟦Comparison,LEXICO,Equal⟧,HasLeftZeroes⟦Comparison,LEXICO⟧test data = { Less, Equal, Greater }endA generator that detects the LEXICO catamorphism (rather, thefact that it has left zeros) can choose to generate special code.© 2006 Sun Microsystems, Inc. All rights reserved.118


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Zeroes Can Stop Iteration EarlyDONE!×××7×0×34LEXICODONE!LEXICO LessLEXICO EqualLEXICO GreaterLEXICO Equal1 2EqualEqual© 2006 Sun Microsystems, Inc. All rights reserved.119


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Code for Lexicographic Comparisontrait LexOrder⟦T,E⟧extends { TotalOrder⟦T,≤,CMP⟧,Indexable⟦LexOrder⟦T,E⟧,E⟧ }where { T extends LexOrder⟦T,E⟧,E extends TotalOrder⟦T,≤,CMP⟧ }endopr =(self,other:T):Boolean =|self| = |other| AND:AND[i←self.indices] self[i]=other[i]opr CMP(self,other:T):Comparison = doprefix = self.indices ∩ other.indices(LEXICO[i←prefix] self[i] CMP other[i]) &LEXICO (|self| CMP |other|)endopr ≤(self,other:T):Boolean =(self CMP other) ≠ Greater© 2006 Sun Microsystems, Inc. All rights reserved.120


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006String Comparisontrait Stringextends { LexOrder⟦String,Character⟧, ... }opr [i:IndexInt]: Character = ......test data = { “foo”, “foobar”, “quux”, “” }end© 2006 Sun Microsystems, Inc. All rights reserved.121


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006Introduction<strong>Language</strong> OverviewBasics of ParallelismComponents and APIsDefining Mathematical OperatorsPolymorphism and Type InferenceParallelism: Generators and ReducersContracts, Properties, and TestingSummary© 2006 Sun Microsystems, Inc. All rights reserved.122


<strong>Fortress</strong> <strong>Programming</strong> <strong>Language</strong> <strong>Tutorial</strong>, PLDI, 11 June 2006<strong>Fortress</strong> Goals• Reduce application complexity• Reduce compiler complexity• Powerful language for library coding> Put compiler complexity into modular <strong>Fortress</strong> source code> Provide powerful abstractions for application coding> Enable the language to grow• Simplify application coding and deck checking> Make mathematical code look like “whiteboard notation”• Make it easy to code parallel algorithms© 2006 Sun Microsystems, Inc. All rights reserved.123


Guy SteeleJan-Willem Maessenhttp://research.sun.com/projects/plrgJanWillem.Maessen@sun.comGuy.Steele@sun.comhttp://research.sun.com/projects/plrgCarl Eastlund, Guy Steele, Jan-Willem Maessen, Yossi Lev, Eric Allen,Joe Hallett, Sukyoung Ryu, Sam Tobin-Hochstadt, David Chase, João Dias

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

Saved successfully!

Ooh no, something went wrong!