Skip to content
Prev 60648 / 63421 Next

Matrix issues when building R with znver3 architecture under GCC 11

Hi Kieran,
On 4/12/22 02:36, Kieran Short wrote:
R gets used and tested most with the default optimizations, without use 
of model-specific instructions and with -O2 (GCC). It happens time to 
time that some people try other optimization options and run into 
problems. In principle, there are these cases (seen before):

(1) the test in R package (or R) is wrong - it (unintentionally) expects 
behavior which has been observed in builds with default optimizations, 
but is not necessarily the only correct one; in case of numerical 
tolerances set empirically, they could simply be too tight

(2) the algorithm in R package or R has a bug - the result is really 
wrong and it is because the algorithm is (unintentionally) not portable 
enough, it (unintentionally) only works with default optimizations or 
lower; in case of numerical results, this can be because it expects more 
precision from the floating point computations than mandated by IEEE, or 
assumes behavior not mandated

(3) the optimization by design violates some properties the algorithm 
knowingly depends on; with numerical computations, this can be a sort of 
"fast" (and similarly referred to) mode which violates IEEE floating 
point standard by design, in the aim of better performance; due to the 
nature of the algorithm depending on IEEE, and poor luck, the results 
end up completely wrong

(4) there is a bug in the C or Fortran compiler (GCC as we use GCC) that 
only exhibits with the unusual optimizations; the compiler produces 
wrong code

So, when you run into a problem like this and want to get that fixed, 
the first thing is to identify which case of the above it is, in case of 
1 and 2 also differentiate between base R and a package (and which 
concrete package). Different people maintain these things and you would 
ideally narrow down the problem to a very small, isolated, reproducible 
example to support your claim where the bug is. If you do this right, 
the problem can often get fixed very fast.

Such an example for (1) could be: few lines of standalone R code using 
Matrix that produces correct results, but the test is not happy. With 
pointers to the real check in the tests that is wrong. And an 
explanation why the result is wrong.

For (2)-(4) it would be a minimal standalone C/Fortran example including 
only the critical function/part of algorithm that is not correct/not 
portable/not compiled correctly, with results obtained with 
optimizations where it works and where it doesn't. Unless you find an 
obvious bug in R easy to explain (2), when the example would not have to 
be standalone. With such standalone C example, you could easily test the 
results with different optimizations and compilers, it is easier to 
analyze, and easier to produce a bug report for GCC. What would make it 
harder in this case is that it needs special hardware, but you could 
still try with the example, and worry about that later (one option is 
running in an emulator, and again a standalone example really helps 
here). In principle, as it needs special hardware, the chances someone 
else would do this work is smaller. Indeed, if it turns out to be (3), 
it is unlikely to get resolved, but at least would get isolated (you 
would know what not to run).

As a user, if you run into a problem like this and do not want to get it 
fixed, but just work it around somehow. First, it may be dangerous, 
possibly one would get incorrect results from computations, but say in 
applications where they are verified externally. You could try disabling 
individual specific optimization until the tests pass. You could try 
with later versions of gcc-11 (even unreleased) or gcc-12. Still, a lot 
of this is easier with a small example, too. You could ignore the 
failing test. And it may not be worth it - it may be that you could get 
your speedups in a different, but more reliable way.

Using wsl2 on its own should not necessarily be a problem and the way 
you built gcc from the description should be ok, but at some point it 
would be worth checking under Linux and running natively - because even 
if these are numerical differences, they could be in principle caused by 
running on Windows (or in wsl2), at least in the past such differences 
were seen (related to (2) above). I would recommend checking on Linux 
natively once you have at least a standalone R example.

Best
Tomas