brian-team / brian2

Brian is a free, open source simulator for spiking neural networks.
http://briansimulator.org
Other
948 stars 222 forks source link

Better handle subexpressions in state updates #412

Open mstimberg opened 9 years ago

mstimberg commented 9 years ago

Currently, subexpressions are handled quite elaborately handled in statements (re-calculation is avoided) but in state updates, they are simply replaced so they don't appear in the generated code. This makes the code quite complicated and hard to read (especially for typical HH-like equations). It would be nicer if state updater would treat them in a smarter way, but it is a non-trivial problem. The best solution I could think of:

I think this can get quite complicated, but it should be doable. Not sure it is worth the effort, though.

thesamovar commented 9 years ago

Isn't the problem that the end result can be simplified in a way that the subexpressions don't appear any more? If so, maybe a more useful thing to do (but that won't help readability) would be to carry out common subexpression analysis on the final generated abstract code in a similar way to the LIO stuff?

mstimberg commented 9 years ago

We could try common subexpression analysis (at least in some simple way -- using sympy will take too long I think), but I'm a bit worried that we end up with a huge list of newly defined expressions that don't have much of a relation to the original equations (e.g. you don't find anything resembling alpha in it). On the other hand, maybe that's ok and we should think of the subexpressions as something that only helps with the readability of the equations but has nothing to do with the final code? Final question: does it actually buy us much, at least with -Ofast, gcc will most likely eliminate common subexpressions better than we do. Hmm, not sure whether it actually optimizes expressions including function calls (e.g. does it know that exp(x) will always return the same value?). Oh and couldn't our creation of temporary variables make the use of registers impossible in some cases?

Maybe we should do some clearer analysis of the potential benefits before starting such a fundamental change.

thesamovar commented 9 years ago

It's definitely not a high priority. Maybe a good student project at some point?

mstimberg commented 9 years ago

Maybe a good student project at some point?

Good idea, it might be nice for a computer science student since it doesn't need any background in computational neuroscience. I moved the milestone to a vague "2.0", but I don't think it's even essential for the 2.0 release.