Matthew wrote to me about his project to generate code that can call BLAS/LAPACK routines.
We decided to CC the mailing list:
---------- Forwarded message ----------
Hi Matthew,
That sounds like an exciting project! See my comments below.
On Wed, Oct 24, 2012 at 12:29 AM, Matthew Rocklin <@
gmail.com> wrote:
> Hi Øyvind,
>
> I'm working on SymPy's matrix expressions module. I'd like to translate
> sympy matrix expressions like this
>
> X = MatrixSymbol('X', n, m)
> Y = MatrixSymbol('Y', n, m)
> expr = (X*X.T).I*Y
>
> with predicates likes this
>
> context = Q.invertible(X) & Q.positive_definite(Y)
>
> Into Fortran code that calls the relevant BLAS/LAPACK calls. This is a
> project of many parts (matrix expressions, logic programming, optimal
> selection of BLAS routines) and one of those parts is code generation.
>
> I see that you are the main contributor to the codegen module (see
> contribution counts below) and was wondering if I could ask you for your
> advice while I work on this.
Yes, you can :-)
>
> {"Vladimir Peric":3,"Ronan Lamy":1,"Joan Creus":1,"Siddhanathan
> Shanmugam":1,"Julio Idichekop Filho":1,"Aaron Meurer":1,"Oyvind
> Jensen":2,"Chris Smith":7,"Toon Verstraelen":6,"Øyvind Jensen":70}
>
> I've read through the source and docs once without going too deeply.
> I'm now wondering what I should read more deeply.
I would fire up a debugger and step through the code generetion for a
simple example. By reading and understanding all the code that is
executed, for say a matrix-vector product, you should get a good grasp
on it.
>
> I suspect I'll want to create Routines for each BLAS/LAPACK function, is
> this correct?
>
> Should I translate MatrixExprs to Indexed for better compatibility with
> codegen? How would I make codegen work natively with MatrixExprs?
I introduced Indexed exactly because I needed a Sympy object that
would map nicely into low level arrays with symbolic indexing. As far
as I know the codegen framework is not aware of MatrixExprs. But I am
not up to date on Sympy at the moment. It may be a good idea to see if
it is possible to merge Indexed with the other Matrix stuff.
Since you plan to call BLAS/LAPACK, you probably won't need to
generate loops(?). In that case, you don't actually need to use
Indexed.
>
> In one of your blogposts I noticed that you found Theano and made a
> conversion tool. The link on your post was broken. Does this still exist? I
> actively use both projects. Frederic (a core dev in Theano) and I actually
> made such a translation tool at the last SciPy conference. It appears we
> were just copying your work from years ago.
What I made was a simple routine that could map a Sympy tree into a
Theano-*like* tree, but I didn't actually populate the Theano-like
tree with Theano objects. So, your tool is probably way ahead of mine.
 I'll see if I can fix that link, though...
>
> I get the feeling I'm now thinking about the same problems that you thought
> about three years ago. If you are free to chat about this topic I think you
> could probably easily come up with solutions to many of my problems.
>
> Best,
> -Matthew
It would be cool if this can be implemented in such a way that a user
can provide custom rules for using external libraries. For instance,
if the codegen framework would accept user provided logic to determine
if a subexpression should be calculated with a given library call.
This kind of framework could also be used to enable codegen to
optimize common subexpressions.
If you don't mind, maybe we should CC the mailing list?
Cheers,
Øyvind