Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-28 10:07:07

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file celeritas/global/TrackExecutor.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "corecel/Assert.hh"
0010 #include "corecel/Types.hh"
0011 #include "corecel/math/Algorithms.hh"
0012 #include "corecel/sys/ThreadId.hh"
0013 #include "celeritas/track/TrackFunctors.hh"
0014 
0015 #include "CoreTrackData.hh"
0016 #include "CoreTrackDataFwd.hh"
0017 #include "CoreTrackView.hh"
0018 
0019 namespace celeritas
0020 {
0021 //---------------------------------------------------------------------------//
0022 /*!
0023  * Call a \c CoreTrackView executor for a given ThreadId.
0024  *
0025  * This class can be used to call a functor that applies to \c CoreTrackView
0026  * using a \c ThreadId, so that the tracks can be easily looped over as a
0027  * group on CPU or GPU. It applies a remapping from \em thread to \em slot if
0028  * the tracks are sorted. Otherwise, thread and track slot have the same
0029  * numerical value.
0030  *
0031  * This is used primarily by \c ActionLauncher .
0032  *
0033  * \code
0034 void foo_kernel(CoreParamsPtr const params,
0035                 CoreStatePtr const state)
0036 {
0037     TrackExecutor execute{params, state, MyTrackApplier{}};
0038 
0039     for (auto tid : range(ThreadID{123}))
0040     {
0041         step(tid);
0042     }
0043 }
0044 \endcode
0045  *
0046  * \todo Rename to ThreadExecutor. The template parameter, which must operate
0047  * on a core track view, is a track executor.
0048  */
0049 template<class T>
0050 class TrackExecutor
0051 {
0052   public:
0053     //!@{
0054     //! \name Type aliases
0055     using ParamsPtr = CoreParamsPtr<MemSpace::native>;
0056     using StatePtr = CoreStatePtr<MemSpace::native>;
0057     using Applier = T;
0058     //!@}
0059 
0060   public:
0061     //! Construct with core data and executor
0062     CELER_FUNCTION TrackExecutor(
0063         ParamsPtr params, StatePtr state, T&& execute_track)
0064         : params_{params}
0065         , state_{state}
0066         , execute_track_{celeritas::forward<T>(execute_track)}
0067     {
0068     }
0069 
0070     //! Call the underlying function, using indirection array if needed
0071     CELER_FUNCTION void operator()(ThreadId thread)
0072     {
0073         CELER_EXPECT(thread < state_->size());
0074         CoreTrackView track(*params_, *state_, thread);
0075         return execute_track_(track);
0076     }
0077 
0078   private:
0079     ParamsPtr const params_;
0080     StatePtr const state_;
0081     T execute_track_;
0082 };
0083 
0084 //---------------------------------------------------------------------------//
0085 /*!
0086  * Launch the track only when a certain condition applies to the sim state.
0087  *
0088  * The condition \c C must have the signature \code
0089  * (SimTrackView const&) -> bool
0090   \endcode
0091  *
0092  * see \c make_active_track_executor for an example where this is used to apply
0093  * only to active (or killed) tracks.
0094  */
0095 template<class C, class T>
0096 class ConditionalTrackExecutor
0097 {
0098   public:
0099     //!@{
0100     //! \name Type aliases
0101     using ParamsPtr = CoreParamsPtr<MemSpace::native>;
0102     using StatePtr = CoreStatePtr<MemSpace::native>;
0103     using Applier = T;
0104     //!@}
0105 
0106   public:
0107     //! Construct with condition and operator
0108     CELER_FUNCTION ConditionalTrackExecutor(
0109         ParamsPtr params, StatePtr state, C&& applies, T&& execute_track)
0110         : params_{params}
0111         , state_{state}
0112         , applies_{celeritas::forward<C>(applies)}
0113         , execute_track_{celeritas::forward<T>(execute_track)}
0114     {
0115     }
0116 
0117     //! Launch the given thread if the track meets the condition
0118     CELER_FUNCTION void operator()(ThreadId thread)
0119     {
0120         CELER_EXPECT(thread < state_->size());
0121         CoreTrackView track(*params_, *state_, thread);
0122         if (!applies_(track))
0123         {
0124             return;
0125         }
0126 
0127         // NOTE: "return value type" error means the executor function is
0128         // incorrectly returning a value
0129         return execute_track_(track);
0130     }
0131 
0132   private:
0133     ParamsPtr const params_;
0134     StatePtr const state_;
0135     C applies_;
0136     T execute_track_;
0137 };
0138 
0139 //---------------------------------------------------------------------------//
0140 // DEDUCTION GUIDES
0141 //---------------------------------------------------------------------------//
0142 template<class T>
0143 CELER_CTAD_FUNCTION TrackExecutor(
0144     CoreParamsPtr<MemSpace::native>, CoreStatePtr<MemSpace::native>, T&&)
0145     -> TrackExecutor<T>;
0146 
0147 template<class C, class T>
0148 CELER_CTAD_FUNCTION ConditionalTrackExecutor(
0149     CoreParamsPtr<MemSpace::native>, CoreStatePtr<MemSpace::native>, C&&, T&&)
0150     -> ConditionalTrackExecutor<C, T>;
0151 
0152 //---------------------------------------------------------------------------//
0153 // FREE FUNCTIONS
0154 //---------------------------------------------------------------------------//
0155 /*!
0156  * Return a track executor that applies only to active, non-errored tracks.
0157  */
0158 template<class T>
0159 inline CELER_FUNCTION decltype(auto) make_active_track_executor(
0160     CoreParamsPtr<MemSpace::native> params,
0161     CoreStatePtr<MemSpace::native> const& state,
0162     T&& apply_track)
0163 {
0164     return ConditionalTrackExecutor{
0165         params, state, AppliesValid{}, celeritas::forward<T>(apply_track)};
0166 }
0167 
0168 //---------------------------------------------------------------------------//
0169 /*!
0170  * Return a track executor that only applies if the action ID matches.
0171  *
0172  * \note This should generally only be used for post-step actions and other
0173  * cases where the IDs *explicitly* are set. Many explicit actions apply to all
0174  * threads, active or not.
0175  */
0176 template<class T>
0177 inline CELER_FUNCTION decltype(auto) make_action_track_executor(
0178     CoreParamsPtr<MemSpace::native> params,
0179     CoreStatePtr<MemSpace::native> state,
0180     ActionId action,
0181     T&& apply_track)
0182 {
0183     CELER_EXPECT(action);
0184     return ConditionalTrackExecutor{params,
0185                                     state,
0186                                     IsStepActionEqual{action},
0187                                     celeritas::forward<T>(apply_track)};
0188 }
0189 
0190 //---------------------------------------------------------------------------//
0191 /*!
0192  * Return a track executor that only applies for the given along-step action.
0193  */
0194 template<class T>
0195 inline CELER_FUNCTION decltype(auto) make_along_step_track_executor(
0196     CoreParamsPtr<MemSpace::native> params,
0197     CoreStatePtr<MemSpace::native> state,
0198     ActionId action,
0199     T&& apply_track)
0200 {
0201     CELER_EXPECT(action);
0202     return ConditionalTrackExecutor{params,
0203                                     state,
0204                                     IsAlongStepActionEqual{action},
0205                                     celeritas::forward<T>(apply_track)};
0206 }
0207 
0208 //---------------------------------------------------------------------------//
0209 }  // namespace celeritas