Using function pointers to methods of classes without the gil

我的梦境 提交于 2020-01-18 10:50:27

问题


Part of my works requires a lot of calculations, but they are often fairly straight-forward and can in principle quite easily be done in parallel with Cython's prange, requiring nogil. However, given that I tried to write my Cython code with a focus on having cdef classes as building blocks I encountered a problem.

Let's say I got an numerical integration routine or similar which takes a function pointer as an input

ctypedef double (* someFunctionPointer) (double tt) nogil

cdef double integrationRoutine(someFunctionfointer ff) nogil:
    # Doing something
    # ...
    return result

Now each of my integration points is actually a "larger" simulation (lots of parameters and so on I load/set/input or whatever), which is implemented in a class. So my initial approach was doing something like

cdef class SimulationClass:

    cdef double simulationFunctionPointer(SimulationClass self, double tt) nogil:
        # ...

where I though I could just hand "simulationFunctionPointer" to "integrationRoutine" and would be fine. This does of course not work because of the self argument.

All my work-arounds either require to

  • Not use cdef classes, rather something like a C struct (tricky if SimulationClass references a lot of other classes, parameters and so on)
  • Execute something with gil (because I want to work the SimulationClass; I wrote some stand alone function which took SimulationClass as a void*, but then I need to cast it to SimulationClass again, which requires the gil)

Any advice or ideas how to approach this problem? Is my first approach possible in other languages like C++?

Cheers


回答1:


You can use with gil: around the blocks that need the GIL, and then with nogil: around the important inner blocks that will take most of your run time. To give a trivial example

from cython.parallel import prange

cdef class Simulation:
    cdef double some_detail

    def __cinit__(self,double some_detail):
        self.some_detail = some_detail

    def do_long_calculation(self, double v):
        with nogil:
            pass # replace pass with something long and time-consuming
        return v*self.some_detail


def run_simulations(int number_of_simulations):
    cdef int n
    for n in prange(number_of_simulations,nogil=True):
        with gil: # immediately get the gil back to do the "pythony bits"
            sim = Simulation(5.3*n)
            sim.do_long_calculation(1.2) # and release again in here"

Provided the nogil section in do_long_calculation runs from longer than the section where you set up and pass the simulations (which can run in parallel with do_long_calculation, but not with itself) this is reasonably efficient.


An additional small comment about turning a bound method into a function pointer: you really struggle to do this in Cython. The best workround I have is to use ctypes (or possibly also cffi) which can turn any Python callable into a function pointer. The way they do this appears to involve some runtime code generation which you probably don't want to replicate. You can combine this method with Cython, but it probably adds a bit of overhead to the function call (so make sure do_long_calculation is actually long!)

The following works (credit to http://osdir.com/ml/python-cython-devel/2009-10/msg00202.html)

import ctypes
# define the function type for ctypes
ftype = ctypes.CFUNCTYPE(ctypes.c_double,ctypes.c_double)

S = Simulation(3.0)
f = ftype(S.do_long_calculation) # create the ctypes function pointer

cdef someFunctionPointer cy_f_ptr = (<someFunctionPointer*><size_t>ctypes.addressof(f))[0] # this is pretty awful!


来源:https://stackoverflow.com/questions/34878942/using-function-pointers-to-methods-of-classes-without-the-gil

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!