Running C++ functions in parallel with python multithreading: does PyROOT release the GIL?

Hi,
I’d like to execute some C++ workload in parallel in multiple threads, and steer everything from python.
Below is what I have so far. It seems that threads run sequentially, however.

I suppose that’s because PyROOT does not release the GIL? If I run time.sleep(2) instead of the C++ function threads do run concurrently.

How can I convince the threads to run concurrently in this scenario?

Cheers,
Enrico

import ROOT
from threading import Thread
from time import sleep
from collections import deque

ROOT.gInterpreter.Declare("""
    void foo() { for (int i=0; i < 1000000000; ++i)
    ;
    std::cout << "done" << std::endl; }
""")

ROOT.ROOT.EnableImplicitMT()
threads = [Thread(target=ROOT.foo) for _ in range(ROOT.ROOT.GetImplicitMTPoolSize())]
deque(map(Thread.start, threads))
deque(map(Thread.join, threads))

ROOT Version: master
Platform: linux


There is an attribute of MethodProxys called _threaded:

      { (char*)"_threaded", (getter)mp_getthreaded, (setter)mp_setthreaded,
            (char*)"If true, releases GIL on call into C++", NULL },

You can try setting it to True for ROOT.foo.

It works, thanks!
Adding ROOT.foo._threaded = True the runtime of the last three lines decreases from ~14.5 seconds to ~2.5 on my 8 core machine.

Cheers,
Enrico

P.S.
is this documented anywhere? :smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.