# BLAS fails in Julia's multithreaded mode with too many threads

**URL:** <https://discourse.julialang.org/t/blas-fails-in-julias-multithreaded-mode-with-too-many-threads/2106>\
**Category:** General Usage\
**Tags:** question, blas, hpc\
**Created:** [February 14, 2017, 11:20pm UTC](https://discourse.julialang.org/t/blas-fails-in-julias-multithreaded-mode-with-too-many-threads/2106 "2017-02-14T23:20:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ay2718](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@ay2718](https://discourse.julialang.org/u/ay2718)\
**Post date:** [February 14, 2017, 11:20pm UTC](https://discourse.julialang.org/t/blas-fails-in-julias-multithreaded-mode-with-too-many-threads/2106/1 "2017-02-14T23:20:55Z")

</div>

I am using a supercomputer with 80 cores that is running Scientific Linux. If I start Julia in multithreaded mode with 17 or fewer threads and run the following code, everything works fine. However, if I use 18 or more threads, I get segfault errors from BLAS. Interestingly, everything works fine if b is a vector, or if one of a or b is a sparse matrix. Is this an error with BLAS, or am I doing something wrong?

a = rand(10, 10); b = rand(10, 8); c = zeros(1000);  
Threads.@threads for i = 1:1000  
c[i] = sum(a\*b);  
end

---

<div class="post-metadata">

**Author:** ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)\
**Post date:** [February 15, 2017, 2:54am UTC](https://discourse.julialang.org/t/blas-fails-in-julias-multithreaded-mode-with-too-many-threads/2106/2 "2017-02-15T02:54:44Z")

</div>

Unless you built Julia without OpenBLAS, consider this [FAQ from the OpenBLAS wiki](https://github.com/xianyi/OpenBLAS/wiki/Faq#multi-threaded):

> If your application is already multi-threaded, it will conflict with OpenBLAS multi-threading. Thus, you must set OpenBLAS to use single thread

`BLAS.set_num_threads(1)` for Julia v0.5.

---

<div class="post-metadata">

**Author:** ![ay2718](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@ay2718](https://discourse.julialang.org/u/ay2718)\
**Post date:** [February 15, 2017, 3:01am UTC](https://discourse.julialang.org/t/blas-fails-in-julias-multithreaded-mode-with-too-many-threads/2106/3 "2017-02-15T03:01:41Z")

</div>

I have already set BLAS.set\_num\_threads(1) prior to running the  
multithreaded for loop. The following code still fails:

BLAS.set\_num\_threads(1)  
a = rand(10, 10); b = rand(10, 8); c = zeros(1000);  
Threads.@threads for i = 1:1000  
c[i] = sum(a\*b);  
end

---

<div class="post-metadata">

**Author:** ![kpamnany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kpamnany/32/206168_2.png) [@kpamnany](https://discourse.julialang.org/u/kpamnany)\
**Post date:** [February 15, 2017, 3:16am UTC](https://discourse.julialang.org/t/blas-fails-in-julias-multithreaded-mode-with-too-many-threads/2106/4 "2017-02-15T03:16:47Z")

</div>

I think this is [https://github.com/JuliaLang/julia/issues/14857](https://github.com/JuliaLang/julia/issues/14857). AFAIK, the only solution is to build your own Julia with the right setting for OpenBLAS threads/buffers.

OP: consider posting the actual log, when you’re reporting a crash.

@jameson, @tkelman: more people are going to run into this as larger core counts become more common (or more people who have access to larger core counts start to use Julia). 🙂

---

<div class="post-metadata">

**Author:** ![ay2718](https://avatars.discourse-cdn.com/v4/letter/a/db5fbb/32.png) [@ay2718](https://discourse.julialang.org/u/ay2718)\
**Post date:** [February 15, 2017, 12:15pm UTC](https://discourse.julialang.org/t/blas-fails-in-julias-multithreaded-mode-with-too-many-threads/2106/5 "2017-02-15T12:15:26Z")

</div>

Thank you very much!
