Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-10 07:49:40

0001 #pragma once
0002 
0003 struct sevent ;
0004 struct quad4 ;
0005 struct sphoton ;
0006 struct salloc ;
0007 struct sslice ;
0008 struct qat4 ;
0009 struct quad6 ;
0010 struct NP ;
0011 
0012 struct SEvt ;
0013 
0014 struct sphoton_selector ;
0015 struct sphotonlite_selector ;
0016 
0017 
0018 #include <vector>
0019 #include <string>
0020 #include "plog/Severity.h"
0021 #include "SComp.h"
0022 #include "QUDARAP_API_EXPORT.hh"
0023 #include "NP_future.h"
0024 
0025 /**
0026 QEvt.hh
0027 =======
0028 
0029 Canonical *event* instanciated within QSim::QSim
0030 
0031 Unlike typical CPU side event classes with many instances the QEvt/sevent is rather "static"
0032 and singular with long lived buffers of defined maximum capacity that get reused for each launch.
0033 
0034 * Note that CUDA has no realloc the old OContext::resizeBuffer is an OptiX < 7 extension.
0035   Hence decided to define maximum buffer sizes as calculated from max number of photons for each launch
0036   and arrange that lanches to never exceed those maximums
0037 
0038 * hence all GPU buffers can get allocated once at initialization
0039   with the configured maximum sizes and simply get reused from event to event
0040   (or more specifically from launch to launch which might not map one-to-one with events)
0041 
0042 * this simplifies memory handling as free-ing only needed at termination, so can get away with
0043   not doing it
0044 
0045 * so "resizing" just becomes changing a CPU/GPU side constant eg *num_photons*
0046 * just need to ensure that launches are always arranged to be below the max
0047 
0048 * how to decide the maximums ? depends on available VRAM and also should be user configurable
0049   within some range
0050 
0051 
0052 QEvt::setGenstep is the primary method for lifecycle understanding
0053 -----------------------------------------------------------------------
0054 
0055 When Opticks is integrated with a Geant4 based detector simulation framework
0056 this primary QEvt::setGenstep method is invoked with a stack like the below,
0057 where the upper part depends on details of how Opticks is integrated with the simulation framework::
0058 
0059     "G4VSensitiveDetector::EndOfEvent(G4HCofThisEvent* HCE)" (stub overridded by the below)
0060     junoSD_PMT_v2::EndOfEvent(G4HCofThisEvent* HCE)
0061     junoSD_PMT_v2_Opticks::EndOfEvent(G4HCofThisEvent*, int eventID )
0062     junoSD_PMT_v2_Opticks::EndOfEvent_Simulate(int eventID )
0063     G4CXOpticks::simulate(int eventID, bool reset_ )
0064     QSim::simulate(int eventID, bool reset_)
0065     QEvt::setGenstep
0066 
0067 
0068 **/
0069 
0070 
0071 
0072 struct QUDARAP_API QEvt : public SCompProvider
0073 {
0074     friend struct QEvtTest ;
0075     friend struct QEvt_setInputPhoton_Test ;
0076 
0077     static constexpr const char* QEvt__LIFECYCLE = "QEvt__LIFECYCLE" ;
0078     static bool LIFECYCLE ;
0079 
0080     static const plog::Severity LEVEL ;
0081     static QEvt* INSTANCE ;
0082     static QEvt* Get();
0083     static const bool SEvt_NPFold_VERBOSE ;
0084     static std::string Desc();
0085 
0086 
0087     sevent* getDevicePtr() const ;
0088 
0089     QEvt();
0090 
0091 private:
0092     void init();
0093     void init_SEvt();
0094 
0095     // NB members needed on both CPU+GPU or from the QEvt.cu functions
0096     // should reside inside the sevent.h instance not up here in QEvt.hh
0097 
0098 public:
0099     SEvt*             sev ;
0100 private:
0101     sphoton_selector*     photon_selector ;
0102     sphotonlite_selector* photonlite_selector ;
0103 
0104     sevent*           evt ;
0105     sevent*           d_evt ;
0106     const NP*         gs ;
0107     sslice*           gss ;
0108 
0109     NP*               input_photon ;
0110 public:
0111     int               upload_count ;
0112 
0113     /**
0114     std::string       meta ;
0115     Q: IS THIS meta NEEDED ? SEvt HAS meta TOO ?
0116     A: YES, for now. The metadata gets collected in SEvt::gather_components
0117        via SCompProvider method QEvt::getMeta (OR SEvt::getMeta)
0118 
0119     A2: Dont need the meta, need the method that access the underlying SEvt.
0120     **/
0121 public:
0122     // PRIMARY ACTION OF QEvt : genstep uploading
0123     //int setGenstep();
0124     int setGenstepUpload_NP(const NP* gs);
0125     int setGenstepUpload_NP(const NP* gs,  const sslice* sl );
0126     unsigned long long get_photon_slot_offset() const ;
0127     void clear();
0128 private:
0129 
0130     int setGenstepUpload(const quad6* qq0, int num_gs );
0131     int setGenstepUpload(const quad6* qq0, int gs_start, int gs_stop );
0132     void device_alloc_genstep_and_seed();
0133     void setInputPhotonAndUpload();
0134     void setInputPhotonSimtraceAndUpload();
0135     void checkInputPhoton() const ;
0136 
0137     //int setGenstep(quad6* gs, unsigned num_gs );
0138     unsigned count_genstep_photons();
0139     void     fill_seed_buffer();
0140     void     count_genstep_photons_and_fill_seed_buffer();
0141 
0142 public:
0143     // who uses these ? TODO: switch to comp based
0144     bool hasGenstep() const ;
0145     bool hasSeed() const ;
0146     bool hasPhoton() const ;
0147     bool hasPhotonLite() const ;
0148     bool hasRecord() const ;
0149     bool hasRec() const ;
0150     bool hasSeq() const ;
0151     bool hasPrd() const ;
0152     bool hasTag() const ;
0153     bool hasFlat() const ;
0154     bool hasHit() const ;
0155     bool hasHitLite() const ;
0156     bool hasSimtrace() const ;
0157 public:
0158     static constexpr const char* TYPENAME = "QEvt" ;
0159     // SCompProvider methods
0160     std::string getMeta() const ;  // returns underlying (SEvt)sev->meta
0161     const char* getTypeName() const ;
0162     NP*      gatherComponent(unsigned comp) const ;
0163 public:
0164     // [ expedient getters : despite these coming from SEvt
0165     NP*      getGenstep() const ;
0166     NP*      getInputPhoton() const ;
0167     // ]
0168 
0169     NP*      gatherPhoton() const ;
0170     NP*      gatherPhotonLite() const ;
0171 
0172     NP*      gatherHit() const ;
0173     NP*      gatherHitLite() const ;
0174     NP*      gatherHitLiteMerged() const ;
0175     NP*      gatherHitMerged() const ;
0176 
0177 
0178 #ifndef PRODUCTION
0179     NP*      gatherSeed() const ;
0180     NP*      gatherDomain() const ;
0181     NP*      gatherGenstepFromDevice() const ;
0182     void     gatherSimtrace(     NP* t ) const ;
0183     NP*      gatherSimtrace() const ;
0184     void     gatherSeq(          NP* seq) const ;
0185     NP*      gatherSeq() const ;       // seqhis..
0186     NP*      gatherPrd() const ;
0187     NP*      gatherFlat() const ;
0188     NP*      gatherRecord() const ;    // full step records
0189     NP*      gatherTag() const ;
0190     NP*      gatherRec() const  ;      // compressed step record
0191 #endif
0192 
0193 public:
0194     void     gatherPhoton(       NP* p ) const ;
0195     void     gatherPhotonLite(   NP* l ) const ;
0196 
0197 private:
0198     NP*      gatherComponent_(unsigned comp) const ;
0199     NP*      gatherHit_() const ;
0200     NP*      gatherHitLite_() const ;
0201 public:
0202 
0203     template<typename T>
0204     static NP*       PerLaunchMerge(sevent* evt, cudaStream_t stream );
0205 
0206     template<typename T>
0207     static NP*       FinalMerge(      const NP* hitmerged_or_hitlitemerged, cudaStream_t stream);
0208 
0209     template<typename T>
0210     static NP_future FinalMerge_async(const NP* hitmerged_or_hitlitemerged, cudaStream_t stream);
0211 
0212 public:
0213     size_t   getNumHit() const ;
0214     size_t   getNumHitLite() const ;
0215 private:
0216     void     setNumPhoton(size_t num_photon) ;
0217     void     setNumSimtrace(size_t num_simtrace) ;
0218     void     device_alloc_photon();
0219     void     device_alloc_simtrace();
0220     static void SetAllocMeta(salloc* alloc, const sevent* evt);
0221     void     uploadEvt();
0222 public:
0223     size_t getNumPhoton() const ;
0224     size_t getNumSimtrace() const ;
0225 public:
0226     std::string desc() const ;
0227     std::string desc_alloc() const ;
0228 
0229     void checkEvt() ;  // GPU side
0230 
0231 };
0232 
0233