# Help for basic usage of @distributed for

**URL:** https://discourse.julialang.org/t/help-for-basic-usage-of-distributed-for/31666
**Category:** Julia at Scale
**Tags:** question
**Created:** [November 29, 2019, 4:36pm UTC](https://discourse.julialang.org/t/help-for-basic-usage-of-distributed-for/31666 "2019-11-29T16:36:27Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Massimiliano\_Comin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/massimiliano_comin/32/10936_2.png) [@Massimiliano\_Comin](https://discourse.julialang.org/u/Massimiliano_Comin)
#### Post date: [November 29, 2019, 4:36pm UTC](https://discourse.julialang.org/t/help-for-basic-usage-of-distributed-for/31666/1 "2019-11-29T16:36:27Z")

</div>

Hello,

I have a computation that I want to perform several times for several realizations of disorder. For this I wrote a serial script doing only one computation, and a separate script that calls it and distributes these independent computations among all workers.

In another script, I used pmap (because the computations were heavy and parallelized over only a few workers) and the serial function was saving data alone, and everything worked fine, since I did not need to retrieve data.

Now I need to compute some final computations (sum, means) on the overall array after the distributed for loop, but I am running into problems as when I try to do this I recover arrays of zeros.

Here is the sample code of my parallel script: (called with julia -p nprocs parallel\_script.jl)

```julia
# Broadcast script
@everywhere using SharedArrays
@everywhere path = joinpath(@ __DIR__ , "serial_script.jl")
@everywhere include(path)

# Define SharedArrays
array_one = SharedArray{Float64}(N)
array_two = SharedArray{Float64}(N)
array_three = SharedMatrix{Float64}(N,N)

# Parallel independent realizations
@distributed for i in 1:N
    array_one[i], array_two[i], array_three[:,i] = my_serial_function()
end

# Now I would like to do retrieve the data, sum(array_three,dims=2) and save it ! How?
# Doing fetch(Epsilon) etc... gives all zeros.

```

Please note that variables such as N are declared in serial\_script.jl i.e. the code is not crashing whatsoever.

Thank you for your help!

---

<div class="post-metadata">

### Author: ![jemiryguo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jemiryguo/32/18633_2.png) [@jemiryguo](https://discourse.julialang.org/u/jemiryguo)
#### Post date: [October 1, 2020, 9:27am UTC](https://discourse.julialang.org/t/help-for-basic-usage-of-distributed-for/31666/2 "2020-10-01T09:27:12Z")

</div>

maybe you should using `@sync @distributed` instead or

```julia
t = @distributed for i in 1:N
    array_one[i], array_two[i], array_three[:,i] = my_serial_function()
end

wait(t)

```

? `@distributed` just scheduale the task.
