Are differences in results too large to be round off error?
Show older comments
There is a multi-step calculation that I have accidentally been passing the same input numbers into. I didn't realize it, because the results are different (0.00051972 vs. 0.00052553, for example). Now that I know those results come from the same input numbers, is it possible to have differences this large due to roundoff error?
Accepted Answer
More Answers (1)
Walter Roberson
on 20 Jul 2023
1 vote
In cases where MATLAB can automatically vectorize calculations on larger arrays, splitting the work between multiple CPUs, then the different CPUs might reply at different times in different runs, so potentially some operations might not be done in the same order on each run.
This would depend on the implementation, about whether it deals with the fastest-responding core first or if it waits for each core in turn, or if it has some kind of buffering system designed to record outputs even if one core "laps" others in calculations (that is, is it possible for one core to do two full tasks before another core finishes one task? Taking into account "efficiency" versus "power" cores, it should be assumed that it is possible if you dispatch more work to each core as it finishes the previous task.)
If the order of "consolidating" calculations is not deliberately controlled in the implementation, then two different runs of the same program on the same system could potentially give different results.
One of the easier to understand cases where this could be a potential issue, is sum() over an entire array. In a perfect world, you could divide the array up into as many pieces as there were cores and have each core sum its chunk, and add the chunk-sums into a running total as each core made its results available. But this assumes that if you do (A+B)+C that the result will be identical to if you did A+(B+C) and the same as B+(A+C). That assumption fails if you are doing double-precision floating point. Consider 1 + 1e-30 + -1 . In 53 bit double precision, 1e-30 is too small to matter for 1, so (1+1e-30) --> 1 exactly. Add the -1 and you would get exact 0. But if you did (1+-1)+1e-30 then you would get 1e-30 . So in double precision the order matters.
I seem to recall seeing something a few releases ago, either in the release notes or in one of the bug reports, that in this particular case, Mathworks has been careful to arrange that the partial sums will be added in a consistent order even if the CPUs return partial results in a different order. But the note was specific to summation, and did not in itself apply more generally to arithmetic such as the \ operator. So for the case of summation, the results should be consistent on any one system provided the number of cores did not change... but the number of cores could potentially change, and the results could potentially be different on a different system (with a different number of cores, or with a different chip that had access to different hardware vector mathematics.)
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!