2012-03-01 56 views
0

在我为结果分配numpy数组的cdef中,出现以下错误。Cython MemoryError

---> 56  cdef np.ndarray[DTYPE_t, ndim=2] alignpmf = np.zeros([bin_len, out_len*bin_len],dtype=float) 
MemoryError: 

相关的代码是:

from __future__ import division 
import numpy as np 
cimport numpy as np 
cimport cython 
DTYPE = np.int 
DTYPE_f = np.float 
ctypedef np.float_t DTYPE_t 
ctypedef np.int_t DTYPE_i 

... 

@cython.boundscheck(False) 
@cython.wraparound(False) 
def full_pmfs(np.ndarray[DTYPE_i, ndim=2] align, np.ndarray[DTYPE_i, ndim=1] bins): 

    assert align.dtype == DTYPE 
    assert bins.dtype == DTYPE 
    cdef int loop_ind_i, loop_ind_j, inner_count, inner_count_start, inner_count_stop 
    cdef int bin_len = bins.shape[0] 
    cdef int i_start_ind, i_stop_ind 
    cdef int seqs = align.shape[0] 
    cdef int residues = align.shape[1] 
    cdef int size = residues * bin_len 
    cdef int out_len = residues**2 - residues // 2) 
    cdef np.ndarray[DTYPE_t, ndim=2] alignpmf = np.zeros([bin_len, 
    out_len*bin_len],dtype=float) 
    ... 

是什么原因造成的错误任何线索?如果我用python编写相同的代码,我不会收到内存错误。当我运行纯粹的numpy或者cython代码时,它几乎不会消耗我的内存(这个盒子上的12GB)。作为参考,bin_len可能在20左右,out_len可能是80,000。

的PYX与蟒蛇编译setup.py build_ext --inplace:

from distutils.core import setup 
from distutils.extension import Extension 
from Cython.Distutils import build_ext 
import numpy 

ext_modules = [Extension("mi", ["mi.pyx"])] 

setup(
    name = 'MI calcs', 
    cmdclass = {'build_ext': build_ext}, 
    ext_modules = ext_modules, 
    include_dirs = [numpy.get_include(),], 
    ) 
+0

你确定bin_len和out_len是你认为的值吗?也许之前插入一个打印语句? – tillsten 2012-06-04 19:24:38

回答

1

我不能重建错误 - 在你的代码删除尾部“)”(当你计算残留后// 2)并按以下方式调用它:

from numpy import * 
import mi 
if __name__ == '__main__': 
    a = ones((20,300),mi.DTYPE) 
    b = ones(20,mi.DTYPE) 
    mi.full_pmfs(a,b) # gives you bin_len = 20 and out_len = 89850 

这对我来说很好。

你是怎么调用函数的?另外,有时候,我的经验中,有时候cython的错误信息可能会有点错位,也许这是后面的陈述?