浮点运算结果的重现性 [英] Reproducibility of floating point operation result

查看:114
本文介绍了浮点运算结果的重现性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

浮点运算是否可能在不同的CPU上产生不同的结果?
通过CPUs我的意思是所有的x86和x64。并且通过不同的结果我的意思,即使只有一个最不重要的位是不同的。我需要知道,如果我可以使用浮点运算项目,在不同的机器上具有完全相同的结果对应相同的输入至关重要。 p>

编辑:添加c ++标记。

也要澄清:我需要可重复的结果运行时。

解决方案

在游戏行业,这被称为确定性锁步,对于客户端和服务器需要就物理对象(玩家,射弹,变形地形等)的状态达成一致的实时网络游戏非常重要。



根据Glenn Fiedler的关于浮点决定论的文章,回答是;如果在同一架构上运行相同的二进制文件并 限制使用比基本浮点更不明确的功能,则可以获得相同的结果。否则,如果您使用不同的编译器,或允许您的代码使用SSE或80位浮点数,那么不同的可执行文件和不同的计算机之间的结果会有所不同。



Yosef Kreinin 建议




  • 扫描汇编器输出以进行代数优化并将其应用于源代码;

  • 抑制融合乘法和其他高级指令(例如 sin 三角函数);

  • 并使用SSE或SSE2,否则将FPU CSR设置为64位。 (是的,这与Glenn Fiedler的建议冲突。)



当然,不同机器;采用中间输出的哈希,因此您可以知道模拟在哪里和何时发生偏差。


Is it possible for an floating-point arithmetic operation to yield different results on different CPUs? By CPUs i mean all of x86 and x64. And by different results i mean even if only a single least important bit is different.. I need to know if I can use floating point operations on project where it's vital to have exactly the same results corresponding to same input on different machines.

Edit: added c++ tag.
Also to clarify: I need reproducible results run-time. I wouldn't expect identical results from different compilations.

解决方案

In the gaming industry this is referred to as deterministic lockstep, and is very important for real-time networked games where the clients and server need to be in agreement about the state of physics objects (players, projectiles, deformable terrain etc).

According to Glenn Fiedler's article on Floating Point Determinism, the answer is "a resoundingly limp maybe"; if you run the same binary on the same architecture and restrict the use of features that are less well specified than basic floating-point, then you can get the same results. Otherwise, if you use different compilers, or allow your code to use SSE or 80-bit floating point, then results will vary between different executables and different machines.

Yosef Kreinin recommends:

  • scanning assembler output for algebraic optimisations and applying them to your source code;
  • suppressing fused multiply-add and other advanced instructions (e.g. the sin trigonometric function);
  • and using SSE or SSE2, or otherwise setting the FPU CSR to 64-bit. (Yes, this conflicts with Glenn Fiedler's recommendation.)

And of course, test your code on multiple different machines; take hashes of intermediate outputs, so you can tell just where and when your simulations are diverging.

这篇关于浮点运算结果的重现性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