Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparison of multi-threading models in Julia >=1.3 and Python 3.x

I would like to understand, from the user point of view, the differences in multithreading programming models between Julia >= 1.3 and Python 3.

Is there one that is more efficient than the other (in the sense that rising the thread numbers reduces more the computational time) ? In which situations (e.g. one model may have an edge, but only on computational or memory intensive tasks) ?

Is one that is more practical/provide higher level functions than the other ?

Is one that is more flexible than the other (e.g. it can be applied to a wider set of cases) ?

like image 652
Antonello Avatar asked Jan 20 '20 14:01

Antonello


People also ask

Is Python good for multi threading?

Python doesn't support multi-threading because Python on the Cpython interpreter does not support true multi-core execution via multithreading. However, Python does have a threading library. The GIL does not prevent threading.

What are different multi threading model?

There exists three established multithreading models classifying these relationships are: Many to one multithreading model. One to one multithreading model. Many to Many multithreading models.

Does Julia support multithreading?

Starting Julia with multiple threadsWhen both are specified, then -t / --threads takes precedence. The number of threads can either be specified as an integer ( --threads=4 ) or as auto ( --threads=auto ), where auto sets the number of threads to the number of local CPU threads.

Which multithreading model is efficient?

Many to One Model This model is quite efficient as the user space manages the thread management. A disadvantage of the many to one model is that a thread blocking system call blocks the entire process. Also, multiple threads cannot run in parallel as only one thread can access the kernel at a time.


1 Answers

There are several differences between the languages with Julia providing many levels of functionality on this what you can find in Python. You have the following types of parallelism (I am discussing here the standard language features not functionality available via external libraries):

  1. SIMD (signle-instruction-multiple-data) feature of CPUs
  • Julia: combine @simd with @inbounds (see https://docs.julialang.org/en/v1/manual/performance-tips/)
  • Python: not supported
  1. Green threads (also called Coroutines). (This is not an actual threading - but allows to use one system thread across many tasks. This is particularly useful to parallelize IO operations such as web scraping or inter-process communication - for an example if one task is waiting for IO, another tasks can execute in parallel.)
  • Julia: use a combination of @sync (to collect a group of tasks) and @async (to spawn new tasks) macros (for more details see https://docs.julialang.org/en/v1/manual/parallel-computing/)
  • Python: use asyncio (for more details see https://docs.python.org/3/library/asyncio-task.html)
  1. Multihreading: run several tasks in parallel within a single process (and shared memory) across several system threads:
  • Julia: use Threads.@threads macro to parallelize loops and Threads.@spawn to launch tasks on separate system threads. Use locks or atomic values to control the parallel execution. (for more details see https://docs.julialang.org/en/v1/manual/parallel-computing/)

  • Python: not useful for CPU-dominated tasks due to GIL (global-interpreter-lock) (see the comment by @Jim below)

  1. Multi-processing
  • Julia: use macros from the Distibuted package to parallelize loops and spawn remote processes (for more details see https://docs.julialang.org/en/v1/manual/parallel-computing/)

  • Python: use multiprocessing library - for more details see https://docs.python.org/3.8/library/multiprocessing.html

like image 87
Przemyslaw Szufel Avatar answered Oct 13 '22 10:10

Przemyslaw Szufel