Ruby Fiddle和全局解释器锁(GIL) [英] Ruby Fiddle and Global Interpreter Lock (GIL)
问题描述
我正在使用小提琴进行呼叫用于某些线性代数计算(Mac OSX).
I am using Fiddle to call an external C Function for some heavy linear algebra calculations (Mac OSX).
模块定义类似于
module BLAS
extend Fiddle::Importer
dlload $BLAS_PATH
extern 'void cblas_dgemm(int, int, int, int, int, int, double, double*, int, double*, int, double, double*, int)'
extern 'void cblas_dgemv(int, int, int, int, double, double*, int, double*, int, double, double*, int)'
.......
这些库针对Mac osx进行了优化,可以使用所有可用的内核.不幸的是,当我从Ruby中使用Fiddle调用这些函数时,仅使用了一个内核,我的理解是这是由于GIL
造成的.
The libraries have been optimized for mac osx and can use all the cores available. Unfortunately when I call these functions from Ruby with Fiddle only one core is used and my understanding is that this happens because of the GIL
.
是否可以在执行外部功能期间释放锁?
Is there a way to release the lock during the execution of external functions?
我正在使用ruby 2.0.0-p247
(不是系统Ruby).
I am using ruby 2.0.0-p247
(not the system Ruby).
推荐答案
In ruby 2.3 this is the default (see the issue on the ruby bug tracker).
在此之前,它看起来像不是在Fiddle api中公开的.在ruby c API中有一个名为rb_thread_call_without_gvl
的函数,其功能与在锡罐上所说的完全一样:释放gvl,调用传递的函数,重新获取gvl并返回.使用起来有点棘手-请务必阅读 thread.c中的注释.
Prior to that, it doesn't look like it was exposed in the Fiddle api. There is a function in the ruby c API called rb_thread_call_without_gvl
that does pretty much exactly what it says on the tin: it releases the gvl, calls the passed function, reacquires the gvl and returns. It's a little tricky to use - be sure to read the comments in thread.c.
如果您无法升级,我怀疑您最好的选择是编写一个非常薄的C层,用rb_thread_call_without_gvl
包装调用,然后使用小提琴来调用该包装器库.
If you can't upgrade, I suspect your best bet is to write a very thin C layer that wraps calls with rb_thread_call_without_gvl
and then use fiddle to call that wrapper library.
这篇关于Ruby Fiddle和全局解释器锁(GIL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!