Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 10:31:23

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2022-2024 UT-Battelle, LLC, and other Celeritas developers.
0003 // See the top-level COPYRIGHT file for details.
0004 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0005 //---------------------------------------------------------------------------//
0006 //! \file celeritas/global/Stepper.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <memory>
0011 #include <vector>
0012 
0013 #include "corecel/Types.hh"
0014 #include "corecel/cont/Span.hh"
0015 #include "corecel/data/CollectionStateStore.hh"
0016 #include "celeritas/Types.hh"
0017 #include "celeritas/geo/GeoFwd.hh"
0018 #include "celeritas/phys/Primary.hh"
0019 #include "celeritas/random/RngParamsFwd.hh"
0020 #include "celeritas/track/TrackInitData.hh"
0021 
0022 #include "CoreState.hh"
0023 #include "CoreTrackData.hh"
0024 
0025 namespace celeritas
0026 {
0027 //---------------------------------------------------------------------------//
0028 class CoreParams;
0029 struct Primary;
0030 class ExtendFromPrimariesAction;
0031 
0032 class ActionSequence;
0033 
0034 //---------------------------------------------------------------------------//
0035 /*!
0036  * State-specific options for the stepper.
0037  *
0038  * - \c params : Problem definition
0039  * - \c num_track_slots : Maximum number of threads to run in parallel on GPU
0040  *   \c stream_id : Unique (thread/task) ID for this process
0041  * - \c action_times : Whether to synchronize device between actions for timing
0042  */
0043 struct StepperInput
0044 {
0045     std::shared_ptr<CoreParams const> params;
0046     StreamId stream_id{};
0047     size_type num_track_slots{};
0048     bool action_times{false};
0049 
0050     //! True if defined
0051     explicit operator bool() const
0052     {
0053         return params && stream_id && num_track_slots > 0;
0054     }
0055 };
0056 
0057 //---------------------------------------------------------------------------//
0058 /*!
0059  * Track counters for a step.
0060  */
0061 struct StepperResult
0062 {
0063     size_type generated{};  //!< New primaries added
0064     size_type queued{};  //!< Pending track initializers at end of step
0065     size_type active{};  //!< Active tracks at start of step
0066     size_type alive{};  //!< Active and alive at end of step
0067 
0068     //! True if more steps need to be run
0069     explicit operator bool() const { return queued > 0 || alive > 0; }
0070 };
0071 
0072 //---------------------------------------------------------------------------//
0073 /*!
0074  * Interface class for stepper classes.
0075  *
0076  * This allows higher-level classes not to care whether the stepper operates on
0077  * host or device.
0078  *
0079  * \note This class and its daughter may be removed soon to facilitate step
0080  * gathering.
0081  */
0082 class StepperInterface
0083 {
0084   public:
0085     //!@{
0086     //! \name Type aliases
0087     using Input = StepperInput;
0088     using ActionSequenceT = ActionSequence;
0089     using SpanConstPrimary = Span<Primary const>;
0090     using result_type = StepperResult;
0091     using SPState = std::shared_ptr<CoreStateInterface>;
0092     //!@}
0093 
0094   public:
0095     // Default virtual destructor
0096     virtual ~StepperInterface();
0097 
0098     // Warm up before stepping
0099     virtual void warm_up() = 0;
0100 
0101     // Transport existing states
0102     virtual StepperResult operator()() = 0;
0103 
0104     // Transport existing states and these new primaries
0105     virtual StepperResult operator()(SpanConstPrimary primaries) = 0;
0106 
0107     // Reseed the RNGs at the start of an event for reproducibility
0108     virtual void reseed(EventId event_id) = 0;
0109 
0110     //! Get action sequence for timing diagnostics
0111     virtual ActionSequenceT const& actions() const = 0;
0112 
0113     //! Get the core state interface
0114     virtual CoreStateInterface const& state() const = 0;
0115 
0116     //! Get a shared pointer to the state (TEMPORARY)
0117     virtual SPState sp_state() = 0;
0118 
0119   protected:
0120     StepperInterface() = default;
0121     CELER_DEFAULT_COPY_MOVE(StepperInterface);
0122 };
0123 
0124 //---------------------------------------------------------------------------//
0125 /*!
0126  * Manage a state vector and execute a single step on all of them.
0127  *
0128  * \note This is likely to be removed and refactored since we're changing how
0129  * primaries are created and how multithread state ownership is managed.
0130  *
0131  * \code
0132    Stepper<MemSpace::host> step(input);
0133 
0134    // Transport primaries for the initial step
0135    StepperResult alive_tracks = step(my_primaries);
0136    while (alive_tracks)
0137    {
0138        // Transport secondaries
0139        alive_tracks = step();
0140    }
0141    \endcode
0142  */
0143 template<MemSpace M>
0144 class Stepper final : public StepperInterface
0145 {
0146   public:
0147     //!@{
0148     //! \name Type aliases
0149     using StateRef = CoreStateData<Ownership::reference, M>;
0150     //!@}
0151 
0152   public:
0153     // Construct with problem parameters and setup options
0154     explicit Stepper(Input input);
0155 
0156     // Default destructor
0157     ~Stepper();
0158 
0159     // Warm up before stepping
0160     void warm_up() final;
0161 
0162     // Transport existing states
0163     StepperResult operator()() final;
0164 
0165     // Transport existing states and these new primaries
0166     StepperResult operator()(SpanConstPrimary primaries) final;
0167 
0168     // Reseed the RNGs at the start of an event for reproducibility
0169     void reseed(EventId event_id) final;
0170 
0171     //! Get action sequence for timing diagnostics
0172     ActionSequenceT const& actions() const final { return *actions_; }
0173 
0174     //! Access core data, primarily for debugging
0175     StateRef const& state_ref() const { return state_->ref(); }
0176 
0177     //! Get the core state interface for diagnostic output
0178     CoreStateInterface const& state() const final { return *state_; }
0179 
0180     //! Reset the core state counters and data so it can be reused
0181     void reset_state() { state_->reset(); }
0182 
0183     //! Get a shared pointer to the state (TEMPORARY, DO NOT USE)
0184     SPState sp_state() final { return state_; }
0185 
0186   private:
0187     // Params data
0188     std::shared_ptr<CoreParams const> params_;
0189     // Primary initialization
0190     std::shared_ptr<ExtendFromPrimariesAction const> primaries_action_;
0191     // State data
0192     std::shared_ptr<CoreState<M>> state_;
0193     // Call sequence
0194     std::shared_ptr<ActionSequenceT> actions_;
0195 };
0196 
0197 //---------------------------------------------------------------------------//
0198 // EXPLICIT INSTANTIATION
0199 //---------------------------------------------------------------------------//
0200 
0201 extern template class Stepper<MemSpace::host>;
0202 extern template class Stepper<MemSpace::device>;
0203 
0204 //---------------------------------------------------------------------------//
0205 }  // namespace celeritas