numpy, pandas出现Illegal instruction (core dumped)问题的解决方案

系统信息:CentOS Linux release 7.2.1511 (Core),CPU:Intel(R) Xeon(R) CPU E5-2620 v4 @ 2.10GHz

在近期的项目中使用pandas读取csv文件时出现状态码为132的Illegal instruction (core dumped)错误,经过调试

import pandas as pd
data=pd.read_csv('./data.csv')
发现使用read_csv()时会出现Illegal instruction (core dumped)错误,在google查询该问题后找到github上的两条相关的讨论帖:

read_csv throws Illegal instruction (core dumped) on a simple csv file

Numpy crashes during tests with a "Illegal instruction (core dumped)".
在按照第一条链接中的回复进行相关试验

import numpy as np

a = np.arange(0, 100, 1)
np.where(a>90)
发现同样出现Illegal instruction (core dumped)错误,同时会出现该错误的还有

pd.DataFrame()

pd.read_table('./data.csv',sep=',')

随后转到第二条链接找到相关信息及问题所在。查询相关问题时找到了在简书上的一篇博客:

Numpy在云虚拟机上crash问题

这里对其问题分析进行转述,详细可查看简书链接


root cause
  1. 宿主机CPU并不支持AVX或者AVX2标签,但是在创建虚拟机时,给系统打上了AVX/AVX2的tag。
  2. 当Pandas创建的DataFrame列大于31时,会走到AVX/AVX2的路径,因为虚拟机有AVX/AVX2的标签,但是执行到AVX/AVX2的逻辑时,因为宿主机并不支持,所以core dump Illegal instruction。
  3. core栈如下:
Program received signal SIGILL, Illegal instruction.
0xb61338a8 in LONG_greater_avx2 (args=args@entry=0xbfffe52c,
    dimensions=dimensions@entry=0xbfffe538, steps=steps@entry=0xbfffe544,
    __NPY_UNUSED_TAGGEDfunc=__NPY_UNUSED_TAGGEDfunc@entry=0x0)
    at numpy/core/src/umath/loops.c.src:936
936    numpy/core/src/umath/loops.c.src: No such file or directory.
(gdb) bt
#0  0xb61338a8 in LONG_greater_avx2 (args=args@entry=0xbfffe52c,
    dimensions=dimensions@entry=0xbfffe538, steps=steps@entry=0xbfffe544,
    __NPY_UNUSED_TAGGEDfunc=__NPY_UNUSED_TAGGEDfunc@entry=0x0)
    at numpy/core/src/umath/loops.c.src:936
作者:biggeng
链接:https://www.jianshu.com/p/989d3aa8f5a2
來源:简书

解决方案:

1、删除系统已有的numpy库

pip uninstall numpy

2、下载numpy源代码

git clone https://github.com/numpy/numpy.git numpy
执行完成之后会在当前目录生成一个numpy目录 cd numpy/ 进入该目录

3、修改Numpy库文件

执行 cd numpy/core/include/numpy/ 在该目录下找到 npy_common.h 文件,建议使用ftp将其传到本地进行编辑(看的清楚,比较方便),找到第104行的位置(如图),将AVX/AVX2 设为0。

左边为修改前,右边为修改后。

numpy, pandas出现Illegal instruction (core dumped)问题的解决方案_第1张图片

这里贴上代码方便复制粘贴

#ifdef HAVE___BUILTIN_CPU_SUPPORTS



    #define NPY_CPU_SUPPORTS_AVX2 0




    #define NPY_CPU_SUPPORTS_AVX 0

#else
  #define NPY_CPU_SUPPORTS_AVX 0
  #define NPY_CPU_SUPPORTS_AVX2 0
#endif
linux下可以通过 vi/vim 编辑 npy_common.h ,:104 跳转到104行,修改代码后 :wq 保存退出。


4、编译并安装numpy

在安装前先确保安装了 cython, python-dev, python-pip

CentOS下:
yum install python-pip (一般都有不需要)
yum install python-devel
pip install Cython

Ubuntu下:
sudo apt-get install python-dev  
sudo apt-get install python-pip  
pip install Cython
在 numpy 根目录下执行

python setup.py build

编译过程中若是出现缺少相关module组件的话,可以自行安装后再执行编译

python setup.py install

完成之后重新测试 pandas.read_csv(), pandas.DataFrame() 等代码时若没有出现Illegal instruction (core dumped)错误,则成功解决了该问题。



你可能感兴趣的:(python,python,pandas,numpy,CentOS,linux)