Re: [Cython] aritmetic with arrays in Cython

2014-08-08 Thread Matěj Laitl
On Friday 08 of August 2014 07:05:08 Stefan Behnel wrote:
> Ian Henriksen schrieb am 08.08.2014 um 00:58:
> > On Thu, Aug 7, 2014 at 1:58 AM, Matěj Laitl wrote:
> >> you may also check out https://github.com/strohel/Ceygen if it would suit
> >> your needs.
> 
> Interesting project. Thanks for sharing.
> 
> > Ideally, I'm still trying to find a way to be able to do
> > something syntactically like this:
> > 
> > cpdef whateverfunction(double[:] a, float[:] b):
> > return a + a * b + .5
> > 
> > It would be ideal if that sort of thing were already a part of Cython. All
> > things considered though, most of what I'm looking for seems to be there.
> 
> Eigen can apparently do these things already:
> 
> http://eigen.tuxfamily.org/dox/TopicLazyEvaluation.html
> 
> It might not be all that much work to integrate this with Cython to enable
> syntax support. Or have some kind of preprocessor, but I guess that's not
> going to be simpler than have it in Cython as an optional AST transform.
> Basically, whenever an arithmetic expression consists of a mixture of
> memory views and (optionally) scalars, it could be mapped to the kind of
> code that Ceygen uses, just at an arbitrarily complex expression length.

