# Solving equation within Distributed loop

**URL:** <https://discourse.julialang.org/t/solving-equation-within-distributed-loop/52905>\
**Category:** General Usage\
**Tags:** parallel, distributed, optimization\
**Created:** [January 5, 2021, 10:44pm UTC](https://discourse.julialang.org/t/solving-equation-within-distributed-loop/52905 "2021-01-05T22:44:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![danicaratelli](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@danicaratelli](https://discourse.julialang.org/u/danicaratelli)\
**Post date:** [January 5, 2021, 10:44pm UTC](https://discourse.julialang.org/t/solving-equation-within-distributed-loop/52905/1 "2021-01-05T22:44:28Z")

</div>

Hello, I am trying to solve an equation within a distributed loop. A MWE follows:

```julia
using Distributed

addprocs(2);
include("newfile.jl")

#Parameters:
par1 = 5;
par2 = 2;
@everywhere begin
    using CSV, DataFrames, BlackBoxOptim;
end
@everywhere mod_pars = Dict("par1"=>$par1,"par2"=>$par2);
@everywhere par_values = collect(1:10);
@distributed for iv = 1:10
    mod_pars["par1"] = par_values[iv];        
    myfun(mod_pars,iv);    
end

rmprocs(workers())

```

where `newfile` is just:

```julia
@everywhere g(mod_pars,x1,x2) = mod_pars["par1"]*((1.0 - x1)^2 + 100.0 * (x2 - x1^2)^2);

@everywhere function myfun(mod_pars,iv)
    f(x) = g(mod_pars,x[1],x[2]);
    res = bboptimize(f; SearchRange = (-5.0, 5.0), NumDimensions = 2);
    df_tmp = DataFrame();
    df_tmp[!,:values1] = ones(10)*mod_pars["par1"];
    df_tmp[!,:values2] = ones(10)*best_candidate(res)[1];
    CSV.write(joinpath("figures","data_"*string(iv)*".csv"),df_tmp);
end

```

This does not work with `@distributed` but does without and I am not sure why. Any suggestions?

Thank you very much!

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [January 5, 2021, 11:21pm UTC](https://discourse.julialang.org/t/solving-equation-within-distributed-loop/52905/2 "2021-01-05T23:21:31Z")

</div>

I think you might be removing the processes before they do any work. The [documentation](https://docs.julialang.org/en/v1/stdlib/Distributed/#Distributed.@distributed) indicates that `@distributed` returns immediately without waiting for the processes to complete and suggests `@sync @distributed ` if you want to wait.

---

<div class="post-metadata">

**Author:** ![danicaratelli](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@danicaratelli](https://discourse.julialang.org/u/danicaratelli)\
**Post date:** [January 5, 2021, 11:39pm UTC](https://discourse.julialang.org/t/solving-equation-within-distributed-loop/52905/3 "2021-01-05T23:39:23Z")

</div>

Thank you, that worked splendidly.

One question though. Does this mean that once a first machine is done with its iteration, it will wait until all other machines are done with theirs?

Thanks again.

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [January 6, 2021, 12:00am UTC](https://discourse.julialang.org/t/solving-equation-within-distributed-loop/52905/4 "2021-01-06T00:00:11Z")

</div>

Yes, I think that is what is meant by “The specified range is partitioned and locally executed across all workers.” Each worker process gets part of the range to work on, then `@sync` waits for all workers to finish.
