Back to home page

EIC code displayed by LXR

 
 

    


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

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/optical/gen/detail/WlsGeneratorExecutor.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "corecel/Macros.hh"
0010 #include "corecel/Types.hh"
0011 #include "corecel/math/Algorithms.hh"
0012 #include "celeritas/optical/CoreTrackView.hh"
0013 #include "celeritas/optical/WavelengthShiftData.hh"
0014 #include "celeritas/optical/gen/WavelengthShiftGenerator.hh"
0015 #include "celeritas/track/CoreStateCounters.hh"
0016 #include "celeritas/track/Utils.hh"
0017 
0018 #include "GeneratorAlgorithms.hh"
0019 
0020 namespace celeritas
0021 {
0022 namespace optical
0023 {
0024 namespace detail
0025 {
0026 //---------------------------------------------------------------------------//
0027 // LAUNCHER
0028 //---------------------------------------------------------------------------//
0029 /*!
0030  * Generate WLS photons from optical distribution data.
0031  */
0032 struct WlsGeneratorExecutor
0033 {
0034     //// DATA ////
0035 
0036     CRefPtr<CoreParamsData, MemSpace::native> params;
0037     RefPtr<CoreStateData, MemSpace::native> state;
0038     NativeCRef<WavelengthShiftData> wls;
0039     NativeCRef<WavelengthShiftData> wls2;
0040     NativeRef<WlsGeneratorStateData> data;
0041     size_type buffer_size{};
0042 
0043     //// FUNCTIONS ////
0044 
0045     // Generate optical photons
0046     inline CELER_FUNCTION void operator()(ThreadId tid) const;
0047 };
0048 
0049 //---------------------------------------------------------------------------//
0050 // INLINE DEFINITIONS
0051 //---------------------------------------------------------------------------//
0052 /*!
0053  * Generate WLS photons from optical distribution data.
0054  */
0055 CELER_FUNCTION void WlsGeneratorExecutor::operator()(ThreadId tid) const
0056 {
0057     CELER_EXPECT(state);
0058     CELER_EXPECT(data);
0059 
0060     using DistId = ItemId<WlsDistributionData>;
0061 
0062     auto* counters = state->init.counters.data().get();
0063 
0064     // Original code set the number of threads to the minimum between of number
0065     // of vacancies and the number of pending in the auxiliary state. To avoid
0066     // accessing the state counters to compute this min, we skip the extra
0067     // threads if state.counters.num_vacancies < aux_state.counters.num_pending
0068     if (!(tid < counters->num_vacancies))
0069     {
0070         return;
0071     }
0072     // Get the cumulative sum of the number of photons in the
0073     // distributions. The values are used to determine which threads will
0074     // generate from the corresponding distribution
0075     auto offsets = data.offsets[ItemRange<size_type>(
0076         ItemId<size_type>(0), ItemId<size_type>(buffer_size))];
0077 
0078     // Find the distribution this thread will generate from
0079     size_type dist_idx = find_distribution_index(offsets, tid.get());
0080     CELER_ASSERT(dist_idx < data.distributions.size());
0081     auto& dist = data.distributions[DistId(dist_idx)];
0082     CELER_ASSERT(dist);
0083 
0084     // Create the view to the new track to be initialized
0085     CoreTrackView vacancy{
0086         *params, *state, [&] {
0087             // Get the vacancy from the back in case there
0088             // are more vacancies than photons to generate
0089             TrackSlotId idx{
0090                 index_before(counters->num_vacancies, ThreadId(tid.get()))};
0091             return state->init.vacancies[idx];
0092         }()};
0093 
0094     // Generate one track from the distribution
0095     auto rng = vacancy.rng();
0096     if (dist.type == GeneratorType::wls)
0097     {
0098         CELER_ASSERT(wls);
0099         vacancy = WavelengthShiftGenerator(wls, dist)(rng);
0100     }
0101     else
0102     {
0103         CELER_ASSERT(wls2);
0104         vacancy = WavelengthShiftGenerator(wls2, dist)(rng);
0105     }
0106 
0107     // Update the number of photons left to generate
0108     atomic_add(&dist.num_photons, size_type(-1));
0109 }
0110 
0111 //---------------------------------------------------------------------------//
0112 }  // namespace detail
0113 }  // namespace optical
0114 }  // namespace celeritas