Repetitive Matrix Addition using Threads

Greetings All,

Please suggest how to reduce the Matrix addition using @threads. In the following example, I am trying to add 10x10 ones matrix 100 times. Ideally, the sum of all elements of the final matrix should be 10000. I am unable to run this in parallel as because of race conditions it is giving the wrong result. I will really appreciate any help in this regard.

#!/usr/bin/julia

using LinearAlgebra, Base.Threads

function TestReduction()

   X = zeros(10,10);
   Y = ones(10,10);
 
   Threads.@threads for i = 1:100
      X = X + Y;
   end

   println("Sum(X) = " , sum(X));
end 

TestReduction()

One option:

julia> using ThreadsX

julia> X = zeros(10,10);

julia> Y = ones(10,10);

julia> ThreadsX.map(+, X, Y)
10×10 Matrix{Float64}:
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0
 1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0  1.0

julia> ThreadsX.mapreduce(+, +, X, Y)
100.0