atcollab / at

Accelerator Toolbox
Apache License 2.0
48 stars 31 forks source link

perf: improve by a factor ~ 10 the performance #688

Closed lnadolski closed 8 months ago

lnadolski commented 8 months ago

key: using atgetfieldvalues with indices

Atsummary was very slow when calling ElementRadiation.

The main reason is the call of the function atgetfieldvalues for the full ring. It is very slow if the full ring is used instead of the indices where the required field is present.

This a quick fix % SLOW when elements do not have the 'BendingAngle' % angle=atgetfieldvalues(ring,'BendingAngle'); % isdipole=isfinite(angle) & (angle~=0);

% Much faster with indices dipoleIdx=findcells(ring,'BendingAngle'); angle=atgetfieldvalues(ring, dipoleIdx, 'BendingAngle'); isdipole = atgetcells(ring,'BendingAngle'); isdipole(dipoleIdx) = isfinite(angle) & (angle~=0);

This could be maybe improved evermore. A a SOLEIL II lattice the gain to run atsummary is a factor 10 (1.5 s instead of 15 s).

The 2 main modifications are when calling atgetfieldvalues for dipoles and quadrupole.

The rest of the modifications are only style.

lfarv commented 8 months ago

@lnadolski I get a factor 4 speedup for my EBS lattice: still surprisingly good. Thanks for contributing!