Skip to content

Default to flat backend when compatible#74

Merged
cwoolfo1 merged 5 commits intomainfrom
codex/flat-default
Mar 16, 2026
Merged

Default to flat backend when compatible#74
cwoolfo1 merged 5 commits intomainfrom
codex/flat-default

Conversation

@rogeriojorge
Copy link
Member

Summary

  • Default to a flat particle backend when it is compatible (periodic BCs, identical shapes/flags), with a safe fallback to the original path.
  • Preserve physics: no algorithmic changes to solvers or field updates, only data layout and compatibility fixes.
  • Add flat-compatible metadata + per-particle q/m support so diagnostics and output stay consistent.

Changes (minimal core edits)

  • PyPIC3D/initialization.py: choose flat backend by default when compatible; fallback to default for incompatible configs.
  • PyPIC3D/flat_particles.py: flat species structure + compatibility normalization + momentum helper.
  • PyPIC3D/boris.py: vmap handles per-particle charge/mass arrays.
  • PyPIC3D/utils.py: output metadata uses original species list when flat backend is active.

Benchmarks (long runs, CPU, JAX x64)

Configs:

  • two_stream: t_wind = 1.6239e-7 (3× longer)
  • weibel: Nt = 18000 (3× longer)

Runtime summary

Case Main s/step Flat s/step Speedup Main total (s) Flat total (s) Speedup
Two-stream 0.003242 0.001097 2.95× 15.94 5.39 2.95×
Weibel 0.001141 0.000932 1.22× 20.53 16.78 1.22×

Raw results JSON (artifacts branch):

  • docs/benchmarks/results.json

Plots (artifacts branch)

Runtime per step

Runtime per step

Total wall time

Total wall time

Speedup per step

Speedup per step

Speedup total

Speedup total

Two-stream energy

Two-stream E-field energy

Two-stream energy error

Weibel energy

Weibel E-field energy

Weibel energy error

Notes

  • Artifacts are stored in codex/flat-default-artifacts to keep plots/results out of the main branch.
  • For incompatible setups (non-periodic BCs, differing shape factors, etc.), the code automatically falls back to the original particle path.

@rogeriojorge
Copy link
Member Author

I think this is it. Two stream instability is 3 times faster. @cwoolfo1 it is ready for review

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR switches the default particle backend to a new “flat” particle layout when the simulation configuration is compatible, with automatic fallback to the existing species-per-list backend when it is not. The goal is to improve runtime performance while keeping solver/field physics unchanged and preserving output metadata.

Changes:

  • Default to the flat particle backend when compatible; otherwise fall back to the existing backend.
  • Add flat_particles.py implementing a flattened species container, compatibility checks, and flattening conversion.
  • Update Boris pusher to support per-particle charge/mass arrays and update TOML parameter dumping to preserve original species metadata.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
PyPIC3D/initialization.py Adds fast_backend selection logic and defaults to flat when compatible.
PyPIC3D/flat_particles.py Introduces flattened particle species representation and flattening utilities.
PyPIC3D/boris.py Extends vmap setup to handle scalar vs per-particle q/m.
PyPIC3D/utils.py Uses species_meta to dump original species metadata when flat backend is active.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +145 to +174
def tree_flatten(self):
children = (self.x1, self.x2, self.x3, self.v1, self.v2, self.v3)
aux_data = (
self.name,
self.N_particles,
self.charge,
self.mass,
self.weight,
self.T,
self.x_wind,
self.y_wind,
self.z_wind,
self.dx,
self.dy,
self.dz,
self.x_bc,
self.y_bc,
self.z_bc,
self.update_pos,
self.update_v,
self.update_x,
self.update_y,
self.update_z,
self.update_vx,
self.update_vy,
self.update_vz,
self.shape,
self.dt,
self.species_meta,
)
Comment on lines +171 to +174
self.shape,
self.dt,
self.species_meta,
)
if particles[0].x_bc != "periodic" or particles[0].y_bc != "periodic" or particles[0].z_bc != "periodic":
return False
if not _same([p.update_pos for p in particles]) or not _same([p.update_v for p in particles]):
return False
@cwoolfo1
Copy link
Member

Updated openPMD particle diagnostics to account for flat particles

@cwoolfo1
Copy link
Member

I am getting a 1.26x speedup for the two stream instability

@cwoolfo1
Copy link
Member

WITHOUT FLATTENED BACKEND:

Kernel Compile Time: 1.0667036710001412 s
Average Profiled Step Time: 0.0014490590188429742 s
particle_push: 42.72% (2.888942229990789 s total, 0.0006190148339384592 s/step)
position_update: 13.15% (0.8892241920384549 s total, 0.00019053443154884398 s/step)
current_deposition: 27.18% (1.8379176599828497 s total, 0.00039381136918424035 s/step)
electric_field_update: 3.61% (0.24443554394019884 s total, 5.237530403689712e-05 s/step)
magnetic_field_update: 2.70% (0.18282780899608042 s total, 3.9174589457056016e-05 s/step)
particle_boundary_conditions: 10.64% (0.7194110059917875 s total, 0.00015414849067747752 s/step)

WITH FLATTENED BACKEND:

Kernel Compile Time: 0.6272344169992721 s
Average Profiled Step Time: 0.0008352886466648828 s
particle_push: 52.63% (2.051678864982023 s total, 0.00043961407006257186 s/step)
position_update: 5.16% (0.20111267403626698 s total, 4.3092494972416325e-05 s/step)
current_deposition: 30.40% (1.1852355049177277 s total, 0.00025396089670403423 s/step)
electric_field_update: 4.58% (0.17846876304065518 s total, 3.824057489621924e-05 s/step)
magnetic_field_update: 3.46% (0.13504225502219924 s total, 2.89355592505248e-05 s/step)
particle_boundary_conditions: 3.76% (0.1467540519861359 s total, 3.1445050779116326e-05 s/step)

Biggest performance speedups appear to happen in the particle pusher and the position updates/bc. Current deposition remains about the same

@cwoolfo1 cwoolfo1 merged commit a7eb9f6 into main Mar 16, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants