2012-03-01 79 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的錯誤信息可能會有點錯位,也許這是後面的陳述?