我在CodeGolf.stackexchange.com上运行游戏,玩家提交机器人在游戏中互相竞争。
在这个阶段有70个机器人和(N *(N + 1))/ 2个游戏,比赛进行得很慢,所以现在我想要并行化它。其中一个游戏规则是机器人可以写入自己的数据目录,所以我想确保我没有一个机器人实例同时玩2个游戏。
我已经编写了一个IEnuemable<T>
生成器来回放有效匹配(有效的是当前两个玩家都没有参与其他匹配)但我遇到了某种并发/阻塞问题导致了Enumerable无限循环。
在某个任意点,while(any matches)
调用将保持循环,因为剩下的唯一匹配涉及activePlayers
列表中的玩家,因此它将继续循环。这将暗示正在进行积极的比赛。
但是永远不会再调用IsCompleted
事件,因此无论运行的匹配是以某种方式被阻止,还是已经完成,但我在_activePlayers.TryRemove()
代码中遇到了并发错误。
public interface IMatchGenerator
{
void CompleteMatch(Match match);
}
public class MatchGenerator : IMatchGenerator
{
private static object SYNC_LOCK = new object();
private ConcurrentDictionary<Player, Player> _activePlayers; //tracks players actively playing
private ConcurrentQueue<Match> _allMatches;
public MatchGenerator()
{
_activePlayers = new ConcurrentDictionary<Player, Player>();
}
public IEnumerable<Match> Generate(IList<Match> matches)
{
//take the list of matches passed in and stick them in a queue.
_allMatches = new ConcurrentQueue<Match>(matches);
//keep looping while there are matches to play
while (_allMatches.Any())
{
Match nextMatch;
lock (SYNC_LOCK)
{
_allMatches.TryDequeue(out nextMatch); //Grab from front of queue
if (!_activePlayers.ContainsKey(nextMatch.Player1) &&
!_activePlayers.ContainsKey(nextMatch.Player2))
{
//If neither player is in the active player list, then this is a
//good match so add both players
_activePlayers.TryAdd(nextMatch.Player1, nextMatch.Player1);
_activePlayers.TryAdd(nextMatch.Player2, nextMatch.Player2);
}
else
{
//Otherwise push this match back in to the start of the queue...
//FIFO should move on to next;
_allMatches.Enqueue(nextMatch);
nextMatch = null;
}
}
if (nextMatch != null)
yield return nextMatch;
}
}
public void CompleteMatch(Match match)
{
//Matches in progress have the generator attached to them and will call
//home when they are complete to remove players from the active list
Player junk1, junk2;
lock (SYNC_LOCK)
{
_activePlayers.TryRemove(match.Player1, out junk1);
_activePlayers.TryRemove(match.Player2, out junk2);
}
if (junk1 == null || junk2 == null)
{
Debug.WriteLine("Uhoh! a match came in for completion but on of the players who should have been in the active list didn't get removed");
}
}
}
正在使用此代码。
var mg = new MatchGenerator();
//Code to generate IList<Match> or all player combinations and attach mg
Parallel.ForEach(mg.Generate(matches),
new ParallelOptions() {MaxDegreeOfParallelism = 8},
match =>
{
var localMatch = match;
try
{
PlayMatch(localMatch, gameLogDirectory, results);
}
finally
{
localMatch.IsCompleted();
}
});
从这里开始,它有点冗长,但并没有太多的事情发生。
PlayMatch(...)
调用方法调用Play
并在其中包含一些stringbuilder代码。
Plays
根据正在播放的机器人调用几个外部进程(即ruby / python等...它还将StreamWrites转换为每个玩家的日志文件,但假设一次只有一个玩家机器人在运行,那里应该是这里的任何冲突。
整个控制程序可在GitHub @
上找到https://github.com/eoincampbell/big-bang-game/blob/master/BigBang.Orchestrator/Program.cs
public static Result Play(Player p1, Player p2, string gameLogDirectory)
{
var dir = Player.PlayerDirectory;
var result = new Result() { P1 = p1, P2 = p2, P1Score = 0, P2Score = 0 };
string player1ParamList = string.Empty, player2ParamList = string.Empty;
List<long> p1Times = new List<long>(), p2Times = new List<long>();
Stopwatch sw1 = new Stopwatch(), sw2 = new Stopwatch(), swGame = new Stopwatch();
var sb = new StringBuilder();
var proc = new Process
{
StartInfo =
{
UseShellExecute = false, RedirectStandardOutput = true, WorkingDirectory = dir
}
};
swGame.Start();
sb.AppendLine("+--------------------------------------------------------------------------------------------+");
sb.AppendFormat("| Starting Game between {0} & {1} \n", p1.Name, p2.Name);
sb.AppendLine("| ");
for (var i = 0; i < 1; i++)
{
sw1.Reset();
sw1.Start();
var o1 = ProcessRunner.RunPlayerProcess(ref proc, player1ParamList, player2ParamList, p1, dir);
sw1.Stop();
p1Times.Add(sw1.ElapsedMilliseconds);
//System.Threading.Thread.Sleep(1);
sw2.Reset();
sw2.Start();
var o2 = ProcessRunner.RunPlayerProcess(ref proc, player2ParamList, player1ParamList, p2, dir);
sw2.Stop();
p2Times.Add(sw2.ElapsedMilliseconds);
var whoWon = GetWinner(o1, o2, ref player1ParamList, ref player2ParamList);
var whoWonMessage = "Draw Match";
if (whoWon == "P1")
{
result.P1Score++;
whoWonMessage = string.Format("{0} wins", p1.Name);
}
else if (whoWon == "P2")
{
result.P2Score++;
whoWonMessage = string.Format("{0} wins", p2.Name);
}
sb.AppendFormat("| {0} plays {1} | {2} plays {3} | {4}\n", p1.Name, o1, p2.Name, o2, whoWonMessage);
}
swGame.Stop();
sb.AppendLine("| ");
sb.AppendFormat("| Game Time: {0}", swGame.Elapsed);
result.WriteLine(sb.ToString());
var resultMessage = string.Format("Result: {0} vs {1}: {2} - {3}",
result.P1,
result.P2,
result.P1Score,
result.P2Score);
sb.AppendLine("| ");
sb.AppendFormat("| {0}", resultMessage);
using (var p1sw = new StreamWriter(Path.Combine(gameLogDirectory, p1.Name + ".log"), true))
{
p1sw.WriteLine(sb.ToString());
}
using (var p2sw = new StreamWriter(Path.Combine(gameLogDirectory, p2.Name + ".log"), true))
{
p2sw.WriteLine(sb.ToString());
}
result.P1AvgTimeMs = p1Times.Average();
result.P2AvgTimeMs = p2Times.Average();
return result;
}
答案 0 :(得分:4)
我认为您的问题是由于在Parallel.ForEach()
上使用IEnumerable<T>
造成的,可能会无限期地生成下一个元素。这与using Parallel.ForEach()
on BlockingCollection.GetConsumingEnumerable()
基本相同:
默认情况下,Parallel.ForEach和PLINQ使用的分区算法使用分块以最小化同步成本:而不是每个元素锁定一次,它将获取锁定,获取一组元素(一个块) ,然后释放锁。
但是因为在你的情况下,序列中的下一个元素在处理前一个元素之前不会产生,这将导致无限期阻塞。
我认为这里正确的解决方案是使用BlockingCollection<T>
代替yield return
:将yield return nextMatch
替换为blockingCollection.Add(nextMatch)
,然后在单独的线程上运行Generate()
使用blockingCollection.GetConsumingPartitioner()
中上述博客文章中的Parallel.ForEach()
。
我也不喜欢你的Generate()
在没有有效匹配的情况下浪费整个CPU内核基本上什么都不做,但这是一个单独的问题。