Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:59:57

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 geocel/g4/detail/GeantGeoNavCollection.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <memory>
0010 #include <vector>
0011 
0012 #include "corecel/Types.hh"
0013 #include "corecel/cont/Span.hh"
0014 #include "corecel/sys/ThreadId.hh"
0015 #include "geocel/GeantGeoUtils.hh"
0016 
0017 template<class>
0018 class G4ReferenceCountedHandle;
0019 
0020 namespace celeritas
0021 {
0022 namespace detail
0023 {
0024 //---------------------------------------------------------------------------//
0025 /*!
0026  * Collection-like container for managing Geant4 navigation states.
0027  *
0028  * The default is a null-op implementation. Overloads for the host memory space
0029  * are defined below.
0030  */
0031 template<Ownership W, MemSpace M>
0032 struct GeantGeoNavCollection
0033 {
0034     explicit CELER_FUNCTION operator bool() const { return false; }
0035     CELER_FUNCTION TrackSlotId::size_type size() const { return 0; }
0036     template<Ownership W2, MemSpace M2>
0037     CELER_FUNCTION GeantGeoNavCollection&
0038     operator=(GeantGeoNavCollection<W2, M2>&)
0039     {
0040         return *this;
0041     }
0042     CELER_FUNCTION void reset() { CELER_ASSERT_UNREACHABLE(); }
0043 };
0044 
0045 //---------------------------------------------------------------------------//
0046 
0047 template<class T>
0048 struct G4ExternDeleter
0049 {
0050     void operator()(T* ptr) noexcept;
0051 };
0052 
0053 //---------------------------------------------------------------------------//
0054 
0055 using GeantTouchableHandle = G4ReferenceCountedHandle<GeantTouchableBase>;
0056 using UPTouchHandle = std::unique_ptr<GeantTouchableHandle,
0057                                       G4ExternDeleter<GeantTouchableHandle>>;
0058 using UPNavigator = std::unique_ptr<G4Navigator, G4ExternDeleter<G4Navigator>>;
0059 
0060 //---------------------------------------------------------------------------//
0061 // HOST MEMSPACE
0062 //---------------------------------------------------------------------------//
0063 /*!
0064  * Manage navigation states in host memory.
0065  */
0066 template<>
0067 struct GeantGeoNavCollection<Ownership::value, MemSpace::host>
0068 {
0069     std::vector<UPTouchHandle> touch_handles;
0070     std::vector<UPNavigator> navigators;
0071 
0072     // Resize with a number of states on the given Geant4 thread ID
0073     void resize(size_type size, G4VPhysicalVolume* world, StreamId sid);
0074 
0075     //! State size
0076     CELER_FUNCTION TrackSlotId::size_type size() const
0077     {
0078         return touch_handles.size();
0079     }
0080 
0081     //! True if constructed properly
0082     explicit operator bool() const
0083     {
0084         return !touch_handles.empty()
0085                && navigators.size() == touch_handles.size();
0086     }
0087 
0088     // Clean up on the original thread, necessary for thread-local G4 alloc
0089     void reset() { CELER_ASSERT_UNREACHABLE(); }
0090 };
0091 
0092 //---------------------------------------------------------------------------//
0093 /*!
0094  * Reference a host-owned navigation state.
0095  */
0096 template<>
0097 struct GeantGeoNavCollection<Ownership::reference, MemSpace::host>
0098 {
0099     Span<UPTouchHandle> touch_handles;
0100     Span<UPNavigator> navigators;
0101 
0102     // Default constructors
0103     GeantGeoNavCollection() = default;
0104     GeantGeoNavCollection(GeantGeoNavCollection const&) = default;
0105 
0106     // Obtain reference from host memory
0107     GeantGeoNavCollection&
0108     operator=(GeantGeoNavCollection<Ownership::value, MemSpace::host>& other);
0109     // Default assignment
0110     GeantGeoNavCollection& operator=(GeantGeoNavCollection const&) = default;
0111 
0112     // Get the navigation state for a given track slot
0113     GeantTouchableHandle& touch_handle(TrackSlotId tid) const;
0114     // Get the navigation state for a given track slot
0115     G4Navigator& navigator(TrackSlotId tid) const;
0116 
0117     //! State size
0118     CELER_FUNCTION TrackSlotId::size_type size() const
0119     {
0120         return touch_handles.size();
0121     }
0122 
0123     //! True if constructed properly
0124     explicit operator bool() const
0125     {
0126         return !touch_handles.empty()
0127                && navigators.size() == touch_handles.size()
0128                && touch_handles.front() && navigators.front();
0129     }
0130 
0131     // Clean up on the original thread, necessary for thread-local G4 alloc
0132     void reset();
0133 };
0134 
0135 //---------------------------------------------------------------------------//
0136 }  // namespace detail
0137 }  // namespace celeritas