3

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\

Community
  • 1
  • 1
strpeter
  • 2,562
  • 3
  • 27
  • 48

1 Answers1

0

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)
Community
  • 1
  • 1
AndreyT
  • 1,449
  • 1
  • 15
  • 28
  • I am still using Numpy version 1.9.1 and the gcc 4.8.1 (which is the most recent one for Windows). It could be the case that I did something wrong with the configuration of my MinGW. – strpeter Sep 28 '15 at 15:53
  • Can you pastebin your generated CythonModule2.c file? I want compare it with mine. – AndreyT Sep 29 '15 at 11:17
  • I uploaded the generated file `CythonModule2.c` on http://pastebin.com/aZmGURMs so you can check with your version. – strpeter Oct 01 '15 at 08:24
  • Files is very similar. Also I compile your *.c file without any problem. It's just a guess, but what if you install 64bit python version? – AndreyT Oct 02 '15 at 15:32