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

Improve visualization performance through multi-threading #2604

Open
EwoutH opened this issue Jan 9, 2025 · 5 comments · May be fixed by #2656
Open

Improve visualization performance through multi-threading #2604

EwoutH opened this issue Jan 9, 2025 · 5 comments · May be fixed by #2656

Comments

@EwoutH
Copy link
Member

EwoutH commented Jan 9, 2025

Mesa currently runs model execution and visualization rendering on the same thread, creating performance bottlenecks when running computationally intensive models with multiple visualizations. This impacts responsiveness, especially in Jupyter notebooks. The UI can lag or freeze during model execution since visualization updates block the main thread.

We could improve performance by leveraging Solara's use_thread() functionality to run visualizations on separate threads from model execution. This would allow the model to continue executing while visualizations update asynchronously.

There could be (at least) three threads:

  1. Main thread for model execution
  2. Separate thread for Solara UI/event handling
  3. Worker thread pool for rendering visualizations (plots, space drawings, etc.)

Some key questions to resolve:

  • How to handle thread synchronization between model and vis? (and how much is need? One direction or bi?)
  • Should threading be configurable or always enabled?
  • Thread per component vs shared thread pool?

Next steps would be to prototype basic thread separation, benchmark improvements, and document the threading behavior.

See also:

CC @quaquel @Corvince

@HMNS19
Copy link
Contributor

HMNS19 commented Jan 18, 2025

This looks interesting! Can I start working on it?

@EwoutH
Copy link
Member Author

EwoutH commented Jan 18, 2025

Sure!

@HMNS19
Copy link
Contributor

HMNS19 commented Jan 31, 2025

Few models (like Wolf-Sheep) throw errors with unidirectional threading. I went ahead and added bidirectional sync for the simulator.

Should we make bidirectional sync mandatory for all models, or keep it configurable? Also, any ideas on a better way to handle this—maybe using asyncio.Queue instead of events?

@EwoutH
Copy link
Member Author

EwoutH commented Jan 31, 2025

Few models (like Wolf-Sheep) throw errors with unidirectional threading. I went ahead and added bidirectional sync for the simulator.

Could you figure out why some models throw these errors? Do they need bidirectional sync, and if so any reason why?

CC @quaquel

@HMNS19
Copy link
Contributor

HMNS19 commented Feb 1, 2025

I think the issue is happening because force_update calls the plot during the model stepping. This causes the plot to try to update while the simulation is still modifying the model data, leading to mismatched array lengths. Since the arrays aren't updated in sync, it triggers the error.

Image

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