要在一般情况下选择并行操作还是串行操作,很难在没有测试的情况下知道哪种实现方式最好,因为存在开销。显然,需要一些时间来训练“决策者”使用哪种方法。我认为这种方法不可能完美,因此具有概率性质。
示例代码:
x,y,z
对“决策者”有影响。我认为一个非常天真的实现方式是在开始时给予两种方法相等的机会,然后根据过去的表现开始偏爱其中一种。这忽略了 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;
}
}