Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:59:33

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