Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-27 10:26:36

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 celeritas/track/detail/CounterExecutors.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <type_traits>
0010 
0011 #include "corecel/Macros.hh"
0012 #include "corecel/Types.hh"
0013 #include "corecel/data/ObserverPtr.hh"
0014 #include "corecel/math/Algorithms.hh"
0015 #include "corecel/sys/ThreadId.hh"
0016 
0017 #include "../CoreStateCounters.hh"
0018 
0019 namespace celeritas
0020 {
0021 namespace detail
0022 {
0023 //---------------------------------------------------------------------------//
0024 /*!
0025  * Initialize the num_generated, num_cut, num_errored and num_pending counters.
0026  *
0027  * This is called for the core loop immediately before a step iteration is
0028  * started in Stepper<M>::async() .
0029  *
0030  * \todo Instead of manually calling there, should it be part of the pre-init
0031  * action?
0032  *
0033  * \todo For the main EM loop, these are reset once per *step*. For the optical
0034  * loop, they are reset once per *transport*.
0035  */
0036 struct InitializeCountersExecutor
0037 {
0038     //// DATA ////
0039 
0040     ObserverPtr<CoreStateCounters, MemSpace::native> counters;
0041     size_type num_pending{};
0042 
0043     //// FUNCTIONS ////
0044 
0045     CELER_FORCEINLINE_FUNCTION void operator()(ThreadId tid) const
0046     {
0047         CELER_EXPECT(tid == ThreadId{0});
0048 
0049         counters->num_generated = 0;
0050         counters->num_cut = 0;
0051         counters->num_errored = 0;
0052         counters->num_pending = num_pending;
0053     }
0054 };
0055 
0056 //---------------------------------------------------------------------------//
0057 /*!
0058  * Add to the num_pending counter based on photons from buffered
0059  * optical distribution data.
0060  *
0061  * This is a \em thread executor suitable for \c KernelLauncher or \c
0062  * launch_kernel with a \c num_threads value of 1.
0063  *
0064  * \note This is templated on the counter type (allowed to be integer *or*
0065  * pointer to integer) to allow the counter value to live on the device rather
0066  * than being copied through the kernel launch.
0067  */
0068 template<typename CounterType>
0069 struct AddPendingExecutor
0070 {
0071     //// DATA ////
0072 
0073     ObserverPtr<CoreStateCounters, MemSpace::native> counters;
0074     CounterType num_photons;
0075 
0076     //// FUNCTIONS ////
0077 
0078     // Add number of primaries waiting to be generated
0079     CELER_FORCEINLINE_FUNCTION void operator()(ThreadId tid) const
0080     {
0081         CELER_EXPECT(tid == ThreadId{0});
0082 
0083         size_type temp_num_photons{};
0084         if constexpr (std::is_integral_v<CounterType>)
0085         {
0086             // Copied via kernel launch
0087             temp_num_photons = num_photons;
0088         }
0089         else
0090         {
0091             // Lives elsewhere in this memspace
0092             temp_num_photons = *num_photons;
0093         }
0094         counters->num_pending += temp_num_photons;
0095     }
0096 };
0097 
0098 //---------------------------------------------------------------------------//
0099 /*!
0100  * Update track initializer counters after processing primaries.
0101  *
0102  * This uses a primary count sent from the host via the kernel launch, so the
0103  * number of primaries must be positive.
0104  */
0105 struct AddPrimariesCountExecutor
0106 {
0107     //// DATA ////
0108 
0109     ObserverPtr<CoreStateCounters, MemSpace::native> counters;
0110     size_type num_primaries{};
0111 
0112     //// FUNCTIONS ////
0113 
0114     CELER_FORCEINLINE_FUNCTION void operator()(ThreadId tid) const
0115     {
0116         CELER_EXPECT(tid == ThreadId{0});
0117         CELER_EXPECT(num_primaries > 0);
0118 
0119         counters->num_initializers += num_primaries;
0120         counters->num_generated += num_primaries;
0121         counters->num_pending = 0;
0122     }
0123 };
0124 
0125 //---------------------------------------------------------------------------//
0126 /*!
0127  * Update num_active state counter based on the number of vacancies.
0128  */
0129 struct UpdateActiveExecutor
0130 {
0131     //// DATA ////
0132 
0133     ObserverPtr<CoreStateCounters, MemSpace::native> counters;
0134     size_type state_size{};
0135 
0136     //// FUNCTIONS ////
0137 
0138     CELER_FORCEINLINE_FUNCTION void operator()(ThreadId tid) const
0139     {
0140         CELER_EXPECT(tid == ThreadId{0});  // single thread kernel
0141         CELER_EXPECT(state_size >= counters->num_vacancies);
0142 
0143         size_type num_new_tracks
0144             = min(counters->num_vacancies, counters->num_initializers);
0145 
0146         // Update initializers/vacancies
0147         counters->num_initializers -= num_new_tracks;
0148         counters->num_vacancies -= num_new_tracks;
0149 
0150         // Store number of active tracks at the start of the loop
0151         counters->num_active = state_size - counters->num_vacancies;
0152 
0153         CELER_ENSURE(counters->num_active <= state_size);
0154     }
0155 };
0156 
0157 //---------------------------------------------------------------------------//
0158 /*!
0159  * Update the num_alive counter based on the number of photons that are still
0160  * alive after compacting vacancies.
0161  *
0162  * (Note this is only used in optical photon loop, not core loop.)
0163  */
0164 struct UpdateAliveExecutor
0165 {
0166     //// DATA ////
0167 
0168     ObserverPtr<CoreStateCounters, MemSpace::native> counters;
0169     size_type state_size;
0170 
0171     //// FUNCTIONS ////
0172 
0173     // Update number of photons that are still alive
0174     CELER_FORCEINLINE_FUNCTION void operator()(ThreadId tid) const
0175     {
0176         CELER_EXPECT(tid == ThreadId{0});  // single thread kernel
0177         CELER_EXPECT(state_size >= counters->num_vacancies);
0178 
0179         counters->num_alive = state_size - counters->num_vacancies;
0180     }
0181 };
0182 
0183 //---------------------------------------------------------------------------//
0184 /*!
0185  * Update the num_secondaries and num_initializers and then update the number
0186  * of alive tracks to include these secondaries.
0187  *
0188  * The last entry in the secondary counts array holds the exclusive sum, which
0189  * is the number of secondaries.
0190  */
0191 struct UpdateSecondariesCountExecutor
0192 {
0193     //// DATA ////
0194 
0195     ObserverPtr<CoreStateCounters, MemSpace::native> counters;
0196     ObserverPtr<size_type, MemSpace::native> secondary_count;
0197     size_type state_size{};
0198 
0199     //// FUNCTIONS ////
0200 
0201     CELER_FORCEINLINE_FUNCTION void operator()(ThreadId tid) const
0202     {
0203         CELER_EXPECT(tid == ThreadId{0});  // single thread kernel
0204         CELER_EXPECT(secondary_count);
0205         CELER_EXPECT(state_size >= counters->num_vacancies);
0206 
0207         size_type num_secondaries = *secondary_count;
0208         counters->num_secondaries = num_secondaries;
0209         counters->num_initializers += num_secondaries;
0210         counters->num_alive = state_size - counters->num_vacancies;
0211     }
0212 };
0213 
0214 //---------------------------------------------------------------------------//
0215 }  // namespace detail
0216 }  // namespace celeritas