# @everywhere and Dictionaries

**URL:** <https://discourse.julialang.org/t/everywhere-and-dictionaries/52880>\
**Category:** General Usage\
**Tags:** parallel, distributed\
**Created:** [January 5, 2021, 2:33pm UTC](https://discourse.julialang.org/t/everywhere-and-dictionaries/52880 "2021-01-05T14:33:50Z")\
**Posts on this page:** 7\
**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, 2:33pm UTC](https://discourse.julialang.org/t/everywhere-and-dictionaries/52880/1 "2021-01-05T14:33:50Z")

</div>

I am running some code in parallel.

I have some parameters that I save in a dictionary:

```julia
mod_pars = Dict("par1"=>par1,"par2"=>par2);

```

and for different values of `par1` I want to run a particular function. So given a list of values `p_values`, I do the following:

```julia
@distributed for i=1:length(p_values)
      mod_pars["par1"] = p_values[i];
      res = solve_model(mod_pars);
     #plus some line to save res 
end

```

While I write  
`@everywhere function solve_model`  
I try to do:

```julia
@everywhere mod_pars = Dict("par1"=>par1,"par2"=>par2);

```

but it gives me an error `LoadError: On worker 2:...`. Must I @everywhere each of the parameters individually as well?

Also, am I allowed to change `mod_pars` in each loop iteration as I do?

Thank you!

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [January 5, 2021, 2:49pm UTC](https://discourse.julialang.org/t/everywhere-and-dictionaries/52880/2 "2021-01-05T14:49:12Z")

</div>

You deleted important parts of the error message, so I can only guess, that you encounter this error or similar:

```julia
julia> using Distributed

julia> addprocs(2)
2-element Array{Int64,1}:
 26
 27

julia> par1=1
1

julia> par2=2
2

julia> @everywhere mod_pars = Dict("par1"=>par1,"par2"=>par2);
ERROR: On worker 2:
UndefVarError: par1 not defined
top-level scope at none:1

```

If I am correct, than you are correct, you need `@everywhere` on `par1` and `par2` as those are unknown at the workers. Or you can interpolate, like:

```julia
@everywhere mod_pars = Dict("par1"=>$par1,"par2"=>$par2);

```

Both ways may be not appropriate for your real problem, so it would be best to provide a [MWE](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757).

---

<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, 2:57pm UTC](https://discourse.julialang.org/t/everywhere-and-dictionaries/52880/3 "2021-01-05T14:57:22Z")

</div>

Thank you! Yes you are correct.

But then is there no problem with changing `mod_pars` within the loop even though I have applied the `@everywhere` operator on it?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [January 5, 2021, 3:05pm UTC](https://discourse.julialang.org/t/everywhere-and-dictionaries/52880/4 "2021-01-05T15:05:45Z")

</div>

The changes would be local only, not at the workers. Thats what I meant with

> maybe not appropriate

If you provide a MWE (minimal working example) we (or others) can find a solution.

---

<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, 3:14pm UTC](https://discourse.julialang.org/t/everywhere-and-dictionaries/52880/5 "2021-01-05T15:14:36Z")

</div>

I see. here is a quick example:

```julia
using Distributed, PyPlot;

addprocs(2);

#Parameters:
par1 = 5;
par2 = 2;

@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];
    plot(1:10,ones(10)*mod_pars["par1"]);
    savefig(joinpath("figures","plot"*"_iv"*".png"));
    close();
end

```

This runs, doesn’t give me an error but also does not save any figures.

Thank you!

---

<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, 3:51pm UTC](https://discourse.julialang.org/t/everywhere-and-dictionaries/52880/6 "2021-01-05T15:51:37Z")

</div>

This is another example (also doesn’t seem to work).

```julia
using Distributed, CSV, DataFrames;

addprocs(2);

#Parameters:
par1 = 5;
par2 = 2;

@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];
    df_tmp = DataFrame();
    df_tmp[!,:values1] = ones(10)*mod_pars["par1"];
    df_tmp[!,:values2] = ones(10)*mod_pars["par2"];
    CSV.write(joinpath("figures","data_"*string(iv)*".csv"),df_tmp);
end

```

It ends with no error, just by saying:

```julia
Task (runnable) @0x00007f00fde7dae0

```

---

<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, 4:28pm UTC](https://discourse.julialang.org/t/everywhere-and-dictionaries/52880/7 "2021-01-05T16:28:23Z")

</div>

I think I figured it out! I need to declare I am using a particular package on all the workers, that is:

```julia
using Distributed;

@everywhere begin
     using CSV, DataFrames
end

addprocs(2);

#Parameters:
par1 = 5;
par2 = 2;

@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];
    df_tmp = DataFrame();
    df_tmp[!,:values1] = ones(10)*mod_pars["par1"];
    df_tmp[!,:values2] = ones(10)*mod_pars["par2"];
    CSV.write(joinpath("figures","data_"*string(iv)*".csv"),df_tmp);
end

```
