我正在尝试诊断为什么在我的游戏代码中出现这种并发模式:
代码发生在初始化游戏的板存储的构造函数中。该板大约有450个六边形,750个六边形向下,存储分为32 x 32块,以便更好地定位后续操作,如下所示。
并行实现是完全序列化的,因为当我使用编译器标志FIRST_WAY和SERIAL et编译和运行时,此代码片段的运行时间完全相同。
第二次并行化尝试的前提是PLINQ没有在线程之间充分分离内存,因此它以最佳方式显式地将存储行分配给最多6个线程。
显示的堆栈轨迹完全是所有橙色螺纹段的典型;并行尝试都会产生相同的并发模式。
任何有关诊断此事的意见或建议都将受到最高的评价。
public sealed class BlockedBoardStorage32x32V2<T> : BoardStorage<T> {
const int _grouping = 32; // must be power of 2 in this implementation
const int _buffer = _grouping - 1;
/// <summary>Construct a new instance of extent <paramref name="sizeHexes"/> and
/// initialized using <paramref name="initializer"/>.</summary>
public BlockedBoardStorage32x32V2(HexSize sizeHexes, Func<HexCoords,T> initializer)
: base (sizeHexes) {
#if FIRST_WAY
#if SERIAL
var store = Enumerable.Range(0,(MapSizeHexes.Height + _buffer) / _grouping)
#else
var store = ParallelEnumerable.Range(0,(MapSizeHexes.Height + _buffer) / _grouping)
.AsOrdered()
#endif
.Select(y => InitializeRow(initializer, y * _grouping))
.ToArray();
#else
var range = (MapSizeHexes.Height + _buffer) / _grouping;
var threadCount = 8;
var threadRange = ( range + (threadCount-1) ) / threadCount;
var store = ParallelEnumerable.Range(0, threadCount).AsOrdered()
.SelectMany(thread => Enumerable.Range(0,threadRange),(t,i) => t*threadRange + i)
.Where(i => i < range)
.Select(y => InitializeRow(initializer, y * _grouping))
.ToArray();
#endif
_backingStore = new FastList<FastList<FastList<T>>>(store);
}
构建新行和块的实用程序例程
private FastList<FastList<T>> InitializeRow(Func<HexCoords,T> initializer, int block_j) {
var row = new FastList<T>[(MapSizeHexes.Width + _buffer) / _grouping];
for (var x = 0; x < row.Length; x++) {
row[x] = InitializeBlock(initializer, block_j, x * _grouping);
}
return new FastList<FastList<T>>(row);
}
private FastList<T> InitializeBlock(Func<HexCoords,T> initializer, int block_j, int block_i) {
var block = new T[_grouping * _grouping];
for (int i = 0, index = 0; i < _grouping; i++) {
for (var j = 0; j < _grouping; j++, index++) {
var coords = HexCoords.NewUserCoords(block_i + j, block_j + i);
block[index] = IsOnboard(coords) ? initializer(coords) : default(T);
}
}
return new FastList<T>(block);
}
和类 FastList 是Joe Duffy's Simple Fast List Enumerator
的改编版更新
以下是上面有问题的构造函数的调用例程:
private static BoardStorage<IBoardHex> HexInitializer(
IMapDefinition mapDefinition,
BridgeCollection bridges
) {
if(mapDefinition==null) throw new ArgumentNullException("mapDefinition");
if(bridges ==null) throw new ArgumentNullException("bridges");
// The line calling the constructor being inquired on:
////////////////////////////////////////////////////
var bs = new BlockedBoardStorage32x32<IBoardHex>(mapDefinition.Size,
coords => GetBoardHex(mapDefinition,coords));
bs.ForEach(new HexFinalizer(bs));
bridges.ForEach( bridge => BridgeHexGenerator(bs,bridge) );
bs.ForEach(new HexDirectedCostSetter(bs));
return bs;
}
并将初始化函数传递给构造函数:
private static BoardHex GetBoardHex(IMapDefinition mapDefinition, HexCoords coords) {
int x = coords.User.X,
y = coords.User.Y;
var terrain = mapDefinition.Terrain[y][x];
var elevation = mapDefinition.Elevations[y][x];
var features = mapDefinition.Features[y][x];
switch (terrain) {
default:
case 'x': return new BlockedHex (coords,elevation,features,HexType.Blocked);
case 'w': return new WaterHex (coords,elevation,features,HexType.Water);
case ' ': return new ClearHex (coords,elevation,features,HexType.Clear);
case 'f': return new ForestHex (coords,elevation,features,HexType.Forest);
case 'o': return new OrchardHex (coords,elevation,features,HexType.Orchard);
case 's': return new MarshHex (coords,elevation,features,HexType.Marsh);
case 'b': return new BuildingHex (coords,elevation,features,HexType.Building);
case 'c': return new ChateauHex (coords,elevation,features,HexType.Chateau);
case 'v': return new VillageHex (coords,elevation,features,HexType.Village);
case 'r': return new RoughHex (coords,elevation,features,HexType.Rough);
case 'e': return new FieldHex (coords,elevation,features,HexType.Field);
case 'd': return new WoodsHex (coords,elevation,features,HexType.Woods);
case 'y': return new CityHex (coords,elevation,features,HexType.City);
}
}
更新2 :
这些结果来自于运行在Intel i7四核超线程上以提供8个处理器。
答案 0 :(得分:1)
您正在使用工作站GC(&#34; WKS&#34;)。切换到服务器GC。它是平行的。
当存在大量垃圾和大量并行时,WKS GC真的很糟糕。
更新 - 来自OP的其他详细信息:
.exe.config文件的相关更改是添加:
<runtime>
<gcServer enabled="true"/>
</runtime>
这样就产生了一个(非常清晰的)并发图,运行时间不到40ms而不是120ms。这个大地图上应用程序的整体启动时间从大约6.0秒减少到5.0秒。