在numpy中使用mkl_set_num_threads [英] Using mkl_set_num_threads with numpy
问题描述
我正在尝试使用mkl_set_num_threads
这样设置numpy计算的线程数
I'm trying to set the number of threads for numpy calculations with mkl_set_num_threads
like this
import numpy
import ctypes
mkl_rt = ctypes.CDLL('libmkl_rt.so')
mkl_rt.mkl_set_num_threads(4)
但我不断遇到细分错误:
but I keep getting an segmentation fault:
Program received signal SIGSEGV, Segmentation fault.
0x00002aaab34d7561 in mkl_set_num_threads__ () from /../libmkl_intel_lp64.so
获取线程数没问题:
print mkl_rt.mkl_get_max_threads()
如何使我的代码正常工作? 还是有另一种方法可以在运行时设置线程数?
How can I get my code working? Or is there another way to set the number of threads at runtime?
推荐答案
Ophion引导了我正确的方法.尽管有文档说明,但必须通过引用来传递mkl_set_num_thread
的参数.
Ophion led me the right way. Despite the documentation, one have to transfer the parameter of mkl_set_num_thread
by reference.
现在我已经定义了用于获取和设置线程的函数
Now I have defined to functions, for getting and setting the threads
import numpy
import ctypes
mkl_rt = ctypes.CDLL('libmkl_rt.so')
mkl_get_max_threads = mkl_rt.mkl_get_max_threads
def mkl_set_num_threads(cores):
mkl_rt.mkl_set_num_threads(ctypes.byref(ctypes.c_int(cores)))
mkl_set_num_threads(4)
print mkl_get_max_threads() # says 4
它们按预期工作.
根据Rufflewind的说法,C函数的名称以大写形式编写,期望按值使用参数:
according to Rufflewind, the names of the C-Functions are written in capital-case, which expect parameters by value:
import ctypes
mkl_rt = ctypes.CDLL('libmkl_rt.so')
mkl_set_num_threads = mkl_rt.MKL_Set_Num_Threads
mkl_get_max_threads = mkl_rt.MKL_Get_Max_Threads
这篇关于在numpy中使用mkl_set_num_threads的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!