Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-14 08:50:58

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/XorwowRngData.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <cstdint>
0010 
0011 #include "corecel/Assert.hh"
0012 #include "corecel/Macros.hh"
0013 #include "corecel/Types.hh"
0014 #include "corecel/cont/Array.hh"
0015 #include "corecel/cont/Span.hh"
0016 #include "corecel/data/Collection.hh"
0017 
0018 namespace celeritas
0019 {
0020 //---------------------------------------------------------------------------//
0021 //! 32-bit unsigned integer type for xorwow
0022 using XorwowUInt = std::uint32_t;
0023 //! Seed type used to generate initial states for the RNG
0024 using XorwowSeed = Array<XorwowUInt, 1>;
0025 
0026 //---------------------------------------------------------------------------//
0027 /*!
0028  * Persistent data for XORWOW generator.
0029  */
0030 template<Ownership W, MemSpace M>
0031 struct XorwowRngParamsData
0032 {
0033     //// TYPES ////
0034 
0035     using JumpPoly = Array<XorwowUInt, 5>;
0036     using ArrayJumpPoly = Array<JumpPoly, 32>;
0037 
0038     //// DATA ////
0039 
0040     //! \todo Use full 256-bit seed to generate initial states for the RNGs
0041     //! For now, just 4 bytes (same as our existing cuda/hip interface)
0042     XorwowSeed seed;
0043 
0044     // Jump polynomials
0045     ArrayJumpPoly jump;
0046     ArrayJumpPoly jump_subsequence;
0047 
0048     //// METHODS ////
0049 
0050     static CELER_CONSTEXPR_FUNCTION size_type num_words()
0051     {
0052         return JumpPoly{}.size();
0053     }
0054     static CELER_CONSTEXPR_FUNCTION size_type num_bits()
0055     {
0056         return 8 * sizeof(XorwowUInt);
0057     }
0058 
0059     //! Whether the data is assigned
0060     explicit CELER_FUNCTION operator bool() const { return true; }
0061 
0062     //! Assign from another set of data
0063     template<Ownership W2, MemSpace M2>
0064     XorwowRngParamsData& operator=(XorwowRngParamsData<W2, M2> const& other)
0065     {
0066         CELER_EXPECT(other);
0067         seed = other.seed;
0068         jump = other.jump;
0069         jump_subsequence = other.jump_subsequence;
0070         return *this;
0071     }
0072 };
0073 
0074 //---------------------------------------------------------------------------//
0075 /*!
0076  * Initialize an RNG.
0077  */
0078 struct XorwowRngInitializer
0079 {
0080     Array<unsigned int, 1> seed{0};
0081     ull_int subsequence{0};
0082     ull_int offset{0};
0083 };
0084 
0085 //---------------------------------------------------------------------------//
0086 //! Individual RNG state
0087 struct XorwowState
0088 {
0089     Array<XorwowUInt, 5> xorstate;  //!< x, y, z, w, v
0090     XorwowUInt weylstate;  //!< d
0091 };
0092 
0093 //---------------------------------------------------------------------------//
0094 /*!
0095  * XORWOW generator states for all threads.
0096  */
0097 template<Ownership W, MemSpace M>
0098 struct XorwowRngStateData
0099 {
0100     //// TYPES ////
0101 
0102     template<class T>
0103     using Items = Collection<T, W, M>;
0104     template<class T>
0105     using StateItems = StateCollection<T, W, M>;
0106 
0107     //// DATA ////
0108 
0109     StateItems<XorwowState> state;  //!< Track state [track]
0110 
0111     //// METHODS ////
0112 
0113     //! True if assigned
0114     explicit CELER_FUNCTION operator bool() const { return !state.empty(); }
0115 
0116     //! State size
0117     CELER_FUNCTION size_type size() const { return state.size(); }
0118 
0119     //! Assign from another set of states
0120     template<Ownership W2, MemSpace M2>
0121     XorwowRngStateData& operator=(XorwowRngStateData<W2, M2>& other)
0122     {
0123         CELER_EXPECT(other);
0124         state = other.state;
0125         return *this;
0126     }
0127 };
0128 
0129 //---------------------------------------------------------------------------//
0130 // Initialize XORWOW states with well-distributed random data
0131 void initialize_xorwow(Span<XorwowState> state,
0132                        XorwowSeed const& seed,
0133                        StreamId stream);
0134 
0135 //---------------------------------------------------------------------------//
0136 // Resize and seed the RNG states
0137 template<MemSpace M>
0138 void resize(XorwowRngStateData<Ownership::value, M>* state,
0139             HostCRef<XorwowRngParamsData> const& params,
0140             StreamId stream,
0141             size_type size);
0142 
0143 //---------------------------------------------------------------------------//
0144 }  // namespace celeritas