Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:53:46

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/random/detail/RngReseedExecutor.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "corecel/Types.hh"
0010 #include "corecel/random/data/RngData.hh"
0011 #include "corecel/random/engine/RngEngine.hh"
0012 #include "corecel/sys/ThreadId.hh"
0013 #include "celeritas/Types.hh"
0014 
0015 namespace celeritas
0016 {
0017 namespace detail
0018 {
0019 //---------------------------------------------------------------------------//
0020 /*!
0021  * Reinitialize a track's random state from a unique event ID.
0022  */
0023 class RngReseedExecutor
0024 {
0025   public:
0026     using ParamsCRef = NativeCRef<RngParamsData>;
0027     using StateRef = NativeRef<RngStateData>;
0028 
0029   public:
0030     // Construct with state and event ID
0031     inline CELER_FUNCTION
0032     RngReseedExecutor(ParamsCRef const&, StateRef const&, UniqueEventId id);
0033 
0034     // Initialize the given track slot
0035     inline CELER_FUNCTION void operator()(TrackSlotId tid) const;
0036 
0037     //! Initialize from the given thread
0038     CELER_FORCEINLINE_FUNCTION void operator()(ThreadId tid) const
0039     {
0040         return (*this)(TrackSlotId{tid.unchecked_get()});
0041     }
0042 
0043   private:
0044     ParamsCRef const params_;
0045     StateRef const state_;
0046     UniqueEventId::size_type stride_;
0047 };
0048 
0049 //---------------------------------------------------------------------------//
0050 // INLINE DEFINITIONS
0051 //---------------------------------------------------------------------------//
0052 /*!
0053  * Construct with state and event ID.
0054  */
0055 CELER_FUNCTION RngReseedExecutor::RngReseedExecutor(ParamsCRef const& params,
0056                                                     StateRef const& state,
0057                                                     UniqueEventId id)
0058     : params_{params}, state_{state}, stride_{id.unchecked_get() * state.size()}
0059 {
0060     CELER_EXPECT(params_ && state_);
0061     CELER_EXPECT(id);
0062     static_assert(sizeof(ull_int) == sizeof(UniqueEventId::size_type));
0063 }
0064 
0065 //---------------------------------------------------------------------------//
0066 /*!
0067  * Initialize the given track slot.
0068  */
0069 CELER_FUNCTION void RngReseedExecutor::operator()(TrackSlotId tid) const
0070 {
0071     CELER_EXPECT(tid < state_.size());
0072     RngEngine::Initializer_t init;
0073     init.seed = params_.seed;
0074     init.subsequence = stride_ + tid.unchecked_get();
0075 
0076     RngEngine engine(params_, state_, tid);
0077     engine = init;
0078 }
0079 
0080 //---------------------------------------------------------------------------//
0081 }  // namespace detail
0082 }  // namespace celeritas