Bug in rayon at summation large vectors?

Hi community,

currently I try to learn rust and his multithreading function .

But at the first step I got different result's at summation a Vector.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=a76925087b1e0e1c7abb8c8b669db1c4

Is this a bug in rayon or did I something wrong ?

If this is a bug, then I will open an issue for rayon

btw, this issues only occurs with large vectors.

Bifi

Results:

playground
-> 1274731711 nanos init vectors with random
499916.63
499916.63
499921.13
ready

local
-> 769636100 nanos init vectors with random
500186.9
500186.9
500178.56
ready

Floating point addition is not associative due to rounding. The sequential sum looks like

((((a +b) + c) + d) + e) + f

whereas the parallel sum might look more like

((a + b) + c) + ((d + e) + f)

So differences are to be expected.

4 Likes

Addition on floating point numbers isn't associative. That is results can depend on an order of summation. You'll get yet another result if you'll sum them backwards: c.iter().rev().sum().

You may be interested in Kahan summation algorithm

An old project manager of mine used to tell new boys on the team:

"If you think you need floating point to solve a problem then you don't understand the problem. If you actually need floating point to solve a problem then you will not understand the problem."

Of course our hardware at the time did not support floating point and we could not afford the time to do it in software. But every now and then something pops up to remind me of how right he was.

9 Likes

Hi,

thx for the fast answers.

sorry my fault, i wasn't thinking about the inaccuracy of floating point values.

No bug in rayon, bug in my head :blush:

Bifi

Nice, that's cool.

I'll remember that

Bifi