Need help fixing Gibbs Sampler using DArrays

Hi everyone, I’m giving a first steps demonstration to some colleagues. However, I am not an expert in Julia. I wanted to show a simple example of distributed computing with Julia. This is the example that I want to show (original credit to Douglas Bates). However, the last line is broken. I can’t figure out how to fix it. The documentation says that the first argument to DArrays should be a “function that accepts a tuple of index ranges”.

using Distributions
using Distributed
using DistributedArrays
# addprocs(3)

@everywhere function gibbs(N, thin)
    mat = Array{Float64,2}(undef, N, 2)
    x = y = 0.0
    for i = 1:N
        for j = 1:thin
            x = rand(Gamma(3.0, 1 / (y^2 + 4)))
            y = rand(Normal(1 / (1 + x), 1 / (2 + 2x)))
        mat[i, 1] = x
        mat[i, 2] = y

@time X = gibbs(500, 10_000)

dgibbs(N, thin) = DArray(I -> gibbs(map(length, I)[1], thin), (N, 2))
@time X = dgibbs(500, 10_000)

Nevermind. I just figured out that I need using Distributions and using DistributedArrays in an @everywhere block.

I believe it works. However, I welcome any improvements or comments.

1 Like