Emanuel Schorsch | 14 Apr 04:17 2016
Picon

confused about extension type declarations

I apologize if the wording of this question isn't correct. I have looked at the docs but I seem to be missing some piece of the puzzle. The basic idea is that I have a player class and want to be able to type define that Roster.players is a list of players so that operations are faster. Looking around I'm beginning to think that I can't declare a list of players which isn't great but I think I can get most of the benefits by at least type declaring <cur_player> as a Player object. 

Here is the meat of player.pyx
cdef class Player:
    cdef
public name
    cdef
public int age


   
def __init__(self, name="DEFAULT", age=-1):
       
self.name = name
       
self.age = age


My understanding is that the .pyx file is the implementation file and the .pxd file is the declarations file. I first tried having the declarations of name and age only in the .pxd file but it wouldn't compile properly. It seems wrong and redundant to have to declare name and age in both the .pyx and the .pxd file but I can't import Player in Roster.pyx unless I have them defined in the .pxd as well. I've tried a few different ways of importing but none of them seem to work. I wish I had a more articulate question but I just can't seem to make any progress on this. I attached what I believe is a minimal not working example. My instincts tell me that I'm misunderstanding the role of either .pyx or .pxd files or maybe there just isn't a way to do what I'm looking for. 

I'm cythonizing player.pyx and roster.pyx so maybe the obstacle is that I want to be able to initialize player and roster objects in runner.py without cythonizing it. Does the initialization need to be done inside a cythonized file? If so what is the best approach, just bite the bullet and cythonize runner.py or write a thin wrapper in demo/sub1 that is cythonized and allows me still setup in the pure python of runner.py. I appreciate any hints you can give me.

Here is the demo: https://github.com/emschorsch/cython_demo

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Ecolss Logan | 14 Apr 05:37 2016
Picon

Assign C 2d array to pointer **

What I want to do is as follows:

cdef int a[10][10]
cdef int **b = &a[0]   # assign the 2d array pointer a to b, error here

I know that I could use typed memoryview like this:
cdef int[:,:] b = <int[:10,:10]>a

but I wonder how to assign 2d array to a pointer **, which could be simply done in C.

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Martin Bammer | 13 Apr 07:50 2016
Picon

How to make cdef symbol visible to Python code?

Hi,

Following example fails:

definitions.py:
A = 1


definitions.pxd
cdef public int A


main.py:
import plugin
plugin.Test()


main.pxd:
from definitions cimport A


plugin.py:
from definitions import *

def Test():
   return A


definitions.py and main.py are cythonized. plugin.py not. A is not accessible in plugin.py because it is not visible to Python code. How can I make A visible to Python code?


Regards, Martin

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Antony Lee | 13 Apr 07:42 2016
Picon

Bug: cython misses dependencies from include directive

I have

setup.py::
import sys
from setuptools import setup
from Cython.Build import cythonize
setup(ext_modules=cythonize("testmod.pyx", include_path=sys.path))

and

testmod.pyx::
IF UNAME_SYSNAME != "Windows":
    include "cysignals/signals.pxi"

