Skip to main content
Solved

Python Multithreading with POP

  • September 21, 2023
  • 1 reply
  • 192 views

Alex.Tripsas

I’m working on running multiple POP simulations and they each take about 30 minutes.  I’m debating trying out the python package “threading” to try and run multiple runs at the same time in order to save some computing time.  Are there going to be any limitations on the Zemax side of this equation?

Best answer by chaasjes

Hi,

I tried something similar and judging from my experience, this is not impossible. It's not easy, either, and I doubt if using multiple threads will improve run times, since POP already seems to use multiple threads. Quoting from the documentation:

The physical optics propagation routines in OpticStudio are designed to work on multiple CPU computers

 

That being said, there are some limitations on both the OpticStudio side and the Python side:

  1. You can't connect to multiple OpticStudio instances from a single Python script. More information about this here: So if you want to perform the analyses in multiple OpticStudio instances, you will need to use multiprocessing instead of threading.
  2. Instead of using multiple OpticStudio instances, you can create multiple optical systems in a single OpticStudio instance, which is probably the easiest way to achieve what you want. I recommend to do this in a standalone application. If you want to easily switch between extension mode (for development / debugging) and standalone mode (for the real simulations), you can try ZOSPy, which lets you switch between these modes by changing only a single line of code.
  3. Each thread or process needs to have a separate optical system or OpticStudio instance attached to it. Using multiprocessing or threading, this is hard (but maybe not impossible); when I tried this, I soon ran into problems. I ended up using Ray instead, which has support for stateful workers (which can be either threads or processes). This allows you to attach an optical system to a thread, and run multiple jobs sequentially in the same optical system (so you don't have to reinitialize the connection, although you should of course take care of removing the old system before you start a new simulation).

Hopefully this helps and I'm curious to see if you succeeded in getting parallel computations in OpticStudio working!

View original
Did this topic help you find an answer to your question?

1 reply

chaasjes
Forum|alt.badge.img
  • Visible
  • 33 replies
  • Answer
  • September 27, 2023

Hi,

I tried something similar and judging from my experience, this is not impossible. It's not easy, either, and I doubt if using multiple threads will improve run times, since POP already seems to use multiple threads. Quoting from the documentation:

The physical optics propagation routines in OpticStudio are designed to work on multiple CPU computers

 

That being said, there are some limitations on both the OpticStudio side and the Python side:

  1. You can't connect to multiple OpticStudio instances from a single Python script. More information about this here: So if you want to perform the analyses in multiple OpticStudio instances, you will need to use multiprocessing instead of threading.
  2. Instead of using multiple OpticStudio instances, you can create multiple optical systems in a single OpticStudio instance, which is probably the easiest way to achieve what you want. I recommend to do this in a standalone application. If you want to easily switch between extension mode (for development / debugging) and standalone mode (for the real simulations), you can try ZOSPy, which lets you switch between these modes by changing only a single line of code.
  3. Each thread or process needs to have a separate optical system or OpticStudio instance attached to it. Using multiprocessing or threading, this is hard (but maybe not impossible); when I tried this, I soon ran into problems. I ended up using Ray instead, which has support for stateful workers (which can be either threads or processes). This allows you to attach an optical system to a thread, and run multiple jobs sequentially in the same optical system (so you don't have to reinitialize the connection, although you should of course take care of removing the old system before you start a new simulation).

Hopefully this helps and I'm curious to see if you succeeded in getting parallel computations in OpticStudio working!


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings