Search in sources :

Example 6 with TopologicalOrderIterator

use of org.jgrapht.traverse.TopologicalOrderIterator in project jstorm by alibaba.

the class SubtopologyBolt method prepare.

@Override
public void prepare(Map conf, TopologyContext context, BatchOutputCollector batchCollector) {
    int thisComponentNumTasks = context.getComponentTasks(context.getThisComponentId()).size();
    for (Node n : _nodes) {
        if (n.stateInfo != null) {
            State s = n.stateInfo.spec.stateFactory.makeState(conf, context, context.getThisTaskIndex(), thisComponentNumTasks);
            context.setTaskData(n.stateInfo.id, s);
        }
    }
    DirectedSubgraph<Node, Object> subgraph = new DirectedSubgraph(_graph, _nodes, null);
    TopologicalOrderIterator it = new TopologicalOrderIterator<Node, Object>(subgraph);
    int stateIndex = 0;
    while (it.hasNext()) {
        Node n = (Node) it.next();
        if (n instanceof ProcessorNode) {
            ProcessorNode pn = (ProcessorNode) n;
            String batchGroup = _batchGroups.get(n);
            if (!_myTopologicallyOrdered.containsKey(batchGroup)) {
                _myTopologicallyOrdered.put(batchGroup, new ArrayList());
            }
            _myTopologicallyOrdered.get(batchGroup).add(pn.processor);
            List<String> parentStreams = new ArrayList();
            List<Factory> parentFactories = new ArrayList();
            for (Node p : TridentUtils.getParents(_graph, n)) {
                parentStreams.add(p.streamId);
                if (_nodes.contains(p)) {
                    parentFactories.add(_outputFactories.get(p));
                } else {
                    if (!_roots.containsKey(p.streamId)) {
                        _roots.put(p.streamId, new InitialReceiver(p.streamId, getSourceOutputFields(context, p.streamId)));
                    }
                    _roots.get(p.streamId).addReceiver(pn.processor);
                    parentFactories.add(_roots.get(p.streamId).getOutputFactory());
                }
            }
            List<TupleReceiver> targets = new ArrayList();
            boolean outgoingNode = false;
            for (Node cn : TridentUtils.getChildren(_graph, n)) {
                if (_nodes.contains(cn)) {
                    targets.add(((ProcessorNode) cn).processor);
                } else {
                    outgoingNode = true;
                }
            }
            if (outgoingNode) {
                targets.add(new BridgeReceiver(batchCollector));
            }
            TridentContext triContext = new TridentContext(pn.selfOutFields, parentFactories, parentStreams, targets, pn.streamId, stateIndex, batchCollector);
            pn.processor.prepare(conf, context, triContext);
            _outputFactories.put(n, pn.processor.getOutputFactory());
        }
        stateIndex++;
    }
// TODO: get prepared one time into executor data... need to avoid the ser/deser
// for each task (probably need storm to support boltfactory)
}
Also used : DirectedSubgraph(org.jgrapht.graph.DirectedSubgraph) TopologicalOrderIterator(org.jgrapht.traverse.TopologicalOrderIterator) Factory(storm.trident.tuple.TridentTuple.Factory) ProjectionFactory(storm.trident.tuple.TridentTupleView.ProjectionFactory) RootFactory(storm.trident.tuple.TridentTupleView.RootFactory) State(storm.trident.state.State) TridentContext(storm.trident.planner.processor.TridentContext)

Example 7 with TopologicalOrderIterator

use of org.jgrapht.traverse.TopologicalOrderIterator in project storm by apache.

the class ProcessorBoltDelegate method prepare.

