Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 09:29:26

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