每当我为多维内存 View 的一个切片分配单个值时,Cython似乎都会使用错误的步幅,除非该切片沿第一个维度。我在下面给出一个完整的例子:

# bug.py
import numpy as np

def bug():
    #cdef int[:, ::1] a
    a = 2*np.ones((2, 2), dtype=np.intc)
    a[:, :1] = 1
    print(np.asarray(a))

如果我们在Python中运行此代码(例如python3 -c 'import bug; bug.bug()'),则会得到
[[1 2]
 [1 2]]

按预期打印。现在,通过将文件重命名为bug.pyx,用Cython对其进行编译,将以下内容保存在同一目录中的Makefile中,
# Makefile
python = python3
python_config = $(python)-config
CC = gcc
CFLAGS  = $(shell $(python_config) --cflags) -fPIC
CFLAGS += $(shell $(python_config) --includes)
python_libdir = $(shell $(python) -c "import sysconfig; \
    print(sysconfig.get_config_var('LIBDIR'));")
LDLIBS  = -L$(python_libdir) -Wl,-rpath=$(python_libdir)
LDLIBS += $(shell $(python_config) --libs)
LDFLAGS = $(shell $(python_config) --ldflags) -shared

bug.so: bug.c; $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o bug.so bug.c
bug.c: bug.pyx; $(python) -m cython -3 $<

并运行make。现在再次运行python3 -c 'import bug; bug.bug()',它会拾取已编译的bug.so,然后再次打印出
[[1 2]
 [1 2]]

如果现在取消注释cdef声明,再次运行makepython3 -c 'import bug; bug.bug()',我们得到
[[1 1]
 [2 2]]

这是错误的。我不相信int[:, ::1]声明是错误的,因为Cython会提示。如果我只用int[:, :]替换它,它就可以工作。另外,如果我将a的第一个维度赋给a[:1, :] = 1,那么它也可以工作。

这是一个已知问题,还是我以某种方式误解了Cython内存 View 的这种看似基本用法?

最佳答案

我提交了bug report,此后一直是fixed

关于python - Cython:将单个元素分配给多维内存 View 切片,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55959627/

10-12 23:28