public void ComputeHeuristic(InstNode node) { if (node.StaticCost != 0) { ex++; return; } node.StaticCost = 0; foreach (var x in node.Content) { node.StaticCost += InstructionRuntime.GetInstructionRuntime(x); } var leftH = 0u; var rightH = 0u; if (node.Left != null) { ComputeHeuristic(node.Left); leftH = node.Left.Heuristic + (uint)(node.Left.Content.Count * CacheMissOverhead); } if (node.Right != null) { ComputeHeuristic(node.Right); rightH = node.Right.Heuristic + (uint)(node.Right.Content.Count * CacheMissOverhead); } node.Heuristic = node.StaticCost + Math.Max(leftH, rightH); }
private void ExtractGVNodes(InstNode root, Dictionary <InstNode, GraphVizNode> nodeDict) { GraphTraversal.PerformTraversal <InstNode>(root, (node, fringeAdder) => { if (!nodeDict.ContainsKey(node)) { var ms = new MemoryStream(); var sw = new StreamWriter(ms); PrettyPrinter.PrettyPrint(node.Content, sw); sw.Flush(); ms.Seek(0, SeekOrigin.Begin); var data = new StreamReader(ms).ReadToEnd(); nodeDict.Add(node, new GraphVizNode() { Id = IdC++, Label = data }); if (node.Left != null) { fringeAdder(node.Left); } if (node.Right != null) { fringeAdder(node.Right); } } }); }
public InstNode FindNodeByInstAddress(InstNode node, uint address) { foreach (var x in node.Content) { if (x.Address == address) { return(node); } } if (node.Left != null) { var nd = FindNodeByInstAddress(node.Left, address); if (nd != null) { return(nd); } } if (node.Right != null) { var nd = FindNodeByInstAddress(node.Right, address); if (nd != null) { return(nd); } } return(null); }
public uint CalculateWCET(InstNode nd) { uint pc; if (visitedNodes.TryGetValue(nd, out pc)) { return(pc); } var cycles = (uint)(nd.Content.Count * 0); cycles += nd.StaticCost; var lc = 0u; var rc = 0u; if (nd.Left != null) { lc = CalculateWCET(nd.Left); } if (nd.Right != null) { rc = CalculateWCET(nd.Right); } cycles += Math.Max(lc, rc); visitedNodes.Add(nd, cycles); return(cycles); }
public ISet <Tuple <InstNode, InstNode> > FindCycles(InstNode root) { var cycles = new HashSet <Tuple <InstNode, InstNode> >(); FindCyclesRec(root, new HashSet <InstNode>(), cycles); return(cycles); }
public bool SearchRec(InstNode current, InstNode target, HashSet <InstNode> visitedNodes, ISet <InstNode> paths) { bool found = false; if (current == target) { found = true; } if (visitedNodes.Contains(current)) { return(found); } visitedNodes.Add(current); paths.Add(current); var pth = new HashSet <InstNode>(); if (current.Left != null && SearchRec(current.Left, target, visitedNodes, pth)) { found = true; foreach (var x in pth) { paths.Add(x); } } pth = new HashSet <InstNode>(); if (current.Right != null && SearchRec(current.Right, target, visitedNodes, pth)) { found = true; foreach (var x in pth) { paths.Add(x); } } return(found); }
private void LinearizeRec(InstNode node, ISet <InstNode> incNodes) { if (node.Left != null) { if (incNodes.Contains(node.Left)) { LinearizeRec(node.Left, incNodes); } else { node.Left = null; } } if (node.Right != null) { if (incNodes.Contains(node.Right)) { LinearizeRec(node.Right, incNodes); } else { node.Right = null; } } }
public uint CalculateWCET2(InstNode root) { var visitedNodes = new Dictionary <InstNode, Func <uint> >(); var cache = new Dictionary <InstNode, uint>(); GraphTraversal.PerformTraversal <InstNode>(root, (node, fringeAdder) => { if (visitedNodes.ContainsKey(node)) { return; } var cycles = (uint)(node.Content.Count * 0); cycles += node.StaticCost; Func <uint> lc = () => 0u; Func <uint> rc = () => 0u; if (node.Left != null) { fringeAdder(node.Left); lc = () => { uint c; if (cache.TryGetValue(node.Left, out c)) { return(c); } c = visitedNodes[node.Left](); cache.Add(node.Left, c); return(c); }; } if (node.Right != null) { fringeAdder(node.Right); rc = () => { uint c; if (cache.TryGetValue(node.Right, out c)) { return(c); } c = visitedNodes[node.Right](); cache.Add(node.Right, c); return(c); }; } visitedNodes.Add(node, () => cycles + Math.Max(lc(), rc())); }, true); return(visitedNodes[root]()); }
private ISet <Tuple <InstNode, InstNode> > FindCycles2(InstNode root) { var visitedNodes = new HashSet <InstNode>(); var cycles = new HashSet <Tuple <InstNode, InstNode> >(); GraphTraversal.PerformTraversal <InstNode>(root, (node, fringeAdder) => { if (visitedNodes.Contains(node)) { return; } visitedNodes.Add(node); if (node.Left != null) { if (visitedNodes.Contains(node.Left)) { cycles.Add(new Tuple <InstNode, InstNode>(node, node.Left)); } } if (node.Right != null) { if (visitedNodes.Contains(node.Right)) { cycles.Add(new Tuple <InstNode, InstNode>(node, node.Right)); } } if (node.Left != null) { if (!visitedNodes.Contains(node.Left)) { fringeAdder(node.Left); } } if (node.Right != null) { if (!visitedNodes.Contains(node.Right)) { fringeAdder(node.Right); } } }); return(cycles); }
private void FindCyclesRec(InstNode node, HashSet <InstNode> visitedNodes, ISet <Tuple <InstNode, InstNode> > cycles) { if (visitedNodes.Contains(node)) { return; } visitedNodes.Add(node); if (node.Left != null) { if (visitedNodes.Contains(node.Left)) { cycles.Add(new Tuple <InstNode, InstNode>(node, node.Left)); } } if (node.Right != null) { if (visitedNodes.Contains(node.Right)) { cycles.Add(new Tuple <InstNode, InstNode>(node, node.Right)); } } if (node.Left != null) { if (!visitedNodes.Contains(node.Left)) { FindCyclesRec(node.Left, visitedNodes, cycles); } } if (node.Right != null) { if (!visitedNodes.Contains(node.Right)) { FindCyclesRec(node.Right, visitedNodes, cycles); } } }
public void Linearize(InstNode root) { var ind = 0; var gv = new GraphViz(); while (true) { var cycles = FindCycles(root); if (cycles.Count == 0) { return; } var cyclesN = new List <Tuple <Tuple <InstNode, InstNode>, ISet <InstNode> > >(); foreach (var x in cycles) { var set = new HashSet <InstNode>(); SearchRec(x.Item2, x.Item1, new HashSet <InstNode>(), set); cyclesN.Add(new Tuple <Tuple <InstNode, InstNode>, ISet <InstNode> >(x, set)); } var availableCycles = new List <Tuple <Tuple <InstNode, InstNode>, ISet <InstNode> > >(); foreach (var x in cyclesN) { var valid = true; foreach (var y in cyclesN) { if (x != y) { foreach (var e in x.Item2) { if (y.Item2.Contains(e)) { valid = false; } } } } if (valid) { availableCycles.Add(x); } } if (availableCycles.Count == 0) { throw new InvalidOperationException("Error During Linearization"); } foreach (var y in availableCycles) { var set = y.Item2; var x = y.Item1; var n1 = x.Item2; var n2 = x.Item1.Clone(); var n3 = x.Item1; if (n2.Left == n1) { n2.Left = null; n3.Left = null; } if (n2.Right == n1) { n2.Right = null; n3.Right = null; } var n3l = n3.Left; var n3r = n3.Right; n3.Left = null; n3.Right = null; var lst = n1.CloneAndReturnSN(n3); lst.Item2.Left = n3l; lst.Item2.Right = n3r; n3.Left = n3l; n3.Right = n3l; LinearizeRec(n1, set); var ccs = new CycleCountSelector(gv.CreateGraph(n1)); if (ccs.ShowDialog() != System.Windows.Forms.DialogResult.OK) { Console.WriteLine("Operation Cancelled"); Environment.Exit(0); } var N = ccs.NumberOfRepetitions; //var N = 3; if (N == 1) { n1.Left = lst.Item1.Left; n2.Right = lst.Item1.Right; continue; } var last = new Tuple <InstNode, InstNode>(n1, n3); for (int j = 2; j < N; j++) { var clone = last.Item1.CloneAndReturnSN(last.Item2); //var clone = n1.CloneAndReturnSN(n3); last.Item2.Left = clone.Item1; last = clone; } //last.Item2.Left = n2.Left; //last.Item2.Right = n2.Right; last.Item2.Left = lst.Item1; } if (cycles.Count == availableCycles.Count) { return; } } }