Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-01-10 10:05:47

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/OpticalGenAlgorithms.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "corecel/Macros.hh"
0010 #include "corecel/Types.hh"
0011 #include "corecel/cont/Span.hh"
0012 #include "corecel/data/Collection.hh"
0013 #include "corecel/math/Algorithms.hh"
0014 
0015 #include "../GeneratorData.hh"
0016 
0017 namespace celeritas
0018 {
0019 namespace detail
0020 {
0021 //---------------------------------------------------------------------------//
0022 template<MemSpace M>
0023 using GeneratorDistributionRef
0024     = Collection<GeneratorDistributionData, Ownership::reference, M>;
0025 
0026 //---------------------------------------------------------------------------//
0027 struct IsInvalid
0028 {
0029     // Check if the distribution data is valid
0030     CELER_FUNCTION bool operator()(GeneratorDistributionData const& data) const
0031     {
0032         return !data;
0033     }
0034 };
0035 
0036 //---------------------------------------------------------------------------//
0037 /*!
0038  * Find the index of the distribution from which to generate the primary.
0039  *
0040  * This finds the index in offsets for which offsets[result - 1] <= value <
0041  * offsets[result].
0042  */
0043 inline CELER_FUNCTION size_type find_distribution_index(Span<size_type> offsets,
0044                                                         size_type value)
0045 {
0046     CELER_EXPECT(!offsets.empty());
0047 
0048     auto iter = celeritas::lower_bound(offsets.begin(), offsets.end(), value);
0049     CELER_ASSERT(iter != offsets.end());
0050 
0051     if (value == *iter)
0052     {
0053         ++iter;
0054     }
0055     return iter - offsets.begin();
0056 }
0057 
0058 //---------------------------------------------------------------------------//
0059 // Remove all invalid distributions from the buffer.
0060 size_type remove_if_invalid(GeneratorDistributionRef<MemSpace::host> const&,
0061                             size_type,
0062                             size_type,
0063                             StreamId);
0064 size_type remove_if_invalid(GeneratorDistributionRef<MemSpace::device> const&,
0065                             size_type,
0066                             size_type,
0067                             StreamId);
0068 
0069 //---------------------------------------------------------------------------//
0070 // Count the number of optical photons in the distributions.
0071 size_type count_num_photons(GeneratorDistributionRef<MemSpace::host> const&,
0072                             size_type,
0073                             size_type,
0074                             StreamId);
0075 size_type count_num_photons(GeneratorDistributionRef<MemSpace::device> const&,
0076                             size_type,
0077                             size_type,
0078                             StreamId);
0079 
0080 //---------------------------------------------------------------------------//
0081 // Calculate the inclusive prefix sum of the number of optical photons
0082 size_type inclusive_scan_photons(
0083     GeneratorDistributionRef<MemSpace::host> const&,
0084     Collection<size_type, Ownership::reference, MemSpace::host> const&,
0085     size_type,
0086     StreamId);
0087 size_type inclusive_scan_photons(
0088     GeneratorDistributionRef<MemSpace::device> const&,
0089     Collection<size_type, Ownership::reference, MemSpace::device> const&,
0090     size_type,
0091     StreamId);
0092 
0093 //---------------------------------------------------------------------------//
0094 // INLINE DEFINITIONS
0095 //---------------------------------------------------------------------------//
0096 #if !CELER_USE_DEVICE
0097 inline size_type
0098 remove_if_invalid(GeneratorDistributionRef<MemSpace::device> const&,
0099                   size_type,
0100                   size_type,
0101                   StreamId)
0102 {
0103     CELER_NOT_CONFIGURED("CUDA OR HIP");
0104 }
0105 
0106 inline size_type
0107 count_num_photons(GeneratorDistributionRef<MemSpace::device> const&,
0108                   size_type,
0109                   size_type,
0110                   StreamId)
0111 {
0112     CELER_NOT_CONFIGURED("CUDA OR HIP");
0113 }
0114 
0115 inline size_type inclusive_scan_photons(
0116     GeneratorDistributionRef<MemSpace::device> const&,
0117     Collection<size_type, Ownership::reference, MemSpace::device> const&,
0118     size_type,
0119     StreamId)
0120 {
0121     CELER_NOT_CONFIGURED("CUDA OR HIP");
0122 }
0123 #endif
0124 //---------------------------------------------------------------------------//
0125 }  // namespace detail
0126 }  // namespace celeritas