【问题标题】:Cythonize error: failed with exit status 2. numpy and pyvistaCythonize 错误:退出状态为 2 失败。numpy 和 pyvista
【发布时间】:2020-03-30 15:09:59
【问题描述】:

我想对以下代码进行 Cythonize,但收到错误消息。

import numpy as np
cimport numpy as np
import pyvista as pv
from mesh_funcs import *
cimport cython 
from libcpp cimport bool
#import matplotlib.pyplot as plt


#Getting mesh points from pyvista unfortunately with a for loop
cdef class pv_cell:
    def pv_grid_cell_data(mesh2,bool points_toggle=True,
                          bool centres_toggle=True,bool volumes_toggle=True, bool areas_toggle=True):

        if areas_toggle==True:
            points_toggle==True

        if centres_toggle==True:
            points_toggle==True

        cdef np.ndarray gcp=np.zeros([mesh2.n_cells,8,3])
        cdef np.ndarray gcc=np.zeros([mesh2.n_cells,3])
        cdef np.ndarray gcv=np.zeros([mesh2.n_cells,3])
        cdef np.ndarray grid_facets=np.array([[0,1,2,3], [0,1,5,4], [1,2,6,5], [7,6,2,3], [7,3,0,4], [4,5,6,7]])
        cdef np.ndarray gca=np.zeros([mesh2.n_cells,6])

        for n1 in range(0,mesh2.n_cells):
            if points_toggle==True:
                gcp[n1]=mesh2.extract_cells(n1).points

            if centres_toggle==True:
                gcc[n1]=[np.mean(gcp[n1][:,0]),np.mean(gcp[n1][:,1]),np.mean(gcp[n1][:,2])]

            if volumes_toggle==True:
                gcv[n1]=mesh2.extract_cells(n1).compute_cell_sizes()["Volume"]

            if areas_toggle==True:

                for n2 in range(0,6):
                    ph8=gcp[n1][grid_facets[n2]]
                    gca[n1,n2]=tri_area(ph8[[0,2,3]])+tri_area(ph8[[0,1,3]])
        return gcp,gcc,gcv,gca

我的setup.py如下

from setuptools import setup
from Cython.Build import cythonize
import numpy

setup(
    ext_modules=cythonize("pv_cell_funcs.pyx"),include_dirs=[numpy.get_include()])

我使用以下命令启动 setup.py。

python setup.py build_ext --inplace
pause

错误很长。它指出 bool 是一个未声明的标识符。然后它列出了很多语法错误。最后一行是

error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\BuildTools\\VC\\Tools\\MSVC\\...x64\\cl.exe' failed with exit status 2

【问题讨论】:

  • 请查看minimal reproducible example,您的示例不是最小的(您可能不需要那么多代码来触发错误)并且不完整(您只提供最后一行错误信息)。
  • 我的意思是“来自 libcpp cimport bool”可能足以触发错误...
  • 我已将错误消息作为图片包含在内。这个问题不仅仅针对 cython。问题是关于在 cython 中使用 numpy 和 pyvista。 Cython 对制作 2d 和 3d 模型的人很感兴趣,因为计算所需的循环数量以及因此需要进行性能优化。
  • 你完全错过了我关于最小示例的观点:如果示例是最小错误,则消息不长,可以逐字粘贴。因为您的示例不是最小的,所以可能存在多个问题。
  • 我明白了。澄清一下,这段代码在 python 中运行良好。一旦我尝试将代码转换为 cython,就发生了这个问题。

标签: python numpy cython cythonize pyvista


【解决方案1】:

因为你正在跑步:

from libcpp cimport bool

您应该在setup.py 中将您的语言从默认的c 更改为c++

from setuptools import setup
from Cython.Build import cythonize
import numpy

setup(
    ext_modules=cythonize("pv_cell_funcs.pyx", language='c++'),
    include_dirs=[numpy.get_include()])

请参阅How do I declare an object of type bool? 在 cython 中使用 bool

【讨论】:

  • 你好 Alex,我已经这样做了,但我的错误是一样的。我已将错误添加为问题中的图片。
  • 在本地使用 cython=0.29.14numpy=1.18.1 为我工作。请问您使用的是否是现代版本的numpycython
  • 我通过 anaconda 安装了 cython 0.29.15。我的 numpy 版本是 1.18.1。我在此链接cython.readthedocs.io/en/latest/src/tutorial/… 中成功完成了斐波那契示例
  • 我会将您的 cython 脚本减少到几行,然后从那里构建它。您的 cython 脚本有几个缺陷(包括对extract_cells 的重复调用),无法使其达到您希望的性能。 Cython 并不是真正可以从 python 复制和粘贴的东西,并且通常需要大量返工才能使其更像 C。
  • 这周我会这样做,并将您的回复标记为答案,并在周末关闭 github 问题!谢谢!
猜你喜欢
  • 2016-09-16
  • 1970-01-01
  • 1970-01-01
  • 2013-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多