Parallel version of code not giving the right answer

I think calling the SharedArray constructor on each worker creates a fresh copy everywhere, you only want the SharedArray to be created on the master and then pass a reference to each worker.

Does this work more like you expect?

using Distributed
using SharedArrays

@everywhere begin
using LinearAlgebra
const N = 5
const β = 1.0 
const M = 20 
const Δ = N/(2M) 

W(r) = - (r >= 0.5) * r + (r <= -0.5) * r + (r >= -0.5)*(r <= 0.5)*(-r^2 - 1/4)
U(ν, r) = Δ^2 * dot(ν, map(t -> W(r/Δ - (t - 1/2)) + W(r/Δ + (t - 1/2)), 1:M))

end

function ρ_ν(ν, r, niter = 15_000)
    res = @sync @distributed (+) for _ = 1:niter
        sample = N * (rand(N)-1/2*ones(N)); sample[1] = r
        ρ = prod(map(t->exp(β*U(ν, t)),sample))
        for k = 1:N-1 for q = k+1:N
                ρ *= exp(β * abs(sample[k] - sample[q]))
        end end
        ρ
    end
    N^(N-1) * res/niter
end


function PGD(ε)
    err = ε + 1
    iter = 1
    λ(t) = 10/t^(1/4)
    ν = SharedArray{Float64,1}(M)
    while err > ε
        err = 0
        ρ = map(t->ρ_ν(ν, (t-1/2)*Δ),1:M)
        ρ = ρ./(2*Δ*sum(ρ))
        for i = 1:M
            η = Δ^3 * sum(map(t->W((t - 1/2) - (i - 1/2)) + W(-(t-1/2)-(i-1/2)), 1:M).*(ones(M).- N*ρ))
            print("\n")
            print("eta =", η)
            print("\n")
            ν[i] += λ(iter) * η
            err += abs(η)
        end
        iter += 1
        print(ν, '\n')
    end
end
1 Like