3
votes

I want to use Cython to speed up my Python code in analogy to the Cython's Numpy tutorial. I give you a MWE of what I intended:

Test Function:

import pyximport
pyximport.install()
import CythonModule2 as cm2

print cm2.read_data()

Cython Module CythonModule2.pyx:

from libc.stdio cimport *
import numpy as np
cimport numpy as np

cdef packed struct CData:
    np.float32_t A
    np.uint8_t CH1, CH2
    np.int64_t D

def read_data():
    cdef np.ndarray[CData] coins = np.empty(10, dtype=[('A', 'f4'),
                                                       ('CH1', '|u1'), ('CH2', '|u1'),
                                                       ('D', '<i8')])
    return coins;

The definition in the function read_data() produces the following error message (and C++ "terminated in an unusual way"):

ValueError: Buffer dtype mismatch; next field is at offset 6 but 8 expected

I could format all entries in 64bit (eight byte) variables but I want to keep the data as small as possible in order to save space.

By the way: My computers setup is as following:

  • Windows 7 64bit
  • 32bit Python(x,y) 2.7.6
  • MinGW's compiler mingw32-gcc-4.8.1.
  • Cython version 0.23.2
  • Numpy version 1.10.0

I think it is not the same error as in the previous question since I updated Cython: Passing a structured numpy array with strings to a cython function

Edit

By compiling the module with distutils there is the same error displayed. When executing the function, the above error occurs.

from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize('CythonModule2.pyx'))
import CythonModule2 as cm2
print cm2.read_data()

Edit 2: Output files

There is no file called CythonModule2.pyd. The created files (CythonModule2.c, CythonModule2.def, CythonModule2.o) are stored in the following location:

C:\Users\Myself.pyxbld\temp.win32-2.7\Release\pyrex\

1
u1 is unicode, which is 3 or 4 bytes. - hpaulj
So that means I should rewrite the line with CH1 and CH2? The problem still exists if I remove them from CData and the definition. Do you have another idea about the float and int? - strpeter
I updated Numpy to version 1.10.0 but the problem was not solved. - strpeter
Sorry, I mistook u1 for U1. It is the uint8 that you expected. - hpaulj

1 Answers

0
votes

GCC: 5.2

Cython: 0.23.2

Python: 2.7.10

Numpy: 1.9.2

OS: Linux

With my environment all works fine. The result of running script:

[(1.5573903757535362e+29, 155, 127, 9195066190654341120)
 (7.709522375552512e+37, 229, 106, 7556991212100550555)
 (4.57762169340988e-41, 176, 211, 9056738852782958815)
 (nan, 0, 0, 140305456158120)
 (8.456264184549564e+24, 155, 127, 9195058375810875392)
 (-98784247808.0, 223, 104, 753066985721462683)
 (0.04620097950100899, 65, 0, 7696651763176177664)
 (38982272417792.0, 0, 0, 140305575418824)
 (0.04620097950100899, 61, 61, 4412750543122677053)