f2py function release GIL

巧了我就是萌 提交于 2019-12-23 12:01:39

问题


Does the Global Interpretter Lock (GIL) get released when I call an f2py wrapped function?

(I'm happy to try to discover on my own, but I'm not familiar enough with the numpy source to know where to start looking)...

To clarify, a good answer to this question would either help me to know where in the numpy source to look for a Py_BEGIN_ALLOW_THREADS or it would simply let me know if the GIL is released (preferably with some evidence).


回答1:


No, f2py defaults to leaving the GIL in place. However, you can release the GIL by adding the threadsafe directive.

example:

subroutine foo(a)
!f2py threadsafe
!f2py intent(out) :: a
integer a
a = 5
end subroutine foo

Now compile it:

f2py -c -m foo --build-dir test_build foo.f90

And we can check the source code:

grep THREAD test_build/src.*/*.c
build/src.linux-x86_64-2.7/testmodule.c:      Py_BEGIN_ALLOW_THREADS
build/src.linux-x86_64-2.7/testmodule.c:      Py_END_ALLOW_THREADS

However, if we repeat the process removing the !f2py threadsafe line, then the macros to release the GIL aren't included.



来源:https://stackoverflow.com/questions/15976369/f2py-function-release-gil

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