# @everywhere everywhere

**URL:** <https://discourse.julialang.org/t/everywhere-everywhere/33811>\
**Category:** General Usage\
**Created:** [January 26, 2020, 3:05pm UTC](https://discourse.julialang.org/t/everywhere-everywhere/33811 "2020-01-26T15:05:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)\
**Post date:** [January 26, 2020, 3:05pm UTC](https://discourse.julialang.org/t/everywhere-everywhere/33811/1 "2020-01-26T15:05:25Z")

</div>

I’m using `Distributed` and `pmap` together for some fairly standard problems, but I realized that, to get my code to run, I am putting `@everywhere` on just about every line of code, and I’m trying to see if I can simplify this or otherwise clean up what I’m doing.

As an example, suppose I have a function `f(x,y)`, and I want to apply

```julia
x1vals = pmap(x->f(x,y0), x0vals)

```

for some fixed value of `y0` and some array `x0vals`. In my case, `y0` is the solution of an optimization problem, minimizing another function, `g(y)`. Currently, I have to do:

```julia
@everywhere using Optim
@everywhere function g(y)
# code for g goes here
end 
@everywhere y_guess = #starting guess for y
@everywhere min = optimize(g, y_guess) 
@everywhere y0 = min.minimizer

```

Again, this code works fine, but:

- It would appear I am running the minimization problem on each worker, which seems a bit wasteful
- IMO, the code is getting a bit cluttered with the `@everywhere` lines everywhere.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [January 26, 2020, 4:48pm UTC](https://discourse.julialang.org/t/everywhere-everywhere/33811/2 "2020-01-26T16:48:18Z")

</div>

You can put all initialization code in a separate file and include the file everywhere

---

<div class="post-metadata">

**Author:** ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)\
**Post date:** [January 26, 2020, 9:01pm UTC](https://discourse.julialang.org/t/everywhere-everywhere/33811/3 "2020-01-26T21:01:10Z")

</div>

I guess that’s probably the simplest solution, at least with regard to simplifying code.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [January 26, 2020, 10:06pm UTC](https://discourse.julialang.org/t/everywhere-everywhere/33811/4 "2020-01-26T22:06:30Z")

</div>

I wrote a handbook for my colleagues a while back, you might find some tricks there if you’re new to distributed computing in Julia  
[https://lup.lub.lu.se/search/publication/873af4d5-6229-4ad2-b907-c0ae0f667822](https://lup.lub.lu.se/search/publication/873af4d5-6229-4ad2-b907-c0ae0f667822)

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [January 26, 2020, 10:23pm UTC](https://discourse.julialang.org/t/everywhere-everywhere/33811/5 "2020-01-26T22:23:57Z")

</div>

You can also just do

```julia
@everywhere begin
    using Optim
    function g(y)
        # code for g goes here
    end 
    y_guess = #starting guess for y
    min = optimize(g, y_guess) 
    y0 = min.minimizer
end

```

---

<div class="post-metadata">

**Author:** ![t-pollington](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/t-pollington/32/20074_2.png) [@t-pollington](https://discourse.julialang.org/u/t-pollington)\
**Post date:** [April 16, 2021, 4:21pm UTC](https://discourse.julialang.org/t/everywhere-everywhere/33811/6 "2021-04-16T16:21:44Z")

</div>

Thanks @Mason. I applied your ans to mine, so:  
I ran my program with `include(main.jl)`, of which contained an `include("functions.jl)` in the preamble. In `functions.jl` I then top-and-tailed the code with:

```julia
@everywhere begin # first line of file
[rest of functions.jl]
end # last line of file

```