void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
    this.stormConf = stormConf;
    topologyContext = context;
    outputCollector = collector;
    DirectedSubgraph<Node, Edge> subgraph = new DirectedSubgraph<>(graph, new HashSet<>(nodes), null);
    TopologicalOrderIterator<Node, Edge> it = new TopologicalOrderIterator<>(subgraph);
    while (it.hasNext()) {
        Node node = it.next();
        if (!(node instanceof ProcessorNode)) {
            throw new IllegalStateException("Not a processor node " + node);
        }
        ProcessorNode processorNode = (ProcessorNode) node;
        List<ProcessorNode> children = StreamUtil.getChildren(subgraph, processorNode);
        ProcessorContext processorContext;
        if (children.isEmpty()) {
            processorContext = createEmittingContext(processorNode);
        } else {
            Multimap<String, ProcessorNode> streamToChildren = ArrayListMultimap.create();
            for (ProcessorNode child : children) {
                for (String stream : child.getParentStreams(processorNode)) {
                    streamToChildren.put(stream, child);
                }
            }
            ForwardingProcessorContext forwardingContext = new ForwardingProcessorContext(processorNode, streamToChildren);
            if (hasOutgoingChild(processorNode, new HashSet<>(children))) {
                processorContext = new ChainedProcessorContext(processorNode, forwardingContext, createEmittingContext(processorNode));
            } else {
                processorContext = forwardingContext;
            }
        }
        processorNode.initProcessorContext(processorContext);
    }
    if (timestampField != null) {
        for (EmittingProcessorContext ctx : emittingProcessorContexts) {
            ctx.setTimestampField(timestampField);
        }
    }
    for (String stream : streamToInitialProcessors.keySet()) {
        streamToInputTaskCount.put(stream, getStreamInputTaskCount(context, stream));
    }
}
Also used : ForwardingProcessorContext(org.apache.storm.streams.processors.ForwardingProcessorContext) DirectedSubgraph(org.jgrapht.graph.DirectedSubgraph) TopologicalOrderIterator(org.jgrapht.traverse.TopologicalOrderIterator) ChainedProcessorContext(org.apache.storm.streams.processors.ChainedProcessorContext) ProcessorContext(org.apache.storm.streams.processors.ProcessorContext) EmittingProcessorContext(org.apache.storm.streams.processors.EmittingProcessorContext) ForwardingProcessorContext(org.apache.storm.streams.processors.ForwardingProcessorContext) ChainedProcessorContext(org.apache.storm.streams.processors.ChainedProcessorContext) EmittingProcessorContext(org.apache.storm.streams.processors.EmittingProcessorContext)

Example 8 with TopologicalOrderIterator

use of org.jgrapht.traverse.TopologicalOrderIterator in project storm by apache.

the class SubtopologyBolt method prepare.

@Override
public void prepare(Map conf, TopologyContext context, BatchOutputCollector batchCollector) {
    int thisComponentNumTasks = context.getComponentTasks(context.getThisComponentId()).size();
    for (Node n : _nodes) {
        if (n.stateInfo != null) {
            State s = n.stateInfo.spec.stateFactory.makeState(conf, context, context.getThisTaskIndex(), thisComponentNumTasks);
            context.setTaskData(n.stateInfo.id, s);
        }
    }
    DirectedSubgraph<Node, Object> subgraph = new DirectedSubgraph(_graph, _nodes, null);
    TopologicalOrderIterator it = new TopologicalOrderIterator<>(subgraph);
    int stateIndex = 0;
    while (it.hasNext()) {
        Node n = (Node) it.next();
        if (n instanceof ProcessorNode) {
            ProcessorNode pn = (ProcessorNode) n;
            String batchGroup = _batchGroups.get(n);
            if (!_myTopologicallyOrdered.containsKey(batchGroup)) {
                _myTopologicallyOrdered.put(batchGroup, new ArrayList());
            }
            _myTopologicallyOrdered.get(batchGroup).add(pn.processor);
            List<String> parentStreams = new ArrayList<>();
            List<Factory> parentFactories = new ArrayList<>();
            for (Node p : TridentUtils.getParents(_graph, n)) {
                parentStreams.add(p.streamId);
                if (_nodes.contains(p)) {
                    parentFactories.add(_outputFactories.get(p));
                } else {
                    if (!_roots.containsKey(p.streamId)) {
                        _roots.put(p.streamId, new InitialReceiver(p.streamId, getSourceOutputFields(context, p.streamId)));
                    }
                    _roots.get(p.streamId).addReceiver(pn.processor);
                    parentFactories.add(_roots.get(p.streamId).getOutputFactory());
                }
            }
            List<TupleReceiver> targets = new ArrayList<>();
            boolean outgoingNode = false;
            for (Node cn : TridentUtils.getChildren(_graph, n)) {
                if (_nodes.contains(cn)) {
                    targets.add(((ProcessorNode) cn).processor);
                } else {
                    outgoingNode = true;
                }
            }
            if (outgoingNode) {
                targets.add(new BridgeReceiver(batchCollector));
            }
            TridentContext triContext = new TridentContext(pn.selfOutFields, parentFactories, parentStreams, targets, pn.streamId, stateIndex, batchCollector);
            pn.processor.prepare(conf, context, triContext);
            _outputFactories.put(n, pn.processor.getOutputFactory());
        }
        stateIndex++;
    }
// TODO: get prepared one time into executor data... need to avoid the ser/deser
// for each task (probably need storm to support boltfactory)
}
Also used : DirectedSubgraph(org.jgrapht.graph.DirectedSubgraph) ArrayList(java.util.ArrayList) TopologicalOrderIterator(org.jgrapht.traverse.TopologicalOrderIterator) Factory(org.apache.storm.trident.tuple.TridentTuple.Factory) ProjectionFactory(org.apache.storm.trident.tuple.TridentTupleView.ProjectionFactory) RootFactory(org.apache.storm.trident.tuple.TridentTupleView.RootFactory) State(org.apache.storm.trident.state.State) TridentContext(org.apache.storm.trident.planner.processor.TridentContext)

