This makes the model m0
once, and then copies it to each worker. You need to construct the model with a different env on each worker.
using Distributed
addprocs(3; exeflags = "--project=/tmp/gur") # Replace with your project
@everywhere begin
using JuMP
using Gurobi
const ENV = Gurobi.Env()
end
@everywhere begin
model = Model(() -> Gurobi.Optimizer(ENV))
set_silent(model)
@variable(model, 0 <= x <= 1)
function update_and_solve(a)
println("Solving $(a) from $(myid())")
@objective(model, Max, a * x)
optimize!(model)
sleep(1.0)
return objective_value(model)
end
end
julia> @distributed (+) for a in 1:10
update_and_solve(a)
end
From worker 3: Solving 5 from 3
From worker 2: Solving 1 from 2
From worker 4: Solving 8 from 4
From worker 3: Solving 6 from 3
From worker 2: Solving 2 from 2
From worker 4: Solving 9 from 4
From worker 4: Solving 10 from 4
From worker 3: Solving 7 from 3
From worker 2: Solving 3 from 2
From worker 2: Solving 4 from 2
55.0