Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-10-26 09:34:37

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 corecel/random/data/RanluxppRngData.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include "corecel/Assert.hh"
0010 #include "corecel/Macros.hh"
0011 #include "corecel/Types.hh"
0012 #include "corecel/data/Collection.hh"
0013 #include "corecel/random/data/RanluxppTypes.hh"
0014 
0015 namespace celeritas
0016 {
0017 
0018 //---------------------------------------------------------------------------//
0019 /*!
0020  * Persistent trivially copiable data for the Ranluxpp random number generator.
0021  */
0022 struct RanluxppRngParamsDataImpl
0023 {
0024     //// DATA ////
0025 
0026     //! User-provided seed
0027     RanluxppUInt seed = 0;
0028 
0029     //! Maximum bit position in the state
0030     static constexpr int max_position = sizeof(RanluxppArray9) * 8;
0031 
0032     //! Stores \f$a^2048 mod m\f$ for RCARRY values of \f$a\f$ and \f$m\f$.
0033     RanluxppArray9 advance_state;
0034 
0035     //! Stores \f$a^(2048 * (2^96)) mod m\f$
0036     RanluxppArray9 advance_sequence;
0037 
0038     //// FUNCTIONS ////
0039 
0040     //! Whether the data is assigned.
0041     explicit CELER_CONSTEXPR_FUNCTION operator bool() const
0042     {
0043         return advance_state[0] != 0 && advance_sequence[0] != 0;
0044     }
0045 };
0046 
0047 template<Ownership W, MemSpace M>
0048 struct RanluxppRngParamsData : RanluxppRngParamsDataImpl
0049 {
0050     //! Assign from another set of data.
0051     template<Ownership W2, MemSpace M2>
0052     RanluxppRngParamsData& operator=(RanluxppRngParamsData<W2, M2> const& other)
0053     {
0054         CELER_EXPECT(other);
0055         static_cast<RanluxppRngParamsDataImpl&>(*this) = other;
0056         return *this;
0057     }
0058 };
0059 
0060 //---------------------------------------------------------------------------//
0061 /*!
0062  * Individual RNG state for Ranluxpp.
0063  */
0064 struct RanluxppRngState
0065 {
0066     //// DATA ////
0067     //! Ranluxpp state number and carry bit
0068     RanluxppNumber value;
0069 
0070     //! Current position in the state.
0071     int position;
0072 };
0073 
0074 //---------------------------------------------------------------------------//
0075 /*!
0076  * Initializes an RNG state.
0077  */
0078 struct RanluxppRngStateInitializer
0079 {
0080     //! Ranluxpp state number and carry bit
0081     RanluxppNumber value;
0082 };
0083 
0084 //---------------------------------------------------------------------------//
0085 /*!
0086  * Initializer object for the Ranluxpp engine
0087  */
0088 struct RanluxppInitializer
0089 {
0090     //! Seed
0091     RanluxppUInt seed{0ull};
0092 
0093     //! Thread-local id.
0094     RanluxppUInt subsequence{0ull};
0095 
0096     //! Offset into rng stream
0097     RanluxppUInt offset{0ull};
0098 };
0099 
0100 //---------------------------------------------------------------------------//
0101 /*!
0102  * State data for Ranluxpp generator.
0103  */
0104 template<Ownership W, MemSpace M>
0105 struct RanluxppRngStateData
0106 {
0107     //// TYPES ////
0108     template<class T>
0109     using StateItems = StateCollection<T, W, M>;
0110 
0111     //// DATA ////
0112 
0113     StateItems<RanluxppRngState> state;
0114 
0115     //// METHODS ////
0116     //! True if assigned.
0117     explicit CELER_FUNCTION operator bool() const { return !state.empty(); }
0118 
0119     //! State size.
0120     CELER_FUNCTION size_type size() const { return state.size(); }
0121 
0122     //! Assign from another set of states.
0123     template<Ownership W2, MemSpace M2>
0124     RanluxppRngStateData& operator=(RanluxppRngStateData<W2, M2>& other)
0125     {
0126         CELER_EXPECT(other);
0127         state = other.state;
0128         return *this;
0129     }
0130 };
0131 
0132 //---------------------------------------------------------------------------//
0133 // Resize and seed the RNG states
0134 template<MemSpace M>
0135 void resize(RanluxppRngStateData<Ownership::value, M>* state,
0136             HostCRef<RanluxppRngParamsData> const& params,
0137             StreamId stream,
0138             size_type size);
0139 
0140 //---------------------------------------------------------------------------//
0141 }  // namespace celeritas