Numpy/SciPy 中的快速傅立叶变换 (FFT) 不是线程化的。 Enthought Python 附带 Intel MKL 数值库,该库能够进行线程 FFT。如何获得这些例程?
以下代码适用于 Windows 7 Ultimate 64 位上的 Enthought 7.3-1(64 位)。我还没有对它进行基准测试,但它肯定会同时使用所有核心,而不仅仅是一个。
from ctypes import *
class Mkl_Fft:
c_double_p = POINTER(c_double)
def __init__(self,num_threads=8):
self.dfti = cdll.LoadLibrary("mk2_rt.dll")
self.dfti.MKL_Set_Num_Threads(num_threads)
self.Create = self.dfti.DftiCreateDescriptor_d_md
self.Commit = self.dfti.DftiCommitDescriptor
self.ComputeForward = self.dfti.DftiComputeForward
def fft(self,a):
Desc_Handle = c_void_p(0)
dims = (c_int*2)(*a.shape)
DFTI_COMPLEX = c_int(32)
rank = 2
self.Create(byref(Desc_Handle), DFTI_COMPLEX, rank, dims )
self.Commit(Desc_Handle)
self.ComputeForward(Desc_Handle, a.ctypes.data_as(self.c_double_p) )
Usage:
import numpy as np
a = np.ones( (32,32), dtype = complex128 )
fft = Mkl_Fft()
fft.fft(a)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)