(because cysignals doesn't work on Windows right now).

Compilation fails:
testmod.c:269:28: fatal error: struct_signals.h: No such file or directory

probably because cython's distutils metadata at the top of the generated testmod.c is empty, whereas it contains

    "distutils": {
        "depends": [
            "/usr/lib/python3.5/site-packages/cysignals/pxi.h",
            "/usr/lib/python3.5/site-packages/cysignals/struct_signals.h"
        ],
        "extra_link_args": [
            "-Ddummy"
        ],
        "include_dirs": [
            "/usr/lib/python3.5/site-packages/cysignals"
        ]
    }

in absence of the UNAME_SYSNAME guard.

Best,

Antony

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Martin Bammer | 12 Apr 23:55 2016
Picon

fatal error C1061: compiler limit : blocks nested too deeply

When I cimport many constants defined with "cdef int" in a pxd file then the CL (MSVC) fails to compile the created cpp-file.
The problem is the function __pyx_import_star_set created by cython. For each imported constant an "else if" is created. CL doesn't like this.
This problem occurs with all newer cython versions.

Here are the first few lines of this function:

static int __pyx_import_star_set(PyObject *o, PyObject* py_name, char *name) {
  static const char* internal_type_names[] = {
    "TestModule",
    "__pyx_ctuple___dunderpyx_ctuple_float__dunderand_Py_ssize_t__dunderand_int",
    "__pyx_ctuple___dunderpyx_ctuple_float__dunderand_Py_ssize_t__dunderand_int_struct",
    "__pyx_ctuple_double",
    "__pyx_ctuple_double_struct",
    "__pyx_ctuple_float",
    "__pyx_ctuple_float__and_Py_ssize_t__and_int",
    "__pyx_ctuple_float__and_Py_ssize_t__and_int_struct",
    "__pyx_ctuple_float_struct",
    "__pyx_ctuple_int",
    "__pyx_ctuple_int__and_Py_ssize_t",
    "__pyx_ctuple_int__and_Py_ssize_t_struct",
    "__pyx_ctuple_int__and_int",
    "__pyx_ctuple_int__and_int__and_Py_ssize_t",
    "__pyx_ctuple_int__and_int__and_Py_ssize_t_struct",
    "__pyx_ctuple_int__and_int_struct",
    "__pyx_ctuple_int_struct",
    "__pyx_ctuple_long",
    "__pyx_ctuple_long_struct",
    "__pyx_opt_args_11TestModule_11TestModule_Function1",
    "__pyx_opt_args_11TestModule_11TestModule_Function2",
    "__pyx_opt_args_11TestModule_11TestModule_Function3",
    "__pyx_scope_struct____Pyx_CFunc_object____TestModule____dict___to_py",
    "__pyx_scope_struct____Pyx_CFunc_object____TestModule____object___to_py",
    "__pyx_scope_struct____Pyx_CFunc_object____object___to_py",
    "__pyx_scope_struct____Pyx_CFunc_void____TestModule____object___to_py",
    0
  };
  const char** type_name = internal_type_names;
  while (*type_name) {
    if (__Pyx_StrEq(name, *type_name)) {
      PyErr_Format(PyExc_TypeError, "Cannot overwrite C type %s", name);
      goto bad;
    }
    type_name++;
  }
  if (0);
  else if (__Pyx_StrEq(name, "CACHE_APPEND")) {
    __pyx_v_9TestBase_CACHE_APPEND = __Pyx_PyInt_As_int(o); if (unlikely((__pyx_v_9TestBase_CACHE_APPEND == (int)-1) && PyErr_Occurred())) __PYX_ERR(3, 137, __pyx_L2_error)
  }
  else if (__Pyx_StrEq(name, "CACHE_CLEANUP")) {
    __pyx_v_9TestBase_CACHE_CLEANUP = __Pyx_PyInt_As_int(o); if (unlikely((__pyx_v_9TestBase_CACHE_CLEANUP == (int)-1) && PyErr_Occurred())) __PYX_ERR(3, 135, __pyx_L2_error)
  }
  else if (__Pyx_StrEq(name, "CACHE_CLEAR")) {
    __pyx_v_9TestBase_CACHE_CLEAR = __Pyx_PyInt_As_int(o); if (unlikely((__pyx_v_9TestBase_CACHE_CLEAR == (int)-1) && PyErr_Occurred())) __PYX_ERR(3, 134, __pyx_L2_error)
  }
  else if (__Pyx_StrEq(name, "CACHE_IGNORE")) {
    __pyx_v_9TestBase_CACHE_IGNORE = __Pyx_PyInt_As_int(o); if (unlikely((__pyx_v_9TestBase_CACHE_IGNORE == (int)-1) && PyErr_Occurred())) __PYX_ERR(3, 140, __pyx_L2_error)
  }
  else if (__Pyx_StrEq(name, "CACHE_LOCAL")) {
    __pyx_v_9TestBase_CACHE_LOCAL = __Pyx_PyInt_As_int(o); if (unlikely((__pyx_v_9TestBase_CACHE_LOCAL == (int)-1) && PyErr_Occurred())) __PYX_ERR(3, 141, __pyx_L2_error)
  }
...


Regards, Martin

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Justin Tervala | 11 Apr 16:15 2016
Picon

Cython Class inheritance and embedding in C++

I need to embed my Cython classes in some C++ code, and I cannot get the inheritance to work, and keep getting segmentation faults when trying to access fields of the base class. Due to the constraints of the project, it is not possible to wrap the C++ class or remove the classes from the Cython code. Here is a distilled example of my problem:

<<< file: fooClass.pyx >>>

from math import sin cdef public class FooSuper[object FooSuper, type FooSuperType]: def __init__ (self, a): print "FooSuper.__init__ START" self.a = a print "FooSuper.__init__ END" cdef public class Foo[object Foo, type FooType]: def __init (self, a, b): print "Foo.__init__ START" FooSuper.__init__(self, a) self.b = b print "Foo.__init__ END" cdef double bar (self, double c): return sin(self.a * c) cdef public Foo buildFoo (double a, double b): print "buildFoo(a,b) START" return Foo(a,b) cdef public double foobar (Foo foo, double c): print "foobar(Foo,c) START" return foo.bar(d)

<<< file: foo.cc >>>

#include <Python.h> #include "fooClass.h" #include <iostream> int main(){ Py_Initialize(); initfooClass(); Foo *foo = buildFoo(1.0, 2.0); std::cout << foobar(foo, 3.0) << std::endl; Py_Finalize() }

When I run this, I receive the following:

<<< std out >>>

buildFoo(a,b) Foo.__init__ START foobar(Foo,d) Segmentation fault

I know that this approach to embedding Cython in C++ works with just the Foo class, but when I try to extend Foo from FooSuper, the FooSuper.__init__ is never called. I know this problem must come from my method of initializing, but I have converted both __init__s to __cinit__s with no change. Clearly the segfault is coming from the fields of FooSuper not being initialized at all. I have also tried adding a hollow __cinit__(self, *args) to the FooSuper class with no change. If anyone can help me out, I'd be very appreciative. Thanks.

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Antoine Fressancourt | 10 Apr 23:01 2016
Picon

Embedding Cython and distutils

Hello,

I have a program using Cython that I work on. Cython has helped me improve the performance of my program with an impressive gap. 

Now, I would like to generate an executable using Cython's embedding capabilities. In the documentation I read, lots of things allude to the use of Makefile to compile a .pyx file in an executable. 

As in my own process I am using python's distutils, is there a way to generate an executable using this method? is there an Extension option to set? 

Thanks in advance for your answer,

Antoine

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Ecolss Logan | 10 Apr 10:04 2016
Picon

performance of Python's `sum` vs `np.sum` vs writing a naive sum

Python has a built-in function `sum`, I just wonder what the performance will be if I use Python's `sum` in Cython, below is the code in comparison to `np.sum` and simple code-up sum.

# Python's sum
cpdef long py_sum(long[:] A):
  cdef long s
  s = sum(A)
  return s

# simple code-up sum
cpdef long cy_sum(long[:] A):
  cdef long i, n = A.shape[0], s = 0
  for i in range(n):
    s += A[i]
  return s


Below is the comparison of 3 sums:
In [44]: %timeit py_sum(a)
100 loops, best of 3: 2.7 ms per loop

In [45]: %timeit cy_sum(a)
100000 loops, best of 3: 5.08 µs per loop

In [46]: %timeit np.sum(a)
The slowest run took 27.32 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 7 µs per loop

Apparently, `py_sum` is way too slow, `cy_sum` and `np.sum` is about the same.
I wonder how Python's `sum` is handled in Cython, and why it's so slow?
Does Cython simply call the implementation of Python's `sum`?

Thanks


--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Daπid | 8 Apr 18:05 2016
Picon
Gravatar

Possible deadlock in prange

Hi,

The attached script hangs in a non deterministic way.

The single threaded version works fine, but the parallel usually runs
all the computations, and then refuses to exit. I can see the threads
on htop, sleeping and using no CPU. The gist of the program is this:
each thread performs some computation independently, and it is stored
in an independent position in a memoryview.

    for i in xrange(n): # or parallel.prange(n, num_threads=4)
        s = 0.
        for j in xrange(n_data):
            s += data[j, 0, 2]
        output[i] = s / n_data

Am I doing something wrong, or is it a bug?

Incidentally, the line where output[i] is written in the parallel
version is yellow, and the generated code shows some locking and GIL
going on, am I doing it the correct way?

This happens on Linux, GCC 5.3.1 and 4.9.3, Cython 0.23.5 and 0.24,
and Python 2.7.

Thank you,

/David.

--

-- 

--- 
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Attachment (_predict_parallel.pyx): text/x-python, 1539 bytes
Attachment (script.py): text/x-python-script, 457 bytes
Attachment (setup.py): text/x-python-script, 490 bytes
Joshua Wilson | 8 Apr 05:59 2016
Picon

Unexpected nan with double complex

Hello all,

Consider the following cython code:

def f(double complex z):
    print z
    print z/z

When I run it I get the following results:

>>> f(1e154)
(1e+154+0j)
(1+0j)
>>> f(1e155)
(1e+155+0j)
(nan+0j)

Now 1e155 is healthily within double precision range, so I would expect the second result to also be 1, and indeed the equivalent c code does give 1. Is this a bug?

This is with cython 0.23.4 and gcc 4.8.2.

- Josh


--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Shashi Da | 6 Apr 18:24 2016
Picon

How to let multiple Cython modules share a single global(static) C/C++ variable

For example. Assume there's a source file A.c which defines a static variable int a and two functions set_a
and get_a to set and get its value.
Then there are two Cython pyx files calls these functions. After compiling, both pyd files have a
independent variable a, and the values are not shared between modules.

My actual problem is similar but more complex. I have a static std::set pool defined in a cpp file. This pool
is used to save memory by storing same value only once,and refer them by pointers. To increase the speed,
accessing the pool is implemented by c++, and multiple Cython modules may access the poll directly or
indirectly via C++ code. Similar to the previous problem, after compiling, each module have a
independent pool, and this is obviously not what I want.

I think it can be solved if I can compile multiple pyx files into a single pyd. But I don't know how to do it. I
tried include multiple pyx into a single pyx, but there are some weird compiling problems related to cimport.

Compiling the CPP file into a single DLL and link it to pyd files dynamicly may help, but I have no idea how to
achieve this, too. Further more, this method may be harder to be cross-platform.

--

-- 

--- 
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users+unsubscribe <at> googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gmane