gpt4 book ai didi

c# - 是什么导致了这种并行化失败?

转载 作者:太空宇宙 更新时间:2023-11-03 13:11:58 25 4
gpt4 key购买 nike

我正在尝试诊断为什么这种并发模式会出现在我的游戏代码中:

enter image description here

代码出现在为游戏初始化棋盘存储的构造函数中。该板大约有 450 个六边形,向下有 750 个六边形,存储被分组为 32 x 32 的 block ,以更好地本地化后续操作,如下所示。

并行实现是完全序列化的,因为这段代码片段的运行时间与我使用编译器标志 FIRST_WAY 和 SERIAL 等编译和运行的时间完全相同。

第二次并行化尝试的前提是 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);
}

构建新行和 block 的实用例程是

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:

这些结果是在英特尔 i7 四核超线程提供 8 个处理器上运行的。

最佳答案

您正在使用工作站 GC(“WKS”)。切换到服务器 GC。是平行的。

当存在大量垃圾和大量并行时,WKS GC 真的很糟糕。


更新 - 来自 OP 的更多细节:

对 .exe.config 文件的相关更改是添加:

   <runtime>
<gcServer enabled="true"/>
</runtime>

这导致了一个像这样的(非常清晰的)并发图,运行时间不到 40 毫秒而不是 120 毫秒,应用程序在这张大 map 上的总体启动时间从大约 6.0 秒减少到 5.0 秒。

enter image description here

关于c# - 是什么导致了这种并行化失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28132907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com