Why do the results of the same MATLAB operation differ for different CPUs?

25 views (last 30 days)
I am running the same MATLAB code on two machines with the same operating system and bitness (or on the same machine using different numbers of computational threads), but I get different results. Why does this happen?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 5 Dec 2022
Small differences (within the effects of round-off) are expected for (1) different processors, or (2) even on the same processor when changing the number of computational threads. This is a direct consequence of performance optimizations specific to different processors and multi-threading.
For example, the matrix multiplication below may produce a relative error of 1.5481e-16 on a certain machine when switching between 1 and 4 computational threads:
>> rng(0,'twister');
>> A = rand(4e4, 31);
>> maxNumCompThreads(1);
>> R1 = A'*A;
>> maxNumCompThreads(4);
>> R2 = A'*A;
>> relError = norm(R1-R2)./norm(R1)
relError =
   1.5481e-16
Similar differences are expected when switching between different processors.
Which functions suffer from this issue?
As mentioned above, slight differences in floating point arithmetic are the result of differences between computer hardware. These differences are not specific to any particular functions.
Is there any way to avoid this issue?
In general, you can get the same results for the same code by using the same hardware and configuration.
Another possibility is to use the Symbolic Math Toolbox for MATLAB, which supports exact symbolic computations and variable-precision arithmetic, both of which should give you reproducible results on different machines and when using different numbers of computational threads. Please note that symbolic and variable-precision computations will generally be slower than using regular floating point values (e.g. double).
e.g.
>> rng(0,'twister');
>> A = vpa(rand(4e4,31));
>> maxNumCompThreads(1);
>> R1 = A'*A;
>> maxNumCompThreads(4);
>> R2 = A'*A;
>> relError = norm(R1-R2)./norm(R1)
 
relError =
 
0.0

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!