Large improvement in time needed for particles creation - #848
Open
freddieknets wants to merge 5 commits into
Open
freddieknets wants to merge 5 commits into
freddieknets wants to merge 5 commits into
Conversation
…ude (particle generation was bottlenecked by this)
giadarol
reviewed
Sep 3, 2026
| # in calculations. | ||
| if kwargs[field].dtype != xotype._dtype: | ||
| kwargs[field] = kwargs[field].astype(xotype._dtype) | ||
| # PyOpenCL gets fussy if types don't match in calculations. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Particles generation can take a long time.
For instance, generating 100M particles on my machines (same time on a MacBook M3 and AMD Ryzen 7 9850X3D) takes a bit more than 30 seconds.
This PR addresses this in two steps, and in doing so, gains more than an order of magnitude in speed (from 30 to 2.2 seconds).
PDG optimisation
Numeric PDG-ID arrays were processed recursively in Python—one function call and several type checks per particle.
Ion lookup scanned and lowercased all 236 short/long element names. Initial mass-table construction triggered this hundreds of times.
The new approach:
Very strong speed gain: from 30 seconds down to 10 seconds.
Particles
__init__optimisationKey improvements:
particle_idallocationpdg_id, and random state; reused buffers, pyopencl, extra capacity, invalid particles, and signed -0.0 retain the original handling_update_energy_deviationsavoids unusedpzetacalculations and reuses the already-computedptauexpression forrvv, eliminating a second square root on the physical CPU path; unusual/negative-energy inputs use the original formula_update_zetano longer derivestauunless supplied and returns immediately for defaultsFurther speed gain: from 10 seconds down to 2.2 seconds.
All Xtrack tests pass on CPU; currently running CUDA tests overnight.
Checklist
Mandatory:
Optional: