From: <ta...@us...> - 2010-04-14 01:38:14
|
Revision: 13852 http://x10.svn.sourceforge.net/x10/?rev=13852&view=rev Author: tardieu Date: 2010-04-14 01:38:07 +0000 (Wed, 14 Apr 2010) Log Message: ----------- implemented @Uncounted async Modified Paths: -------------- trunk/x10.compiler/src/x10/visit/Desugarer.java trunk/x10.runtime/src-x10/x10/lang/Activity.x10 trunk/x10.runtime/src-x10/x10/lang/Runtime.x10 Added Paths: ----------- trunk/x10.runtime/src-x10/x10/compiler/Uncounted.x10 Modified: trunk/x10.compiler/src/x10/visit/Desugarer.java =================================================================== --- trunk/x10.compiler/src/x10/visit/Desugarer.java 2010-04-13 16:09:44 UTC (rev 13851) +++ trunk/x10.compiler/src/x10/visit/Desugarer.java 2010-04-14 01:38:07 UTC (rev 13852) @@ -128,6 +128,7 @@ private static final Name EVAL_AT = Name.make("evalAt"); private static final Name EVAL_FUTURE = Name.make("evalFuture"); private static final Name RUN_ASYNC = Name.make("runAsync"); + private static final Name RUN_UNCOUNTED_ASYNC = Name.make("runUncountedAsync"); private static final Name HERE = Name.make("here"); private static final Name NEXT = Name.make("next"); private static final Name LOCK = Name.make("lock"); @@ -260,6 +261,11 @@ // Begin asyncs private Stmt visitAsync(Node old, Async a) throws SemanticException { Position pos = a.position(); + if (hasAnnotation(a, UNCOUNTED)) { + if (old instanceof Async && ((Async) old).place() instanceof Here) + return uncountedAsync(pos, a.body()); + return uncountedAsync(pos, a.body(), a.place()); + } if (old instanceof Async && ((Async) old).place() instanceof Here) return async(pos, a.body(), a.clocks()); Stmt specializedAsync = specializeAsync(old, a); @@ -293,6 +299,7 @@ private static final Name XOR = Name.make("xor"); private static final Name FENCE = Name.make("fence"); private static final QName IMMEDIATE = QName.make("x10.compiler.Immediate"); + private static final QName UNCOUNTED = QName.make("x10.compiler.Uncounted"); private static final QName REMOTE_OPERATION = QName.make("x10.compiler.RemoteOperation"); /** @@ -310,7 +317,7 @@ * TODO: move into a separate pass! */ private Stmt specializeAsync(Node old, Async a) throws SemanticException { - if (!hasAnnotation(a, QName.make("x10.compiler.Immediate"))) + if (!hasAnnotation(a, IMMEDIATE)) return null; if (a.clocks().size() != 0) return null; @@ -405,6 +412,30 @@ xts.Void(), types, xContext())); return result; } + + private Stmt uncountedAsync(Position pos, Stmt body, Expr place) throws SemanticException { + List<Expr> l = new ArrayList<Expr>(1); + l.add(place); + List<Type> t = new ArrayList<Type>(1); + t.add(xts.Place()); + return makeUncountedAsyncBody(pos, l, t, body); + } + + private Stmt uncountedAsync(Position pos, Stmt body) throws SemanticException { + return makeUncountedAsyncBody(pos, new LinkedList<Expr>(), + new LinkedList<Type>(), body); + } + + private Stmt makeUncountedAsyncBody(Position pos, List<Expr> exprs, List<Type> types, Stmt body) throws SemanticException { + Closure closure = synth.makeClosure(body.position(), xts.Void(), + synth.toBlock(body), xContext()); + exprs.add(closure); + types.add(closure.closureDef().asType()); + Stmt result = xnf.Eval(pos, + synth.makeStaticCall(pos, xts.Runtime(), RUN_UNCOUNTED_ASYNC, exprs, + xts.Void(), types, xContext())); + return result; + } // end Async @@ -481,7 +512,7 @@ * TODO: move into a separate pass! */ private Stmt specializeFinish(Finish f) throws SemanticException { - if (!hasAnnotation(f, QName.make("x10.compiler.Immediate"))) + if (!hasAnnotation(f, IMMEDIATE)) return null; Position pos = f.position(); ClassType target = (ClassType) xts.typeForName(REMOTE_OPERATION); Added: trunk/x10.runtime/src-x10/x10/compiler/Uncounted.x10 =================================================================== --- trunk/x10.runtime/src-x10/x10/compiler/Uncounted.x10 (rev 0) +++ trunk/x10.runtime/src-x10/x10/compiler/Uncounted.x10 2010-04-14 01:38:07 UTC (rev 13852) @@ -0,0 +1,55 @@ +/* + * This file is part of the X10 project (http://x10-lang.org). + * + * This file is licensed to You under the Eclipse Public License (EPL); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.opensource.org/licenses/eclipse-1.0.php + * + * (C) Copyright IBM Corporation 2006-2010. + */ + +package x10.compiler; + +import x10.lang.annotations.StatementAnnotation; + +/** + * @Uncounted may be used to annotate a non-clocked async. + * The body of an @Uncounted async may only contain @Uncounted asyncs. + * The body of an @Uncounted async may contain a finish, which in turn can contain both regular and @Uncounted asyncs. + * @Uncounted asyncs are not accounted for by the enclosing finish. + * That is, the enclosing finish may complete before the async completes. + * + * CURRENT LIMITATIONS: + * The compiler does not enforce any of the above restrictions. + * If the main method return before all of the uncounted asyncs have completed + * then the application may exit without waiting for the uncounted asyncs to complete. + * Exceptions in uncounted asyncs are discarded (but the runtime prints debug a message to the error console). + * + * EXAMPLE: + * + * import x10.compiler.Uncounted; + * + * class Box[T] { + * var t:T; + * + * def this(init:T) { t = init; } + * } + * + * public class Foo { + * public static def main(args:Rail[String]!) { + * val box = new Box[Boolean](false); + * @Uncounted async (here.next()) { + * Runtime.println("HELLO"); + * @Uncounted async (here.prev()) { + * atomic box.t = true; + * } + * } + * await box.t; + * } + * } + * + */ +public interface Uncounted + extends StatementAnnotation { +} Modified: trunk/x10.runtime/src-x10/x10/lang/Activity.x10 =================================================================== --- trunk/x10.runtime/src-x10/x10/lang/Activity.x10 2010-04-13 16:09:44 UTC (rev 13851) +++ trunk/x10.runtime/src-x10/x10/lang/Activity.x10 2010-04-14 01:38:07 UTC (rev 13852) @@ -84,16 +84,30 @@ } /** + * Create uncounted activity. + */ + def this(body:()=>Void, safe:Boolean) { + this.finishState = null; + this.safe = safe; + this.body = body; + } + + /** * Run activity. */ def run():Void { - try { - body(); - } catch (t:Throwable) { - finishState.pushException(t); - } - if (null != clockPhases) clockPhases.drop(); - finishState.notifyActivityTermination(); + try { + body(); + } catch (t:Throwable) { + if (null != finishState) { + finishState.pushException(t); + } else { + Runtime.println("Uncaught exception in uncounted activity"); + t.printStackTrace(); + } + } + if (null != clockPhases) clockPhases.drop(); + if (null != finishState) finishState.notifyActivityTermination(); Runtime.dealloc(body); } Modified: trunk/x10.runtime/src-x10/x10/lang/Runtime.x10 =================================================================== --- trunk/x10.runtime/src-x10/x10/lang/Runtime.x10 2010-04-13 16:09:44 UTC (rev 13851) +++ trunk/x10.runtime/src-x10/x10/lang/Runtime.x10 2010-04-14 01:38:07 UTC (rev 13852) @@ -27,15 +27,15 @@ */ public final class Runtime { - @Native("java", "java.lang.System.out.println(#1)") + @Native("java", "java.lang.System.err.println(#1)") @Native("c++", "x10aux::system_utils::println((#1)->toString()->c_str())") public native static def println(o:Object) : Void; - @Native("java", "java.lang.System.out.println()") + @Native("java", "java.lang.System.err.println()") @Native("c++", "x10aux::system_utils::println(\"\")") public native static def println() : Void; - @Native("java", "java.lang.System.out.printf(#4, #5)") + @Native("java", "java.lang.System.err.printf(#4, #5)") @Native("c++", "x10aux::system_utils::printf(#4, #5)") public native static def printf[T](fmt:String, t:T) : Void; @@ -1020,6 +1020,27 @@ execute(new Activity(body, state, safe())); } + public static def runUncountedAsync(place:Place, body:()=>Void):Void { + val ok = safe(); + if (place.id == hereInt()) { + execute(new Activity(body, ok)); + } else { + var closure:()=>Void; + // Workaround for XTENLANG_614 + if (ok) { + closure = ()=>execute(new Activity(body, true)); + } else { + closure = ()=>execute(new Activity(body, false)); + } + runAtNative(place.id, closure); + dealloc(closure); + } + } + + public static def runUncountedAsync(body:()=>Void):Void { + execute(new Activity(body, safe())); + } + /** * Run at statement */ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |