【发布时间】:2019-12-27 22:07:24
【问题描述】:
ctypedef struct ReturnRows:
double[50000] v1
double[50000] v2
double[50000] v3
double[50000] v4
有效,但是
ctypedef struct ReturnRows:
double[100000] v1
double[100000] v2
double[100000] v3
double[100000] v4
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV) 失败
这对我来说没有意义,因为上限应该接近专用于该处理任务的系统的可用限制。是否以某种方式设置了上限?
这是我的构建器:
from distutils.core import setup
import numpy as np
from distutils.core import setup, Extension
from Cython.Build import cythonize
file_names = ['example_cy.pyx', 'enricher_cy.pyx']
for fn in file_names:
print("cythonize %s" % fn)
setup(ext_modules = cythonize(fn),
author='CGi',
author_email='hi@so.com',
description='Utils for faster data enrichment.',
packages=['distutils', 'distutils.command'],
include_dirs=[np.get_include()])
来自问题:我如何使用结构?我迭代它,来自熊猫数据框:
cpdef ReturnRows cython_atrs(list v1, list v2, list v3, list v4):
cdef ReturnRows s_ReturnRows # Allocate memory for the struct
s_ReturnRows.v1 = [0] * 50000
s_ReturnRows.v2 = [0] * 50000
s_ReturnRows.v3 = [0] * 50000
s_ReturnRows.v4 = [0] * 50000
# tmp counters to keep track of the latest data averages and so on.
cdef int lines_over_v1 = 0
cdef double all_ranges = 0
cdef int some_index = 0
for i in range(len(v3)-1):
# trs
s_ReturnRows.v1[i] = (round(v2[i] - v3[i],2))
# A lot more calculations, why I really need this loop.
【问题讨论】:
-
这能回答你的问题吗? Segmentation fault on large array sizes
-
@eod 谢谢,但遗憾的是不是真的。提出的唯一真正的解决方案是使用
ulimit -s unlimited禁用操作系统的内存限制,这基本上不是一个好主意。我正在寻找一种方法来实际存储这些大量数据,并符合操作系统内存管理。 -
如果不知道如何分配这些结构(您没有显示),这不是一个非常有用的问题。链接问题给出的更有用的解决方案是在堆而不是堆栈上分配(C++ 中的
new,但 Cython 中可能是malloc) -
@DavidW 添加了一些有关处理的信息。也许主要问题可能导致:是否可以预先分配和使用像这样更大的区域,或者这可能更适合其他数据结构(也许比如2darrays?)。我有兴趣最终将超过 2 亿行存储到一个旋转数据结构中,并且认为原生 c 类型最合适。
标签: python-3.x memory-management segmentation-fault cython ubuntu-18.04