Skip to content

Commit

Permalink
initialize the distributions on the GPU (#498)
Browse files Browse the repository at this point in the history
* initialize the distributions on the GPU

* Fix Thermal: `const` operator

* Python Support, Cleaning

* Cleanup: Unused, Resize

Fix the missing resize before redistribute.
Remove the old, unusued, serial initialization logic.

* Thermal Distribution: GPU Memory

Remove stack memory usage and use GPU memory for pre-computed CDF.

* Cosmetic Cleaning

* Waterbag: Clean C++ Math

Avoid promoting to double in C functions.

* Cosmetic Cleaning for Clarity

* Fix CUDA Compile

On older NVCC

* Analysis: Slightly Increase Sampling Tolerance

Run on local GPU (RTX A2000).

---------

Co-authored-by: Axel Huebl <[email protected]>
  • Loading branch information
atmyers and ax3l authored Jan 5, 2024
1 parent 5c6017e commit 71df3bd
Show file tree
Hide file tree
Showing 33 changed files with 499 additions and 272 deletions.
3 changes: 1 addition & 2 deletions docs/source/usage/python.rst
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,14 @@ Particles

This class stores particles, distributed over MPI ranks.

.. py:method:: add_n_particles(lev, x, y, t, px, py, pt, qm, bchchg)
.. py:method:: add_n_particles(x, y, t, px, py, pt, qm, bchchg)
Add new particles to the container for fixed s.

Note: This can only be used *after* the initialization (grids) have
been created, meaning after the call to :py:meth:`ImpactX.init_grids`
has been made in the ImpactX class.

:param lev: mesh-refinement level
:param x: positions in x
:param y: positions in y
:param t: positions as time-of-flight in c*t
Expand Down
2 changes: 1 addition & 1 deletion examples/alignment/analysis_alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def get_moments(beam):
)

atol = 0.0 # ignored
rtol = 1.8 * num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 2.0 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand Down
2 changes: 1 addition & 1 deletion examples/apochromatic/analysis_apochromatic.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def get_moments(beam):
)

atol = 0.0 # ignored
rtol = 15.0 * num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 19.0 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand Down
4 changes: 2 additions & 2 deletions examples/chicane/analysis_chicane.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def get_moments(beam):
)

atol = 0.0 # ignored
rtol = num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 2.2 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand All @@ -85,7 +85,7 @@ def get_moments(beam):
)

atol = 0.0 # ignored
rtol = num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 2.2 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand Down
4 changes: 2 additions & 2 deletions examples/compression/analysis_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def get_moments(beam):
print(f" gamma={initial_gamma_ref:e}")

atol = 0.0 # ignored
rtol = 1.8 * num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 1.9 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand Down Expand Up @@ -99,7 +99,7 @@ def get_moments(beam):


atol = 0.0 # ignored
rtol = 1.8 * num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 1.9 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand Down
2 changes: 1 addition & 1 deletion examples/cyclotron/analysis_cyclotron.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def get_moments(beam):
)

atol = 0.0 # ignored
rtol = 1.8 * num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 2.0 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand Down
4 changes: 2 additions & 2 deletions examples/distgen/analysis_gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def get_moments(beam):
)

atol = 0.0 # ignored
rtol = num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 2.3 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand All @@ -85,7 +85,7 @@ def get_moments(beam):
)

atol = 0.0 # ignored
rtol = num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 2.3 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand Down
4 changes: 2 additions & 2 deletions examples/distgen/analysis_kurth4d.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def get_moments(beam):
)

atol = 0.0 # ignored
rtol = num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 1.3 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand All @@ -85,7 +85,7 @@ def get_moments(beam):
)

atol = 0.0 # ignored
rtol = num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 1.3 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand Down
4 changes: 2 additions & 2 deletions examples/distgen/analysis_semigaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def get_moments(beam):
)

atol = 0.0 # ignored
rtol = num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 2.3 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand All @@ -85,7 +85,7 @@ def get_moments(beam):
)

atol = 0.0 # ignored
rtol = num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 2.3 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand Down
2 changes: 1 addition & 1 deletion examples/expanding_beam/analysis_expanding.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def get_moments(beam):
)

atol = 0.0 # ignored
rtol = num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 1.5 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand Down
4 changes: 2 additions & 2 deletions examples/fodo/analysis_fodo.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def get_moments(beam):
)

