@Override public int getIterationDepth(Map<String, Integer> inputDepths) throws IterationTypeMismatchException { if (isLeaf()) // No children! throw new IterationTypeMismatchException( "Cross product with no children"); int temp = 0; for (IterationStrategyNode child : getChildren()) temp += child.getIterationDepth(inputDepths); return temp; } }
@Override public int getIterationDepth(Map<String, Integer> inputDepths) throws IterationTypeMismatchException { // Check that all input depths are the same if (isLeaf()) // No children! throw new IterationTypeMismatchException("Dot product with no children"); int depth = getChildAt(0).getIterationDepth(inputDepths); for (IterationStrategyNode childNode : getChildren()) if (childNode.getIterationDepth(inputDepths) != depth) throw new IterationTypeMismatchException( "Mismatched input types for dot product node"); return depth; } }
/** * If this node receives a job it will always be pushed without modification * up to the parent */ @Override public void receiveJob(int inputIndex, Job newJob) { pushJob(newJob); }
private IterationStrategyImpl getISCross(int depthA, int depthB) { IterationStrategyImpl is1 = new IterationStrategyImpl(); NamedInputPortNode nipn1 = new NamedInputPortNode("a", depthA); NamedInputPortNode nipn2 = new NamedInputPortNode("b", depthB); is1.addInput(nipn1); is1.addInput(nipn2); CrossProduct cp = new CrossProduct(); nipn1.setParent(cp); nipn2.setParent(cp); cp.setParent(is1.getTerminalNode()); return is1; }
private IterationStrategyImpl getISDot(int depthA, int depthB) { IterationStrategyImpl is1 = new IterationStrategyImpl(); NamedInputPortNode nipn1 = new NamedInputPortNode("a", depthA); NamedInputPortNode nipn2 = new NamedInputPortNode("b", depthB); is1.addInput(nipn1); is1.addInput(nipn2); DotProduct dp = new DotProduct(); nipn1.setParent(dp); nipn2.setParent(dp); dp.setParent(is1.getTerminalNode()); return is1; }
/** * Connect up a new named input port node to the first child of the terminal * node. If the terminal node doesn't have any children then create a new * cross product node, connect it to the terminal and connect the new input * port node to the cross product (saneish default behaviour) * * @param nipn */ public synchronized void connectDefault(NamedInputPortNode nipn) { if (terminal.getChildCount() == 0) { CrossProduct cp = new CrossProduct(); cp.setParent(terminal); nipn.setParent(cp); } else nipn.setParent((AbstractIterationStrategyNode) terminal .getChildAt(0)); }
@Before public void makeNodes() throws Exception { root = new DummyTerminalNode(); input1 = new NamedInputPortNode("input1", 1); input2 = new NamedInputPortNode("input2", 2); crossProduct1 = new CrossProduct(); crossProduct2 = new CrossProduct(); dotProduct1 = new DotProduct(); dotProduct2 = new DotProduct(); }
@Override public synchronized void insert(MutableTreeNode child, int index) { if (getChildCount() > 0 && getChildAt(0) != child) throw new IllegalStateException( "The terminal node can have maximum one child"); super.insert(child, index); } }
@Override public synchronized void insert(MutableTreeNode child) { insert(child, getChildCount()); }
@Test public void cantSetSelfParent() throws Exception { crossProduct1.insert(dotProduct1); try { dotProduct1.setParent(dotProduct1); fail("Didn't throw IllegalArgumentException"); } catch (IllegalArgumentException ex) { // Make sure we didn't loose our old parent and // ended up in a funny state assertEquals(crossProduct1, dotProduct1.getParent()); assertEquals(dotProduct1, crossProduct1.getChildAt(0)); } }
@Test public void cantAddSelf() throws Exception { dotProduct1.setParent(crossProduct1); try { dotProduct1.insert(dotProduct1); fail("Didn't throw IllegalArgumentException"); } catch (IllegalArgumentException ex) { // Make sure we didn't loose our old parent and // ended up in a funny state assertEquals(crossProduct1, dotProduct1.getParent()); assertEquals(dotProduct1, crossProduct1.getChildAt(0)); } }
@Override public int getIterationDepth(Map<String, Integer> inputDepths) throws IterationTypeMismatchException { return getTerminalNode().getIterationDepth(inputDepths); }
/** * Push the specified completion event to the parent node */ protected final void pushCompletion(Completion completion) { if (parent != null) parent.receiveCompletion(parent.getIndex(this), completion); }
protected CompletionState getCompletionState(String owningProcess) { if (ownerToCompletion.containsKey(owningProcess)) return ownerToCompletion.get(owningProcess); CompletionState cs = new CompletionState(getChildCount()); ownerToCompletion.put(owningProcess, cs); return cs; }
@Override public final void receiveJob(int inputIndex, Job newJob) { innerReceiveJob(inputIndex, newJob); if (newJob.getIndex().length == 0) pingCompletionState(inputIndex, newJob.getOwningProcess(), false, newJob.getContext()); }
@Override public void removeFromParent() { if (parent != null) { IterationStrategyNode oldParent = parent; parent = null; oldParent.remove(this); } }
/** * Completion events are passed straight through the same as jobs */ @Override public void receiveCompletion(int inputIndex, Completion completion) { pushCompletion(completion); }
/** * Returns true iff completions have been received for all other inputs. * * @param inputIndex * @param owningProcess * @return true iff completions have been received for all other inputs */ private boolean canClearCache(int inputIndex, String owningProcess) { boolean[] completionState = getCompletionState(owningProcess).inputComplete; for (int i = 0; i < completionState.length; i++) if (i != inputIndex && !completionState[i]) return false; return true; }
/** * Clear the child list and parent of this node */ @Override public final synchronized void clear() { for (IterationStrategyNode child : children) child.setParent(null); children.clear(); this.parent = null; }