void Network::topologicalSortExecutionNetwork() { // Note: we don't need to do a full-fledged topological sort here, as we do not // have any DAG, we actually have a dependency tree. This way we can just do a // depth-first search, with ref-counting to account for diamond shapes in the tree. // this is similar to the wavefront design pattern used in parallelization // Using DFS here also has the advantage that it makes as much as possible use // of cache locality // 1- get all the nodes and count the number of refs they have NodeVector nodes = depthFirstSearch(_executionNetworkRoot); map<NetworkNode*, int> refs; // this initialization should be useless, but let's do it anyway for clarity for (int i=0; i<(int)nodes.size(); i++) refs[nodes[i]] = 0; // count the number of refs for each node for (int i=0; i<(int)nodes.size(); i++) { const NodeVector& children = nodes[i]->children(); for (int j=0; j<(int)children.size(); j++) { refs[children[j]] += 1; } } // 2- do DFS again, manually this time and only visit node which have no refs anymore _toposortedNetwork.clear(); NodeStack toVisit; toVisit.push(_executionNetworkRoot); refs[_executionNetworkRoot] = 1; while (!toVisit.empty()) { NetworkNode* currentNode = toVisit.top(); toVisit.pop(); if (--refs[currentNode] == 0) { _toposortedNetwork.push_back(currentNode->algorithm()); // keep this node, it is good const NodeVector& children = currentNode->children(); for (int i=0; i<(int)children.size(); i++) { toVisit.push(children[i]); } } } E_DEBUG(ENetwork, "-------------------------------------------------------------------------------------------"); for (int i=0; i<(int)_toposortedNetwork.size(); i++) { E_DEBUG_NONL(ENetwork, " → " << _toposortedNetwork[i]->name()); } E_DEBUG(ENetwork, ""); // for adding a newline E_DEBUG(ENetwork, "-------------------------------------------------------------------------------------------"); }
Node PicklerPrivate::fromCaseOperator(Kind k, uint32_t nchildren) { kind::MetaKind m = metaKindOf(k); bool parameterized = (m == kind::metakind::PARAMETERIZED); uint32_t npops = nchildren + (parameterized? 1 : 0); NodeStack aux; while(npops > 0) { Assert(!d_stack.empty()); Node top = d_stack.top(); aux.push(top); d_stack.pop(); --npops; } NodeBuilder<> nb(d_nm, k); while(!aux.empty()) { Node top = aux.top(); nb << top; aux.pop(); } return nb; }