Distributed solving ODEs problem

Hi, I have a problem with using distributed solving ODEs, here is a simple example,

@everywhere begin
    function test(dy, y, p, t)
        a, b, c = p
        dy[1] = a * y[1] + b * y[2] + c
        dy[2] = b * y[2] + c * y[3] + a
        dy[3] = c * y[3] + a * y[1] + b
    end
end


@everywhere begin
    function main(a, b, c)

        tstart = 0.0
        tend = 1.0e-2
        tspan = (tstart, tend)
        u0 = [0.0, 0.0, 1.0]
        p = [a, b, c]

        prob = ODEProblem(test, u0, tspan, p)
        y = solve(prob, Tsit5())

        return y[1, end]
    end
end

using DifferentialEquations, LinearAlgebra, Distributed,SharedArrays

atemp = [1, 2, 3, 4, 5]
btemp = [2, 3, 4, 5, 6]
ctemp = [3, 4, 5, 6, 7]

#addprocs(2)
F = SharedVector{Float64}(5)
@distributed for i = 1:5
    a = atemp[i]
    b = btemp[i]
    c = ctemp[i]

    F[i] = main(a, b, c)
end

here is the error
Task (runnable) @0x0000000073511560
Unhandled Task ERROR: On worker 5:
UndefVarError: ODEProblem not defined

Anyone can show me how to solve this problem? Thanks~

I’d try

@everywhere using DifferentialEquations, LinearAlgebra, Distributed

1 Like

Seems work, but if @everywhere is put before using DifferentialEquations, LinearAlgebra, Distributed, the program doesn’t know it, unless I run using Distributed once in REPL, then the program knows it, a little bit weird

I believe all workers should run in the same Pkg environment. Could this be a problem? Hm, not sure.

Maybe I am happy too early, it doesn’t work, it doesn’t know ctemp on worker 2, even I set it to be sharedvectors, any suggestions?

You’re just missing a few more @everywheres. If it’s global, it needs to be sent.

I already add @everywhere to all the functions, for the arrays, like atemp, @everywhere cannot be applied, I tried SharedArray, and also doesn’t work, where can i put the @everywhere? could you be more spectific?

Yes it can and it should.

1 Like