Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
bertrandhaut opened this issue Dec 12, 2024 · 3 comments · May be fixed by #728
Open

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

bertrandhaut opened this issue Dec 12, 2024 · 3 comments · May be fixed by #728

Comments

@bertrandhaut
Copy link

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?

@t-sommer
Copy link
Contributor

t-sommer commented Dec 12, 2024

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)

@bertrandhaut
Copy link
Author

Thanks for the feedback. We will try that.

@bertrandhaut
Copy link
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants