Bob and multi-threading with python

54 views
Skip to first unread message

Manuel Günther

unread,
Nov 20, 2013, 11:58:45 AM11/20/13
to bob-...@googlegroups.com
Today I tried to run some experiments with Bob using python's threading module.
I created 8 parallel threads using Bob code, but I got only a single thread running at a time.

After discussion with André, he said that it's a python-related issue. It seems that python can run jobs only in a single thread.

BUT:
When we have some code implemented in C++, there is actually the *possibility* to run parallel threads. The way to achieve this is to unlock python's "Global Interpreter Lock" (GIL) before starting C++ code.
This can be done in the binding functions, by simply creating an object of the bob::python::no_gil class, e.g.

#include <bob/python/gil.h>

static void my_bound_function(...){
  // set up python
  ...
  // release the GIL
  bob::python::no_gil no_gil_object;
  // call C++ function
  my_parallelizable_function(...);
}

The only thing to note is that the called function must be **thread safe**, which means that no memory is used internally to cache data.


Now, for most functions and classes of Bob, which perform long calculations, this GIL is not unlocked before execution. Effectively, this means that only one thread of Bob code can run in parallel (of course, you can have multiple processes, but no parallel threads).
As far as possible, it would be nice to change the bindings of those functions to include unlocking the GIL.
Since the thread safety of functions cannot be guaranteed, I would propose to have *two* versions of the function bound, one with and one without unlocking the GIL. The new functions might have a different name (e.g., adding a _nogil at the end) to be able to distinguish between them.

In any case, the user of these functions have to take care of the thread safety. E.g., when objects caches data, you can get them thread safe by copying the object in each of the parallel threads.

If I want to apply these changes in Bob, is there anything else that I have to take care of, and that I might have forgotten?

Cheers
Manuel

André Anjos

unread,
Nov 20, 2013, 12:05:33 PM11/20/13
to bob-...@googlegroups.com
Just a small add-on:

After unlocking the GIL, your code *cannot* use any python, boost::python or bob::python construction. Only the pure C++ code can be called at that point.

To use Python constructs again, you must re-acquire the GIL.

A


--
-- You received this message because you are subscribed to the Google Groups bob-devel group. To post to this group, send email to bob-...@googlegroups.com. To unsubscribe from this group, send email to bob-devel+...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/bob-devel or directly the project website at http://idiap.github.com/bob/
---
You received this message because you are subscribed to the Google Groups "bob-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bob-devel+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Dr. André Anjos
Idiap Research Institute
Centre du Parc - rue Marconi 19
CH-1920 Martigny, Suisse
Phone: +41 27 721 7763
Fax: +41 27 721 7712
http://andreanjos.org
Reply all
Reply to author
Forward
0 new messages