strictfp in Java
Asked Answered
D

4

9

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

Design answered 3/3, 2011 at 13:22 Comment(1)
possible duplicate of When should I use the "strictfp" keyword in java?Headless
H
16

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.

Horehound answered 3/3, 2011 at 13:27 Comment(3)
the differences between strictfp and not re performance are probably going to be negligible, if there are any, and are probably going to go opposite directions on different platforms. However, if you're that concerned about it - test it both ways and pick the "faster" one.Curare
It is worth mentioning that without strictfp, situations like overflows or underflows may have really different resultsKalindi
If you wanna read more about this modifier, here is a detailed oracle definition: docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.4Leningrad
K
1

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.

Kalindi answered 3/3, 2011 at 13:33 Comment(0)
W
-1

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

Writ answered 25/1, 2013 at 9:53 Comment(0)
I
-1

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.

Ise answered 3/7, 2014 at 19:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.