# Poor performance on cluster multithreading

**URL:** https://discourse.julialang.org/t/poor-performance-on-cluster-multithreading/12248
**Category:** Performance
**Tags:** performance, parallel, multithreading, cluster
**Created:** [July 8, 2018, 1:09pm UTC](https://discourse.julialang.org/t/poor-performance-on-cluster-multithreading/12248 "2018-07-08T13:09:12Z")
**Posts on this page:** 1
**Showing post:** 16

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [July 8, 2018, 9:02pm UTC](https://discourse.julialang.org/t/poor-performance-on-cluster-multithreading/12248/16 "2018-07-08T21:02:14Z")

</div>

> [@Question about Multi-threading Performance](https://discourse.julialang.org/t/question-about-multi-threading-performance/12075/3):
>
> randn isn’t threadsafe. using Compat, Compat.Random const twisters = [MersenneTwister() for i ∈ 1:Threads.nthreads()]; #Multithreaded. function tf!(x::Vector{Matrix{Float64}},N::Int64) Threads.@threads for ii=1:N id = Threads.threadid() twister = twisters[id] @inbounds x\_thd = x[id] for nn=1:100 for mm=1:100 @inbounds x\_thd[mm,nn] += randn(twister) end end end return nothing end yields: julia\> @btime…

You can solve the problem like this:

```julia
using Compat, Compat.Random

const twisters = [MersenneTwister() for i ∈ 1:Threads.nthreads()];

#Multithreaded.
function tf!(x::Vector{Matrix{Float64}},N::Int64)
    Threads.@threads for ii=1:N
        id = Threads.threadid()
        twister = twisters[id]
        @inbounds x_thd = x[id]
        for nn=1:100
            for mm=1:100
                @inbounds x_thd[mm,nn] += randn(twister)
            end
        end
    end
    return nothing
end

```

---

_[View the full topic](https://discourse.julialang.org/t/poor-performance-on-cluster-multithreading/12248)._
