# Threaded loop shows error when using sparse matrix, but not with dense matrices

**URL:** <https://discourse.julialang.org/t/threaded-loop-shows-error-when-using-sparse-matrix-but-not-with-dense-matrices/87292>\
**Category:** New to Julia\
**Tags:** sparse, threads\
**Created:** [September 14, 2022, 10:53pm UTC](https://discourse.julialang.org/t/threaded-loop-shows-error-when-using-sparse-matrix-but-not-with-dense-matrices/87292 "2022-09-14T22:53:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![andresdrenik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andresdrenik/32/35777_2.png) [@andresdrenik](https://discourse.julialang.org/u/andresdrenik)\
**Post date:** [September 14, 2022, 10:53pm UTC](https://discourse.julialang.org/t/threaded-loop-shows-error-when-using-sparse-matrix-but-not-with-dense-matrices/87292/1 "2022-09-14T22:53:43Z")

</div>

I’m having trouble with a parallelized for loop that replaces the values of a sparse matrix. Here is a simple example that reproduces the error:

```julia
using SparseArrays

function test(θ,Ŵ_prime,Ŵ)

    value, index = findmax((θ.^(0.5)).*(Ŵ_prime.-Ŵ))

    return (value = value[1], index = index)

end

gridsize = 100
θ_vec = ones(gridsize)
Ŵsol = rand(gridsize)
A_θ_e_new = spzeros(Float64,gridsize,gridsize)

Threads.@threads for i in eachindex(Ŵsol)
    idx = test(θ_vec,Ŵsol,Ŵsol[i])[2]
    A_θ_e_new[i,idx] = 1.0
end

```

This produces the following error message:  
` nested task error: BoundsError: attempt to access 77-element Vector{Int64} at index [59]`

However, if I changed the sparse matrix  
`A_θ_e_new = spzeros(Float64,gridsize,gridsize)`  
for the dense matrix  
`A_θ_e_new = zeros(Float64,gridsize,gridsize)`  
the code seems to work well. Any suggestion for what is causing the error? Thank you!

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [September 15, 2022, 2:37am UTC](https://discourse.julialang.org/t/threaded-loop-shows-error-when-using-sparse-matrix-but-not-with-dense-matrices/87292/2 "2022-09-15T02:37:05Z")

</div>

The problem is that adding new non-zeros to a sparse matrix is not thread-safe, whereas adding to a dense matrix is.

---

<div class="post-metadata">

**Author:** ![andresdrenik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andresdrenik/32/35777_2.png) [@andresdrenik](https://discourse.julialang.org/u/andresdrenik)\
**Post date:** [September 15, 2022, 4:05am UTC](https://discourse.julialang.org/t/threaded-loop-shows-error-when-using-sparse-matrix-but-not-with-dense-matrices/87292/3 "2022-09-15T04:05:19Z")

</div>

Thank you! I didn’t know this. Is there a reason for this? Should we expect this to be a permanent feature?

---

<div class="post-metadata">

**Author:** ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)\
**Post date:** [September 15, 2022, 7:15am UTC](https://discourse.julialang.org/t/threaded-loop-shows-error-when-using-sparse-matrix-but-not-with-dense-matrices/87292/4 "2022-09-15T07:15:34Z")

</div>

In the docs ([Sparse Arrays · The Julia Language](https://docs.julialang.org/en/v1/stdlib/SparseArrays/)) it says that the actual values of the matrix are stored in a flat vector. If you are setting a non-zero element, you will be resizing this vector, which is why it is not thread safe. This happens on writes, but reads are likely thread safe, writes are unlikely to be thread safe in the future.
