Example #1
0
        public static ICompoundGraph <TVertex, TEdge> CreateCompoundGraph <TVertex, TEdge>(
            int vertexCount,
            int edgeCount,
            [NotNull, InstantHandle] Func <int, TVertex> vertexFactory,
            [NotNull, InstantHandle] Func <TVertex, TVertex, TEdge> edgeFactory,
            [NotNull] Random random)
            where TEdge : IEdge <TVertex>
        {
            var graph = new CompoundGraph <TVertex, TEdge>(false, vertexCount);

            var verticesMap = new Dictionary <int, TVertex>();

            for (int i = 0; i < vertexCount; ++i)
            {
                TVertex vertex = vertexFactory(i);
                verticesMap[i] = vertex;
                graph.AddVertex(vertex);
            }

            for (int i = 0; i < vertexCount / 3; ++i)
            {
                int     vertexIndex1;
                int     vertexIndex2;
                TVertex vertex1;
                TVertex vertex2;
                do
                {
                    vertexIndex1 = random.Next(vertexCount);
                    vertexIndex2 = random.Next(vertexCount);
                    vertex1      = verticesMap[vertexIndex1];
                    vertex2      = verticesMap[vertexIndex2];
                } while (vertexIndex1 == vertexIndex2 ||
                         graph.IsChildVertex(vertex2));

                graph.AddChildVertex(vertex1, vertex2);
            }

            for (int i = 0; i < edgeCount; ++i)
            {
                int     childIndex;
                int     parentIndex;
                TVertex child;
                TVertex parent;
                do
                {
                    childIndex  = random.Next(vertexCount);
                    parentIndex = random.Next(vertexCount);
                    child       = verticesMap[childIndex];
                    parent      = verticesMap[parentIndex];
                } while (childIndex == parentIndex ||
                         graph.ContainsEdge(parent, child));

                // Create the edge between the 2 vertex
                graph.AddEdge(edgeFactory(parent, child));
            }

            return(graph);
        }
        private IBidirectionalGraph <object, IEdge <object> > ConvertGraph(PocGraph graph)
        {
            var g = new CompoundGraph <object, IEdge <object> >();

            foreach (var item in graph.Vertices)
            {
                g.AddVertex(item.ID);
            }

            foreach (var item in graph.Edges)
            {
                g.AddEdge(new Edge <object>(item.Source.ID, item.Target.ID));
            }


            return(g);
        }
        public TestCompoundLayout()
        {
            InitializeComponent();

            var g = new CompoundGraph<object, IEdge<object>>();

            var vertices = new string[30];
            for (int i = 0; i < 30; i++)
            {
                vertices[i] = i.ToString(CultureInfo.InvariantCulture);
                g.AddVertex(vertices[i]);
            }

            for (int i = 6; i < 15; i++)
            {
                g.AddChildVertex(vertices[i%5], vertices[i]);
            }
            g.AddChildVertex(vertices[5], vertices[4]);
            g.AddChildVertex(vertices[5], vertices[2]);
            g.AddChildVertex(vertices[16], vertices[0]);
            g.AddChildVertex(vertices[16], vertices[1]);
            g.AddChildVertex(vertices[16], vertices[3]);
            g.AddChildVertex(vertices[16], vertices[20]);
            g.AddChildVertex(vertices[16], vertices[21]);
            g.AddChildVertex(vertices[16], vertices[22]);
            g.AddChildVertex(vertices[16], vertices[23]);
            g.AddChildVertex(vertices[16], vertices[24]);
            g.AddChildVertex(vertices[4], vertices[25]);
            g.AddChildVertex(vertices[4], vertices[26]);
            g.AddChildVertex(vertices[4], vertices[27]);

            g.AddEdge(new Edge<object>(vertices[0], vertices[1]));
            g.AddEdge(new Edge<object>(vertices[0], vertices[2]));
            g.AddEdge(new Edge<object>(vertices[2], vertices[4]));
            g.AddEdge(new Edge<object>(vertices[0], vertices[7]));
            g.AddEdge(new Edge<object>(vertices[8], vertices[7]));
            g.AddEdge(new Edge<object>(vertices[3], vertices[20]));
            g.AddEdge(new Edge<object>(vertices[20], vertices[21]));
            g.AddEdge(new Edge<object>(vertices[20], vertices[22]));
            g.AddEdge(new Edge<object>(vertices[22], vertices[23]));
            g.AddEdge(new Edge<object>(vertices[23], vertices[24]));
            g.AddEdge(new Edge<object>(vertices[0], vertices[28]));
            g.AddEdge(new Edge<object>(vertices[0], vertices[29]));
            g.AddEdge(new Edge<object>(vertices[25], vertices[27]));
            g.AddEdge(new Edge<object>(vertices[26], vertices[25]));
            g.AddEdge(new Edge<object>(vertices[14], vertices[27]));
            g.AddEdge(new Edge<object>(vertices[14], vertices[26]));
            g.AddEdge(new Edge<object>(vertices[14], vertices[25]));
            g.AddEdge(new Edge<object>(vertices[26], vertices[27]));

            layout.LayoutMode = LayoutMode.Automatic;
            layout.LayoutAlgorithmType = "CompoundFDP";
            layout.OverlapRemovalConstraint = AlgorithmConstraints.Automatic;
            layout.OverlapRemovalAlgorithmType = "FSA";
            layout.HighlightAlgorithmType = "Simple";
            layout.Graph = g;
        }
