Ray Tracing Speed Improvement
Checklist
- [x] I have searched the existing issues and discussions for a similar question or feature request.
- [x] I have read the documentation and tried to find an answer there.
- [x] I am using the latest version of Optiland.
- [x] I have included all necessary context.
Feature Request
Is your feature request related to a problem? Please describe.
I have been using Optiland for the design of a beam shaping unit, to generate a given irradiance pattern. Since designs like these usually involve complicated aspheres and even freeform surface types, I noticed a significant slowdown in ray tracing speed, compared to the same system setup in Zemax non-seq mode. Therefore, I started investigating the issue a bit more, and noticed that the slowdowns were mainly coming from the NewtonRaphsonGeometry in its distance method.
Describe the solution you'd like Steps in order to solve this issue (open to discussion/improvements!):
- Profile the code using sample systems (to be designed) and compare the performance against Zemax's, using both backends (numpy and torch at the time). Create a clear overview comparing the performance.
- Once identified the key pain points, brainstorm on possible improvements: better initial guess for highly aspheric surfaces, clever "tricks" to improve the convergence (cross check strategies with those from papers, etc.), consider using gradients when the torch backend is being used, etc.
- Create a clear implementation strategy to improve those points discobered earlier.
- iterate until the performance is improved as we idealize it.
Describe alternatives you've considered N/A
Additional context As an additional profiling tool, we could think of using pyinstrument , as suggested by @HarrisonKramer .
Hi all,
To get started with this task, I have created a new branch feat/ray_tracing_performance.
There, I created a new directory, optiland/benchmark. Inside you will find two modules,
benchmarking_systems.py- contains several optical systems to be tested and traced, along with a method that usesoptic.tracefor the fields, and wavelengths present in each of the systems.performance_profiler.py- the main script to run the ray tracing for the proposed systems and profile the code, using pyinstrument. I tested it in my computer, was working as intended, but be sure to have pyinstrument installed. The output will be an hmtl file with the individual systems profiling results.
These are the main steps I would propose. Ofc open to suggestions to improve the workflow.
Next, I would suggest that different contributors, and other members of the community run the performance_profiler.py in their machines, and we collect all the data we will have. I expect that the slowest methods on my machine will be the slowest methods on all the other machines too, but just to be sure and to have variety, would be nice to check for different laptops/desktops - does it make sense?
EDIT: if it is too complicated of a setup for this task, we can always do it the usual way - we take the systems from benchmarking_systems.py and run just a jupyter notebook using our own profiler preferences.
After having a couple of analysis, we can investigate further, where and how to improve the performance.
Looking forward to hearing feedback from all.
Best, ~Manuel