我在业余时间用 C# 编写了这个时髦的小型 2D N 体模拟。它在串行实现中运行良好,以良好的帧速率运行,最高可达约 1000 个主体,此时它开始滞后。
我修改了算法,在单独的 block 中进行位置和速度更新,以便在 CPU 的单独内核上运行,我注意到性能有了小幅提升。
请注意,大部分时间花在了实际的数学运算上,还有一点时间花在了场景的实际绘制上。
我刚刚下载了 Microsoft Accelerator V2 库并从头开始重新编写了整个代码以使用它。它像以前一样工作,但速度明显变慢了!而之前能顺利跑近1000分,现在在加速器下只能拿到10分左右。
Here是我的代码。这是我第一次使用 Accelerator 做任何事情,所以我很可能搞砸了。
该类称为 Test
因为这是我的第一次测试。构造函数只是创建长度为 n
的数组对于物体的 x 和 y 位置、它们的 x 和 y 速度以及它们的质量。 dec
只是一个阻尼因子,因此舍入误差会使它内爆而不是爆炸,并且g
只是我要离开的引力常数 1.0f
现在。
Tick()
是执行所有更新的函数。我首先考虑关于任何给定点 i 的所有点,找到半径,在其他点的方向上构造一个单位向量,通过减速度和重力常数缩放该单位向量,然后求和为 x 和 y 速度更新对于这一点。
然后我更新所有速度和所有位置,并转换回 float[]
正如我所说,代码在技术上确实有效。除了大幅减速外,结果与我的串行实现相同。
知道我可能做错了什么吗?
我感觉它可能是第 85 行和第 86 行 - 我将点 i 的 x 和 y 速度更新加在一起,并将其存储到我的 float 组中,这意味着我需要调用 Target.ToArray1D()[0]
得到总和值。
我这样做的原因是我首先更新所有点,然后更新点,然后根据速度应用位置变化。
即我不希望在时间 t + 1 我用时间 t 的其余点更新点 0,然后下一个在时间 t 再次用其余点更新点 1,但用新的 t + 1 更新点 0。如果这是有道理的话。
public void Tick()
{
FPA fxPos = new FPA(xPos);
FPA fyPos = new FPA(yPos);
FPA fxVel = new FPA(xVel);
FPA fyVel = new FPA(yVel);
FPA fMass = new FPA(mass);
float[] xUpd = new float[n]; // x-update for velocity
float[] yUpd = new float[n]; // y-update for velocity
for (int i = 0; i < n; i++)
{
// x- and y-pos about point i:
FPA ixPos = PA.Subtract(fxPos, xPos[i]);
FPA iyPos = PA.Subtract(fyPos, yPos[i]);
// radius from point i:
FPA iRad = PA.Sqrt(PA.Add(PA.Multiply(ixPos, ixPos), PA.Multiply(iyPos, iyPos)));
// x- and y-pos are now unit vectors:
ixPos = PA.Divide(ixPos, iRad);
iyPos = PA.Divide(iyPos, iRad);
// vectors are now scaled by mass:
ixPos = PA.Multiply(fMass, ixPos);
iyPos = PA.Multiply(fMass, iyPos);
// vectors are now scaled by G and deceleration:
ixPos = PA.Multiply(dec * g, ixPos);
iyPos = PA.Multiply(dec * g, iyPos);
// sum to get ith update value:
xUpd[i] = target.ToArray1D(PA.Sum(ixPos))[0];
yUpd[i] = target.ToArray1D(PA.Sum(iyPos))[0];
}
FPA fxUpd = new FPA(xUpd);
FPA fyUpd = new FPA(yUpd);
// update velocities:
fxVel = PA.Add(fxUpd, fxVel);
fyVel = PA.Add(fyUpd, fyVel);
// update positions:
fxPos = PA.Add(fxVel, fxPos);
fyPos = PA.Add(fyVel, fyPos);
xPos = target.ToArray1D(fxPos);
yPos = target.ToArray1D(fyPos);
xVel = target.ToArray1D(fxVel);
yVel = target.ToArray1D(fyVel);
}
最佳答案
您应该重新考虑您的逻辑以真正使用并行计算。您仍然一次只处理一个点。要利用该库的优势,您应该考虑对整个元素数组执行一个操作。
关于c# - Microsoft Accelerator 比 C# 中的串行实现慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12489879/