我對PLINQ有一些奇怪的結果,我似乎無法解釋。我一直在嘗試對Alpha Beta樹進行並行化搜索以加速搜索過程,但是它正在有效地減緩搜索速度。我期望提高並行度,我會每秒線性地增加節點數......並且在修剪被推出之前處理額外的節點。當節點數量相匹配的期望,我的時間不:瞭解樹搜索中的PLINQ瓶頸
非PLINQ,參觀 節點:61418, 運行時間:0:00.67
並行度:1,參觀 節點:61418, 運行時:0:01.48
平行度:2, 節點訪問:75504, 運行時:0:10.08
平行度:4, 節點訪問:95664, 運行時間:1:51.98
平行度:8, 節點訪問:108148, 運行時間:1:48.94
任何人都幫我識別可能的罪犯?
相關代碼:
public int AlphaBeta(IPosition position, AlphaBetaCutoff parent, int depthleft)
{
if (parent.Cutoff)
return parent.Beta;
if (depthleft == 0)
return Quiesce(position, parent);
var moves = position.Mover.GetMoves().ToList();
if (!moves.Any(m => true))
return position.Scorer.Score();
//Young Brothers Wait Concept...
var first = ProcessScore(moves.First(), parent, depthleft);
if(first >= parent.Beta)
{
parent.Cutoff = true;
return parent.BestScore;
}
//Now parallelize the rest...
if (moves.Skip(1)
.AsParallel()
.WithDegreeOfParallelism(1)
.WithMergeOptions(ParallelMergeOptions.NotBuffered)
.Select(m => ProcessScore(m, parent, depthleft))
.Any(score => parent.BestScore >= parent.Beta))
{
parent.Cutoff = true;
return parent.BestScore;
}
return parent.BestScore;
}
private int ProcessScore(IMove move, AlphaBetaCutoff parent, int depthleft)
{
var child = ABFactory.Create(parent);
if (parent.Cutoff)
{
return parent.BestScore;
}
var score = -AlphaBeta(move.MakeMove(), child, depthleft - 1);
parent.Alpha = score;
parent.BestScore = score;
if (score >= parent.Beta)
{
parent.Cutoff = true;
}
return score;
}
然後爲整個樹的各級分享α+β參數的數據結構...
public class AlphaBetaCutoff
{
public AlphaBetaCutoff Parent { get; set; }
private bool _cutoff;
public bool Cutoff
{
get
{
return _cutoff || (Parent != null && Parent.Cutoff);
}
set
{
_cutoff = value;
}
}
private readonly object _alphaLock = new object();
private int _alpha = -10000;
public int Alpha
{
get
{
if (Parent == null) return _alpha;
return Math.Max(-Parent.Beta, _alpha);
}
set
{
lock (_alphaLock)
{
_alpha = Math.Max(_alpha, value);
}
}
}
private int _beta = 10000;
public int Beta
{
get
{
if (Parent == null) return _beta;
return -Parent.Alpha;
}
set
{
_beta = value;
}
}
private readonly object _bestScoreLock = new object();
private int _bestScore = -10000;
public int BestScore
{
get
{
return _bestScore;
}
set
{
lock (_bestScoreLock)
{
_bestScore = Math.Max(_bestScore, value);
}
}
}
}
http://blogs.msdn.com/b/pfxteam/archive/2008/01/31/7357135.aspx 有一些有趣的註釋,可能對我的問題有用... – tbischel 2013-03-21 19:17:30