I was wondering if there's any crate doing this. Scenario: a paralell visit to a graph in which each visiting thread must update an atomic by adding a value to it, and you need to perform an update at every node. Contention is horrible. But sum is associative (modulo approximation errors), so each thread might update a local variable and all the local variables could be added to atomic variable. Contention would be in the order of the number of threads, not in the order of the number of nodes.
My ideal solution would be to have some kind of wrapper around an atomic variable. You can clone the wrapper, and a method add
will increase a local variable inside the wrapper. When the wrapper is dropped, the local variable is added to the shared atomic variable.
You could do this with the threads returning a value and then folding it, but for a number of reasons in this case we would prefer to pass this "locally buffered atomic" to rayon methods of the type *_with
and avoid the folding.