Example 9 with TopologicalOrderIterator

use of org.jgrapht.traverse.TopologicalOrderIterator in project jop by jop-devel.

the class WCAInvoker method runAnalysis.

///////////////////////////////////////////////////////////////////////////////
// Private methods
///////////////////////////////////////////////////////////////////////////////
private Set<MethodInfo> runAnalysis(DirectedGraph<ExecutionContext, ContextEdge> reversed) {
    // Phew. The WCA only runs on acyclic callgraphs, we can therefore assume the
    // reversed graph to be a DAG
    TopologicalOrderIterator<ExecutionContext, ContextEdge> topOrder = new TopologicalOrderIterator<ExecutionContext, ContextEdge>(reversed);
    Set<MethodInfo> changed = new LinkedHashSet<MethodInfo>();
    while (topOrder.hasNext()) {
        ExecutionContext node = topOrder.next();
        // At times like this I really wish Java would have type aliases ..
        RecursiveWcetAnalysis<AnalysisContextLocal>.LocalWCETSolution<AnalysisContextLocal> sol = recursiveAnalysis.computeSolution(node.getMethodInfo(), new AnalysisContextLocal(cacheApproximation, node.getCallString()));
        wcaNodeFlow.put(node, sol.getNodeFlowVirtual());
        // TODO some logging would be nice, keep target-method WCET for comparison of speedup
        if (node.getMethodInfo().equals(wcetTool.getTargetMethod())) {
            lastWCET = sol.getCost().getCost();
            logger.info("WCET: " + lastWCET);
        }
        changed.add(node.getMethodInfo());
    }
    return changed;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ExecutionContext(com.jopdesign.common.code.ExecutionContext) TopologicalOrderIterator(org.jgrapht.traverse.TopologicalOrderIterator) MethodInfo(com.jopdesign.common.MethodInfo) AnalysisContextLocal(com.jopdesign.wcet.analysis.AnalysisContextLocal) RecursiveWcetAnalysis(com.jopdesign.wcet.analysis.RecursiveWcetAnalysis) ContextEdge(com.jopdesign.common.code.CallGraph.ContextEdge)

Aggregations

TopologicalOrderIterator (org.jgrapht.traverse.TopologicalOrderIterator)9 DirectedSubgraph (org.jgrapht.graph.DirectedSubgraph)4 ArrayList (java.util.ArrayList)3 MethodInfo (com.jopdesign.common.MethodInfo)2 ContextEdge (com.jopdesign.common.code.CallGraph.ContextEdge)2 ExecutionContext (com.jopdesign.common.code.ExecutionContext)2 LinkedHashSet (java.util.LinkedHashSet)2 TridentContext (storm.trident.planner.processor.TridentContext)2 State (storm.trident.state.State)2 Factory (storm.trident.tuple.TridentTuple.Factory)2 ProjectionFactory (storm.trident.tuple.TridentTupleView.ProjectionFactory)2 RootFactory (storm.trident.tuple.TridentTupleView.RootFactory)2 InvokeEdge (com.jopdesign.common.code.CallGraph.InvokeEdge)1 MethodNode (com.jopdesign.common.code.CallGraph.MethodNode)1 AppInfoError (com.jopdesign.common.misc.AppInfoError)1 AnalysisManager (com.jopdesign.jcopter.analysis.AnalysisManager)1 ExecFrequencyProvider (com.jopdesign.jcopter.analysis.ExecFrequencyProvider)1 LocalExecFrequencyProvider (com.jopdesign.jcopter.analysis.LocalExecFrequencyProvider)1 GreedyOrder (com.jopdesign.jcopter.greedy.GreedyConfig.GreedyOrder)1 AnalysisContextLocal (com.jopdesign.wcet.analysis.AnalysisContextLocal)1