# Threads.@threads not working, even without i/o

**URL:** <https://discourse.julialang.org/t/threads-threads-not-working-even-without-i-o/22177>\
**Category:** General Usage\
**Created:** [March 22, 2019, 1:58am UTC](https://discourse.julialang.org/t/threads-threads-not-working-even-without-i-o/22177 "2019-03-22T01:58:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [March 22, 2019, 1:58am UTC](https://discourse.julialang.org/t/threads-threads-not-working-even-without-i-o/22177/1 "2019-03-22T01:58:31Z")

</div>

I am continuing my reporting of bugs with multi-threading on Julia. I am using v1.1.0 and running on 4 threads. The following code

using SparseArrays;  
using Serialization;  
N=1000;  
oneN=fill(1.0, N);  
K=spzeros(N,N);  
Threads.@threads for i in 1:N  
K[i,i] = 1.0;  
end

gives the strange error

Error thrown in threaded loop on thread 2: BoundsError(a=Array{Float64, (17,)}[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], i=23)  
Error thrown in threaded loop on thread 0: BoundsError(a=Array{Float64, (17,)}[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], i=22)  
Error thrown in threaded loop on thread 1: BoundsError(a=Array{Float64, (17,)}[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], i=22)  
Error thrown in threaded loop on thread 3: BoundsError(a=Array{Float64, (17,)}[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], i=23)

This is a very simple use of multi-threading, without any I/O. Could you tell me what is going wrong ? Thanks.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 22, 2019, 2:06am UTC](https://discourse.julialang.org/t/threads-threads-not-working-even-without-i-o/22177/2 "2019-03-22T02:06:09Z")

</div>

I’ll be surprised if sparse array is thread safe.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [March 22, 2019, 3:01am UTC](https://discourse.julialang.org/t/threads-threads-not-working-even-without-i-o/22177/3 "2019-03-22T03:01:50Z")

</div>

Multithreading is indeed rather tricky, and you need to think through how your memory access patterns will work across all threads — particularly when you’re mutating the same object across multiple threads.

In the case of sparse arrays, multiple threads attempting to use indexed assignment will cause them all to try to mutate the backing arrays simultaneously and rapidly leave them in a bad state.

I encourage you to first maximize your single-threaded performance. Profile, follow [the performance tips](https://docs.julialang.org/en/v1.1/manual/performance-tips/), and try to eliminate allocations in your hot loops. In the case of a sparse array, that often means creating arrays of indices and values and using the `sparse` constructor instead of incrementally filling it. That will likely give you far bigger gains than 4x threading ever could. Once you have things lean and mean, then is the time to parallelize — but you need to be very cautious about how you structure your algorithm to ensure there aren’t race conditions like this.

---

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [March 23, 2019, 3:29pm UTC](https://discourse.julialang.org/t/threads-threads-not-working-even-without-i-o/22177/4 "2019-03-23T15:29:38Z")

</div>

Thanks, I will try that.
