Skip to content

Commit

Permalink
Add environment variable to kill Celeritas-supported tracks in Geant4 (
Browse files Browse the repository at this point in the history
  • Loading branch information
amandalund committed Dec 13, 2023
1 parent cb176ff commit 8670a6f
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 21 deletions.
11 changes: 7 additions & 4 deletions app/celer-g4/TrackingAction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void TrackingAction::PreUserTrackingAction(G4Track const* track)
== !SharedParams::CeleritasDisabled());
CELER_EXPECT(static_cast<bool>(*params_) == static_cast<bool>(*transport_));

if (SharedParams::CeleritasDisabled())
if (SharedParams::CeleritasDisabled() && !SharedParams::KillOffloadTracks())
return;

auto const& allowed_particles = params_->OffloadParticles();
Expand All @@ -59,9 +59,12 @@ void TrackingAction::PreUserTrackingAction(G4Track const* track)
track->GetDefinition())
!= std::end(allowed_particles))
{
// Celeritas is transporting this track
ExceptionConverter call_g4exception{"celer0003", params_.get()};
CELER_TRY_HANDLE(transport_->Push(*track), call_g4exception);
if (!SharedParams::CeleritasDisabled())
{
// Celeritas is transporting this track
ExceptionConverter call_g4exception{"celer0003", params_.get()};
CELER_TRY_HANDLE(transport_->Push(*track), call_g4exception);
}
const_cast<G4Track*>(track)->SetTrackStatus(fStopAndKill);
}
}
Expand Down
1 change: 1 addition & 0 deletions app/celer-g4/celer-g4.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ void print_usage(std::string_view exec_name)
" CELER_DISABLE: nonempty disables offloading\n"
" CELER_DISABLE_DEVICE: nonempty disables CUDA\n"
" CELER_DISABLE_ROOT: nonempty disables ROOT I/O\n"
" CELER_KILL_OFFLOAD: nonempty kills offload tracks\n"
" CELER_LOG: global logging level\n"
" CELER_LOG_LOCAL: thread-local logging level\n"
<< std::endl;
Expand Down
1 change: 1 addition & 0 deletions doc/main/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ tell what variables are in use or may be useful.
HEPMC3_VERBOSE celeritas HepMC3 debug verbosity
VECGEOM_VERBOSE celeritas VecGeom CUDA verbosity
CELER_DISABLE accel Disable Celeritas offloading entirely
CELER_KILL_OFFLOAD accel Kill Celeritas-supported tracks in Geant4
CELER_STRIP_SOURCEDIR accel Strip directories from exception output
======================= ========= ==========================================

Expand Down
42 changes: 42 additions & 0 deletions src/accel/SharedParams.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
#include <utility>
#include <vector>
#include <CLHEP/Random/Random.h>
#include <G4Electron.hh>
#include <G4Gamma.hh>
#include <G4ParticleDefinition.hh>
#include <G4ParticleTable.hh>
#include <G4Positron.hh>
#include <G4RunManager.hh>
#include <G4Threading.hh>

Expand Down Expand Up @@ -170,6 +173,23 @@ bool SharedParams::CeleritasDisabled()
return result;
}

bool SharedParams::KillOffloadTracks()
{
static bool const result = [] {
if (celeritas::getenv("CELER_KILL_OFFLOAD").empty())
return false;

if (CeleritasDisabled())
{
CELER_LOG(info) << "Killing Geant4 tracks supported by Celeritas "
"offloading since the 'CELER_KILL_OFFLOAD' "
"environment variable is present and non-empty";
}
return true;
}();
return result;
}

//---------------------------------------------------------------------------//
/*!
* Set up Celeritas using Geant4 data and existing output registery.
Expand Down Expand Up @@ -335,6 +355,28 @@ void SharedParams::Finalize()
CELER_ENSURE(!*this);
}

//---------------------------------------------------------------------------//
/*!
* Get a vector of particles supported by Celeritas offloading.
*/
auto SharedParams::OffloadParticles() const -> VecG4ParticleDef const&
{
if (!CeleritasDisabled())
{
// Get the supported particles from Celeritas
CELER_ASSERT(*this);
return particles_;
}

// In a Geant4-only simulation, use a hardcoded list of supported particles
static VecG4ParticleDef const particles = {
G4Gamma::Gamma(),
G4Electron::Electron(),
G4Positron::Positron(),
};
return particles;
}

//---------------------------------------------------------------------------//
/*!
* Lazily obtained number of streams.
Expand Down
17 changes: 4 additions & 13 deletions src/accel/SharedParams.hh
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ class SharedParams
// True if Celeritas is globally disabled using the CELER_DISABLE env
static bool CeleritasDisabled();

// Whether to kill tracks that would have been offloaded
static bool KillOffloadTracks();

// Construct in an uninitialized state
SharedParams() = default;

Expand All @@ -88,7 +91,7 @@ class SharedParams
inline SPConstParams Params() const;

// Get a vector of particles supported by Celeritas offloading
inline VecG4ParticleDef const& OffloadParticles() const;
VecG4ParticleDef const& OffloadParticles() const;

//! Whether Celeritas core params have been created
explicit operator bool() const { return static_cast<bool>(params_); }
Expand Down Expand Up @@ -172,18 +175,6 @@ auto SharedParams::Params() const -> SPConstParams
return params_;
}

//---------------------------------------------------------------------------//
/*!
* Get a vector of particles supported by Celeritas offloading.
*
* This can only be called after \c Initialize.
*/
auto SharedParams::OffloadParticles() const -> VecG4ParticleDef const&
{
CELER_EXPECT(*this);
return particles_;
}

//---------------------------------------------------------------------------//
/*!
* Hit manager, to be used only by LocalTransporter.
Expand Down
11 changes: 7 additions & 4 deletions src/accel/SimpleOffload.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,20 @@ void SimpleOffload::BeginOfEventAction(G4Event const* event)
*/
void SimpleOffload::PreUserTrackingAction(G4Track* track)
{
if (!*this)
if (!*this && !SharedParams::KillOffloadTracks())
return;

if (std::find(params_->OffloadParticles().begin(),
params_->OffloadParticles().end(),
track->GetDefinition())
!= params_->OffloadParticles().end())
{
// Celeritas is transporting this track
ExceptionConverter call_g4exception{"celer0003", params_};
CELER_TRY_HANDLE(local_->Push(*track), call_g4exception);
if (!SharedParams::CeleritasDisabled())
{
// Celeritas is transporting this track
ExceptionConverter call_g4exception{"celer0003", params_};
CELER_TRY_HANDLE(local_->Push(*track), call_g4exception);
}
track->SetTrackStatus(fStopAndKill);
}
}
Expand Down

0 comments on commit 8670a6f

Please sign in to comment.