Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #108607

Re: Calling python from C with OpenMP

From MRAB <python@mrabarnett.plus.com>
Newsgroups comp.lang.python
Subject Re: Calling python from C with OpenMP
Date 2016-05-13 18:12 +0100
Message-ID <mailman.637.1463159537.32212.python-list@python.org> (permalink)
References <8224bdd2-9afe-487b-804b-f3b88dee2028@googlegroups.com> <1288606789484790413.405054sturla.molden-gmail.com@news.gmane.org> <mailman.618.1463097878.32212.python-list@python.org> <91ea4bb0-2d34-4a27-8b18-640c79712f64@googlegroups.com> <4cb4d566-709d-1139-b515-2eb3cdbbbc9a@mrabarnett.plus.com>

Show all headers | View raw


On 2016-05-13 17:22, Øystein Schønning-Johansen wrote:
> On Friday, May 13, 2016 at 2:04:53 AM UTC+2, Sturla Molden wrote:
>> You must own the GIL before you can safely use the Python C API, object
>> creation and refcounting in particular. Use the "Simplified GIL API" to
>> grab the GIL and release it when you are done.
>
> I've now read about the GIL and it looks like I am in deep problems.
>
> I've added the GILState lock to the threaded loop like this:
>
> #pragma omp parallel for
>     for( int i = 0; i < 10; i++ ){
>         PyGILState_STATE gstate;
>         gstate = PyGILState_Ensure();
>         PyObject *ret = PyObject_CallMethod( mult_obj, "do_multiply", "i", i );
>         if( !ret ){
>             printf("Cannot call 'do_multiply'\n");
>             continue;
>         }
>         printf("The value calculated in Python was: %3d\n", (int) PyLong_AsLong(ret));
>         Py_DECREF(ret);
>         PyGILState_Release(gstate);
>     }
>
> .... but still no success. Have I done it right?
>
> regs,
> -Øystein
>
Every PyGILState_Ensure call must be matched with a PyGILState_Release 
call. The way it's currently written, it won't call PyGILState_Release 
if ret is NULL.

However, I don't think you'll gain much here because you can gain from 
multi-threading only if the threads can run in parallel. You need to 
hold the GIL while making Python calls, and only 1 thread can hold the 
GIL at any time.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Calling python from C with OpenMP oysteijo@gmail.com - 2016-05-12 12:28 -0700
  Re: Calling python from C with OpenMP Sturla Molden <sturla.molden@gmail.com> - 2016-05-13 00:04 +0000
    Re: Calling python from C with OpenMP Øystein Schønning-Johansen <oysteijo@gmail.com> - 2016-05-13 09:22 -0700
      Re: Calling python from C with OpenMP MRAB <python@mrabarnett.plus.com> - 2016-05-13 18:12 +0100
        Re: Calling python from C with OpenMP Øystein Schønning-Johansen <oysteijo@gmail.com> - 2016-05-13 13:16 -0700
          Re: Calling python from C with OpenMP Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-05-13 20:04 -0400
            Re: Calling python from C with OpenMP Paul Rubin <no.email@nospam.invalid> - 2016-05-13 19:09 -0700

csiph-web