[GiNaC-list] Term ordering and compiling C++ code

Doug cape1232 at yahoo.com
Mon May 24 14:48:08 CEST 2010

kreckel wrote:

...and second, sharing is currently not pursued aggressively! Rather, it is exploited whenever it is trivial to do so. (The product rule of differentiation is an example where exactly the same terms pop up again and again so exploiting sharing comes at no cost.)

I think the benefit would still be large, even if coverage isn't 100%.  See below.

Well, I think that if the size of generated code is so prohibitively large and compiler CSE doesn't help you may be better off writing your own algorithm collecting similar
 terms in an associative array. You could then artificially introduce temporaries, in order to help the compiler. This would boil down to a more aggressive variant of GiNaC's subexpression sharing. What do you think?

This is exactly what I have done.  As mentioned, I will post that solution here in the next few days.  This works well, but is very slow, as it takes something that is sub-linear in the size of the expression tree and makes it O(n^2).  

If GiNaC expression tree nodes had a unique identifier, this approach could achieve sub-linear performance.  Specifically, it would be linear in the size of the unique subexpressions, not the size of the actual expression.  (If you have a lot of sharing, your expression can be large, but the number of unique expressions can be small.)

Even if GiNaC doesn't identify every single instance of a shared sub-expression, the benefits could still be
 large.  In fact, I tried an experiment to test this.  I treated ex::gethash() as a unique identifier, even though it isn't.  The code I generated was wrong, of course, but it was very fast to generate it, and very fast to compile and execute.  Thus, whatever number of unique expressions were not actually identified as such was small enough that this approach would still be quite useful.

Also, FYI, when using the hash value as a unique identifier, I only got a few collisions out of thousands of sub-expressions.  That is, I only found a few expressions with the same hash even though they were different expressions.  


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cebix.net/pipermail/ginac-list/attachments/20100524/6dfcd9f8/attachment.htm>

More information about the GiNaC-list mailing list