# Distributed solving ODEs problem

**URL:** <https://discourse.julialang.org/t/distributed-solving-odes-problem/74983>\
**Category:** General Usage\
**Tags:** question, diffeq, distributed\
**Created:** [January 21, 2022, 1:49pm UTC](https://discourse.julialang.org/t/distributed-solving-odes-problem/74983 "2022-01-21T13:49:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)\
**Post date:** [January 21, 2022, 1:49pm UTC](https://discourse.julialang.org/t/distributed-solving-odes-problem/74983/1 "2022-01-21T13:49:04Z")

</div>

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

```julia
@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~

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [January 21, 2022, 1:52pm UTC](https://discourse.julialang.org/t/distributed-solving-odes-problem/74983/2 "2022-01-21T13:52:18Z")

</div>

> [@xspeng](#):
>
> `using DifferentialEquations, LinearAlgebra, Distributed`

I’d try

`@everywhere using DifferentialEquations, LinearAlgebra, Distributed`

---

<div class="post-metadata">

**Author:** ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)\
**Post date:** [January 21, 2022, 2:03pm UTC](https://discourse.julialang.org/t/distributed-solving-odes-problem/74983/3 "2022-01-21T14:03:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [January 21, 2022, 2:10pm UTC](https://discourse.julialang.org/t/distributed-solving-odes-problem/74983/4 "2022-01-21T14:10:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)\
**Post date:** [January 21, 2022, 2:12pm UTC](https://discourse.julialang.org/t/distributed-solving-odes-problem/74983/5 "2022-01-21T14:12:54Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 21, 2022, 2:36pm UTC](https://discourse.julialang.org/t/distributed-solving-odes-problem/74983/6 "2022-01-21T14:36:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)\
**Post date:** [January 21, 2022, 2:43pm UTC](https://discourse.julialang.org/t/distributed-solving-odes-problem/74983/7 "2022-01-21T14:43:33Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 21, 2022, 2:59pm UTC](https://discourse.julialang.org/t/distributed-solving-odes-problem/74983/8 "2022-01-21T14:59:08Z")

</div>

> [@xspeng](#):
>
> for the arrays, like atemp, @everywhere cannot be applied

Yes it can and it should.

---

<div class="post-metadata">

**Author:** ![xspeng](https://avatars.discourse-cdn.com/v4/letter/x/90ced4/32.png) [@xspeng](https://discourse.julialang.org/u/xspeng)\
**Post date:** [January 27, 2022, 10:34am UTC](https://discourse.julialang.org/t/distributed-solving-odes-problem/74983/9 "2022-01-27T10:34:04Z")

</div>

This post was temporarily hidden by the community for possibly being off-topic, unfocused, inappropriate, or spammy.
