FMPy icon indicating copy to clipboard operation
FMPy copied to clipboard

Performance improvement: limit number of calls to "supported_platforms(filename)"

Open bertrandhaut opened this issue 1 year ago • 3 comments

Dear,

We are using fmpy inside a larger application where we are calling numerous times the "simulate_fmu" function with the same FMU. More precisely, we are simulating a large period (e.g. 1 day) and each call to simulate_fmu is limited to 1 minute.

In the current implementation of FMPY, the function "supported_platforms(filename)" is called each time we call simulate_fmu.

It turns out that the total time spent in "supported_platforms" is around 26% of the total computation time (disk access is really slow)

Of course, in this particular situation, the call to the "supported_platforms" might have been done only once.

I was wondering if it was possible to skip the call to "supported_platforms" when it's not the first call for a given fmu.

Some possibilities might be:

  • cache the results of supported_platforms (e.g. @lru_cache)
  • skip if the user is using some set of arguments (e.g. passing a fmu_instance, initialize=False, remote_platform=None)

What do you think of such modifications?

bertrandhaut avatar Dec 12 '24 10:12 bertrandhaut

Have you tried to keep the unzip directory?

from fmpy import *

unzipdir = extract(filename)

simulate_fmu(filename=unzipdir)

There are more tricks to reduce the CPU time for repeated simulations (see https://github.com/CATIA-Systems/FMPy/blob/main/fmpy/examples/efficient_loops.py).

In the case of fmu_instance being passed to simulate_fmu() the check could actually be skipped.

Another option you could try as a workaround is to simply monkey patch supported_platforms():

import fmpy
from fmpy import simulate_fmu

filename = "Dahlquist.fmu"

platforms = fmpy.supported_platforms(filename)

def cached_supported_platforms(filename):
    return platforms

fmpy.supported_platforms = cached_supported_platforms

simulate_fmu(filename)

t-sommer avatar Dec 12 '24 12:12 t-sommer

Thanks for the feedback. We will try that.

bertrandhaut avatar Dec 12 '24 14:12 bertrandhaut

@t-sommer We have implemented your suggestion for the caching (we were already doing the "unzip" of the fmu).

It already helped up by improving 20% on our case.

bertrandhaut avatar Dec 19 '24 14:12 bertrandhaut