Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:21:12

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 accel/LocalTransporter.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <memory>
0010 #include <string>
0011 #include <unordered_map>
0012 #include <vector>
0013 
0014 #include "corecel/Types.hh"
0015 #include "corecel/io/Logger.hh"
0016 #include "geocel/BoundingBox.hh"
0017 #include "celeritas/Types.hh"
0018 #include "celeritas/phys/Primary.hh"
0019 
0020 class G4Track;
0021 class G4EventManager;
0022 
0023 namespace celeritas
0024 {
0025 //---------------------------------------------------------------------------//
0026 namespace detail
0027 {
0028 class HitProcessor;
0029 class OffloadWriter;
0030 }  // namespace detail
0031 
0032 struct SetupOptions;
0033 class SharedParams;
0034 class ParticleParams;
0035 class CoreStateInterface;
0036 class StepperInterface;
0037 
0038 //---------------------------------------------------------------------------//
0039 /*!
0040  * Manage offloading of tracks to Celeritas.
0041  *
0042  * This class \em must be constructed locally on each worker
0043  * thread/task/stream, usually as a shared pointer that's accessible to:
0044  * - a run action (for initialization),
0045  * - an event action (to set the event ID and flush offloaded tracks at the end
0046  *   of the event)
0047  * - a tracking action (to try offloading every track)
0048  *
0049  * \warning Due to Geant4 thread-local allocators, this class \em must be
0050  * finalized or destroyed on the same CPU thread in which is created and used!
0051  *
0052  * \todo Rename \c LocalOffload or something?
0053  */
0054 class LocalTransporter
0055 {
0056   public:
0057     //!@{
0058     //! \name Type aliases
0059     using MapStrReal = std::unordered_map<std::string, real_type>;
0060     //!@}
0061 
0062   public:
0063     // Construct in an invalid state
0064     LocalTransporter() = default;
0065 
0066     // Initialized with shared (across threads) params
0067     LocalTransporter(SetupOptions const& options, SharedParams& params);
0068 
0069     // Alternative to construction + move assignment
0070     inline void Initialize(SetupOptions const& options, SharedParams& params);
0071 
0072     // Set the event ID and reseed the Celeritas RNG (remove in v0.6)
0073     [[deprecated]] void SetEventId(int id) { this->InitializeEvent(id); }
0074 
0075     // Set the event ID and reseed the Celeritas RNG at the start of an event
0076     void InitializeEvent(int);
0077 
0078     // Offload this track
0079     void Push(G4Track const&);
0080 
0081     // Transport all buffered tracks to completion
0082     void Flush();
0083 
0084     // Clear local data and return to an invalid state
0085     void Finalize();
0086 
0087     // Get accumulated action times
0088     MapStrReal GetActionTime() const;
0089 
0090     // Number of buffered tracks
0091     size_type GetBufferSize() const { return buffer_.size(); }
0092 
0093     // Access core state data for user diagnostics
0094     CoreStateInterface const& GetState() const;
0095 
0096     // Access core state data for user diagnostics
0097     CoreStateInterface& GetState();
0098 
0099     //! Whether the class instance is initialized
0100     explicit operator bool() const { return static_cast<bool>(step_); }
0101 
0102   private:
0103     //// TYPES ////
0104 
0105     using SPOffloadWriter = std::shared_ptr<detail::OffloadWriter>;
0106     using BBox = BoundingBox<double>;
0107 
0108     struct BufferAccum
0109     {
0110         double energy{0};  // MeV
0111         double lost_energy{0};  // MeV
0112         std::size_t lost_primaries{0};
0113     };
0114 
0115     struct RunAccum
0116     {
0117         std::size_t events{0};
0118         std::size_t primaries{0};
0119         std::size_t steps{0};
0120         std::size_t lost_primaries{0};
0121         std::size_t hits{0};
0122     };
0123 
0124     //// DATA ////
0125 
0126     std::shared_ptr<ParticleParams const> particles_;
0127     BBox bbox_;
0128 
0129     // Thread-local data
0130     std::shared_ptr<StepperInterface> step_;
0131     std::vector<Primary> buffer_;
0132     std::shared_ptr<detail::HitProcessor> hit_processor_;
0133 
0134     // Current event ID or manager for obtaining it
0135     UniqueEventId event_id_;
0136     G4EventManager* event_manager_{nullptr};
0137 
0138     size_type auto_flush_{};
0139     size_type max_step_iters_{};
0140 
0141     BufferAccum buffer_accum_;
0142     RunAccum run_accum_;
0143 
0144     // Shared across threads to write flushed particles
0145     SPOffloadWriter dump_primaries_;
0146 };
0147 
0148 //---------------------------------------------------------------------------//
0149 /*!
0150  * Helper for making initialization more obvious from user code.
0151  *
0152  * This gives it some symmetry with Finalize, which is provided as an
0153  * exception-friendly destructor.
0154  */
0155 void LocalTransporter::Initialize(SetupOptions const& options,
0156                                   SharedParams& params)
0157 {
0158     *this = LocalTransporter(options, params);
0159 }
0160 
0161 //---------------------------------------------------------------------------//
0162 }  // namespace celeritas