[Swift-commit] cog r3905

swift at ci.uchicago.edu swift at ci.uchicago.edu
Thu May 8 23:30:03 CDT 2014


------------------------------------------------------------------------
r3905 | hategan | 2014-05-08 23:28:53 -0500 (Thu, 08 May 2014) | 1 line

an improved stack frame allocation scheme and made some things allocate a frame (particularly allocateHost, since most of its execution time is waiting for a free host, so having its own frame means that the allocation happens only when the host is in use
------------------------------------------------------------------------
Index: modules/karajan/src/org/globus/cog/karajan/analyzer/ChannelRef.java
===================================================================
--- modules/karajan/src/org/globus/cog/karajan/analyzer/ChannelRef.java	(revision 3904)
+++ modules/karajan/src/org/globus/cog/karajan/analyzer/ChannelRef.java	(working copy)
@@ -15,6 +15,7 @@
 import k.rt.Channel;
 import k.rt.EmptyChannel;
 import k.rt.FixedArgChannel;
+import k.rt.FixedArgChannelDebug;
 import k.rt.FixedFutureArgChannel;
 import k.rt.FutureMemoryChannel;
 import k.rt.MemoryChannel;
@@ -489,11 +490,15 @@
 
 		@Override
 		public void create(Stack stack) {
-			FixedArgChannel<T> c = new FixedArgChannel<T>(stack.top(), first, last);
+		    FixedArgChannel<T> c;
+		    if (CompilerSettings.DEBUG) {
+		        c = new FixedArgChannelDebug<T>(stack.top(), first, last);
+		        c.setNames(names);
+		    }
+		    else {
+		        c = new FixedArgChannel<T>(stack.top(), first, last);
+		    }
 			stack.top().set(index, c);
-			if (CompilerSettings.DEBUG) {
-				c.setNames(names);
-			}
 		}
 		
 		@Override
Index: modules/karajan/src/org/globus/cog/karajan/analyzer/Scope.java
===================================================================
--- modules/karajan/src/org/globus/cog/karajan/analyzer/Scope.java	(revision 3904)
+++ modules/karajan/src/org/globus/cog/karajan/analyzer/Scope.java	(working copy)
@@ -9,6 +9,7 @@
  */
 package org.globus.cog.karajan.analyzer;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
@@ -26,6 +27,14 @@
 import org.globus.cog.karajan.parser.WrapperNode;
 
 public class Scope {
+    public static class Checkpoint {
+        protected final ArrayList<Boolean> map;
+
+		public Checkpoint(ArrayList<Boolean> map) {
+		    this.map = new ArrayList<Boolean>(map);
+		}
+    }
+    
 	public abstract static class Def {
 		public abstract Node newInstance();
 	}
@@ -118,6 +127,14 @@
 		return parent.getContainerScope();
 	}
 	
+	public Checkpoint checkpoint() {
+	    return getContainerScope().checkpoint();
+	}
+	
+	public void restore(Checkpoint c) {
+	    getContainerScope().restore(c);
+	}
+	
 	public boolean hasParams() {
 		return params;
 	}
@@ -494,19 +511,40 @@
 		if (args == null || args.isEmpty()) {
 			return;
 		}
-		int index = getContainerScope().allocateContiguous(args.size());
 		for (Param p : args) {
 			Var v = new ParamWrapperVar.Positional(p);
+			addVarNoIndex(v);
+		}
+	}
+	
+	public void allocatePositionalParams(List<Param> args) {
+		if (args == null || args.isEmpty()) {
+			return;
+		}
+		int index = getContainerScope().allocateContiguous(args.size(), owner);
+		for (Param p : args) {
+			Var v = new ParamWrapperVar.Positional(p);
 			addVar(v, index++);
 		}
 	}
 	
