# Threading: Array comprehension vs. fill

**URL:** <https://discourse.julialang.org/t/threading-array-comprehension-vs-fill/42604>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 6, 2020, 11:54am UTC](https://discourse.julialang.org/t/threading-array-comprehension-vs-fill/42604 "2020-07-06T11:54:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonas-kr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonas-kr/32/1810_2.png) [@jonas-kr](https://discourse.julialang.org/u/jonas-kr)\
**Post date:** [July 6, 2020, 11:54am UTC](https://discourse.julialang.org/t/threading-array-comprehension-vs-fill/42604/1 "2020-07-06T11:54:08Z")

</div>

I have noticed a very strange issue, which occurs in julia 1.4.2 on Windows 10. I have neither been able to reproduce it on Fedora 32 with julia 1.4.2 nor on Ubuntu 20.04 (WSL2) with julia 1.4.1. Define two functions:

```julia
function mwe_comprehension(N)

    out_split = [zeros(N,N) for k in Base.OneTo(Threads.nthreads())]

    Threads.@threads for k in 1:N
        t = Threads.threadid()
        out_split[t] .+= k
    end

    out = broadcast(+, out_split...)
end

```

and

```julia
function mwe_fill(N)

    out_split = fill(zeros(N,N), Threads.nthreads())

    Threads.@threads for k in 1:N
        t = Threads.threadid()
        out_split[t] .+= k
    end

    out = broadcast(+, out_split...)
end

```

If we know run both functions for N=4 we get

```julia
julia> mwe_fill(4)
4×4 Array{Float64,2}:
 40.0 40.0 40.0 40.0
 40.0 40.0 40.0 40.0
 40.0 40.0 40.0 40.0
 40.0 40.0 40.0 40.0

julia> mwe_comprehension(4)
4×4 Array{Float64,2}:
 10.0 10.0 10.0 10.0
 10.0 10.0 10.0 10.0
 10.0 10.0 10.0 10.0
 10.0 10.0 10.0 10.0

```

where only the comprehension version yields the correct output. On both linux OS’ the results are identical. How comes there is a difference? Running fill with a matrix as an argument and the array comprehension should give the same results.

Thank you very much in advance!

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [July 6, 2020, 12:00pm UTC](https://discourse.julialang.org/t/threading-array-comprehension-vs-fill/42604/2 "2020-07-06T12:00:08Z")

</div>

I think the issue is that `fill` gets one array & re-uses it, see e.g. here: [Structure Usage - #2 by mcabbott](https://discourse.julialang.org/t/structure-usage/42269/2) . And I guess that in `mwe_fill(N)` the different threads are over-writing each other’s results.

Edit – actually, no overwriting is necessary, you get the same result without threads. `mwe_comprehension(4)` simply contains `sum(1:4)`, while `mwe_fill(4)` contains that times the number of threads.

But the result of `mwe_fill` is not safe, e.g. `unique(mwe_fill(100))` has a variety of numbers, different on every run.

---

<div class="post-metadata">

**Author:** ![jonas-kr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonas-kr/32/1810_2.png) [@jonas-kr](https://discourse.julialang.org/u/jonas-kr)\
**Post date:** [July 6, 2020, 12:39pm UTC](https://discourse.julialang.org/t/threading-array-comprehension-vs-fill/42604/3 "2020-07-06T12:39:30Z")

</div>

Thank you very much! I did not expect fill to reuse the array - now my observations make sense.

---

<div class="post-metadata">

**Author:** ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)\
**Post date:** [July 6, 2020, 1:09pm UTC](https://discourse.julialang.org/t/threading-array-comprehension-vs-fill/42604/4 "2020-07-06T13:09:25Z")

</div>

See also [Arrays · The Julia Language](https://docs.julialang.org/en/v1.6-dev/base/arrays/#Base.fill)
