繁体   English   中英

如何决定在运行时使用PLINQ和LINQ?

[英]How to decide between using PLINQ and LINQ at runtime?

或者通常在并行和顺序操作之间进行决定。 如果没有测试,由于开销,并行或顺序实现是否最佳,很难知道。 显然,需要一些时间来训练“决策者”使用哪种方法。 我会说这种方法不可能是完美的,所以它本质上是概率性的。 x,y,z确实影响“决策者”。 我认为一个非常幼稚的实施方式是在开始时提供1/2机会,然后根据过去的表现开始偏爱它们。 这无视x,y,z ,无论如何,请分享你的启发式,你的经验,如果有的话,你的建议。

示例代码:

public interface IComputer {
    decimal Compute(decimal x, decimal y, decimal z);
}

public class SequentialComputer : IComputer {
    public decimal Compute( ... // sequential implementation
}

public class ParallelComputer : IComputer {
    public decimal Compute( ... // parallel implementation
}

public class HybridComputer  : IComputer {
    private SequentialComputer sc;
    private ParallelComputer pc;
    private TheDecider td;  // Helps to decide between the two.

    public HybridComputer() {
        sc = new SequentialComputer();
        pc = new ParallelComputer();
        td = TheDecider();
    }

    public decimal Compute(decimal x, decimal y, decimal z) {
        decimal result;
        decimal time;
        if (td.PickOneOfTwo() == 0) {
            // Time this and save result into time.
            result = sc.Compute(...);
        } else {
            // Time this and save result into time.
            result = pc.Compute();
        }
        td.Train(time);
        return result;
    }
}

我将删除计算机专业化并在PLINQ代码上使用WithDegreeOfParallelism。 如果已经知道没有并行性是最佳的,那么让你的决策者返回1。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM