Hi everyone,
I am writing a simple ode solver/time stepper in numba. The right hand side functions we will eventually have to deal with will be quite involved in terms of logic, delays, stochastic inputs etc, so nothing that will be doable in pure numpy. On the other hand that will anyway force us to use a fixed time step solver. My hope was that we could keep things reasonably fast by compiling the right hand side as well as the solver using numba. A first test of that strategy runs into some weird performance characteristics though:
L1 norm of the differences of the two right hand side functions on 100 random drawn vectors: 0.0
1000 x right_hand_side_numba: 0.0154027938843
1000 x right_hand_side_numpy: 0.0550789833069
Time taken to compile ode system: 0.469746112823
Time taken to integrate numpy rhs using odeint: 0.125674962997
Time taken to integrate numba rhs using odeint:
1.17314887047Time taken to integrate using ode_compiled: 0.166043996811
So the numba compiled rhs is faster than the numpy one, good. The two functions also produce exactly the same numerical output.
But integrating the slower numpy function with odeint is faster than integrating the compiled function, by a factor of 10!
My completely trivial time stepper almost gets me back to the speed of odeint/numpy (mostly a bit slower, occasionally a sliver faster).
But I am severely puzzled how odeint can beat the completely trivial solver (which is surely much less accurate).
Does anyone have any insights into why this could possibly happen?
I have attached the script that produces the above numbers. The two important functions in the script are:
rhs_gen:
produces the two right hand side functions we are testing. They are at the minimum level of complexity I am interested in real world (and about the maximum achievable while staying purely within numpy).
ode_system_compiler:
takes a compiled function, defines an ode integrator around that function, compiles that and passes it back.
Eliminating that closure does not seem to change the performance characteristics.
I would be most grateful for any insights here. I never studied CS, so I'm "learning as I go", so pointers to literature I should/need to read would also be much appreciated.
Best,
Frank Hellmann