-	public ParamWrapperVar.IndexRange addOptionalParams(List<Param> args) {
+	public void addOptionalParams(List<Param> args) {
 		if (args == null || args.isEmpty()) {
+			return;
+		}
+		
+		for (Param p : args) {
+			Var v = new ParamWrapperVar.Optional(p, null);
+			addVarNoIndex(v);
+		}
+	}
+	
+	public ParamWrapperVar.IndexRange allocateOptionalParams(List<Param> args) {
+		if (args == null || args.isEmpty()) {
 			return null;
 		}
 		
-		int index = getContainerScope().allocateContiguous(args.size());
+		int index = getContainerScope().allocateContiguous(args.size(), owner);
 		ParamWrapperVar.IndexRange ir = new ParamWrapperVar.IndexRange(index, args.size());
 		for (Param p : args) {
 			Var v = new ParamWrapperVar.Optional(p, ir);
@@ -524,7 +562,11 @@
 	}
 	
 	public int addParams(List<Param> channels, List<Param> optional, List<Param> positional) {
-		int index = getContainerScope().allocateContiguous(channels.size() + optional.size() + positional.size());
+		return addParams(channels, optional, positional, null);
+	}
+	
+	public int addParams(List<Param> channels, List<Param> optional, List<Param> positional, Object owner) {
+		int index = getContainerScope().allocateContiguous(channels.size() + optional.size() + positional.size(), owner);
 		int first = index;
 		for (Param c : channels) {
 			Var.Channel v = new Var.Channel(c.name);
Index: modules/karajan/src/org/globus/cog/karajan/analyzer/ContainerScope.java
===================================================================
--- modules/karajan/src/org/globus/cog/karajan/analyzer/ContainerScope.java	(revision 3904)
+++ modules/karajan/src/org/globus/cog/karajan/analyzer/ContainerScope.java	(working copy)
@@ -33,6 +33,24 @@
 		return this;
 	}
 
+	@Override
+	public Checkpoint checkpoint() {
+	    if (CompilerSettings.DEBUG_STACK_ALLOCATION) {
+	        List<Integer> l = Collections.emptyList();
+            dump("checkpoint", l);
+        }
+		return new Checkpoint(map);
+	}
+
+	@Override
+	public void restore(Checkpoint c) {
+		this.map = c.map;
+		if (CompilerSettings.DEBUG_STACK_ALLOCATION) {
+            List<Integer> l = Collections.emptyList();
+            dump("restore", l);
+        }
+	}
+
 	public int allocate(Var ref) {
 		int index = findUnused();
 		if (index == -1) {
@@ -159,33 +177,37 @@
 	}
 
 	public int allocateContiguous(int size) {
-		int pos = findHole(size);
-		if (pos != -1) {
-			if (CompilerSettings.DEBUG_STACK_ALLOCATION) {
-				dump("allocateContiguous", range(pos, pos + size - 1));
-			}
-			return pos;
-		}
-		else {
-			int tailFree = map.size();
-			for (int i = map.size() - 1; i >= 0; i--) {
-				if (map.get(i)) {
-					break;
-				}
-				else {
-					tailFree = i;
-				}
-			}
-			for (int i = 0; i < size; i++) {
-				set(i + tailFree);
-			}
-			if (CompilerSettings.DEBUG_STACK_ALLOCATION) {
-				dump("allocateContiguous", range(tailFree, tailFree + size - 1));
-			}
-			return tailFree;
-		}
+		return allocateContiguous(size, null);
 	}
 	
+	public int allocateContiguous(int size, Object who) {
+        int pos = findHole(size);
+        if (pos != -1) {
+            if (CompilerSettings.DEBUG_STACK_ALLOCATION) {
+                dump("allocateContiguous(" + size + ") - " + who, range(pos, pos + size - 1));
+            }
+            return pos;
+        }
+        else {
+            int tailFree = map.size();
+            for (int i = map.size() - 1; i >= 0; i--) {
+                if (map.get(i)) {
+                    break;
+                }
+                else {
+                    tailFree = i;
+                }
+            }
+            for (int i = 0; i < size; i++) {
+                set(i + tailFree);
+            }
+            if (CompilerSettings.DEBUG_STACK_ALLOCATION) {
+                dump("allocateContiguous(" + size + ") - " + who, range(tailFree, tailFree + size - 1));
+            }
+            return tailFree;
+        }
+    }
+	
 	private int findHole(int size) {
 		int index = 0;
 		while (index + size < map.size()) {
Index: modules/karajan/src/org/globus/cog/karajan/analyzer/VarRef.java
===================================================================
--- modules/karajan/src/org/globus/cog/karajan/analyzer/VarRef.java	(revision 3904)
+++ modules/karajan/src/org/globus/cog/karajan/analyzer/VarRef.java	(working copy)
@@ -53,7 +53,7 @@
 	}
 	
 	public static class DynamicLocal<T> extends VarRef<T> {
-        protected final int index;
+        public final int index;
         public final String name;
         
         public DynamicLocal(String name, int index) {
Index: modules/karajan/src/org/globus/cog/karajan/compiled/nodes/Import.java
===================================================================
--- modules/karajan/src/org/globus/cog/karajan/compiled/nodes/Import.java	(revision 3904)
+++ modules/karajan/src/org/globus/cog/karajan/compiled/nodes/Import.java	(working copy)
@@ -38,6 +38,7 @@
 import org.globus.cog.karajan.parser.ParsingException;
 import org.globus.cog.karajan.parser.WrapperNode;
 import org.globus.cog.karajan.util.KarajanProperties;
+import org.globus.cog.karajan.util.Pair;
 
 public class Import extends InternalFunction {
 	public static final Logger logger = Logger.getLogger(Import.class);
@@ -137,13 +138,26 @@
 			}
 		}
 	}
+	
+	@Override
+	protected void scanNamed(WrapperNode w, Scope scope, List<Param> params, List<Pair<Param, String>> dynamicOptimized)
+			throws CompilationException {
+		// override to allow imports of non-identifier but static names
+	}
 
 	@Override
+	protected void optimizePositional(WrapperNode w, Scope scope, List<Param> params, List<Pair<Param, String>> dynamicOptimized)
+			throws CompilationException {
+		// see above
+	}
+
+	@Override
+	protected void scanNotSet(WrapperNode w, Scope scope, List<Param> optional)
+			throws CompilationException {
+	}
+
+	@Override
 	public Node compileBody(WrapperNode w, Scope argScope, Scope scope) throws CompilationException {
-		if ("import @ swift.k, line: 137".equals(this.toString())) {
-			System.out.print("");
-		}
-		
 		if (name == null && file.getValue() == null) {
 			throw new CompilationException(w, "Could not statically determine file name");
 		}
@@ -158,7 +172,7 @@
 		context = scope.getVarRef("#context");
 		
 		Resolved r;
-		try{
+		try {
 			r = resolve(name, props.getValue(), fileDir.getValue());
 		}
 		catch (IOException ee) {
Index: modules/karajan/src/org/globus/cog/karajan/compiled/nodes/InternalFunction.java
===================================================================
--- modules/karajan/src/org/globus/cog/karajan/compiled/nodes/InternalFunction.java	(revision 3904)
+++ modules/karajan/src/org/globus/cog/karajan/compiled/nodes/InternalFunction.java	(working copy)
@@ -36,7 +36,9 @@
 import org.globus.cog.karajan.analyzer.Signature;
 import org.globus.cog.karajan.analyzer.StaticChannel;
 import org.globus.cog.karajan.analyzer.Var;
+import org.globus.cog.karajan.analyzer.VarRef;
 import org.globus.cog.karajan.parser.WrapperNode;
+import org.globus.cog.karajan.util.Pair;
 
 public abstract class InternalFunction extends Sequential {
 	protected static final int DYNAMIC = -1;
@@ -149,6 +151,8 @@
 	        			catch (IllegalArgumentException e) {
 	        				throw new ExecutionException(this, e.getMessage());
 	        			}
+	        			i = Integer.MAX_VALUE;
+	        	case Integer.MAX_VALUE:
 			            try {
 			            	runBody(thr);
 			            }
@@ -186,7 +190,7 @@
 	
 	protected void initializeOptional(Stack stack) {
 	    if (firstOptionalIndex != -1) {
-            Arrays.fill(stack.top().getAll(), firstOptionalIndex, lastOptionalIndex, null);
+            Arrays.fill(stack.top().getAll(), firstOptionalIndex, lastOptionalIndex + 1, null);
         }
 	}
 
@@ -197,39 +201,55 @@
 		public final List<Param> optional;
 		public final List<Param> positional;
 		public final List<Param> channelParams;
+		public final List<Pair<Param, String>> dynamicOptimized;
 		public final ParamWrapperVar.IndexRange ir;
 		
 		public ArgInfo(LinkedList<WrapperNode> blocks, Var.Channel vargs, StaticChannel vargValues, 
-				List<Param> optional, List<Param> positional, List<Param> channelParams, ParamWrapperVar.IndexRange ir) {
+				List<Param> optional, List<Param> positional, List<Param> channelParams, 
+				List<Pair<Param, String>> dynamicOptimized, 
+				ParamWrapperVar.IndexRange ir) {
 			this.blocks = blocks;
 			this.vargs = vargs;
 			this.vargValues = vargValues;
 			this.optional = optional;
 			this.positional = positional;
 			this.channelParams = channelParams;
+			this.dynamicOptimized = dynamicOptimized;
 			this.ir = ir;
 		}
 	}
 	
 	protected ArgInfo compileArgs(WrapperNode w, Signature sig, Scope scope) throws CompilationException {
+		if (w.toString().contains("stageIn @ swift-int-staging.k, line: 121")) {
+			System.out.print("");
+		}
 		resolveChannelReturns(w, sig, scope);
 		resolveParamReturns(w, sig, scope);
 		
 		List<Param> channels = getChannelParams(sig);
 		List<Param> optional = getOptionalParams(sig);
 		List<Param> positional = getPositionalParams(sig);
+		
 		// optionals first
 		if (optional != null || positional != null) {
 			scope.setParams(true);
 		}
 		
-		ParamWrapperVar.IndexRange ir = addParams(w, sig, scope, channels, optional, positional);
+		List<Pair<Param, String>> dynamicOptimized = new LinkedList<Pair<Param, String>>();
+		
+		addParams(w, sig, scope, channels, optional, positional);
+		scanNamed(w, scope, optional, dynamicOptimized);
+		optimizePositional(w, scope, positional, dynamicOptimized);
+		scanNamed(w, scope, positional, dynamicOptimized);
+		scanNotSet(w, scope, optional);
+		
+		ParamWrapperVar.IndexRange ir = allocateParams(w, sig, scope, channels, optional, positional);
 					
 		LinkedList<WrapperNode> blocks = checkBlockArgs(w, sig);
 		
 		Var.Channel vargs = null;
 		ArgMappingChannel amc = null;
-		if (!sig.getParams().isEmpty() || hasVargs) {
+		if (!positional.isEmpty() || !optional.isEmpty() || hasVargs) {
 			if (hasVargs) {
 				vargs = scope.lookupChannel("...");
 				vargs.setValue(amc = new ArgMappingChannel(w, positional, true));
@@ -239,29 +259,149 @@
 			}
 		}
 
-		return new ArgInfo(blocks, vargs, amc, optional, positional, channels, ir);
+		return new ArgInfo(blocks, vargs, amc, optional, positional, channels, dynamicOptimized, ir);
 	}
 
-	protected ParamWrapperVar.IndexRange addParams(WrapperNode w, Signature sig, Scope scope, List<Param> channels,
+	protected void scanNotSet(WrapperNode w, Scope scope, List<Param> optional) throws CompilationException {
+		if (w.nodeCount() == 0) {
+			for (Param p : optional) {
+				setArg(w, p, new ArgRef.Static<Object>(p.value));
+			}
+			optional.clear();
+		}
+	}
+
+	protected void scanNamed(WrapperNode w, Scope scope, List<Param> params, 
+			List<Pair<Param, String>> dynamicOptimized) throws CompilationException {
+		Iterator<WrapperNode> i = w.nodes().iterator();
+		while (i.hasNext()) {
+			WrapperNode c = i.next();
+			if (c.getNodeType().equals("k:named")) {
+				if (optimizeNamed(w, c, scope, params, dynamicOptimized)) {
+					i.remove();
+				}
+			}
+		}
+	}
+
+	private boolean optimizeNamed(WrapperNode w, WrapperNode c, Scope scope, List<Param> params, 
+			List<Pair<Param, String>> dynamicOptimized) throws CompilationException {
+		if (c.nodeCount() == 2) {
+			WrapperNode nameNode = c.getNode(0);
+			if (nameNode.getNodeType().equals("k:var")) {
+				String name = nameNode.getText();
+				Param p = getParam(params, name);
+				if (p != null) {
+					WrapperNode valueNode = c.getNode(1);
+					if (setValue(w, scope, p, valueNode, dynamicOptimized)) {
+						params.remove(p);
+						return true;
+					}
+				}
+			}
+		}
+		return false;
+	}
+	
+	protected void optimizePositional(WrapperNode w, Scope scope, List<Param> params, 
+			List<Pair<Param, String>> dynamicOptimized) throws CompilationException {
+		int index = 0;
+		Iterator<WrapperNode> i = w.nodes().iterator();
+		while (i.hasNext() && index < params.size()) {
+			WrapperNode c = i.next();
+			Param p = params.get(index);
+			if (p.type == Param.Type.IDENTIFIER) {
+				return;
+			}
+			if (setValue(w, scope, p, c, dynamicOptimized)) {
+				params.remove(p);
+				i.remove();
+				index--;
+			}
+			index++;
+		}
+	}
+
+	private boolean setValue(WrapperNode w, Scope scope, Param p, WrapperNode c, 
+			List<Pair<Param, String>> dynamicOptimized) throws CompilationException {
+		if (c.getNodeType().equals("k:var")) {
+			VarRef<Object> ref = scope.getVarRef(c.getText());
+			if (ref.isStatic()) {
+				p.setValue(ref.getValue());
+				setArg(w, p, new ArgRef.Static<Object>(ref.getValue()));
+				return true;
+			}
+			else if (ref instanceof VarRef.DynamicLocal) {
+				VarRef.DynamicLocal<Object> ref2 = (VarRef.DynamicLocal<Object>) ref;
+				setArg(w, p, new ArgRef.Dynamic<Object>(ref2.index));
+				dynamicOptimized.add(new Pair<Param, String>(p, c.getText()));
+				return true;
+			}
+			else {
+				return false;
+			}
+		}
+		else if (c.getNodeType().equals("k:num")) {
+			// there is probably a better way to handle these cases
+			// perhaps by compiling children and then allocating entries on the frame
+			Object value;
+			if (c.getText().indexOf(".") >= 0) {
+				value = Double.parseDouble(c.getText());
+			}	
+			else {
+				value = Integer.parseInt(c.getText());
+			}
+			p.setValue(value);
+			setArg(w, p, new ArgRef.Static<Object>(value));
+			return true;
+		}
+		else if (c.getNodeType().equals("k:str")) {
+			p.setValue(c.getText());
+			setArg(w, p, new ArgRef.Static<Object>(c.getText()));
+			return true;
+		}
+		else {
+			return false;
+		}
+	}
+
+	private Param getParam(List<Param> params, String name) {
+		for (Param p : params) {
+			if (p.name.equals(name)) {
+				return p;
+			}
+		}
+		return null;
+	}
+	
+	protected void addParams(WrapperNode w, Signature sig, Scope scope, List<Param> channels,
 			List<Param> optional, List<Param> positional) throws CompilationException {
 		processIdentifierArgs(w, sig);
 		prepareChannelParams(scope, channels);
 		scope.addChannelParams(channels);
 		scope.addPositionalParams(positional);
-		return scope.addOptionalParams(optional);
+		scope.addOptionalParams(optional);
 	}
 
+	protected ParamWrapperVar.IndexRange allocateParams(WrapperNode w, Signature sig, Scope scope, List<Param> channels,
+			List<Param> optional, List<Param> positional) throws CompilationException {
+		scope.allocatePositionalParams(positional);
+		return scope.allocateOptionalParams(optional);
+	}
+
 	@Override
-	protected final Node compileChildren(WrapperNode w, Scope scope) throws CompilationException {
-				
+	protected final Node compileChildren(WrapperNode w, Scope scope) throws CompilationException {				
 		Signature sig = getSignature();
 		
 		Scope argScope = new Scope(w, scope);
-	
+		Scope.Checkpoint chk = argScope.checkpoint();
+		
 		ArgInfo ai = compileArgs(w, sig, argScope);
 		
 		Node n = super.compileChildren(w, argScope);
 		
+		checkStaticOptimizedParams(w, scope, ai.dynamicOptimized);
+		
 		if (ai.ir != null) {
 		    // unused optional parameters
 			scope.releaseRange(ai.ir.currentIndex(), ai.ir.lastIndex());
@@ -273,7 +413,9 @@
 		
 		compileBlocks(w, sig, ai.blocks, scope);
 		
-		if (compileBody(w, argScope, scope) == null && n == null && ai.blocks == null) {
+		Node self = compileBody(w, argScope, scope);
+		
+		if (self == null && n == null && ai.blocks == null) {
 			argScope.close();
 			return null;
 		}
@@ -281,13 +423,22 @@
 		argScope.close();
 		
 		if (this instanceof Pure && ai.blocks == null) {
-			return n;
+			return self;
 		}
 		else {
 			return this;
 		}
 	}
 
+	private void checkStaticOptimizedParams(WrapperNode w, Scope scope, List<Pair<Param, String>> dynamicOptimized) throws CompilationException {
+		for (Pair<Param, String> p : dynamicOptimized) {
+			VarRef<?> ref = scope.getVarRef(p.t);
+			if (ref.isStatic()) {
+				setArg(w, p.s, new ArgRef.Static<Object>(ref.getValue()));
+			}
+		}
+	}
+
 	protected Node compileBody(WrapperNode w, Scope argScope, Scope scope) throws CompilationException {
 		return this;
 	}
Index: modules/karajan/src/org/globus/cog/karajan/compiled/nodes/CompoundNode.java
===================================================================
--- modules/karajan/src/org/globus/cog/karajan/compiled/nodes/CompoundNode.java	(revision 3904)
+++ modules/karajan/src/org/globus/cog/karajan/compiled/nodes/CompoundNode.java	(working copy)
@@ -47,10 +47,14 @@
 
 	protected Node compileChildren(WrapperNode w, Scope scope) throws CompilationException {
 		for (WrapperNode c : w.nodes()) {
+			Scope.Checkpoint sc = scope.checkpoint();
 			Node n = compileChild(c, scope);
 			if (n != null) {
 				addChild(n);
 			}
+			else {
+				scope.restore(sc);
+			}
 		}
 		if (childCount() == 0) {
 			return null;
Index: modules/karajan/src/org/globus/cog/karajan/compiled/nodes/IncludedMain.java
===================================================================
--- modules/karajan/src/org/globus/cog/karajan/compiled/nodes/IncludedMain.java	(revision 3904)
+++ modules/karajan/src/org/globus/cog/karajan/compiled/nodes/IncludedMain.java	(working copy)
@@ -23,7 +23,6 @@
 import org.globus.cog.karajan.analyzer.CompilationException;
 import org.globus.cog.karajan.analyzer.NamedValue;
 import org.globus.cog.karajan.analyzer.Param;
-import org.globus.cog.karajan.analyzer.ParamWrapperVar;
 import org.globus.cog.karajan.analyzer.Scope;
 import org.globus.cog.karajan.analyzer.Signature;
 import org.globus.cog.karajan.analyzer.Var;
@@ -63,20 +62,19 @@
 		scope.addVar(Namespace.VAR_NAME, "");
 		super.addLocals(scope);
 	}
+	
+	
 
 	@Override
-	protected ParamWrapperVar.IndexRange addParams(WrapperNode w, Signature sig, Scope scope, List<Param> channels,
+	protected void addParams(WrapperNode w, Signature sig, Scope scope, List<Param> channels,
 			List<Param> optional, List<Param> positional) throws CompilationException {
-	    ParamWrapperVar.IndexRange ir = super.addParams(w, sig, scope, channels, optional, positional);
-	    
+		super.addParams(w, sig, scope, channels, optional, positional);
 		Scope prev = scope.getRoot().getImportScope();
-		
-		cr_export = bind(prev, scope, "export");
-		cr_stdout = bind(prev, scope, "stdout");
-		cr_stderr = bind(prev, scope, "stderr");
-		cr_vargs = bind(prev, scope, "...");
-		
-		return ir;
+        
+        cr_export = bind(prev, scope, "export");
+        cr_stdout = bind(prev, scope, "stdout");
+        cr_stderr = bind(prev, scope, "stderr");
+        cr_vargs = bind(prev, scope, "...");
 	}
 
 	private <T> ChannelRef<T> bind(Scope scope, Scope prev, String name) {
Index: modules/karajan/src/org/globus/cog/karajan/compiled/nodes/DefNode.java
===================================================================
--- modules/karajan/src/org/globus/cog/karajan/compiled/nodes/DefNode.java	(revision 3904)
+++ modules/karajan/src/org/globus/cog/karajan/compiled/nodes/DefNode.java	(working copy)
@@ -9,15 +9,19 @@
  */
 package org.globus.cog.karajan.compiled.nodes;
 
+import java.util.List;
+
 import org.apache.log4j.Logger;
 import org.globus.cog.karajan.analyzer.ArgRef;
 import org.globus.cog.karajan.analyzer.ChannelRef;
 import org.globus.cog.karajan.analyzer.CompilationException;
+import org.globus.cog.karajan.analyzer.Param;
 import org.globus.cog.karajan.analyzer.Scope;
 import org.globus.cog.karajan.analyzer.Scope.JavaDef;
 import org.globus.cog.karajan.analyzer.Signature;
 import org.globus.cog.karajan.analyzer.Var;
 import org.globus.cog.karajan.parser.WrapperNode;
+import org.globus.cog.karajan.util.Pair;
 
 public class DefNode extends InternalFunction  {
 	private static final Logger logger = Logger.getLogger(DefNode.class);
@@ -65,4 +69,9 @@
 		}
 		return null;
 	}
+
+	@Override
+	protected void optimizePositional(WrapperNode w, Scope scope, List<Param> params, List<Pair<Param, String>> dynamicOptimized)
+			throws CompilationException {
+	}
 }
\ No newline at end of file
Index: modules/karajan/src/org/globus/cog/karajan/compiled/nodes/grid/AllocateHost.java
===================================================================
--- modules/karajan/src/org/globus/cog/karajan/compiled/nodes/grid/AllocateHost.java	(revision 3904)
+++ modules/karajan/src/org/globus/cog/karajan/compiled/nodes/grid/AllocateHost.java	(working copy)
@@ -8,10 +8,7 @@
 
 import java.io.IOException;
 import java.io.PrintStream;
-import java.util.Collections;
-import java.util.HashMap;
 import java.util.LinkedList;
-import java.util.Map;
 
 import k.rt.ConditionalYield;
 import k.rt.Context;
@@ -27,6 +24,7 @@
 import org.globus.cog.karajan.analyzer.ArgRef;
 import org.globus.cog.karajan.analyzer.CompilationException;
 import org.globus.cog.karajan.analyzer.CompilerSettings;
+import org.globus.cog.karajan.analyzer.ContainerScope;
 import org.globus.cog.karajan.analyzer.Scope;
 import org.globus.cog.karajan.analyzer.Signature;
 import org.globus.cog.karajan.analyzer.VarRef;
@@ -46,6 +44,7 @@
 	
 	private VarRef<Context> context;
 	private VarRef<Contact> var;
+	private int frameSize;
 
 	@Override
 	protected Signature getSignature() {
@@ -72,10 +71,14 @@
 				    allocateHost(thr);
 				    i++;
 				case 2:
+					stack.enter(this, frameSize);
+					i++;
+				case 3:
 					if (CompilerSettings.PERFORMANCE_COUNTERS) {
 						startCount++;
 					}
 					body.run(thr);
+					stack.leave();
 					_finally(thr.getStack());
 			}
 		}
@@ -94,8 +97,10 @@
 	@Override
 	protected void compileBlocks(WrapperNode w, Signature sig, LinkedList<WrapperNode> blocks,
 			Scope scope) throws CompilationException {
-	    var = scope.getVarRef(scope.addVar(name));
-		super.compileBlocks(w, sig, blocks, scope);
+		var = scope.getVarRef(scope.addVar(name));
+		ContainerScope cs = new ContainerScope(w, scope);
+		super.compileBlocks(w, sig, blocks, cs);
+		frameSize = cs.size();
 	}
 
 	protected void allocateHost(LWThread thr) {
Index: modules/karajan/src/org/globus/cog/karajan/compiled/nodes/Export.java
===================================================================
--- modules/karajan/src/org/globus/cog/karajan/compiled/nodes/Export.java	(revision 3904)
+++ modules/karajan/src/org/globus/cog/karajan/compiled/nodes/Export.java	(working copy)
@@ -9,16 +9,20 @@
  */
 package org.globus.cog.karajan.compiled.nodes;
 
+import java.util.List;
+
 import org.globus.cog.karajan.analyzer.ArgRef;
 import org.globus.cog.karajan.analyzer.ChannelRef;
 import org.globus.cog.karajan.analyzer.CompilationException;
 import org.globus.cog.karajan.analyzer.NamedValue;
+import org.globus.cog.karajan.analyzer.Param;
 import org.globus.cog.karajan.analyzer.Scope;
-import org.globus.cog.karajan.analyzer.VarRef;
 import org.globus.cog.karajan.analyzer.Scope.Def;
 import org.globus.cog.karajan.analyzer.Signature;
 import org.globus.cog.karajan.analyzer.Var;
+import org.globus.cog.karajan.analyzer.VarRef;
 import org.globus.cog.karajan.parser.WrapperNode;
+import org.globus.cog.karajan.util.Pair;
 
 public class Export extends InternalFunction {
 	private ChannelRef<NamedValue> cr_export;
@@ -63,4 +67,10 @@
 			return this;
 		}
 	}
+
+	@Override
+	protected void optimizePositional(WrapperNode w, Scope scope, List<Param> params, List<Pair<Param, String>> dynamicOptimized)
+			throws CompilationException {
+		// do not optimize positionals
+	}
 }
Index: modules/karajan/src/org/globus/cog/karajan/compiled/nodes/UParallel.java
===================================================================
--- modules/karajan/src/org/globus/cog/karajan/compiled/nodes/UParallel.java	(revision 3904)
+++ modules/karajan/src/org/globus/cog/karajan/compiled/nodes/UParallel.java	(working copy)
@@ -10,20 +10,28 @@
 import k.rt.KRunnable;
 import k.rt.Stack;
 import k.thr.LWThread;
-import k.thr.ThreadSet;
+import k.thr.ThreadSetFixed;
 import k.thr.Yield;
 
 import org.globus.cog.karajan.analyzer.CompilationException;
+import org.globus.cog.karajan.analyzer.ContainerScope;
 import org.globus.cog.karajan.analyzer.Scope;
 import org.globus.cog.karajan.analyzer.TrackingScope;
 import org.globus.cog.karajan.parser.WrapperNode;
 
 public class UParallel extends CompoundNode {
+    private int[] frameSizes;
 	
 	@Override
     protected Node compileChildren(WrapperNode w, Scope scope) throws CompilationException {
+	    frameSizes = new int[w.nodes().size()];
+	    // TODO Allocate separate frames for each sub-thread. It's likely that most sub-threads
+	    // will finish long before a straggling one. This unnecessarily eats all of the space 
+	    // that was allocated on the frame for the finished threads
+	    int index = 0;
         for (WrapperNode c : w.nodes()) {
-            TrackingScope ts = new TrackingScope(scope);
+            ContainerScope cs = new ContainerScope(c, scope);
+            TrackingScope ts = new TrackingScope(cs);
             ts.setTrackChannels(false);
             ts.setAllowChannelReturns(true);
             ts.setTrackNamed(false);
@@ -32,6 +40,7 @@
             Node n = compileChild(c, ts);
             if (n != null) {
                 addChild(n);
+                frameSizes[index++] = cs.size();
             }
         }
         if (childCount() == 0) {
@@ -47,7 +56,7 @@
 	public void run(LWThread thr) throws ExecutionException {
 		int state = thr.checkSliceAndPopState();
 		Stack stack = thr.getStack();
-		ThreadSet ts = (ThreadSet) thr.popState();
+		ThreadSetFixed ts = (ThreadSetFixed) thr.popState();
 		int fc = thr.popIntState();
 		try {
 			switch (state) {
@@ -55,13 +64,13 @@
 					fc = stack.frameCount();
 					state++;
 				case 1:
-					final ThreadSet tsf = new ThreadSet();
+				    int ec = childCount();
+					final ThreadSetFixed tsf = new ThreadSetFixed(ec);
 					ts = tsf;
 					final int fcf = fc;
-					int ec = childCount();
 					for (int i = 0; i < ec; i++) {
 						final int fi = i;
-						LWThread ct = thr.fork(new KRunnable() {
+						LWThread ct = thr.fork(i, new KRunnable() {
 							@Override
 							public void run(LWThread thr) {
 								try {
@@ -83,6 +92,8 @@
 							}
 						});
 						tsf.add(ct);
+						Stack cs = ct.getStack();
+                        cs.enter(this, frameSizes[i]);
 					}
 					ts.startAll();
 					state++;
Index: modules/karajan/src/org/globus/cog/karajan/compiled/nodes/Main.java
===================================================================
--- modules/karajan/src/org/globus/cog/karajan/compiled/nodes/Main.java	(revision 3904)
+++ modules/karajan/src/org/globus/cog/karajan/compiled/nodes/Main.java	(working copy)
@@ -73,11 +73,6 @@
 		setVarCount(cs.parent.size());
 		return fn;
 	}
-	
-	@Override
-	public void run(LWThread thr) {
-		super.run(thr);
-	}
 
 	@Override
 	protected void initializeArgs(Stack stack) {
Index: modules/karajan/src/org/globus/cog/karajan/compiled/nodes/functions/ConstantOp.java
===================================================================
--- modules/karajan/src/org/globus/cog/karajan/compiled/nodes/functions/ConstantOp.java	(revision 0)
+++ modules/karajan/src/org/globus/cog/karajan/compiled/nodes/functions/ConstantOp.java	(revision 3905)
@@ -0,0 +1,39 @@
+//----------------------------------------------------------------------
+//This code is developed as part of the Java CoG Kit project
+//The terms of the license can be found at http://www.cogkit.org/license
+//This message may not be removed or altered.
+//----------------------------------------------------------------------
+
+/*
+ * Created on Dec 18, 2012
+ */
+package org.globus.cog.karajan.compiled.nodes.functions;
+
+import k.rt.Stack;
+
+import org.globus.cog.karajan.analyzer.CompilationException;
+import org.globus.cog.karajan.analyzer.Scope;
+import org.globus.cog.karajan.analyzer.Signature;
+import org.globus.cog.karajan.compiled.nodes.Node;
+import org.globus.cog.karajan.parser.WrapperNode;
+
+public abstract class ConstantOp<R> extends AbstractFunction {
+	@Override
+	public R function(Stack stack) {
+		return value();
+	}
+
+	protected abstract R value();
+
+	@Override
+	protected Signature getSignature() {
+		return new Signature(params());
+	}
+
+	@Override
+	protected Node compileBody(WrapperNode w, Scope argScope, Scope scope)
+			throws CompilationException {
+		staticReturn(scope, value());
+		return null;
+	}
+}
Index: modules/karajan/src/org/globus/cog/karajan/compiled/nodes/user/Function.java
===================================================================
--- modules/karajan/src/org/globus/cog/karajan/compiled/nodes/user/Function.java	(revision 3904)
+++ modules/karajan/src/org/globus/cog/karajan/compiled/nodes/user/Function.java	(working copy)
@@ -19,7 +19,6 @@
 import k.thr.LWThread;
 import k.thr.Yield;
 
-import org.globus.cog.karajan.analyzer.ArgRef;
 import org.globus.cog.karajan.analyzer.ChannelRef;
 import org.globus.cog.karajan.analyzer.CompilationException;
 import org.globus.cog.karajan.analyzer.CompilerSettings;
@@ -123,6 +122,7 @@
 
 	protected void bindArgs(Frame src, Frame dst, int firstIndex, int argCount) {
 		System.arraycopy(src.getAll(), firstIndex, dst.getAll(), 0, argCount);
+		//Arrays.fill(src.getAll(), firstIndex, firstIndex + argCount, null);
 	}
 
 	public void bindChannels(Stack parent, Stack def, List<ChannelRef<Object>> wrapperChannels) {
Index: modules/karajan/src/org/globus/cog/karajan/compiled/nodes/user/InvocationWrapper.java
===================================================================
--- modules/karajan/src/org/globus/cog/karajan/compiled/nodes/user/InvocationWrapper.java	(revision 3904)
+++ modules/karajan/src/org/globus/cog/karajan/compiled/nodes/user/InvocationWrapper.java	(working copy)
@@ -32,6 +32,7 @@
 import org.globus.cog.karajan.compiled.nodes.InternalFunction;
 import org.globus.cog.karajan.compiled.nodes.Node;
 import org.globus.cog.karajan.parser.WrapperNode;
+import org.globus.cog.karajan.util.Pair;
 
 public class InvocationWrapper extends InternalFunction {
 	
@@ -84,15 +85,35 @@
     }
 
 	@Override
-	protected ParamWrapperVar.IndexRange addParams(WrapperNode w, Signature sig, Scope scope, List<Param> channels,
+	protected void addParams(WrapperNode w, Signature sig, Scope scope, List<Param> channels,
 			List<Param> optional, List<Param> positional) throws CompilationException {
+	}
+
+	@Override
+	protected ParamWrapperVar.IndexRange allocateParams(WrapperNode w, Signature sig, Scope scope, List<Param> channels,
+			List<Param> optional, List<Param> positional) throws CompilationException {
 		prepareChannelParams(scope, channels);
 		
-		firstIndex = scope.addParams(channels, optional, positional);
+		firstIndex = scope.addParams(channels, optional, positional, this);
 		argCount = channels.size() + optional.size() + positional.size();
 		return null;
 	}
+	
+	@Override
+	protected void scanNotSet(WrapperNode w, Scope scope, List<Param> optional)
+			throws CompilationException {
+	}
 
+	@Override
+	protected void scanNamed(WrapperNode w, Scope scope, List<Param> params,
+			List<Pair<Param, String>> dynamicOptimized) throws CompilationException {
+	}
+
+	@Override
+	protected void optimizePositional(WrapperNode w, Scope scope, List<Param> params,
+			List<Pair<Param, String>> dynamicOptimized) throws CompilationException {
+	}
+
 	@SuppressWarnings("unchecked")
 	@Override
 	protected void setArg(WrapperNode w, Param r, Object value) throws CompilationException {



More information about the Swift-commit mailing list