Different results on different computers, Matlab 2021b - 64 bit, windows 64 bit, both Intel chips
40 views (last 30 days)
I was testing some code on two different machines, both 64 bit windows, both Matlab 2021b with 64 bit.
I was suprised, that a simple operation with the same variables, the same precision, reproduces slightly different results.
It is not a huge operation, actually just a matrix vector multiplication of a vector A with size 1x16 and a matrix B with 16x3, both in single format, resulting in a vector C of 1x3.
I tested the bit representation of both, the vector A and the matrix B entries, and they are exactly the same.
But when I perform the matrix vector multiplication C = A*B; , the first entry is different on the two machines.
The funny thing is, that, when I perform C(1) = A*B(:,1); I get the same value on both machines, and I get also the same value (but the other different result) when I perform C(1) = sum(A.*B(:,1)');
- when I perform C = A*B, the first entries are different on the two machines ('10111101111101110110011011110110' and '10111101111101110110011011111000')
- when I perform C(1) = A*B(:,1), the values are the same on the two machines ('10111101111101110110011011111000')
- when I perform C(1) = sum(A.*B(:,1)'), the values are the same on the two machines ('10111101111101110110011011110110')
How does this come, and which value to trust?
Christine Tobler on 10 Dec 2021
Edited: Christine Tobler on 10 Dec 2021
First, about "which value to trust?"
Both values are equally trustworthy, the differences in results come down to applying the multiplications and additions of the matrix multiplication in different order. There is no right and wrong choice there, and so it's made based on performance considerations for each hardware architecture.
So for an individual matrix, you could use the symbolic toolbox and compare which machine got closer to the exact result, but this will come down to random luck for any specific input matrix.
The harder question is "how does this come?"
We make sure that MATLAB commands are reproducible, by which we mean: If you're on the same MATLAB version, same machine, same OS, same number of threads allowed for MATLAB to use, no change in any deep-down BIOS settings, the outputs of the same command with the exact same inputs are always the same.
To see what might be going on, can you run
on your machine? This will tell us about which version of the library that we call for matrix multiplication has been chosen. I suspect they might be using different instruction set levels (e.g., AVX2 vs AVX512).