# Multi-threaded tasks, external multi-threaded code, avoiding oversubscription

**URL:** <https://discourse.julialang.org/t/multi-threaded-tasks-external-multi-threaded-code-avoiding-oversubscription/26887>\
**Category:** Internals & Design\
**Created:** [July 27, 2019, 3:49pm UTC](https://discourse.julialang.org/t/multi-threaded-tasks-external-multi-threaded-code-avoiding-oversubscription/26887 "2019-07-27T15:49:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [July 27, 2019, 3:49pm UTC](https://discourse.julialang.org/t/multi-threaded-tasks-external-multi-threaded-code-avoiding-oversubscription/26887/1 "2019-07-27T15:49:13Z")

</div>

Regarding the new tread scheduler and external code that uses threads (discussed a lot at JC19) - just a thought I had, no idea if it’s feasible:

What if a partr task could (optionally) request more than one thread? Kinda like requesting a multi-threaded job on a batch cluster? Depending on the external multi-threaded code, those threads could then just stay blocked until the Julia task (and therefore the external code) are done - some external code may even allow for utilizing the blocked threads directly, if it’s C-API has a mechanism for passing (system) thread-ids.

Just an idea, I realize that this is a complex issue.

---

<div class="post-metadata">

**Author:** ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)\
**Post date:** [July 28, 2019, 1:30am UTC](https://discourse.julialang.org/t/multi-threaded-tasks-external-multi-threaded-code-avoiding-oversubscription/26887/2 "2019-07-28T01:30:16Z")

</div>

You raise a good point, that I think will also be relevant to figure out how to get multithreaded BLAS calls to play nicely with native Julia tasks. I suppose having a way to link multiple tasks together as a single scheduled unit would be one approach. Another would be to just clone a single task onto multiple threads.

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [July 28, 2019, 9:16am UTC](https://discourse.julialang.org/t/multi-threaded-tasks-external-multi-threaded-code-avoiding-oversubscription/26887/3 "2019-07-28T09:16:38Z")

</div>

Yes, BLAS would be an obvious candidate - though it seems (discussions at JuliaCon) that a deeper/custom integration may be possible for BLAS and FFTW. However, for less common external code (e.g. some typical scientific multithreaded C-code written by generations of Phd students, code that no one dares touch anymore 🙂 ) a simple method of “blocking” multiple Julia threads while the external threads run would be extremely helpful.