Exactly! That was one of my ideas when I decided to create Ceygen, but I 
resorted to take the easier route and made uglier static API because I didn't 
(and still don't) have enough knowledge of Cython internals and Mark's "array 
expressions" still looked like they are going to be merged.

What you suggest above would be definitely possible IMO, and it could easily 
cherry-pick some work done on Ceygen, for example how memoryviews are wrapped 
as Eigen arrays: 
https://github.com/strohel/Ceygen/blob/master/ceygen/eigen_cpp.h

Unfortunately, I no longer work on the project (my master's thesis) that 
triggered work on Ceygen, and now I can invest only little maintenance work to 
it.

*However*, if such project is found worth to tacking by Cython devs, I can 
imagine in my wild dreams that there may be a student at my former university 
interested in doing it as their project. (this is rather optimistic statement, 
please don't get too excited yet)

Regards,
Matěj
___
cython-devel mailing list
cython-devel@python.org
https://mail.python.org/mailman/listinfo/cython-devel


Re: [Cython] aritmetic with arrays in Cython

2014-08-08 Thread Stefan Behnel
Matěj Laitl schrieb am 08.08.2014 um 11:07:
> On Friday 08 of August 2014 07:05:08 Stefan Behnel wrote:
>> Eigen can apparently do these things already:
>>
>> http://eigen.tuxfamily.org/dox/TopicLazyEvaluation.html
>>
>> It might not be all that much work to integrate this with Cython to enable
>> syntax support. Or have some kind of preprocessor, but I guess that's not
>> going to be simpler than have it in Cython as an optional AST transform.
>> Basically, whenever an arithmetic expression consists of a mixture of
>> memory views and (optionally) scalars, it could be mapped to the kind of
>> code that Ceygen uses, just at an arbitrarily complex expression length.
> 
> Exactly! That was one of my ideas when I decided to create Ceygen, but I 
> resorted to take the easier route and made uglier static API because I didn't 
> (and still don't) have enough knowledge of Cython internals and Mark's "array 
> expressions" still looked like they are going to be merged.

There are a couple of points in favour of Eigen/Ceygen. It's a way simpler
approach than moving the entire vectorised code generator into Cython. And
it lets Eigen do the hard work, leaving only the interfacing part to
Cython. Basically, both tools can do what they're good at. We're always
happy that there's a C compiler cleaning up after us to which we can
delegate the low level optimisation and tuning stuff. The same would apply
to Eigen. It's a much more cythonic approach than what's implemented in the
pie-in-the-sky array expressions branch.

It requires a C++ compiler, but I think that's an acceptable dependency
these days in places where array computation performance matters. People
even accept a dependency on huge tool stacks like Numba/LLVM for the same
kind of tasks. An array expression syntax is just so much simpler than
writing dumb nested loops.


> Unfortunately, I no longer work on the project (my master's thesis) that 
> triggered work on Ceygen, and now I can invest only little maintenance work 
> to 
> it.

I don't see me being the one to implement this, either. :)


> *However*, if such project is found worth to tacking by Cython devs, I can 
> imagine in my wild dreams that there may be a student at my former university 
> interested in doing it as their project. (this is rather optimistic 
> statement, 
> please don't get too excited yet)

I'm sure there are many people who would benefit from this. Some of them
might even be able to provide funding, if someone wants to pick up this
idea. But past experience shows that we shouldn't count on this either.
It's definitely a nice project to tackle for someone who likes coding.

Stefan

___
cython-devel mailing list
cython-devel@python.org
https://mail.python.org/mailman/listinfo/cython-devel


Re: [Cython] aritmetic with arrays in Cython

2014-08-08 Thread Ian Henriksen
I'd like to work on it. That was why I asked. It's been bothering me for
some time that we don't have this feature yet. This will take me some time
though since I'm not at all familiar with Cython's internals. I've used it
in relatively small settings for speeding up array operations and wrapping
external functions, but I've never had occasion to dig through much of what
it does internally. Some help navigating Cython's internals would be
greatly appreciated.

I'll also have to take some time to become more familiar with eigen itself.
Most of my work has used numpy arrays.

Eigen seems to be ideal for this since it allows us to offload the
expression analysis to another package where it is already
well-implemented. It looks like they have an array class for n-dimensional
array operations as well. My guess is that that would allow a fairly
natural translation from memory views to eigen arrays.

For now, I'll work on learning some of the details of how to use eigen.
Where would be a good place to start from there?

Would it be more helpful for me to start working on Cython bindings for
eigen, or to work on adding this directly as a part of Cython? Where should
I look in Cython's source code to do that? What kinds of modifications
would be necessary?

Thanks!

-Ian


On Fri, Aug 8, 2014 at 4:28 AM, Stefan Behnel  wrote:

> Matěj Laitl schrieb am 08.08.2014 um 11:07:
> > On Friday 08 of August 2014 07:05:08 Stefan Behnel wrote:
> >> Eigen can apparently do these things already:
> >>
> >> http://eigen.tuxfamily.org/dox/TopicLazyEvaluation.html
> >>
> >> It might not be all that much work to integrate this with Cython to
> enable
> >> syntax support. Or have some kind of preprocessor, but I guess that's
> not
> >> going to be simpler than have it in Cython as an optional AST transform.
> >> Basically, whenever an arithmetic expression consists of a mixture of
> >> memory views and (optionally) scalars, it could be mapped to the kind of
> >> code that Ceygen uses, just at an arbitrarily complex expression length.
> >
> > Exactly! That was one of my ideas when I decided to create Ceygen, but I
> > resorted to take the easier route and made uglier static API because I
> didn't
> > (and still don't) have enough knowledge of Cython internals and Mark's
> "array
> > expressions" still looked like they are going to be merged.
>
> There are a couple of points in favour of Eigen/Ceygen. It's a way simpler
> approach than moving the entire vectorised code generator into Cython. And
> it lets Eigen do the hard work, leaving only the interfacing part to
> Cython. Basically, both tools can do what they're good at. We're always
> happy that there's a C compiler cleaning up after us to which we can
> delegate the low level optimisation and tuning stuff. The same would apply
> to Eigen. It's a much more cythonic approach than what's implemented in the
> pie-in-the-sky array expressions branch.
>
> It requires a C++ compiler, but I think that's an acceptable dependency
> these days in places where array computation performance matters. People
> even accept a dependency on huge tool stacks like Numba/LLVM for the same
> kind of tasks. An array expression syntax is just so much simpler than
> writing dumb nested loops.
>
>
> > Unfortunately, I no longer work on the project (my master's thesis) that
> > triggered work on Ceygen, and now I can invest only little maintenance
> work to
> > it.
>
> I don't see me being the one to implement this, either. :)
>
>
> > *However*, if such project is found worth to tacking by Cython devs, I
> can
> > imagine in my wild dreams that there may be a student at my former
> university
> > interested in doing it as their project. (this is rather optimistic
> statement,
> > please don't get too excited yet)
>
> I'm sure there are many people who would benefit from this. Some of them
> might even be able to provide funding, if someone wants to pick up this
> idea. But past experience shows that we shouldn't count on this either.
> It's definitely a nice project to tackle for someone who likes coding.
>
> Stefan
>
> ___
> cython-devel mailing list
> cython-devel@python.org
> https://mail.python.org/mailman/listinfo/cython-devel
>
___
cython-devel mailing list
cython-devel@python.org
https://mail.python.org/mailman/listinfo/cython-devel


Re: [Cython] aritmetic with arrays in Cython

2014-08-08 Thread Stefan Behnel
Hi,

please don't top-post.

Ian Henriksen schrieb am 08.08.2014 um 18:47:
> I'd like to work on it. That was why I asked. It's been bothering me for
> some time that we don't have this feature yet. This will take me some time
> though since I'm not at all familiar with Cython's internals. I've used it
> in relatively small settings for speeding up array operations and wrapping
> external functions, but I've never had occasion to dig through much of what
> it does internally. Some help navigating Cython's internals would be
> greatly appreciated.

Here's a little intro.

https://github.com/cython/cython/wiki/HackerGuide#getting-started

I recommend to start by writing a simple test that does some integer
arithmetic and enable code generation traces for AST nodes in the generated
C code. "cython -a" should make this quite navigable.


> I'll also have to take some time to become more familiar with eigen itself.
> Most of my work has used numpy arrays.
> 
> Eigen seems to be ideal for this since it allows us to offload the
> expression analysis to another package where it is already
> well-implemented. It looks like they have an array class for n-dimensional
> array operations as well. My guess is that that would allow a fairly
> natural translation from memory views to eigen arrays.

You should dig into the code that Ceygen uses for the mapping. That's most
likely the best start you can get.


> Would it be more helpful for me to start working on Cython bindings for
> eigen, or to work on adding this directly as a part of Cython? Where should
> I look in Cython's source code to do that? What kinds of modifications
> would be necessary?

My guess is that this is best implemented with a set of matrix specific AST
nodes for the arithmetic operators. There is a recursive tree processing
phase in Cython's pipeline called "AnalyseExpressionsTransform" or type
analysis, where it figures out what variables reference memory views, what
the result type of an arithmetic expression is, etc. This phase can easily
replace nodes by returning new nodes from the analyse_types() methods of
AST nodes. Currently, arithmetic with memory views is not allowed, so this
needs to be enabled in the corresponding NumBinopNode AST subclasses
(AddNode, MatMulNode, etc. in ExprNodes.py). Write a test and debug it to
see where it fails.

I'm not sure yet how the code generation would look like. In simple cases,
you may be able to implement this into the existing AST arithmetic operator
nodes. But my guess is that you want to have a new set of nodes (probably
in a new module in Cython/Compiler/, e.g. EigenNodes.py) that the
analyse_types() methods of the generic arithmetic AST nodes return instead
of themselves when they determine that they are operating on memory views.
Those would then know how to generate matrix specific arithmetic C++/Eigen
code, potentially even inherit from the general arithmetic nodes to
override only the code generation method. There is a
NewNodeClass.from_node() class method to create a new node from an existing
one.

These specialised AST nodes will be optional in the end, Eigen should only
be needed if in C++ mode and this feature is enabled and used, otherwise,
array arithmetic will continue to be a compile time error.

Try to get something simple like binary matrix multiplication working for
now, or even just matrix x scalar. Map them to Ceygen's API to get a quick
start. Once that's done, try building up the code generation for more
complex nested expressions.

Stefan

___
cython-devel mailing list
cython-devel@python.org
https://mail.python.org/mailman/listinfo/cython-devel


Re: [Cython] aritmetic with arrays in Cython

2014-08-08 Thread Julian Taylor
On 08.08.2014 19:36, Stefan Behnel wrote:
> Hi,
> 
> please don't top-post.
> 
> Ian Henriksen schrieb am 08.08.2014 um 18:47:
>> I'd like to work on it. That was why I asked. It's been bothering me for
>> some time that we don't have this feature yet. This will take me some time
>> though since I'm not at all familiar with Cython's internals. I've used it
>> in relatively small settings for speeding up array operations and wrapping
>> external functions, but I've never had occasion to dig through much of what
>> it does internally. Some help navigating Cython's internals would be
>> greatly appreciated.
> 
> Here's a little intro.
> 
> https://github.com/cython/cython/wiki/HackerGuide#getting-started
> 
> I recommend to start by writing a simple test that does some integer
> arithmetic and enable code generation traces for AST nodes in the generated
> C code. "cython -a" should make this quite navigable.
> 
> 
>> I'll also have to take some time to become more familiar with eigen itself.
>> Most of my work has used numpy arrays.
>>
>> Eigen seems to be ideal for this since it allows us to offload the
>> expression analysis to another package where it is already
>> well-implemented. It looks like they have an array class for n-dimensional
>> array operations as well. My guess is that that would allow a fairly
>> natural translation from memory views to eigen arrays.
> 
> You should dig into the code that Ceygen uses for the mapping. That's most
> likely the best start you can get.
> 
> 
>> Would it be more helpful for me to start working on Cython bindings for
>> eigen, or to work on adding this directly as a part of Cython? Where should
>> I look in Cython's source code to do that? What kinds of modifications
>> would be necessary?
> 
> My guess is that this is best implemented with a set of matrix specific AST
> nodes for the arithmetic operators. There is a recursive tree processing
> phase in Cython's pipeline called "AnalyseExpressionsTransform" or type
> analysis, where it figures out what variables reference memory views, what
> the result type of an arithmetic expression is, etc. This phase can easily
> replace nodes by returning new nodes from the analyse_types() methods of
> AST nodes. Currently, arithmetic with memory views is not allowed, so this
> needs to be enabled in the corresponding NumBinopNode AST subclasses
> (AddNode, MatMulNode, etc. in ExprNodes.py). Write a test and debug it to
> see where it fails.

If someone wants to work on an ast transformer for array expressions I
would not restrict its output to something ceygen can parse but also numpy.
E.g. transforming:

cpdef whateverfunction(double[:] a, float[:] b):
return a + a * b + .5

to

cpdef whateverfunction(double[:] a, float[:] b):
bs = 1
r = empty_like(a)
for i in range(0, a.size, bs):
u = min(i + bs, a.size)
r[i:u] = a[i:u] * b[i:u]
r[i:u] += a[i:u]
r[i:u] += 0.5
return r

this is already very efficient in numpy.
___
cython-devel mailing list
cython-devel@python.org
https://mail.python.org/mailman/listinfo/cython-devel


Re: [Cython] aritmetic with arrays in Cython

2014-08-08 Thread Ian Henriksen
On Fri, Aug 8, 2014 at 12:02 PM, Julian Taylor <
jtaylor.deb...@googlemail.com> wrote:

> On 08.08.2014 19:36, Stefan Behnel wrote:
> > Hi,
> >
> > please don't top-post.
> >
> > Ian Henriksen schrieb am 08.08.2014 um 18:47:
> >> I'd like to work on it. That was why I asked. It's been bothering me for
> >> some time that we don't have this feature yet. This will take me some
> time
> >> though since I'm not at all familiar with Cython's internals. I've used
> it
> >> in relatively small settings for speeding up array operations and
> wrapping
> >> external functions, but I've never had occasion to dig through much of
> what
> >> it does internally. Some help navigating Cython's internals would be
> >> greatly appreciated.
> >
> > Here's a little intro.
> >
> > https://github.com/cython/cython/wiki/HackerGuide#getting-started
> >
> > I recommend to start by writing a simple test that does some integer
> > arithmetic and enable code generation traces for AST nodes in the
> generated
> > C code. "cython -a" should make this quite navigable.
> >
> >
> >> I'll also have to take some time to become more familiar with eigen
> itself.
> >> Most of my work has used numpy arrays.
> >>
> >> Eigen seems to be ideal for this since it allows us to offload the
> >> expression analysis to another package where it is already
> >> well-implemented. It looks like they have an array class for
> n-dimensional
> >> array operations as well. My guess is that that would allow a fairly
> >> natural translation from memory views to eigen arrays.
> >
> > You should dig into the code that Ceygen uses for the mapping. That's
> most
> > likely the best start you can get.
> >
> >
> >> Would it be more helpful for me to start working on Cython bindings for
> >> eigen, or to work on adding this directly as a part of Cython? Where
> should
> >> I look in Cython's source code to do that? What kinds of modifications
> >> would be necessary?
> >
> > My guess is that this is best implemented with a set of matrix specific
> AST
> > nodes for the arithmetic operators. There is a recursive tree processing
> > phase in Cython's pipeline called "AnalyseExpressionsTransform" or type
> > analysis, where it figures out what variables reference memory views,
> what
> > the result type of an arithmetic expression is, etc. This phase can
> easily
> > replace nodes by returning new nodes from the analyse_types() methods of
> > AST nodes. Currently, arithmetic with memory views is not allowed, so
> this
> > needs to be enabled in the corresponding NumBinopNode AST subclasses
> > (AddNode, MatMulNode, etc. in ExprNodes.py). Write a test and debug it to
> > see where it fails.
>
> If someone wants to work on an ast transformer for array expressions I
> would not restrict its output to something ceygen can parse but also numpy.
> E.g. transforming:
>
> cpdef whateverfunction(double[:] a, float[:] b):
> return a + a * b + .5
>
> to
>
> cpdef whateverfunction(double[:] a, float[:] b):
> bs = 1
> r = empty_like(a)
> for i in range(0, a.size, bs):
> u = min(i + bs, a.size)
> r[i:u] = a[i:u] * b[i:u]
> r[i:u] += a[i:u]
> r[i:u] += 0.5
> return r
>
> this is already very efficient in numpy.
>
>
Sorry about the top post.

Stefan, thanks for the direction. I think I know how to proceed now. It'll
probably take me some time to learn how to do all this, but I'll start
reading/working on it now.

Julian, from a dependency standpoint I'd really prefer to use numpy. As
near as I can tell, the people most likely to use this feature already use
numpy. That would also remove the C++ dependency for this. I think the main
reason for using eigen is that it will be easy to interface with and will
eliminate temporaries automatically. In the long term, it could be good to
offer multiple backends for these sorts of operations. Numpy would be a
primary candidate for something like that. I'll spend some time working
with numpy's ufunc api to see if I can figure out a good way to use that
instead.
Thanks!
-Ian
___
cython-devel mailing list
cython-devel@python.org
https://mail.python.org/mailman/listinfo/cython-devel


Re: [Cython] aritmetic with arrays in Cython

2014-08-08 Thread Ian Henriksen
On Fri, Aug 8, 2014 at 3:09 PM, Ian Henriksen <
insertinterestingnameh...@gmail.com> wrote:

> On Fri, Aug 8, 2014 at 12:02 PM, Julian Taylor <
> jtaylor.deb...@googlemail.com> wrote:
>
>> On 08.08.2014 19:36, Stefan Behnel wrote:
>> > Hi,
>> >
>> > please don't top-post.
>> >
>> > Ian Henriksen schrieb am 08.08.2014 um 18:47:
>> >> I'd like to work on it. That was why I asked. It's been bothering me
>> for
>> >> some time that we don't have this feature yet. This will take me some
>> time
>> >> though since I'm not at all familiar with Cython's internals. I've
>> used it
>> >> in relatively small settings for speeding up array operations and
>> wrapping
>> >> external functions, but I've never had occasion to dig through much of
>> what
>> >> it does internally. Some help navigating Cython's internals would be
>> >> greatly appreciated.
>> >
>> > Here's a little intro.
>> >
>> > https://github.com/cython/cython/wiki/HackerGuide#getting-started
>> >
>> > I recommend to start by writing a simple test that does some integer
>> > arithmetic and enable code generation traces for AST nodes in the
>> generated
>> > C code. "cython -a" should make this quite navigable.
>> >
>> >
>> >> I'll also have to take some time to become more familiar with eigen
>> itself.
>> >> Most of my work has used numpy arrays.
>> >>
>> >> Eigen seems to be ideal for this since it allows us to offload the
>> >> expression analysis to another package where it is already
>> >> well-implemented. It looks like they have an array class for
>> n-dimensional
>> >> array operations as well. My guess is that that would allow a fairly
>> >> natural translation from memory views to eigen arrays.
>> >
>> > You should dig into the code that Ceygen uses for the mapping. That's
>> most
>> > likely the best start you can get.
>> >
>> >
>> >> Would it be more helpful for me to start working on Cython bindings for
>> >> eigen, or to work on adding this directly as a part of Cython? Where
>> should
>> >> I look in Cython's source code to do that? What kinds of modifications
>> >> would be necessary?
>> >
>> > My guess is that this is best implemented with a set of matrix specific
>> AST
>> > nodes for the arithmetic operators. There is a recursive tree processing
>> > phase in Cython's pipeline called "AnalyseExpressionsTransform" or type
>> > analysis, where it figures out what variables reference memory views,
>> what
>> > the result type of an arithmetic expression is, etc. This phase can
>> easily
>> > replace nodes by returning new nodes from the analyse_types() methods of
>> > AST nodes. Currently, arithmetic with memory views is not allowed, so
>> this
>> > needs to be enabled in the corresponding NumBinopNode AST subclasses
>> > (AddNode, MatMulNode, etc. in ExprNodes.py). Write a test and debug it
>> to
>> > see where it fails.
>>
>> If someone wants to work on an ast transformer for array expressions I
>> would not restrict its output to something ceygen can parse but also
>> numpy.
>> E.g. transforming:
>>
>> cpdef whateverfunction(double[:] a, float[:] b):
>> return a + a * b + .5
>>
>> to
>>
>> cpdef whateverfunction(double[:] a, float[:] b):
>> bs = 1
>> r = empty_like(a)
>> for i in range(0, a.size, bs):
>> u = min(i + bs, a.size)
>> r[i:u] = a[i:u] * b[i:u]
>> r[i:u] += a[i:u]
>> r[i:u] += 0.5
>> return r
>>
>> this is already very efficient in numpy.
>>
>>
> Sorry about the top post.
>
> Stefan, thanks for the direction. I think I know how to proceed now. It'll
> probably take me some time to learn how to do all this, but I'll start
> reading/working on it now.
>
> Julian, from a dependency standpoint I'd really prefer to use numpy. As
> near as I can tell, the people most likely to use this feature already use
> numpy. That would also remove the C++ dependency for this. I think the main
> reason for using eigen is that it will be easy to interface with and will
> eliminate temporaries automatically. In the long term, it could be good to
> offer multiple backends for these sorts of operations. Numpy would be a
> primary candidate for something like that. I'll spend some time working
> with numpy's ufunc api to see if I can figure out a good way to use that
> instead.
> Thanks!
> -Ian
>

Maybe I should clarify a little about why eigen is a good place to start.
According to http://eigen.tuxfamily.org/dox/TopicLazyEvaluation.html it
already takes care of things like the elimination of temporary variables
and common subexpression reduction at compile time. This should make it
possible to compile array expressions in Cython without having to
re-implement those sorts of optimizations. Ideally we would just have to
map memory view operations to corresponding equivalents from eigen. It's
not yet clear to me how to do things with arbitrary-dimensional arrays or
broadcasting, but, given some more time, a solution may present itself.
-Ian
___
cython

[Cython] Bug: operator[] does not respect except +

2014-08-08 Thread J Robert Ray
I apologize for not having a working demonstration, but I have found this
bug and verified it is still an issue in Cython 0.20.2. No try/catch block
is generated for operator[]. In contrast, a try/catch block does get
generated for operator().

If it is any use, here is a paraphrasing of the pyx code and the generated
cpp code.

cdef extern from "blah.h" namespace "blah":
cdef cppclass Blah:
int operator[](int) except +

...

cdef class PyBlah:
...
def demo(self, int index):
return deref(self.thisptr)[index]

...

/* "Blah.pyx":107
 * def demo(self, int index):
 * return deref(self.thisptr)[index] # <<
 */
  __Pyx_XDECREF(__pyx_r);
  __pyx_t_1 =
__Pyx_PyInt_From_int(((*__pyx_v_self->thisptr)[__pyx_v_index])); if
(unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107;
__pyx_clineno = __LINE__; goto __pyx_L1_error;}
  __Pyx_GOTREF(__pyx_t_1);
  __pyx_r = __pyx_t_1;
  __pyx_t_1 = 0;
  goto __pyx_L0;


Please let me know if I'm doing anything wrong. Thanks!
___
cython-devel mailing list
cython-devel@python.org
https://mail.python.org/mailman/listinfo/cython-devel