# Using Shared Arrays

**URL:** <https://discourse.julialang.org/t/using-shared-arrays/3670>\
**Category:** General Usage\
**Tags:** parallel\
**Created:** [May 12, 2017, 9:45am UTC](https://discourse.julialang.org/t/using-shared-arrays/3670 "2017-05-12T09:45:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Divyansh\_Srivastava](https://avatars.discourse-cdn.com/v4/letter/d/cc9497/32.png) [@Divyansh\_Srivastava](https://discourse.julialang.org/u/Divyansh_Srivastava)\
**Post date:** [May 12, 2017, 9:45am UTC](https://discourse.julialang.org/t/using-shared-arrays/3670/1 "2017-05-12T09:45:37Z")

</div>

> ```julia
> julia> betweenness = SharedArray{Float64}(n_v);
> 
> ```

> julia\> @time for s in reverse(vs)  
> if degree(g,s) \> 0 # this might be 1?  
> state = dijkstra\_shortest\_paths(g, s; allpaths=true)  
> if endpoints  
> \_accumulate\_endpoints!(betweenness, state, g, s)  
> else  
> \_accumulate\_basic!(betweenness, state, g, s)  
> end  
> end  
> end  
> 133.344839 seconds (95.12 M allocations: 15.006 GiB, 1.41% gc time)

> julia\> betweenness2 = SharedArray{Float64}(n\_v);

> julia\> @time for s in vs  
> if degree(g,s) \> 0 # this might be 1?  
> state = dijkstra\_shortest\_paths(g, s; allpaths=true)  
> if endpoints  
> \_accumulate\_endpoints!(betweenness2, state, g, s)  
> else  
> \_accumulate\_basic!(betweenness2, state, g, s)  
> end  
> end  
> end  
> 131.878554 seconds (95.12 M allocations: 15.006 GiB, 1.38% gc time)

> julia\> betweenness2 == betweenness  
> false

> julia\> sum(betweenness2-betweenness)  
> 4.890762284048833e-8

> julia\> betweennessp = SharedArray{Float64}(n\_v);

> julia\> @time @sync @parallel for s in vs  
> if degree(g,s) \> 0 # this might be 1?  
> state = dijkstra\_shortest\_paths(g, s; allpaths=true)  
> if endpoints  
> \_accumulate\_endpoints!(betweennessp, state, g, s)  
> else  
> \_accumulate\_basic!(betweennessp, state, g, s)  
> end  
> end  
> end  
> 75.195969 seconds (334.83 k allocations: 22.154 MiB, 0.01% gc time)

> julia\> sum(betweennessp-betweenness)  
> -888.7666665875818
> 
> ```julia
> 
> ```

I am trying to parallelise a loop. Whenever I run loop sequentially and add to shared array the difference in two successive runs is almost insignificant which is justified. But when i run the same loop in parallel and add to the shared array i get significant difference with answer in previous cases. Can anyone please explain this / point out error

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [May 12, 2017, 1:28pm UTC](https://discourse.julialang.org/t/using-shared-arrays/3670/2 "2017-05-12T13:28:31Z")

</div>

You probably shouldn’t tag user questions as Development. That’s one way to incur the wrath of the gods.

---

<div class="post-metadata">

**Author:** ![Divyansh\_Srivastava](https://avatars.discourse-cdn.com/v4/letter/d/cc9497/32.png) [@Divyansh\_Srivastava](https://discourse.julialang.org/u/Divyansh_Srivastava)\
**Post date:** [May 12, 2017, 3:24pm UTC](https://discourse.julialang.org/t/using-shared-arrays/3670/3 "2017-05-12T15:24:23Z")

</div>

Yup changed that…btw any idea on why is sequential code behaving differently from parallel code?

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [May 12, 2017, 10:11pm UTC](https://discourse.julialang.org/t/using-shared-arrays/3670/4 "2017-05-12T22:11:50Z")

</div>

Have you made sure each process reads and write disjoint segments of the array? Without the code for `_accumulate`, I can’t say for certain, but seems probable that you aren’t writing to independent pieces of it? (and avoiding reading from it?)

---

<div class="post-metadata">

**Author:** ![Divyansh\_Srivastava](https://avatars.discourse-cdn.com/v4/letter/d/cc9497/32.png) [@Divyansh\_Srivastava](https://discourse.julialang.org/u/Divyansh_Srivastava)\
**Post date:** [May 13, 2017, 12:12am UTC](https://discourse.julialang.org/t/using-shared-arrays/3670/5 "2017-05-13T00:12:39Z")

</div>

Thanks!! I was not writing in independent segments and probably that was causing collision.
