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

[WIP] Make Linear Elements Fast #850

Open
wants to merge 13 commits into
base: development
Choose a base branch
from

Conversation

ax3l
Copy link
Member

@ax3l ax3l commented Feb 12, 2025

Make linear elements, like Quad & Drift, as fast as possible by:

  • calculating constants that depend on the element config and reference particle properties before pushing all particles,
  • instead of using a Matrix-Vector calculation (as in LinearElement), we rely on the fact that those matrices are sparse and keep the multiply-adds explicit.

Example: 1 CPU core run of a single Quad, 100M particles
Before:

Execution time ImpactX: 5.3057 seconds (0.05306us / particle)

Now:

Execution time ImpactX: 0.8723 seconds (0.00872us / particle)

Close #849

  • decide on general structure
  • go through all 40+ elements...

@@ -96,24 +118,14 @@ namespace impactx::elements
amrex::ParticleReal & AMREX_RESTRICT py,
amrex::ParticleReal & AMREX_RESTRICT pt,
uint64_t & AMREX_RESTRICT idcpu,
RefPart const & refpart
[[maybe_unused]] RefPart const & refpart
Copy link
Member Author

@ax3l ax3l Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idea: I would remove this fully from the interface to prevent that we can write inefficient code by accident.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed: this will not work to be fully removed, since the refpart for elements that integrate through maps is non-const.

@ax3l ax3l force-pushed the performance-quad-constants branch from 3c74ef7 to 9ed66b3 Compare February 12, 2025 19:33
@ax3l ax3l changed the title [Draft] Make Quad Fast [Draft] Make Quad & Drift Fast Feb 12, 2025
Compute and cache the constants for the push. In particular,
used to pre-compute and cache variables that are independent
of the individually tracked particle.
@ax3l ax3l force-pushed the performance-quad-constants branch from 9ed66b3 to e788211 Compare February 14, 2025 22:33
@ax3l ax3l force-pushed the performance-quad-constants branch 3 times, most recently from 6c6f5ef to 6af0a54 Compare February 14, 2025 22:36
ax3l added 4 commits February 14, 2025 15:21
Make the `Quad` fast by calculating constants that depend
on the element config and reference particle properties
before pushing all particles.
@ax3l ax3l force-pushed the performance-quad-constants branch 2 times, most recently from 63298bf to 55bc5e9 Compare February 14, 2025 23:22
@ax3l ax3l force-pushed the performance-quad-constants branch from 55bc5e9 to 86c0c2b Compare February 14, 2025 23:24
@ax3l ax3l force-pushed the performance-quad-constants branch from db2f5ac to 483e3df Compare February 15, 2025 00:38
@ax3l ax3l changed the title [Draft] Make Quad & Drift Fast [WIP] Make Linear Elements Fast Feb 15, 2025
Comment on lines +159 to +166
xout = m_cos_kxds * x + m_sincx * px;
pxout = m_const_x * x + m_cos_kxds * px;

yout = std::cos(m_ky*slice_ds)*y + sincy*py;
pyout = -m_ky * std::sin(m_ky*slice_ds)*y + std::cos(m_ky*slice_ds)*py;
yout = m_cos_kyds * y + m_sincy * py;
pyout = m_const_y * y + m_cos_kyds * py;

tout = std::cos(m_kt*slice_ds)*t + sinct/(betgam2)*pt;
ptout = -(m_kt*betgam2) * std::sin(m_kt*slice_ds)*t + std::cos(m_kt*slice_ds)*pt;
tout = m_cos_ktds * t + m_const_pt * pt;
ptout = m_const_y * t + m_cos_ktds * pt;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could actually give those members proper m_Rij names...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need to return the linear map for envelope tracking anyway, in a separate function within ConstF. Those matrix element values could be used here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Optimize Lattice: Pre-Calc Constants
2 participants