# Iterating over dict in parallel

**URL:** <https://discourse.julialang.org/t/iterating-over-dict-in-parallel/23912>\
**Category:** General Usage\
**Created:** [May 6, 2019, 1:49pm UTC](https://discourse.julialang.org/t/iterating-over-dict-in-parallel/23912 "2019-05-06T13:49:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daniel/32/6480_2.png) [@daniel](https://discourse.julialang.org/u/daniel)\
**Post date:** [May 6, 2019, 1:49pm UTC](https://discourse.julialang.org/t/iterating-over-dict-in-parallel/23912/1 "2019-05-06T13:49:44Z")

</div>

I expected the following to work:

```julia
using Distributed
addprocs(4)

d = Dict("a" => 1, "b" => 2)
@sync @distributed for (k, v) in d
    println(k)
end

```

But receive this error:

```julia
ERROR: On worker 2:
KeyError: key 1:1 not found
getindex at ./dict.jl:478 [inlined]

```

Iterating over a vector in parallel or iterating over the dict sequentially works fine.  
Could someone explain what’s going on here?

---

<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:** [May 6, 2019, 2:11pm UTC](https://discourse.julialang.org/t/iterating-over-dict-in-parallel/23912/2 "2019-05-06T14:11:53Z")

</div>

Dict is not shared automatically like e.g. the data type SharedArrays.  
[https://docs.julialang.org/en/v1/stdlib/SharedArrays/index.html](https://docs.julialang.org/en/v1/stdlib/SharedArrays/index.html)  
I can not tell an exact solution but you may find the proper way in the Docs:  
[https://docs.julialang.org/en/v1/manual/parallel-computing/](https://docs.julialang.org/en/v1/manual/parallel-computing/)  
especially  
[https://docs.julialang.org/en/v1/manual/parallel-computing/#Data-Movement-1](https://docs.julialang.org/en/v1/manual/parallel-computing/#Data-Movement-1)

---

<div class="post-metadata">

**Author:** ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)\
**Post date:** [May 7, 2019, 2:09am UTC](https://discourse.julialang.org/t/iterating-over-dict-in-parallel/23912/3 "2019-05-07T02:09:45Z")

</div>

Looking at what code `@distributed` generates:

```julia
@macroexpand @distributed for (k, v) in d
    println(k)
end

```

gives a hint what’s going on, in particular the following line from the output:

```julia
for (k, v) = #25#R[#26#lo:#27#hi]

```

Looks like the `@distributed` macro uses `length(d)` to break the object into slices `lo:hi`, in this case `1:1` and `2:2`, but slicing like that doesn’t work with dicts. You can put the items in the dict into an array using `collect` then it should work:

```julia
julia> @sync @distributed for (k, v) in collect(d)
           println(k)
       end
      From worker 2:	b
      From worker 3:	a
Task (done) @0x00007f4b98df8010

```

I think arguably this should work without the `collect`, so you could file an Issue, if there isn’t something similar already.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/daniel/32/6480_2.png) [@daniel](https://discourse.julialang.org/u/daniel)\
**Post date:** [May 7, 2019, 9:16am UTC](https://discourse.julialang.org/t/iterating-over-dict-in-parallel/23912/4 "2019-05-07T09:16:49Z")

</div>

Thank you! that explains a lot.