atol = 0.0 # ignored
rtol = num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 2.2 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand All @@ -86,7 +86,7 @@ def get_moments(beam):
)

atol = 0.0 # ignored
rtol = num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 2.2 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand Down
4 changes: 2 additions & 2 deletions examples/fodo_chromatic/analysis_fodo_chr.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def get_moments(beam):
)

atol = 0.0 # ignored
rtol = num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 2.2 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand All @@ -86,7 +86,7 @@ def get_moments(beam):
)

atol = 0.0 # ignored
rtol = num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 2.2 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand Down
4 changes: 2 additions & 2 deletions examples/fodo_rf/analysis_fodo_rf.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def get_moments(beam):
)

atol = 0.0 # ignored
rtol = num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 2.2 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand All @@ -85,7 +85,7 @@ def get_moments(beam):
)

atol = 0.0 # ignored
rtol = num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 2.2 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand Down
4 changes: 2 additions & 2 deletions examples/iota_lattice/analysis_iotalattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def get_moments(beam):
)

atol = 0.0 # a big number
rtol = 1.5 * num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 1.9 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand All @@ -78,7 +78,7 @@ def get_moments(beam):
)

atol = 0.0 # a big number
rtol = 1.8 * num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 1.9 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand Down
8 changes: 4 additions & 4 deletions examples/iota_lattice/analysis_iotalattice_sdep.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def read_all_files(file_pattern):
print(f" meanH={meanH:e} sigH={sigH:e} meanI={meanI:e} sigI={sigI:e}")

atol = 0.0 # a big number
rtol = 1.5 * num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 1.6 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand All @@ -73,7 +73,7 @@ def read_all_files(file_pattern):
print(f" meanH={meanH:e} sigH={sigH:e} meanI={meanI:e} sigI={sigI:e}")

atol = 0.0 # a big number
rtol = 1.5 * num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 1.6 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand All @@ -94,14 +94,14 @@ def read_all_files(file_pattern):
Hrms = np.sqrt(sigH**2 + meanH**2)
Irms = np.sqrt(sigI**2 + meanI**2)

atol = 4.0e-3 * Hrms
atol = 4.2e-3 * Hrms
rtol = 0.0 # large number
print()
print(f" atol={atol} (ignored: rtol~={rtol})")
print(f" dH_max={beam_joined['dH'].max()}")
assert np.allclose(beam_joined["dH"], 0.0, rtol=rtol, atol=atol)

atol = 5.5e-3 * Irms
atol = 5.7e-3 * Irms
rtol = 0.0
print()
print(f" atol={atol} (ignored: rtol~={rtol})")
Expand Down
4 changes: 2 additions & 2 deletions examples/kurth/analysis_kurth_periodic.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def get_moments(beam):
)

atol = 0.0 # ignored
rtol = 1.5 * num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 1.7 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand All @@ -85,7 +85,7 @@ def get_moments(beam):
)

atol = 0.0 # ignored
rtol = 1.5 * num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 1.7 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand Down
4 changes: 2 additions & 2 deletions examples/multipole/analysis_multipole.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def get_moments(beam):
)

atol = 0.0 # ignored
rtol = num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 2.2 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand All @@ -85,7 +85,7 @@ def get_moments(beam):
)

atol = 0.0 # ignored
rtol = num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 2.5 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand Down
4 changes: 2 additions & 2 deletions examples/quadrupole_softedge/analysis_quadrupole_softedge.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def get_moments(beam):
)

atol = 0.0 # ignored
rtol = num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 2.2 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand All @@ -85,7 +85,7 @@ def get_moments(beam):
)

atol = 0.0 # ignored
rtol = num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 2.2 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand Down
4 changes: 2 additions & 2 deletions examples/solenoid/analysis_solenoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def get_moments(beam):
)

atol = 0.0 # ignored
rtol = num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 1.3 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand All @@ -85,7 +85,7 @@ def get_moments(beam):
)

atol = 0.0 # ignored
rtol = num_particles**-0.5 # from random sampling of a smooth distribution
rtol = 1.3 * num_particles**-0.5 # from random sampling of a smooth distribution
print(f" rtol={rtol} (ignored: atol~={atol})")

assert np.allclose(
Expand Down
Loading

0 comments on commit 71df3bd

Please sign in to comment.