From 08ea9275d05bf3ac9d46c8edd5519bb0021a9f7e Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Sun, 28 Jan 2024 13:46:08 -0800 Subject: [PATCH] Python: Clean CUDA Shutdown Avoid double finalize and segfaults in CUDA debug runs. --- examples/alignment/run_alignment.py | 4 ---- examples/aperture/run_aperture.py | 4 ---- examples/apochromatic/run_apochromatic.py | 4 ---- examples/cfbend/run_cfbend.py | 4 ---- examples/cfbend/run_cfbend_madx.py | 4 ---- examples/cfchannel/run_cfchannel.py | 4 ---- examples/cfchannel/run_cfchannel_10nC.py | 4 ---- examples/chicane/run_chicane.py | 4 ---- examples/chicane/run_chicane_madx.py | 4 ---- examples/compression/run_compression.py | 4 ---- examples/cyclotron/run_cyclotron.py | 4 ---- examples/epac2004_benchmarks/run_bithermal.py | 4 ---- examples/epac2004_benchmarks/run_fodo_rf_SC.py | 4 ---- examples/epac2004_benchmarks/run_thermal.py | 4 ---- examples/expanding_beam/run_expanding.py | 4 ---- examples/fodo/run_fodo.py | 4 ---- examples/fodo/run_fodo_madx.py | 4 ---- examples/fodo/run_fodo_programmable.py | 4 ---- examples/fodo_chromatic/run_fodo_chr.py | 4 ---- examples/fodo_rf/run_fodo_rf.py | 4 ---- examples/iota_lattice/run_iotalattice.py | 4 ---- examples/iota_lattice/run_iotalattice_sdep.py | 4 ---- examples/iota_lens/run_iotalens.py | 4 ---- examples/iota_lens/run_iotalens_sdep.py | 4 ---- examples/kicker/run_hvkicker_madx.py | 4 ---- examples/kicker/run_kicker.py | 4 ---- examples/kicker/run_kicker_madx.py | 4 ---- examples/kurth/run_kurth_10nC_periodic.py | 4 ---- examples/kurth/run_kurth_periodic.py | 4 ---- examples/multipole/run_multipole.py | 4 ---- examples/positron_channel/run_positron.py | 4 ---- .../pytorch_surrogate_model/run_ml_surrogate.py | 1 - .../quadrupole_softedge/run_quadrupole_softedge.py | 4 ---- examples/rfcavity/run_rfcavity.py | 4 ---- examples/rotation/run_rotation.py | 4 ---- examples/solenoid/run_solenoid.py | 4 ---- examples/solenoid/run_solenoid_madx.py | 4 ---- .../solenoid_softedge/run_solenoid_softedge.py | 4 ---- examples/thin_dipole/run_thin_dipole.py | 4 ---- src/ImpactX.H | 4 ++++ src/ImpactX.cpp | 14 ++++++++++---- tests/python/test_dataframe.py | 7 ++++++- 42 files changed, 20 insertions(+), 158 deletions(-) diff --git a/examples/alignment/run_alignment.py b/examples/alignment/run_alignment.py index 88524dcf8..643743566 100755 --- a/examples/alignment/run_alignment.py +++ b/examples/alignment/run_alignment.py @@ -57,7 +57,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/aperture/run_aperture.py b/examples/aperture/run_aperture.py index 7107e0d74..952e801aa 100755 --- a/examples/aperture/run_aperture.py +++ b/examples/aperture/run_aperture.py @@ -62,7 +62,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/apochromatic/run_apochromatic.py b/examples/apochromatic/run_apochromatic.py index 022300827..32e800144 100644 --- a/examples/apochromatic/run_apochromatic.py +++ b/examples/apochromatic/run_apochromatic.py @@ -71,7 +71,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/cfbend/run_cfbend.py b/examples/cfbend/run_cfbend.py index 071f0b727..f28a11a1d 100644 --- a/examples/cfbend/run_cfbend.py +++ b/examples/cfbend/run_cfbend.py @@ -61,7 +61,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/cfbend/run_cfbend_madx.py b/examples/cfbend/run_cfbend_madx.py index d73c1394b..9be91c3c1 100755 --- a/examples/cfbend/run_cfbend_madx.py +++ b/examples/cfbend/run_cfbend_madx.py @@ -48,7 +48,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/cfchannel/run_cfchannel.py b/examples/cfchannel/run_cfchannel.py index bf31e71fa..98b1ccc46 100755 --- a/examples/cfchannel/run_cfchannel.py +++ b/examples/cfchannel/run_cfchannel.py @@ -55,7 +55,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/cfchannel/run_cfchannel_10nC.py b/examples/cfchannel/run_cfchannel_10nC.py index bb5ce9b37..57cb1fde4 100755 --- a/examples/cfchannel/run_cfchannel_10nC.py +++ b/examples/cfchannel/run_cfchannel_10nC.py @@ -60,7 +60,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/chicane/run_chicane.py b/examples/chicane/run_chicane.py index c839436db..6163169e1 100644 --- a/examples/chicane/run_chicane.py +++ b/examples/chicane/run_chicane.py @@ -78,7 +78,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/chicane/run_chicane_madx.py b/examples/chicane/run_chicane_madx.py index 7d8289adf..fae2ead88 100755 --- a/examples/chicane/run_chicane_madx.py +++ b/examples/chicane/run_chicane_madx.py @@ -48,7 +48,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/compression/run_compression.py b/examples/compression/run_compression.py index d54e4bcf9..f2c19cf8b 100644 --- a/examples/compression/run_compression.py +++ b/examples/compression/run_compression.py @@ -58,7 +58,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/cyclotron/run_cyclotron.py b/examples/cyclotron/run_cyclotron.py index 89092b551..5b251b3f4 100755 --- a/examples/cyclotron/run_cyclotron.py +++ b/examples/cyclotron/run_cyclotron.py @@ -64,7 +64,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/epac2004_benchmarks/run_bithermal.py b/examples/epac2004_benchmarks/run_bithermal.py index 871921b80..26dda7849 100755 --- a/examples/epac2004_benchmarks/run_bithermal.py +++ b/examples/epac2004_benchmarks/run_bithermal.py @@ -66,7 +66,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/epac2004_benchmarks/run_fodo_rf_SC.py b/examples/epac2004_benchmarks/run_fodo_rf_SC.py index 7112e975b..9f15b8b1b 100755 --- a/examples/epac2004_benchmarks/run_fodo_rf_SC.py +++ b/examples/epac2004_benchmarks/run_fodo_rf_SC.py @@ -197,7 +197,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/epac2004_benchmarks/run_thermal.py b/examples/epac2004_benchmarks/run_thermal.py index 0211077ea..d713b5928 100755 --- a/examples/epac2004_benchmarks/run_thermal.py +++ b/examples/epac2004_benchmarks/run_thermal.py @@ -64,7 +64,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/expanding_beam/run_expanding.py b/examples/expanding_beam/run_expanding.py index 286fd6095..2fb300968 100755 --- a/examples/expanding_beam/run_expanding.py +++ b/examples/expanding_beam/run_expanding.py @@ -59,7 +59,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/fodo/run_fodo.py b/examples/fodo/run_fodo.py index df60b07a5..65d9a4b29 100755 --- a/examples/fodo/run_fodo.py +++ b/examples/fodo/run_fodo.py @@ -67,7 +67,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/fodo/run_fodo_madx.py b/examples/fodo/run_fodo_madx.py index 34076a905..52079c10d 100755 --- a/examples/fodo/run_fodo_madx.py +++ b/examples/fodo/run_fodo_madx.py @@ -49,7 +49,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/fodo/run_fodo_programmable.py b/examples/fodo/run_fodo_programmable.py index d8f0cd1fd..1af61bbdb 100755 --- a/examples/fodo/run_fodo_programmable.py +++ b/examples/fodo/run_fodo_programmable.py @@ -172,7 +172,3 @@ def my_ref_drift(pge, refpart): # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/fodo_chromatic/run_fodo_chr.py b/examples/fodo_chromatic/run_fodo_chr.py index b24c3003b..7d45152fc 100755 --- a/examples/fodo_chromatic/run_fodo_chr.py +++ b/examples/fodo_chromatic/run_fodo_chr.py @@ -67,7 +67,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/fodo_rf/run_fodo_rf.py b/examples/fodo_rf/run_fodo_rf.py index 34aef795b..125146d39 100644 --- a/examples/fodo_rf/run_fodo_rf.py +++ b/examples/fodo_rf/run_fodo_rf.py @@ -66,7 +66,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/iota_lattice/run_iotalattice.py b/examples/iota_lattice/run_iotalattice.py index dfa06f219..4cdbcc349 100644 --- a/examples/iota_lattice/run_iotalattice.py +++ b/examples/iota_lattice/run_iotalattice.py @@ -122,7 +122,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/iota_lattice/run_iotalattice_sdep.py b/examples/iota_lattice/run_iotalattice_sdep.py index 92e265e8b..823ae5f1d 100644 --- a/examples/iota_lattice/run_iotalattice_sdep.py +++ b/examples/iota_lattice/run_iotalattice_sdep.py @@ -218,7 +218,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/iota_lens/run_iotalens.py b/examples/iota_lens/run_iotalens.py index 9cb3cab73..d47bbbf70 100644 --- a/examples/iota_lens/run_iotalens.py +++ b/examples/iota_lens/run_iotalens.py @@ -56,7 +56,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/iota_lens/run_iotalens_sdep.py b/examples/iota_lens/run_iotalens_sdep.py index ec9289220..ab6739150 100644 --- a/examples/iota_lens/run_iotalens_sdep.py +++ b/examples/iota_lens/run_iotalens_sdep.py @@ -92,7 +92,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/kicker/run_hvkicker_madx.py b/examples/kicker/run_hvkicker_madx.py index 2bd584e82..01945d345 100644 --- a/examples/kicker/run_hvkicker_madx.py +++ b/examples/kicker/run_hvkicker_madx.py @@ -48,7 +48,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/kicker/run_kicker.py b/examples/kicker/run_kicker.py index 87458d386..6aa098547 100644 --- a/examples/kicker/run_kicker.py +++ b/examples/kicker/run_kicker.py @@ -56,7 +56,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/kicker/run_kicker_madx.py b/examples/kicker/run_kicker_madx.py index 88147c770..973e77625 100644 --- a/examples/kicker/run_kicker_madx.py +++ b/examples/kicker/run_kicker_madx.py @@ -48,7 +48,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/kurth/run_kurth_10nC_periodic.py b/examples/kurth/run_kurth_10nC_periodic.py index dcae2fba3..e1b462479 100755 --- a/examples/kurth/run_kurth_10nC_periodic.py +++ b/examples/kurth/run_kurth_10nC_periodic.py @@ -54,7 +54,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/kurth/run_kurth_periodic.py b/examples/kurth/run_kurth_periodic.py index a1e2c11e8..bb60c4d5a 100755 --- a/examples/kurth/run_kurth_periodic.py +++ b/examples/kurth/run_kurth_periodic.py @@ -52,7 +52,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/multipole/run_multipole.py b/examples/multipole/run_multipole.py index dadf0d0b3..9c6e70abc 100644 --- a/examples/multipole/run_multipole.py +++ b/examples/multipole/run_multipole.py @@ -57,7 +57,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/positron_channel/run_positron.py b/examples/positron_channel/run_positron.py index c808d09b5..58c1f0beb 100755 --- a/examples/positron_channel/run_positron.py +++ b/examples/positron_channel/run_positron.py @@ -69,7 +69,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/pytorch_surrogate_model/run_ml_surrogate.py b/examples/pytorch_surrogate_model/run_ml_surrogate.py index b0d14c393..1de19c005 100644 --- a/examples/pytorch_surrogate_model/run_ml_surrogate.py +++ b/examples/pytorch_surrogate_model/run_ml_surrogate.py @@ -265,4 +265,3 @@ def surrogate_push(self, pc, step): sim.lattice.extend([monitor]) sim.evolve() -del sim diff --git a/examples/quadrupole_softedge/run_quadrupole_softedge.py b/examples/quadrupole_softedge/run_quadrupole_softedge.py index 882e0e989..db2598cde 100755 --- a/examples/quadrupole_softedge/run_quadrupole_softedge.py +++ b/examples/quadrupole_softedge/run_quadrupole_softedge.py @@ -76,7 +76,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/rfcavity/run_rfcavity.py b/examples/rfcavity/run_rfcavity.py index 10ee85dc1..5d9ec9b2b 100755 --- a/examples/rfcavity/run_rfcavity.py +++ b/examples/rfcavity/run_rfcavity.py @@ -139,7 +139,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/rotation/run_rotation.py b/examples/rotation/run_rotation.py index 2d47931d8..5a3247a45 100644 --- a/examples/rotation/run_rotation.py +++ b/examples/rotation/run_rotation.py @@ -57,7 +57,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/solenoid/run_solenoid.py b/examples/solenoid/run_solenoid.py index 3faef7d04..bd91a4f31 100755 --- a/examples/solenoid/run_solenoid.py +++ b/examples/solenoid/run_solenoid.py @@ -56,7 +56,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/solenoid/run_solenoid_madx.py b/examples/solenoid/run_solenoid_madx.py index 9a759dd88..da8af86a1 100755 --- a/examples/solenoid/run_solenoid_madx.py +++ b/examples/solenoid/run_solenoid_madx.py @@ -45,7 +45,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/solenoid_softedge/run_solenoid_softedge.py b/examples/solenoid_softedge/run_solenoid_softedge.py index b65e806e2..ff6877f20 100755 --- a/examples/solenoid_softedge/run_solenoid_softedge.py +++ b/examples/solenoid_softedge/run_solenoid_softedge.py @@ -137,7 +137,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/examples/thin_dipole/run_thin_dipole.py b/examples/thin_dipole/run_thin_dipole.py index ebde9e7eb..8bfa92153 100755 --- a/examples/thin_dipole/run_thin_dipole.py +++ b/examples/thin_dipole/run_thin_dipole.py @@ -64,7 +64,3 @@ # run simulation sim.evolve() - -# clean shutdown -del sim -amr.finalize() diff --git a/src/ImpactX.H b/src/ImpactX.H index 09eececfb..5d8dfd7a8 100644 --- a/src/ImpactX.H +++ b/src/ImpactX.H @@ -40,6 +40,10 @@ namespace impactx void operator= (ImpactX const&) = delete; void operator= (ImpactX &&) = delete; + /** Destruct the ImpactX simulation object + */ + ~ImpactX (); + /** Initialize AMReX blocks/grids for domain decomposition & space charge mesh. * * This must come first, before particle beams and lattice elements are diff --git a/src/ImpactX.cpp b/src/ImpactX.cpp index d20e94232..6d796e69b 100644 --- a/src/ImpactX.cpp +++ b/src/ImpactX.cpp @@ -32,16 +32,22 @@ #include -namespace impactx -{ - ImpactX::ImpactX () - { +namespace impactx { + ImpactX::ImpactX() { // todo: if amr.n_cells is provided, overwrite/redefine AmrCore object // todo: if charge deposition and/or space charge are requested, require // amr.n_cells from user inputs } + ImpactX::~ImpactX() + { + m_lattice.clear() + + // this one last + amr_data.release(); + } + void ImpactX::init_grids () { BL_PROFILE("ImpactX::init_grids"); diff --git a/tests/python/test_dataframe.py b/tests/python/test_dataframe.py index a209317b4..88eafe57a 100644 --- a/tests/python/test_dataframe.py +++ b/tests/python/test_dataframe.py @@ -7,13 +7,18 @@ # # -*- coding: utf-8 -*- -from impactx import ImpactX, RefPart, distribution, elements +from impactx import ImpactX, RefPart, amr, distribution, elements def test_df_pandas(): """ This tests using ImpactX and Pandas Dataframes """ + if amr.Config.have_gpu: + # pc.to_df copies to/from host memory + pp_amrex = amr.ParmParse("amrex") + pp_amrex.add("the_arena_is_managed", 1) + sim = ImpactX() sim.particle_shape = 2