# Correct usage of SharedArray and @parallel?

**URL:** <https://discourse.julialang.org/t/correct-usage-of-sharedarray-and-parallel/2979>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 31, 2017, 1:19am UTC](https://discourse.julialang.org/t/correct-usage-of-sharedarray-and-parallel/2979 "2017-03-31T01:19:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaoweiz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaoweiz/32/2049_2.png) [@xiaoweiz](https://discourse.julialang.org/u/xiaoweiz)\
**Post date:** [March 31, 2017, 1:19am UTC](https://discourse.julialang.org/t/correct-usage-of-sharedarray-and-parallel/2979/1 "2017-03-31T01:19:27Z")

</div>

In defining a function, I created a SharedArray and use @parallel to fill each of its elements. At last, I want to return the mean of these elements by calling `mean` on the SharedArray. However, it doesn’t seem to work as expected. Here’s a MWE:

```julia
function foo(m::Int64)
    z = SharedArray(Float64, m)
    @parallel for i = 1:m
        z[i] = 1.0
    end
    mean(z)
end

```

On my Mac, calling `foo(10)` for the first time gives result 0.0, whereas 0.7 afterwards.

If the function returns the SharedArray instead, the output is correct:

```julia
function bar(m::Int64)
    z = SharedArray(Float64, m)
    @parallel for i = 1:m
        z[i] = 1.0
    end
   z
end

```

Calling `bar(10)` gives the expected result.

To add even more confusion, `z = bar(10); mean(z)` and `mean(bar(10))` give different results. The former gives what is expected, whereas the latter behaves as `foo(10)`, i.e. returning 0.0 for the first call and 0.7 afterwords.

I’m totally confused.

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [March 31, 2017, 1:34am UTC](https://discourse.julialang.org/t/correct-usage-of-sharedarray-and-parallel/2979/2 "2017-03-31T01:34:40Z")

</div>

Try `@sync`.  
If you don’t have a reduction operator in `@parallel for`, it doesn’t wait for processes to complete.

```julia
function foo(m::Int64)
    z = SharedArray(Float64, m)
    @sync @parallel for i = 1:m
        z[i] = 1.0
    end
    mean(z)
end

```

[http://docs.julialang.org/en/stable/manual/parallel-computing/#parallel-computing](http://docs.julialang.org/en/stable/manual/parallel-computing/#parallel-computing)

```julia
As you could see, the reduction operator can be omitted if it is not needed. In that case, the loop executes
asynchronously, i.e. it spawns independent tasks on all available workers and returns an array of Future immediately
without waiting for completion. The caller can wait for the Future completions at a later point by calling fetch() on them, or
wait for completion at the end of the loop by prefixing it with @sync, like @sync @parallel for.

```

---

<div class="post-metadata">

**Author:** ![xiaoweiz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaoweiz/32/2049_2.png) [@xiaoweiz](https://discourse.julialang.org/u/xiaoweiz)\
**Post date:** [March 31, 2017, 5:44am UTC](https://discourse.julialang.org/t/correct-usage-of-sharedarray-and-parallel/2979/3 "2017-03-31T05:44:47Z")

</div>

Thanks! It solves my problem. I think it may be of great help if the document can give an example to warn new users.

---

<div class="post-metadata">

**Author:** ![Tero\_Frondelius](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tero_frondelius/32/7629_2.png) [@Tero\_Frondelius](https://discourse.julialang.org/u/Tero_Frondelius)\
**Post date:** [April 1, 2017, 12:24pm UTC](https://discourse.julialang.org/t/correct-usage-of-sharedarray-and-parallel/2979/4 "2017-04-01T12:24:08Z")

</div>

You are the best expert to fix the documentation.
