并行树构造:没有性能改进

时间:2016-02-21 17:20:49

标签: c# parallel-processing tree task-parallel-library

我使用递归树构造算法构建边界卷层次树:

    protected void Subdivide(BoundingVolumeHierarchyNode node, HeFace[] faces)
    {
        if (node.Depth > Height)
            Height = node.Depth;

        if (!NeedsSubdivision(node))
        {
            Debug.Assert(faces.Length > 0);
            node.Triangles = faces;
            return;
        }

        List<HeFace> subsetA = new List<HeFace>(0);
        List<HeFace> subsetB = new List<HeFace>(0);

        MedianPartitionStrategy str = new MedianPartitionStrategy();
        str.ParitionObjects(node, subsetA, subsetB, faces);

        node.Left = new BoundingVolumeHierarchyNode(node.Depth + 1, CreateAabrFromFaces(subsetA.ToArray()), subsetA.Count);
        node.Right = new BoundingVolumeHierarchyNode(node.Depth + 1, CreateAabrFromFaces(subsetB.ToArray()), subsetB.Count);

        Debug.Assert(subsetA.Count > 0);
        Debug.Assert(subsetB.Count > 0);

        //var left = Task.Factory.StartNew(() => Subdivide(node.Left, subsetA.ToArray()));
        //var right = Task.Factory.StartNew(() => Subdivide(node.Right, subsetB.ToArray()));

        //try
        //{
        //    Task.WaitAll(left, right);
        //}
        //catch (AggregateException)
        //{
        //    //handle exceptions here
        //}
        Subdivide(node.Left, subsetA.ToArray());
        Subdivide(node.Right, subsetB.ToArray());
    }

我的算法很慢,所以我试图通过使用并行任务构建树来加快速度。 注释代码并行执行构造。我对并行执行与单一执行进行了基准测试:

  • parallel:00:00:14.6067970
  • 单线程:00:00:15.2736202

所以改进很小。 我的处理器是Intel Core 2 Quad。那么我做错了什么?

0 个答案:

没有答案