Skip to content

Commit

Permalink
Split ExplicitActionInterface to support optical data (celeritas-proj…
Browse files Browse the repository at this point in the history
…ect#1160)

* Split ExplicitActionInterface in ExplicitCoreActionInterface and future alternative.
ActionSequence::execute is now templated on the Params/State type and cast them to the concrete type.

* Add possibly missing include
* Add missing template keyword
* Move using statement down a level
* Apply review suggestion
  • Loading branch information
pcanal committed Mar 22, 2024
1 parent edf7937 commit 6c2a07a
Show file tree
Hide file tree
Showing 29 changed files with 172 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/accel/AlongStepFactory.hh
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class AlongStepFactoryInterface
//!@{
//! \name Type aliases
using argument_type = AlongStepFactoryInput const&;
using result_type = std::shared_ptr<ExplicitActionInterface const>;
using result_type = std::shared_ptr<ExplicitCoreActionInterface const>;
//!@}

public:
Expand Down
4 changes: 2 additions & 2 deletions src/accel/SetupOptions.hh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class G4LogicalVolume;
namespace celeritas
{
struct AlongStepFactoryInput;
class ExplicitActionInterface;
class ExplicitCoreActionInterface;
//---------------------------------------------------------------------------//
/*!
* Control options for initializing Celeritas SD callbacks.
Expand Down Expand Up @@ -89,7 +89,7 @@ struct SetupOptions
using size_type = unsigned int;
using real_type = double;

using SPConstAction = std::shared_ptr<ExplicitActionInterface const>;
using SPConstAction = std::shared_ptr<ExplicitCoreActionInterface const>;
using AlongStepFactory
= std::function<SPConstAction(AlongStepFactoryInput const&)>;
using IntAccessor = std::function<int()>;
Expand Down
2 changes: 1 addition & 1 deletion src/celeritas/geo/detail/BoundaryAction.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace detail
/*!
* Move a track across a boundary.
*/
class BoundaryAction final : public ExplicitActionInterface,
class BoundaryAction final : public ExplicitCoreActionInterface,
public ConcreteAction
{
public:
Expand Down
62 changes: 52 additions & 10 deletions src/celeritas/global/ActionInterface.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ namespace celeritas
class CoreParams;
template<MemSpace M>
class CoreState;
class OpticalParams;
template<MemSpace M>
class OpticalState;

//---------------------------------------------------------------------------//
/*!
Expand Down Expand Up @@ -47,13 +50,6 @@ class CoreState;
*/
class ActionInterface
{
public:
//@{
//! \name Type aliases
using CoreStateHost = CoreState<MemSpace::host>;
using CoreStateDevice = CoreState<MemSpace::device>;
//@}

public:
// Default virtual destructor allows deletion by pointer-to-interface
virtual ~ActionInterface();
Expand Down Expand Up @@ -88,6 +84,13 @@ class ActionInterface
*/
class BeginRunActionInterface : public virtual ActionInterface
{
public:
//@{
//! \name Type aliases
using CoreStateHost = CoreState<MemSpace::host>;
using CoreStateDevice = CoreState<MemSpace::device>;
//@}

public:
//! Set host data at the beginning of a run
virtual void begin_run(CoreParams const&, CoreStateHost&) = 0;
Expand All @@ -101,15 +104,54 @@ class BeginRunActionInterface : public virtual ActionInterface
*/
class ExplicitActionInterface : public virtual ActionInterface
{
public:
//! Dependency ordering of the action
virtual ActionOrder order() const = 0;
};

//---------------------------------------------------------------------------//
/*!
* Interface for an action that launches a kernel or performs an action
* specialized for particles using CoreParams.
* TODO: Template this on 'Core' and 'Optical' and ...
*/
class ExplicitCoreActionInterface : public virtual ExplicitActionInterface
{
public:
//@{
//! \name Type aliases
using CoreStateHost = CoreState<MemSpace::host>;
using CoreStateDevice = CoreState<MemSpace::device>;
//@}

public:
//! Execute the action with host data
virtual void execute(CoreParams const&, CoreStateHost&) const = 0;

//! Execute the action with device data
virtual void execute(CoreParams const&, CoreStateDevice&) const = 0;
};

//! Dependency ordering of the action
virtual ActionOrder order() const = 0;
//---------------------------------------------------------------------------//
/*!
* Interface for an action that launches a kernel or performs an action
* specialized for particles using OpticalParams.
*/
class ExplicitOpticalActionInterface : public virtual ExplicitActionInterface
{
public:
//@{
//! \name Type aliases
using OpticalStateHost = OpticalState<MemSpace::host>;
using OpticalStateDevice = OpticalState<MemSpace::device>;
//@}

public:
//! Execute the action with host data
virtual void execute(OpticalParams const&, OpticalStateHost&) const = 0;

//! Execute the action with device data
virtual void execute(OpticalParams const&, OpticalStateDevice&) const = 0;
};

//---------------------------------------------------------------------------//
Expand All @@ -118,7 +160,7 @@ class ExplicitActionInterface : public virtual ActionInterface
*
* Example:
* \code
class KernellyPhysicsAction final : public ExplicitActionInterface,
class KernellyPhysicsAction final : public ExplicitCoreActionInterface,
public ConcreteAction
{
public:
Expand Down
4 changes: 2 additions & 2 deletions src/celeritas/global/ActionRegistry.hh
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ namespace celeritas
* will change the state of a track on device.
*
* Associated actions use the \c ActionInterface class to provide debugging
* information, and the \c ExplicitActionInterface is used to invoke kernels
* with core data.
* information, and the \c ExplicitCoreActionInterface is used to invoke
* kernels with core data.
*
* New actions should be created with an action ID corresponding to \c
* next_id . Registering an action checks its ID. Actions are always added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ParticleParams;
* have (but do not *need* to have) along-step energy loss, optional energy
* fluctuation, and optional multiple scattering.
*/
class AlongStepGeneralLinearAction final : public ExplicitActionInterface
class AlongStepGeneralLinearAction final : public ExplicitCoreActionInterface
{
public:
//!@{
Expand Down
2 changes: 1 addition & 1 deletion src/celeritas/global/alongstep/AlongStepNeutralAction.hh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace celeritas
* This should only be used for testing and demonstration purposes because real
* EM physics always has continuous energy loss for charged particles.
*/
class AlongStepNeutralAction final : public ExplicitActionInterface
class AlongStepNeutralAction final : public ExplicitCoreActionInterface
{
public:
// Construct with next action ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct RZMapFieldInput;
/*!
* Along-step kernel with MSC, energy loss fluctuations, and a RZMapField.
*/
class AlongStepRZMapFieldMscAction final : public ExplicitActionInterface
class AlongStepRZMapFieldMscAction final : public ExplicitCoreActionInterface
{
public:
//!@{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ParticleParams;
/*!
* Along-step kernel with optional MSC and uniform magnetic field.
*/
class AlongStepUniformMscAction final : public ExplicitActionInterface
class AlongStepUniformMscAction final : public ExplicitCoreActionInterface
{
public:
//!@{
Expand Down
21 changes: 17 additions & 4 deletions src/celeritas/global/detail/ActionSequence.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "corecel/sys/Stopwatch.hh"
#include "corecel/sys/Stream.hh"

#include "ParamsTraits.hh"
#include "../ActionInterface.hh"
#include "../ActionRegistry.hh"
#include "../CoreState.hh"
Expand Down Expand Up @@ -91,9 +92,15 @@ void ActionSequence::begin_run(CoreParams const& params, CoreState<M>& state)
/*!
* Call all explicit actions with host or device data.
*/
template<MemSpace M>
void ActionSequence::execute(CoreParams const& params, CoreState<M>& state)
template<typename Params, template<MemSpace M> class State, MemSpace M>
void ActionSequence::execute(Params const& params, State<M>& state)
{
using ExplicitAction = typename ParamsTraits<Params>::ExplicitAction;

static_assert(
std::is_same_v<State<M>, typename ParamsTraits<Params>::template State<M>>,
"The Params and State type are not matching.");

[[maybe_unused]] Stream::StreamT stream = nullptr;
if (M == MemSpace::device && options_.sync)
{
Expand All @@ -107,7 +114,9 @@ void ActionSequence::execute(CoreParams const& params, CoreState<M>& state)
{
ScopedProfiling profile_this{actions_[i]->label()};
Stopwatch get_time;
actions_[i]->execute(params, state);
auto const& concrete_action
= dynamic_cast<ExplicitAction const&>(*actions_[i]);
concrete_action.execute(params, state);
if (M == MemSpace::device)
{
CELER_DEVICE_CALL_PREFIX(StreamSynchronize(stream));
Expand All @@ -121,7 +130,9 @@ void ActionSequence::execute(CoreParams const& params, CoreState<M>& state)
for (SPConstExplicit const& sp_action : actions_)
{
ScopedProfiling profile_this{sp_action->label()};
sp_action->execute(params, state);
auto const& concrete_action
= dynamic_cast<ExplicitAction const&>(*sp_action);
concrete_action.execute(params, state);
}
}
}
Expand All @@ -140,6 +151,8 @@ ActionSequence::execute(CoreParams const&, CoreState<MemSpace::host>&);
template void
ActionSequence::execute(CoreParams const&, CoreState<MemSpace::device>&);

// TODO: add explicit template instantiation of execute for optical data

//---------------------------------------------------------------------------//
} // namespace detail
} // namespace celeritas
4 changes: 2 additions & 2 deletions src/celeritas/global/detail/ActionSequence.hh
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ class ActionSequence
void begin_run(CoreParams const& params, CoreState<M>& state);

// Launch all actions with the given memory space.
template<MemSpace M>
void execute(CoreParams const& params, CoreState<M>& state);
template<typename Params, template<MemSpace M> class State, MemSpace M>
void execute(Params const&, State<M>& state);

//// ACCESSORS ////

Expand Down
42 changes: 42 additions & 0 deletions src/celeritas/global/detail/ParamsTraits.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//----------------------------------*-C++-*----------------------------------//
// Copyright 2022-2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/global/details/ParamsTraits.hh
//---------------------------------------------------------------------------//
#pragma once

namespace celeritas
{
//---------------------------------------------------------------------------//
class CoreParams;
template<MemSpace M>
class CoreState;
class ExplicitCoreActionInterface;

// TODO: Add optical params and state

namespace detail
{
//---------------------------------------------------------------------------//
/*!
* Map Params class to the corresponding state and explicit action class
*/
template<typename T>
struct ParamsTraits;

template<>
struct ParamsTraits<CoreParams>
{
template<MemSpace M>
using State = CoreState<M>;

using ExplicitAction = ExplicitCoreActionInterface;
};

// TODO: add explicit template instantiation of ParamsTraits for optical data

//---------------------------------------------------------------------------//
} // namespace detail
} // namespace celeritas
2 changes: 1 addition & 1 deletion src/celeritas/phys/Model.hh
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace celeritas
* This class is similar to Geant4's G4VContinuousDiscrete process, but more
* limited.
*/
class Model : public ExplicitActionInterface
class Model : public ExplicitCoreActionInterface
{
public:
//@{
Expand Down
2 changes: 1 addition & 1 deletion src/celeritas/phys/detail/DiscreteSelectAction.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace detail
/*!
* Select a model for tracks undergoing a discrete interaction.
*/
class DiscreteSelectAction final : public ExplicitActionInterface,
class DiscreteSelectAction final : public ExplicitCoreActionInterface,
public ConcreteAction
{
public:
Expand Down
2 changes: 1 addition & 1 deletion src/celeritas/phys/detail/PreStepAction.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace detail
* - Reset track properties (todo: move to track initialization?)
* - Sample the mean free path and calculate the physics step limits.
*/
class PreStepAction final : public ExplicitActionInterface,
class PreStepAction final : public ExplicitCoreActionInterface,
public ConcreteAction
{
public:
Expand Down
2 changes: 1 addition & 1 deletion src/celeritas/track/ExtendFromPrimariesAction.hh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct Primary;
* This will append to the queued TrackInitializer vector any primaries added
* with \c CoreState::insert_primaries .
*/
class ExtendFromPrimariesAction final : public ExplicitActionInterface
class ExtendFromPrimariesAction final : public ExplicitCoreActionInterface
{
public:
//! Construct with explicit Id
Expand Down
9 changes: 8 additions & 1 deletion src/celeritas/track/ExtendFromSecondariesAction.hh
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,16 @@ namespace celeritas
\endverbatim
*/
class ExtendFromSecondariesAction final : public ExplicitActionInterface,
class ExtendFromSecondariesAction final : public ExplicitCoreActionInterface,
public BeginRunActionInterface
{
public:
//@{
//! \name Type aliases
using ExplicitCoreActionInterface::CoreStateDevice;
using ExplicitCoreActionInterface::CoreStateHost;
//@}

public:
//! Construct with explicit Id
explicit ExtendFromSecondariesAction(ActionId id) : id_(id) {}
Expand Down
2 changes: 1 addition & 1 deletion src/celeritas/track/InitializeTracksAction.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace celeritas
* filled by any track initializers remaining from previous steps using the
* position.
*/
class InitializeTracksAction final : public ExplicitActionInterface
class InitializeTracksAction final : public ExplicitCoreActionInterface
{
public:
//! Construct with explicit Id
Expand Down
9 changes: 8 additions & 1 deletion src/celeritas/track/SortTracksAction.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@ namespace celeritas
* automatically determined by TrackOrder. This should not have any impact on
* simulation output: it is only useful for accelerator optimizations.
*/
class SortTracksAction final : public ExplicitActionInterface,
class SortTracksAction final : public ExplicitCoreActionInterface,
public BeginRunActionInterface
{
public:
//@{
//! \name Type aliases
using ExplicitCoreActionInterface::CoreStateDevice;
using ExplicitCoreActionInterface::CoreStateHost;
//@}

public:
// Construct with action ID and sort criteria
SortTracksAction(ActionId id, TrackOrder track_order);
Expand Down
9 changes: 8 additions & 1 deletion src/celeritas/user/ActionDiagnostic.hh
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@ class ActionRegistry;
/*!
* Tally post-step actions for each particle type.
*/
class ActionDiagnostic final : public ExplicitActionInterface,
class ActionDiagnostic final : public ExplicitCoreActionInterface,
public BeginRunActionInterface,
public OutputInterface
{
public:
//@{
//! \name Type aliases
using ExplicitCoreActionInterface::CoreStateDevice;
using ExplicitCoreActionInterface::CoreStateHost;
//@}

public:
//!@{
//! \name Type aliases
Expand Down

0 comments on commit 6c2a07a

Please sign in to comment.