Understanding the GIL Parallelism with Cython Summary

How To Overcome the GIL Limitations (While Staying In Python Ecosphere)

Francesc Alted

Numexpr Author Barcelona Music and Audio Technology

Advanced Scienti Programming in Python 2011 Summer School, St Andrews, Scotland

Francesc Alted How To Overcome the GIL Limitations Understanding the GIL Parallelism with Cython Summary Outline

1 Understanding the GIL

2 Parallelism with Cython

Francesc Alted How To Overcome the GIL Limitations Understanding the GIL Parallelism with Cython Summary What the GIL Is?

GIL stands for Global . Its main goal is to avoid race conditions to happen among dierent Python threads, allowing the Python interpreter to work awlessly in threading environments. This is achieved by preventing threads to use the Python interpreter simultaneously while they run.

Francesc Alted How To Overcome the GIL Limitations Understanding the GIL Parallelism with Cython Summary How the GIL Works

Actually prevents threads from running in parallel in Python.

Francesc Alted How To Overcome the GIL Limitations Understanding the GIL Parallelism with Cython Summary If GIL Is So Bad, Why It Was Introduced?

Actually, it is not that bad: Module developers do not need to worry about evil threading eects. It is unproblematic (and helpful!) when running several I/O bounded threads simultaneously. C extensions usually releases the GIL during operation, allowing to use several threads concurrently.

The GIL is only a problem when tackling CPU-bounded problems in Python, where it eectively prevents threads from running in parallel.

Francesc Alted How To Overcome the GIL Limitations Understanding the GIL Parallelism with Cython Summary If GIL Is So Bad, Why It Was Introduced?

Actually, it is not that bad: Module developers do not need to worry about evil threading eects. It is unproblematic (and helpful!) when running several I/O bounded threads simultaneously. C extensions usually releases the GIL during operation, allowing to use several threads concurrently.

The GIL is only a problem when tackling CPU-bounded problems in Python, where it eectively prevents threads from running in parallel.

Francesc Alted How To Overcome the GIL Limitations Understanding the GIL Parallelism with Cython Summary Ways To Avoid the GIL

Use threaded extensions in C where GIL is not a problem (Numexpr, NumPy with MKL, SciPy with FFTW...): Pro: powerful and very easy to use. Con: not general enough (not a ). Use the multiprocess module: Pro: completely compatible with the threading module. Con: data is not shared among processes. Use Cython's parallel features: Pro: uses native threads internally, so data is shared naturally. Con: Cython is compiled.

Francesc Alted How To Overcome the GIL Limitations Understanding the GIL Parallelism with Cython Summary Cython Parallel Features

Since Cython 0.15, it actually supports a couple of ways to create true CPU-bounded parallel apps: Using Python's threading module. Use prange statement in loops.

You should always remember to release the GIL explicitely.

Francesc Alted How To Overcome the GIL Limitations Understanding the GIL Parallelism with Cython Summary Cython Parallel Features

Since Cython 0.15, it actually supports a couple of ways to create true CPU-bounded parallel apps: Using Python's threading module. Use prange statement in loops.

You should always remember to release the GIL explicitely.

Francesc Alted How To Overcome the GIL Limitations Understanding the GIL Parallelism with Cython Summary Releasing the GIL In Cython

Cython allows to explicitly release the GIL in code sections: cdef np.ndarray x[double] cdef double beta cdef long i with nogil: for i in range(1000): x[i] = sin(i * beta)

...or in complete functions: cdef int myfunc(np.ndarray[double] x, double beta) nogil: cdef long i for i in range(1000): x[i] = sin(i * beta) The code sections in nogil blocks cannot call code in Python space. Else, Cython will abort compilation.

Francesc Alted How To Overcome the GIL Limitations Understanding the GIL Parallelism with Cython Summary Using Cython's prange

'Canonical' and easiest way to parallelize loops in Cython: for i in prange(N, nogil=True): x[i] = sin(i * beta)

This releases the GIL explicitely in the prange loop. Cython's prange makes use of OpenMP in the C output.

Francesc Alted How To Overcome the GIL Limitations Understanding the GIL Parallelism with Cython Summary A Few Notes About OpenMP

OpenMP is an open standard API for programming threads in C, C++ and Fortran. It is a set of: Compiler directives. Library routines. Environment variables that inuence run-time behaviour. Supported by most of current compilers: GCC, Microsoft MSVC, Intel ICC... It only runs eciently in shared-memory multicore platforms.

Francesc Alted How To Overcome the GIL Limitations Understanding the GIL Parallelism with Cython Summary An Example Of Parallel Code with Cython

Francesc Alted How To Overcome the GIL Limitations Understanding the GIL Parallelism with Cython Summary Summary

The GIL was introduced in Python for a good reason, and it is not going to disappear anytime soon. There are several solutions that allow to circumvent GIL limitations for threading apps. Threaded extensions, multiprocess and Cython are among the most interesting ones. Cython is the recommended way to go when you need to program your solution and really want to squeeze all the performance out of your cores in shared memory congurations.

Francesc Alted How To Overcome the GIL Limitations Understanding the GIL Parallelism with Cython Summary More Info

I David Beazley Understanding the Python GIL http://www.dabeaz.com/GIL/

I The Cython crew Cython: C-Extensions for Python http://cython.org

Francesc Alted How To Overcome the GIL Limitations Understanding the GIL Parallelism with Cython Summary What's Next

In the following exercises you will: Get familiar with some parallel code in Cython. Check which speed-ups can you achieve in multi-core processors. Learn curious things about scalability in dierent scenarios.

Francesc Alted How To Overcome the GIL Limitations Understanding the GIL Parallelism with Cython Summary Thank You!

Contact:

[email protected]

Francesc Alted How To Overcome the GIL Limitations