Example #4
0
        public TestCompoundLayout()
        {
            InitializeComponent();

            var g = new CompoundGraph <object, IEdge <object> >();

            var vertices = new string[30];

            for (int i = 0; i < 30; i++)
            {
                vertices[i] = i.ToString();
                g.AddVertex(vertices[i]);
            }

            for (int i = 6; i < 15; i++)
            {
                g.AddChildVertex(vertices[i % 5], vertices[i]);
            }
            g.AddChildVertex(vertices[5], vertices[4]);
            g.AddChildVertex(vertices[5], vertices[2]);
            g.AddChildVertex(vertices[16], vertices[0]);
            g.AddChildVertex(vertices[16], vertices[1]);
            g.AddChildVertex(vertices[16], vertices[3]);
            g.AddChildVertex(vertices[16], vertices[20]);
            g.AddChildVertex(vertices[16], vertices[21]);
            g.AddChildVertex(vertices[16], vertices[22]);
            g.AddChildVertex(vertices[16], vertices[23]);
            g.AddChildVertex(vertices[16], vertices[24]);
            g.AddChildVertex(vertices[4], vertices[25]);
            g.AddChildVertex(vertices[4], vertices[26]);
            g.AddChildVertex(vertices[4], vertices[27]);

            g.AddEdge(new Edge <object>(vertices[0], vertices[1]));
            g.AddEdge(new Edge <object>(vertices[0], vertices[2]));
            g.AddEdge(new Edge <object>(vertices[2], vertices[4]));
            g.AddEdge(new Edge <object>(vertices[0], vertices[7]));
            g.AddEdge(new Edge <object>(vertices[8], vertices[7]));
            //g.AddEdge(new Edge<object>(vertices[13], vertices[12]));
            g.AddEdge(new Edge <object>(vertices[3], vertices[20]));
            g.AddEdge(new Edge <object>(vertices[20], vertices[21]));
            g.AddEdge(new Edge <object>(vertices[20], vertices[22]));
            g.AddEdge(new Edge <object>(vertices[22], vertices[23]));
            g.AddEdge(new Edge <object>(vertices[23], vertices[24]));
            g.AddEdge(new Edge <object>(vertices[0], vertices[28]));
            g.AddEdge(new Edge <object>(vertices[0], vertices[29]));
            g.AddEdge(new Edge <object>(vertices[25], vertices[27]));
            g.AddEdge(new Edge <object>(vertices[26], vertices[25]));
            g.AddEdge(new Edge <object>(vertices[14], vertices[27]));
            g.AddEdge(new Edge <object>(vertices[14], vertices[26]));
            g.AddEdge(new Edge <object>(vertices[14], vertices[25]));
            g.AddEdge(new Edge <object>(vertices[26], vertices[27]));


            layout.LayoutMode                  = LayoutMode.Automatic;
            layout.LayoutAlgorithmType         = "CompoundFDP";
            layout.OverlapRemovalConstraint    = AlgorithmConstraints.Automatic;
            layout.OverlapRemovalAlgorithmType = "FSA";
            layout.HighlightAlgorithmType      = "Simple";
            layout.Graph = g;
        }
        private void InitGraphs()
        {
            graphs = new CompoundGraph <object, IEdge <object> > [6];

            #region Big graph
            var g = new CompoundGraph <object, IEdge <object> >();

            string[] vertices = InitVertices(g, 20);

            for (int i = 6; i < 15; i++)
            {
                g.AddChildVertex(vertices[i % 5], vertices[i]);
            }
            g.AddChildVertex(vertices[5], vertices[4]);
            g.AddChildVertex(vertices[5], vertices[2]);
            g.AddChildVertex(vertices[16], vertices[0]);
            g.AddChildVertex(vertices[16], vertices[1]);
            g.AddChildVertex(vertices[16], vertices[3]);

            g.AddEdge(new Edge <object>(vertices[0], vertices[1]));
            g.AddEdge(new Edge <object>(vertices[0], vertices[2]));
            g.AddEdge(new Edge <object>(vertices[2], vertices[4]));
            g.AddEdge(new Edge <object>(vertices[0], vertices[7]));
            g.AddEdge(new Edge <object>(vertices[8], vertices[7]));
            graphs[BIG_GRAPH] = g;
            #endregion

            #region Small graph

            g        = new CompoundGraph <object, IEdge <object> >();
            vertices = InitVertices(g, 2);

            //add the containments

            /*g.AddChildVertex(vertices[0], vertices[1]);
            *  g.AddChildVertex(vertices[0], vertices[2]);*/

            //add the edges

            /*g.AddEdge(new Edge<object>(vertices[2], vertices[3]));
            *  g.AddEdge(new Edge<object>(vertices[3], vertices[4]));*/

            graphs[SMALL_GRAPH] = g;
            #endregion

            #region Flat graph

            g        = new CompoundGraph <object, IEdge <object> >();
            vertices = InitVertices(g, 10);

            g.AddEdge(new Edge <object>(vertices[0], vertices[1]));
            g.AddEdge(new Edge <object>(vertices[1], vertices[2]));
            g.AddEdge(new Edge <object>(vertices[2], vertices[3]));
            g.AddEdge(new Edge <object>(vertices[3], vertices[0]));
            g.AddEdge(new Edge <object>(vertices[1], vertices[3]));
            g.AddEdge(new Edge <object>(vertices[2], vertices[0]));

            graphs[FLAT_GRAPH] = g;
            #endregion

            #region Repulsion graph
            g        = new CompoundGraph <object, IEdge <object> >();
            vertices = InitVertices(g, 20);

            graphs[REP_GRAPH] = g;
            #endregion

            #region Star

            g        = new CompoundGraph <object, IEdge <object> >();
            vertices = InitVertices(g, 13);

            for (int i = 1; i < 13; i++)
            {
                g.AddEdge(new Edge <object>(vertices[0], vertices[i]));
            }

            for (int i = 0; i < 4; i++)
            {
                g.AddEdge(new Edge <object>(vertices[i * 3 + 1], vertices[i * 3 + 2]));
                g.AddEdge(new Edge <object>(vertices[i * 3 + 1], vertices[i * 3 + 3]));
                g.AddEdge(new Edge <object>(vertices[i * 3 + 2], vertices[i * 3 + 3]));
            }
            graphs[STAR_GRAPH] = g;
            #endregion

            #region Combined

            g        = new CompoundGraph <object, IEdge <object> >();
            vertices = InitVertices(g, 50);

            //add the containments
            g.AddChildVertex(vertices[0], vertices[1]);
            g.AddChildVertex(vertices[0], vertices[2]);

            //add the edges
            g.AddEdge(new Edge <object>(vertices[2], vertices[3]));
            g.AddEdge(new Edge <object>(vertices[3], vertices[4]));

            g.AddEdge(new Edge <object>(vertices[10], vertices[11]));
            g.AddEdge(new Edge <object>(vertices[11], vertices[12]));
            g.AddEdge(new Edge <object>(vertices[12], vertices[13]));
            g.AddEdge(new Edge <object>(vertices[13], vertices[10]));

            for (int i = 6; i < 15; i++)
            {
                g.AddChildVertex(vertices[i % 5 + 20], vertices[i + 20]);
            }
            g.AddChildVertex(vertices[25], vertices[24]);
            g.AddChildVertex(vertices[25], vertices[22]);
            g.AddChildVertex(vertices[36], vertices[20]);
            g.AddChildVertex(vertices[36], vertices[21]);
            g.AddChildVertex(vertices[36], vertices[23]);

            g.AddEdge(new Edge <object>(vertices[20], vertices[21]));
            g.AddEdge(new Edge <object>(vertices[20], vertices[22]));
            g.AddEdge(new Edge <object>(vertices[22], vertices[24]));
            g.AddEdge(new Edge <object>(vertices[20], vertices[27]));
            g.AddEdge(new Edge <object>(vertices[28], vertices[27]));

            graphs[COMBINED_GRAPH] = g;
            #endregion
        }
        private IBidirectionalGraph<object, IEdge<object>> ConvertGraph(PocGraph graph)
        {
            var g = new CompoundGraph<object, IEdge<object>>();

            foreach (var item in graph.Vertices)
            {
                g.AddVertex(item.ID);

            }

            foreach (var item in graph.Edges)
            {
                g.AddEdge(new Edge<object>(item.Source.ID, item.Target.ID));
            }


            return g;
        }
        void RecGraph(MainForm frm, string file)
        {

            var g = new CompoundGraph<object, IEdge<object>>();

            var map = new Dictionary<uint, CodeBlock>();

            string[] ent = File.ReadAllText(file).Split(new string[] { "block:" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string e in ent)
            {
                string[] st = e.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

                var entr = new CodeBlock();

                entr["block"] = st[0];

                for (int i = 1; i < st.Length; i++)
                {
                    if (st[i] == "{")
                        break;
                    else
                    {
                        string[] lst = st[i].Split(new char[] { ':' }, 2);
                        if (lst.Length == 2)
                        {
                            entr[lst[0]] = lst[1];
                        }
                    }
                }

                map[entr.block] = entr;
            }

            map = map.Where(x => x.Value.runs > 0).ToDictionary(x => x.Key, x => x.Value);

            long v = map.Sum(x => x.Value.cost);

            CodeBlock.gcost = v;

            double runcumsum = 0;

            var intrnode = map.Values.OrderByDescending(x => x.cost).Where(x => (runcumsum += x.cost) < 0.60 * v).Take(1000).ToList();

            var varp = intrnode.Select(x => x.hash);

            long v1 = intrnode.Sum(x => x.cost);

            foreach (var r in intrnode)
            {
                g.AddVertex(r);
            }

            var newstuff = new List<CodeBlock>();

        reloop:
            foreach (var r in intrnode)
            {
                CodeBlock va, vb;


                if (map.TryGetValue(r["pNextBlock"], out va))
                {
                    if (!g.ContainsVertex(va)) { newstuff.Add(va); g.AddVertex(va); v1 += va.cost; }

                    g.AddEdge(new Edge<object>(r, va));
                }

                if (map.TryGetValue(r["pBranchBlock"], out vb))
                {
                    if (!g.ContainsVertex(vb)) { newstuff.Add(vb); g.AddVertex(vb); v1 += vb.cost; }

                    g.AddEdge(new Edge<object>(r, vb));
                }
            }

            if (newstuff.Count != 0)
            {
                intrnode = newstuff;
                newstuff = new List<CodeBlock>();
                goto reloop;
            }

            frm.Text = "Showing " + v1 * 100 / v + "% (using " + g.VertexCount + " out of " + map.Count + ", " + g.VertexCount * 100 / map.Count + "% of blocks)";


            var ordlst = map.Values.OrderByDescending(x => x.cost).ToList();

            double rvv = 0;



            foreach (var i in ordlst)
            {
                rvv += i.cost;
                i.cumsum = rvv;
            }

            var cs = frm.chart2.Series.Add("CumSum(cost) / decrease(cost)");
            var pr = frm.chart1.Series.Add("Cost / addr");
            var bl = frm.chart1.Series.Add("gopc / addr");
            var gs = frm.chart1.Series.Add("cycl / addr");

            cs.ChartType = SeriesChartType.StepLine;

            pr.ChartType = SeriesChartType.BoxPlot;
            bl.ChartType = SeriesChartType.BoxPlot;
            gs.ChartType = SeriesChartType.BoxPlot;

            foreach (var i in ordlst)
            {
                cs.Points.AddY(i.cumsum * 100.0 / CodeBlock.gcost);
            }

            rvv = 0;
            foreach (var i in ordlst.OrderBy(x => x.pos))
            {
                rvv += i.cost;
                //if (rvv > v * 0.01)
                {
                    pr.Points.AddXY(i.pos, Math.Log(i.cost + 1));
                    bl.Points.AddXY(i.pos, Math.Log(i.guest_opcodes + 1));
                    gs.Points.AddXY(i.pos, Math.Log(i.guest_cycles / 4 + 1));
                }
            }
            /*
            var vertices = new string[30];
            
            for (int i = 0; i < 30; i++)
            {
                vertices[i] = i.ToString();
                g.AddVertex(vertices[i]);
            }
            
            for (int i = 6; i < 15; i++)
            {
                g.AddChildVertex(vertices[i % 5], vertices[i]);
            }
            g.AddChildVertex(vertices[5], vertices[4]);
            g.AddChildVertex(vertices[5], vertices[2]);
            g.AddChildVertex(vertices[16], vertices[0]);
            g.AddChildVertex(vertices[16], vertices[1]);
            g.AddChildVertex(vertices[16], vertices[3]);
            g.AddChildVertex(vertices[16], vertices[20]);
            g.AddChildVertex(vertices[16], vertices[21]);
            g.AddChildVertex(vertices[16], vertices[22]);
            g.AddChildVertex(vertices[16], vertices[23]);
            g.AddChildVertex(vertices[16], vertices[24]);
            g.AddChildVertex(vertices[4], vertices[25]);
            g.AddChildVertex(vertices[4], vertices[26]);
            g.AddChildVertex(vertices[4], vertices[27]);

            g.AddEdge(new Edge<object>(vertices[0], vertices[1]));
            g.AddEdge(new Edge<object>(vertices[0], vertices[2]));
            g.AddEdge(new Edge<object>(vertices[2], vertices[4]));
            g.AddEdge(new Edge<object>(vertices[0], vertices[7]));
            g.AddEdge(new Edge<object>(vertices[8], vertices[7]));
            //g.AddEdge(new Edge<object>(vertices[13], vertices[12]));
            g.AddEdge(new Edge<object>(vertices[3], vertices[20]));
            g.AddEdge(new Edge<object>(vertices[20], vertices[21]));
            g.AddEdge(new Edge<object>(vertices[20], vertices[22]));
            g.AddEdge(new Edge<object>(vertices[22], vertices[23]));
            g.AddEdge(new Edge<object>(vertices[23], vertices[24]));
            g.AddEdge(new Edge<object>(vertices[0], vertices[28]));
            g.AddEdge(new Edge<object>(vertices[0], vertices[29]));
            g.AddEdge(new Edge<object>(vertices[25], vertices[27]));
            g.AddEdge(new Edge<object>(vertices[26], vertices[25]));
            g.AddEdge(new Edge<object>(vertices[14], vertices[27]));
            g.AddEdge(new Edge<object>(vertices[14], vertices[26]));
            g.AddEdge(new Edge<object>(vertices[14], vertices[25]));
            g.AddEdge(new Edge<object>(vertices[26], vertices[27]));
            */

            layout.LayoutMode = LayoutMode.Automatic;
            layout.LayoutAlgorithmType = "CompoundFDP";
            layout.OverlapRemovalConstraint = AlgorithmConstraints.Automatic;
            layout.OverlapRemovalAlgorithmType = "FSA";
            layout.HighlightAlgorithmType = "Simple";

            /*
            using (FileStream w = File.Create("c:\\fail.bin"))
                g.SerializeToBinary(w);
            */

            layout.Graph = g;
        }
Example #8
0
        private static CompoundGraph <object, IEdge <object> >[] InitGraphs()
        {
            var graphs = new CompoundGraph <object, IEdge <object> > [6];

            #region Big graph

            var graph = new CompoundGraph <object, IEdge <object> >();

            string[] vertices = InitVertices(graph, 20);

            for (int i = 6; i < 15; ++i)
            {
                graph.AddChildVertex(vertices[i % 5], vertices[i]);
            }

            graph.AddChildVertex(vertices[5], vertices[4]);
            graph.AddChildVertex(vertices[5], vertices[2]);
            graph.AddChildVertex(vertices[16], vertices[0]);
            graph.AddChildVertex(vertices[16], vertices[1]);
            graph.AddChildVertex(vertices[16], vertices[3]);

            graph.AddEdge(new Edge <object>(vertices[0], vertices[1]));
            graph.AddEdge(new Edge <object>(vertices[0], vertices[2]));
            graph.AddEdge(new Edge <object>(vertices[2], vertices[4]));
            graph.AddEdge(new Edge <object>(vertices[0], vertices[7]));
            graph.AddEdge(new Edge <object>(vertices[8], vertices[7]));

            graphs[BigGraph] = graph;

            #endregion

            #region Small graph

            graph    = new CompoundGraph <object, IEdge <object> >();
            vertices = InitVertices(graph, 10);

            // Add the containements
            graph.AddChildVertex(vertices[0], vertices[1]);
            graph.AddChildVertex(vertices[0], vertices[2]);
            graph.AddChildVertex(vertices[3], vertices[4]);
            graph.AddChildVertex(vertices[3], vertices[5]);
            graph.AddChildVertex(vertices[3], vertices[6]);
            graph.AddChildVertex(vertices[3], vertices[7]);
            graph.AddChildVertex(vertices[3], vertices[8]);
            graph.AddChildVertex(vertices[3], vertices[9]);

            // Add the edges
            graph.AddEdge(new Edge <object>(vertices[2], vertices[4]));
            graph.AddEdge(new Edge <object>(vertices[1], vertices[5]));

            graphs[SmallGraph] = graph;

            #endregion

            #region Flat graph

            graph    = new CompoundGraph <object, IEdge <object> >();
            vertices = InitVertices(graph, 10);

            graph.AddEdge(new Edge <object>(vertices[0], vertices[1]));
            graph.AddEdge(new Edge <object>(vertices[1], vertices[2]));
            graph.AddEdge(new Edge <object>(vertices[2], vertices[3]));
            graph.AddEdge(new Edge <object>(vertices[3], vertices[0]));
            graph.AddEdge(new Edge <object>(vertices[1], vertices[3]));
            graph.AddEdge(new Edge <object>(vertices[2], vertices[0]));

            graphs[FlatGraph] = graph;

            #endregion

            #region Repulsion graph

            graph = new CompoundGraph <object, IEdge <object> >();
            InitVertices(graph, 50);

            graphs[RepGraph] = graph;

            #endregion

            #region Star

            graph    = new CompoundGraph <object, IEdge <object> >();
            vertices = InitVertices(graph, 13);

            for (int i = 1; i < 13; ++i)
            {
                graph.AddEdge(new Edge <object>(vertices[0], vertices[i]));
            }

            for (int i = 0; i < 4; ++i)
            {
                graph.AddEdge(new Edge <object>(vertices[i * 3 + 1], vertices[i * 3 + 2]));
                graph.AddEdge(new Edge <object>(vertices[i * 3 + 1], vertices[i * 3 + 3]));
                graph.AddEdge(new Edge <object>(vertices[i * 3 + 2], vertices[i * 3 + 3]));
            }

            graphs[StarGraph] = graph;

            #endregion

            #region Combined

            graph    = new CompoundGraph <object, IEdge <object> >();
            vertices = InitVertices(graph, 51);

            // Add the containements
            graph.AddChildVertex(vertices[0], vertices[1]);
            graph.AddChildVertex(vertices[0], vertices[2]);

            // Add the edges
            graph.AddEdge(new Edge <object>(vertices[2], vertices[3]));
            graph.AddEdge(new Edge <object>(vertices[3], vertices[4]));

            graph.AddEdge(new Edge <object>(vertices[10], vertices[11]));
            graph.AddEdge(new Edge <object>(vertices[11], vertices[12]));
            graph.AddEdge(new Edge <object>(vertices[12], vertices[13]));
            graph.AddEdge(new Edge <object>(vertices[13], vertices[10]));

            for (int i = 6; i < 15; ++i)
            {
                graph.AddChildVertex(vertices[i % 5 + 20], vertices[i + 20]);
            }

            graph.AddChildVertex(vertices[25], vertices[24]);
            graph.AddChildVertex(vertices[25], vertices[22]);
            graph.AddChildVertex(vertices[36], vertices[20]);
            graph.AddChildVertex(vertices[36], vertices[21]);
            graph.AddChildVertex(vertices[36], vertices[23]);

            graph.AddEdge(new Edge <object>(vertices[20], vertices[21]));
            graph.AddEdge(new Edge <object>(vertices[20], vertices[22]));
            graph.AddEdge(new Edge <object>(vertices[22], vertices[24]));
            graph.AddEdge(new Edge <object>(vertices[20], vertices[27]));
            graph.AddEdge(new Edge <object>(vertices[28], vertices[27]));

            graph.AddEdge(new Edge <object>(vertices[4], vertices[39]));
            graph.AddEdge(new Edge <object>(vertices[39], vertices[40]));
            graph.AddEdge(new Edge <object>(vertices[39], vertices[41]));
            graph.AddEdge(new Edge <object>(vertices[39], vertices[42]));
            graph.AddEdge(new Edge <object>(vertices[42], vertices[43]));
            graph.AddEdge(new Edge <object>(vertices[42], vertices[44]));

            graph.AddEdge(new Edge <object>(vertices[1], vertices[45]));
            graph.AddEdge(new Edge <object>(vertices[45], vertices[46]));
            graph.AddEdge(new Edge <object>(vertices[45], vertices[47]));
            graph.AddEdge(new Edge <object>(vertices[45], vertices[48]));
            graph.AddEdge(new Edge <object>(vertices[48], vertices[49]));
            graph.AddEdge(new Edge <object>(vertices[48], vertices[50]));

            graphs[CombinedGraph] = graph;

            #endregion

            return(graphs);
        }
        void RecGraph(MainForm frm, string file)
        {
            var g = new CompoundGraph <object, IEdge <object> >();

            var map = new Dictionary <uint, CodeBlock>();

            string[] ent = File.ReadAllText(file).Split(new string[] { "block:" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string e in ent)
            {
                string[] st = e.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

                var entr = new CodeBlock();

                entr["block"] = st[0];

                for (int i = 1; i < st.Length; i++)
                {
                    if (st[i] == "{")
                    {
                        break;
                    }
                    else
                    {
                        string[] lst = st[i].Split(new char[] { ':' }, 2);
                        if (lst.Length == 2)
                        {
                            entr[lst[0]] = lst[1];
                        }
                    }
                }

                map[entr.block] = entr;
            }

            map = map.Where(x => x.Value.runs > 0).ToDictionary(x => x.Key, x => x.Value);

            long v = map.Sum(x => x.Value.cost);

            CodeBlock.gcost = v;

            double runcumsum = 0;

            var intrnode = map.Values.OrderByDescending(x => x.cost).Where(x => (runcumsum += x.cost) < 0.60 * v).Take(1000).ToList();

            var varp = intrnode.Select(x => x.hash);

            long v1 = intrnode.Sum(x => x.cost);

            foreach (var r in intrnode)
            {
                g.AddVertex(r);
            }

            var newstuff = new List <CodeBlock>();

reloop:
            foreach (var r in intrnode)
            {
                CodeBlock va, vb;


                if (map.TryGetValue(r["pNextBlock"], out va))
                {
                    if (!g.ContainsVertex(va))
                    {
                        newstuff.Add(va); g.AddVertex(va); v1 += va.cost;
                    }

                    g.AddEdge(new Edge <object>(r, va));
                }

                if (map.TryGetValue(r["pBranchBlock"], out vb))
                {
                    if (!g.ContainsVertex(vb))
                    {
                        newstuff.Add(vb); g.AddVertex(vb); v1 += vb.cost;
                    }

                    g.AddEdge(new Edge <object>(r, vb));
                }
            }

            if (newstuff.Count != 0)
            {
                intrnode = newstuff;
                newstuff = new List <CodeBlock>();
                goto reloop;
            }

            frm.Text = "Showing " + v1 * 100 / v + "% (using " + g.VertexCount + " out of " + map.Count + ", " + g.VertexCount * 100 / map.Count + "% of blocks)";


            var ordlst = map.Values.OrderByDescending(x => x.cost).ToList();

            double rvv = 0;



            foreach (var i in ordlst)
            {
                rvv     += i.cost;
                i.cumsum = rvv;
            }

            var cs = frm.chart2.Series.Add("CumSum(cost) / decrease(cost)");
            var pr = frm.chart1.Series.Add("Cost / addr");
            var bl = frm.chart1.Series.Add("gopc / addr");
            var gs = frm.chart1.Series.Add("cycl / addr");

            cs.ChartType = SeriesChartType.StepLine;

            pr.ChartType = SeriesChartType.BoxPlot;
            bl.ChartType = SeriesChartType.BoxPlot;
            gs.ChartType = SeriesChartType.BoxPlot;

            foreach (var i in ordlst)
            {
                cs.Points.AddY(i.cumsum * 100.0 / CodeBlock.gcost);
            }

            rvv = 0;
            foreach (var i in ordlst.OrderBy(x => x.pos))
            {
                rvv += i.cost;
                //if (rvv > v * 0.01)
                {
                    pr.Points.AddXY(i.pos, Math.Log(i.cost + 1));
                    bl.Points.AddXY(i.pos, Math.Log(i.guest_opcodes + 1));
                    gs.Points.AddXY(i.pos, Math.Log(i.guest_cycles / 4 + 1));
                }
            }

            /*
             * var vertices = new string[30];
             *
             * for (int i = 0; i < 30; i++)
             * {
             *  vertices[i] = i.ToString();
             *  g.AddVertex(vertices[i]);
             * }
             *
             * for (int i = 6; i < 15; i++)
             * {
             *  g.AddChildVertex(vertices[i % 5], vertices[i]);
             * }
             * g.AddChildVertex(vertices[5], vertices[4]);
             * g.AddChildVertex(vertices[5], vertices[2]);
             * g.AddChildVertex(vertices[16], vertices[0]);
             * g.AddChildVertex(vertices[16], vertices[1]);
             * g.AddChildVertex(vertices[16], vertices[3]);
             * g.AddChildVertex(vertices[16], vertices[20]);
             * g.AddChildVertex(vertices[16], vertices[21]);
             * g.AddChildVertex(vertices[16], vertices[22]);
             * g.AddChildVertex(vertices[16], vertices[23]);
             * g.AddChildVertex(vertices[16], vertices[24]);
             * g.AddChildVertex(vertices[4], vertices[25]);
             * g.AddChildVertex(vertices[4], vertices[26]);
             * g.AddChildVertex(vertices[4], vertices[27]);
             *
             * g.AddEdge(new Edge<object>(vertices[0], vertices[1]));
             * g.AddEdge(new Edge<object>(vertices[0], vertices[2]));
             * g.AddEdge(new Edge<object>(vertices[2], vertices[4]));
             * g.AddEdge(new Edge<object>(vertices[0], vertices[7]));
             * g.AddEdge(new Edge<object>(vertices[8], vertices[7]));
             * //g.AddEdge(new Edge<object>(vertices[13], vertices[12]));
             * g.AddEdge(new Edge<object>(vertices[3], vertices[20]));
             * g.AddEdge(new Edge<object>(vertices[20], vertices[21]));
             * g.AddEdge(new Edge<object>(vertices[20], vertices[22]));
             * g.AddEdge(new Edge<object>(vertices[22], vertices[23]));
             * g.AddEdge(new Edge<object>(vertices[23], vertices[24]));
             * g.AddEdge(new Edge<object>(vertices[0], vertices[28]));
             * g.AddEdge(new Edge<object>(vertices[0], vertices[29]));
             * g.AddEdge(new Edge<object>(vertices[25], vertices[27]));
             * g.AddEdge(new Edge<object>(vertices[26], vertices[25]));
             * g.AddEdge(new Edge<object>(vertices[14], vertices[27]));
             * g.AddEdge(new Edge<object>(vertices[14], vertices[26]));
             * g.AddEdge(new Edge<object>(vertices[14], vertices[25]));
             * g.AddEdge(new Edge<object>(vertices[26], vertices[27]));
             */

            layout.LayoutMode                  = LayoutMode.Automatic;
            layout.LayoutAlgorithmType         = "CompoundFDP";
            layout.OverlapRemovalConstraint    = AlgorithmConstraints.Automatic;
            layout.OverlapRemovalAlgorithmType = "FSA";
            layout.HighlightAlgorithmType      = "Simple";

            /*
             * using (FileStream w = File.Create("c:\\fail.bin"))
             *  g.SerializeToBinary(w);
             */

            layout.Graph = g;
        }
        private void InitGraphs()
        {
            graphs = new CompoundGraph<object, IEdge<object>>[10];

            #region Big graph
            var g = new CompoundGraph<object, IEdge<object>>();

            string[] vertices = InitVertices(g, 20);

            for (int i = 6; i < 15; i++)
            {
                g.AddChildVertex(vertices[i % 5], vertices[i]);
            }
            g.AddChildVertex(vertices[5], vertices[4]);
            g.AddChildVertex(vertices[5], vertices[2]);
            g.AddChildVertex(vertices[16], vertices[0]);
            g.AddChildVertex(vertices[16], vertices[1]);
            g.AddChildVertex(vertices[16], vertices[3]);

            g.AddEdge(new Edge<object>(vertices[0], vertices[1]));
            g.AddEdge(new Edge<object>(vertices[0], vertices[2]));
            g.AddEdge(new Edge<object>(vertices[2], vertices[4]));
            g.AddEdge(new Edge<object>(vertices[0], vertices[7]));
            g.AddEdge(new Edge<object>(vertices[8], vertices[7]));
            graphs[BIG_GRAPH] = g;
            #endregion

            #region Small graph

            g = new CompoundGraph<object, IEdge<object>>();
            vertices = InitVertices(g, 10);

            //add the containments
            g.AddChildVertex(vertices[0], vertices[1]);
            g.AddChildVertex(vertices[0], vertices[2]);
            g.AddChildVertex(vertices[3], vertices[4]);
            g.AddChildVertex(vertices[3], vertices[5]);
            g.AddChildVertex(vertices[3], vertices[6]);
            g.AddChildVertex(vertices[3], vertices[7]);
            g.AddChildVertex(vertices[3], vertices[8]);
            g.AddChildVertex(vertices[3], vertices[9]);

            //add the edges
            g.AddEdge(new Edge<object>(vertices[2], vertices[4]));
            g.AddEdge(new Edge<object>(vertices[1], vertices[5]));
            //g.AddEdge(new Edge<object>(vertices[0], vertices[1]));

            graphs[SMALL_GRAPH] = g;
            #endregion

            #region Flat graph

            g = new CompoundGraph<object, IEdge<object>>();
            vertices = InitVertices(g, 10);

            g.AddEdge(new Edge<object>(vertices[0], vertices[1]));
            g.AddEdge(new Edge<object>(vertices[1], vertices[2]));
            g.AddEdge(new Edge<object>(vertices[2], vertices[3]));
            g.AddEdge(new Edge<object>(vertices[3], vertices[0]));
            g.AddEdge(new Edge<object>(vertices[1], vertices[3]));
            g.AddEdge(new Edge<object>(vertices[2], vertices[0]));

            graphs[FLAT_GRAPH] = g;
            #endregion

            #region Repulsion graph
            g = new CompoundGraph<object, IEdge<object>>();
            vertices = InitVertices(g, 50);

            graphs[REP_GRAPH] = g;
            #endregion

            #region Star

            g = new CompoundGraph<object, IEdge<object>>();
            vertices = InitVertices(g, 13);

            for (int i = 1; i < 13; i++ )
                g.AddEdge(new Edge<object>(vertices[0], vertices[i]));

            for (int i = 0; i < 4; i++)
            {
                g.AddEdge(new Edge<object>(vertices[i * 3 + 1], vertices[i * 3 + 2]));
                g.AddEdge(new Edge<object>(vertices[i * 3 + 1], vertices[i * 3 + 3]));
                g.AddEdge(new Edge<object>(vertices[i * 3 + 2], vertices[i * 3 + 3]));
            }
            graphs[STAR_GRAPH] = g;
            #endregion

            #region Combined

            g = new CompoundGraph<object, IEdge<object>>();
            vertices = InitVertices(g, 51);

            //add the containments
            g.AddChildVertex(vertices[0], vertices[1]);
            g.AddChildVertex(vertices[0], vertices[2]);

            //add the edges
            g.AddEdge(new Edge<object>(vertices[2], vertices[3]));
            g.AddEdge(new Edge<object>(vertices[3], vertices[4]));

            g.AddEdge(new Edge<object>(vertices[10], vertices[11]));
            g.AddEdge(new Edge<object>(vertices[11], vertices[12]));
            g.AddEdge(new Edge<object>(vertices[12], vertices[13]));
            g.AddEdge(new Edge<object>(vertices[13], vertices[10]));

            for (int i = 6; i < 15; i++)
            {
                g.AddChildVertex(vertices[i % 5 + 20], vertices[i + 20]);
            }
            g.AddChildVertex(vertices[25], vertices[24]);
            g.AddChildVertex(vertices[25], vertices[22]);
            g.AddChildVertex(vertices[36], vertices[20]);
            g.AddChildVertex(vertices[36], vertices[21]);
            g.AddChildVertex(vertices[36], vertices[23]);

            g.AddEdge(new Edge<object>(vertices[20], vertices[21]));
            g.AddEdge(new Edge<object>(vertices[20], vertices[22]));
            g.AddEdge(new Edge<object>(vertices[22], vertices[24]));
            g.AddEdge(new Edge<object>(vertices[20], vertices[27]));
            g.AddEdge(new Edge<object>(vertices[28], vertices[27]));

            g.AddEdge(new Edge<object>(vertices[4], vertices[39]));
            g.AddEdge(new Edge<object>(vertices[39], vertices[40]));
            g.AddEdge(new Edge<object>(vertices[39], vertices[41]));
            g.AddEdge(new Edge<object>(vertices[39], vertices[42]));
            g.AddEdge(new Edge<object>(vertices[42], vertices[43]));
            g.AddEdge(new Edge<object>(vertices[42], vertices[44]));

            g.AddEdge(new Edge<object>(vertices[1], vertices[45]));
            g.AddEdge(new Edge<object>(vertices[45], vertices[46]));
            g.AddEdge(new Edge<object>(vertices[45], vertices[47]));
            g.AddEdge(new Edge<object>(vertices[45], vertices[48]));
            g.AddEdge(new Edge<object>(vertices[48], vertices[49]));
            g.AddEdge(new Edge<object>(vertices[48], vertices[50]));

            graphs[COMBINED_GRAPH] = g;
            #endregion
        }
Example #11
0
        public void Evaluate(int SpreadMax)
        {
            if (FBuild[0] || (pd.InputChanged && init))
            {
                graph.Clear();
                VSize.Clear();
                VThickness.Clear();
                VType.Clear();
                VPos.Clear();
                VCache.Clear();

                for (int i = 0; i < FVertices.SliceCount; i++)
                {
                    graph.AddVertex(FVertices[i]);
                    VSize.Add(FVertices[i], new Size(FVSize[i], FVSize[i]));
                    VThickness.Add(FVertices[i], new Thickness(1.0));
                    VType.Add(FVertices[i], FVType[i]);
                    VPos.Add(FVertices[i], new Point(FVPos[i].x, FVPos[i].y));
                    VCache.Add(FVertices[i]);
                }
                for (int i = 0; i < Math.Max(FEdgeFrom.SliceCount, FEdgeTo.SliceCount); i++)
                {
                    graph.AddEdge(new Edge <string>(FEdgeFrom[i], FEdgeTo[i]));
                }

                var cparams = new CompoundFDPLayoutParameters();
                foreach (var prop in typeof(CompoundFDPLayoutParameters).GetProperties())
                {
                    var input = pd.InputPins[prop.Name].Spread[0];
                    if (!(input.ToString().StartsWith("0") && input.ToString().EndsWith("0")))
                    {
                        prop.SetValue(cparams, pd.InputPins[prop.Name].Spread[0]);
                    }
                }
                layout = new CompoundFDPLayoutAlgorithm <string, IEdge <string>, CompoundGraph <string, IEdge <string> > >(graph, VSize, VThickness, VType, VPos, cparams);
                foreach (var prop in typeof(CompoundFDPLayoutParameters).GetProperties())
                {
                    var input = pd.InputPins[prop.Name].Spread[0];
                    if (!(input.ToString().StartsWith("0") && input.ToString().EndsWith("0")))
                    {
                        prop.SetValue(layout.Parameters, pd.InputPins[prop.Name].Spread[0]);
                    }
                }
                layout.Finished += (sender, args) =>
                {
                    FPosOut.SliceCount = VCache.Count;
                    for (int i = 0; i < VCache.Count; i++)
                    {
                        var point = layout.VertexPositions[VCache[i]];
                        FPosOut[i] = new Vector2D(point.X, point.Y);
                    }
                };
                if (FAsync[0])
                {
                    Task.Run(() =>
                    {
                        layout.Compute();
                    });
                }
                else
                {
                    layout.Compute();
                }
                init = true;
            }
            if (layout != null)
            {
                FState[0] = layout.State.ToString();
            }
        }