问题
I'm implementing some neural network library in java , and there are intensive double
(not Double
) matrix operations, Matrices are large and performance is required of course.
So I came to read about strictfp
keyword I honestly didn't understand what it does exactly and I was looking for simple explanation about If i should be using it or not and why
回答1:
strictfp indicates that floating point calculations should use the exact IEEE754 standard. Without strictfp, the VM is free to use other (but platform dependent) representations of intermediate float and double values, in order to increase precision.
Use strictfp if you need the exact same results on multiple platforms. Avoid it if you want the best precision your current platform can give you.
E.g. in the following simple addition:
2.0 + 1.1 + 3.0
Do you want the intermediate results (e.g. 2.0 + 1.1) to be represented as an IEEE754 standard double, or with the best possible precision your platform allows. strictfp ensures the first, not using strictfp allows the VM to use the second alternative.
Not using strictfp will not hurt performance, and may on platforms where the native float types don't map to IEEE754 increase performance, since the VM isn't required to convert back and forth in between native and IEEE754 formats. The answer is platform dependent, you'll need to measure.
回答2:
There's an IEEE standard about storing a floating point number. This standard works well on all platforms, but it has some shortcomings with overflow and underflow for example.
Some platforms have optimized way of storing floating point number, since Java 1.2, the JVM try to use these optimized capababilities. The problem is that now the shortcomings may differ from one platform to another or even completely disappear.
Thus, any code that was relying on these shortcomings may not work on some platforms, the strictfp
keyword was introduced as a workaround. When you use this keyword, Java will use the IEEE standard, allowing a greater compatibility on all the platforms.
However, since platform optimization aren't used anymore, floating point calculations are slower with strictfp
.
回答3:
Concerning performance, you should not mix code with / without the strictfp keyword because it can lead to a 20% - 30% performance penalty (tested on JDK 1.7 on exact fourier transforms).
Concerning accuracy both are identical (more than 1e-14 absolute or relative error): it only allows for use of a larger exponent to avoid underflow or overflow
回答4:
If you want other researchers to train the exact same neural net given the same training data and the same random seeds regardless of their CPU, use strictfp
. Reproducibility of scientific results (or bit-exact unit tests) are the main use for strictfp. For everyday use, it does harm to performance and numerical stability.
来源:https://stackoverflow.com/questions/5181256/strictfp-in-java