Closed MichaelLLi closed 2 years ago
This is indeed strange and something worth investigating. However, I cannot see a clear cause just by looking at your code and mine, in particular since we are talking about smaller time scales than I am used to optimising for typical applications of JiTCDDE. I am afraid that I can only help you if you provide a running example that I can play with or profile. Alternatively, you can profile your code yourself to see in which line of JiTCDDE the time is predominantly spent.
A very wild guess is that unpacking the parameters helps, i.e. use DDE.set_parameters(*params)
instead of DDE.set_parameters(params)
.
Soooo, I transformed your code to something that runs and could reproduce your issue. I then profiled your code to find the reason, namely:
Setting parameters itself is not the problem, but when you do it for the first time, some automatic initialisations are triggered. These would have to happen anyway at some point, so you cannot avoid them. You can verify this by setting parameters again, which is then very fast. The automatic initialisation is comparably slow in your case only because the input spline and the regular past of your equation need to be joined into one spline under the hood. This in turn is slow because it all happens in CHSPy, which is not made for efficiency since I did not expect it to be a relevant bottleneck.
Speeding this up without touching your existing code would require to implement a compiled backend for CHSPy, which is no work I see myself or anybody else doing soon. However, I see the following options to possibly speed up things:
Emulate what jitcdde_input
does by adding a dummy component whose past is the input. See this for an an example on how this is done. This way, you can define a nice past once yourself and avoid that it has to be created by joining components every time.
Use a callback (see callback_functions
in the documentation) instead of jitcdde_input
to implement your input.
Which of these is most efficient or easiest to implement depends on the exact nature of your input, how often it changes, etc.
I made a change which avoid that the joining in question is unnecessarily repeated and thus halves time required for the initialisation in question. You need to use the Github version to use, which you can install with something along the lines of:
pip3 install git+git://github.com/neurophysik/jitcdde
Hi:
I have a question relating to whether or not there is a way to speed up the function of setting parameters. I have followed the tutorial and the example code within #30 to setup the DDE for outer parameter optimization. The hopefully relevant part of the model code is as followed:
Basically, I designed a wrapper function
residuals_totalcases
to wrap the dde modelmodel_long_covid
for downstream optimization purposes (in which I would not bore you here). My question/problem is that as you can see with my liberal use of the tictoc package, I have been trying to figure out what is taking most of the time as the DDE formulation of these equations scale roughly 10x slower than the equivalent set of equations in which there is no delay term (i.e. setting the constant delay to 0). And my timing experiments show the following (timing of each step in seconds):As you can see, apparently the slowest step is the step that sets the parameters which take by far the majority of the time. That is somewhat perplexing to me as I would think setting parameters should not take up most of the time of the integration, so I am wondering if I am doing anything wrong and if there is any way to speed the integration up? Thanks a lot!