Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 10:31:30

0001 //---------------------------------*-C++-*-----------------------------------//
0002 // Copyright 2020-2024 UT-Battelle, LLC, and other Celeritas developers.
0003 // See the top-level COPYRIGHT file for details.
0004 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0005 //---------------------------------------------------------------------------//
0006 //! \file celeritas/random/CuHipRngEngine.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include "corecel/Assert.hh"
0011 #include "corecel/OpaqueId.hh"
0012 #include "corecel/sys/ThreadId.hh"
0013 
0014 #include "CuHipRngData.hh"
0015 #include "distribution/GenerateCanonical.hh"
0016 
0017 namespace celeritas
0018 {
0019 //---------------------------------------------------------------------------//
0020 /*!
0021  * Generate random data on device and host.
0022  *
0023  * The CuHipRngEngine uses a C++11-like interface to generate random data. The
0024  * sampling of uniform floating point data is done with specializations to the
0025  * GenerateCanonical class.
0026  */
0027 class CuHipRngEngine
0028 {
0029   public:
0030     //!@{
0031     //! \name Type aliases
0032     using result_type = unsigned int;
0033     using Initializer_t = CuHipRngInitializer;
0034     using ParamsRef = NativeCRef<CuHipRngParamsData>;
0035     using StateRef = NativeRef<CuHipRngStateData>;
0036     //!@}
0037 
0038   public:
0039     // Construct from state
0040     inline CELER_FUNCTION CuHipRngEngine(ParamsRef const& params,
0041                                          StateRef const& state,
0042                                          TrackSlotId tid);
0043 
0044     // Initialize state from seed
0045     inline CELER_FUNCTION CuHipRngEngine& operator=(Initializer_t const&);
0046 
0047     // Sample a random number
0048     inline CELER_FUNCTION result_type operator()();
0049 
0050   private:
0051     CuHipRngThreadState* state_;
0052 
0053     template<class Generator, class RealType>
0054     friend class GenerateCanonical;
0055 };
0056 
0057 //---------------------------------------------------------------------------//
0058 /*!
0059  * Specialization of GenerateCanonical for CuHipRngEngine, float
0060  */
0061 template<>
0062 class GenerateCanonical<CuHipRngEngine, float>
0063 {
0064   public:
0065     //!@{
0066     //! \name Type aliases
0067     using real_type = float;
0068     using result_type = real_type;
0069     //!@}
0070 
0071   public:
0072     // Sample a random number
0073     inline CELER_FUNCTION result_type operator()(CuHipRngEngine& rng);
0074 };
0075 
0076 //---------------------------------------------------------------------------//
0077 /*!
0078  * Specialization for CuHipRngEngine, double
0079  */
0080 template<>
0081 class GenerateCanonical<CuHipRngEngine, double>
0082 {
0083   public:
0084     //!@{
0085     //! \name Type aliases
0086     using real_type = double;
0087     using result_type = real_type;
0088     //!@}
0089 
0090   public:
0091     // Sample a random number
0092     inline CELER_FUNCTION result_type operator()(CuHipRngEngine& rng);
0093 };
0094 
0095 //---------------------------------------------------------------------------//
0096 // INLINE DEFINITIONS
0097 //---------------------------------------------------------------------------//
0098 /*!
0099  * Construct from state.
0100  */
0101 CELER_FUNCTION
0102 CuHipRngEngine::CuHipRngEngine(ParamsRef const&,
0103                                StateRef const& state,
0104                                TrackSlotId tid)
0105 {
0106     CELER_EXPECT(tid < state.rng.size());
0107     state_ = &state.rng[tid];
0108 }
0109 
0110 //---------------------------------------------------------------------------//
0111 /*!
0112  * Initialize the RNG engine with a seed value.
0113  */
0114 CELER_FUNCTION CuHipRngEngine& CuHipRngEngine::operator=(Initializer_t const& s)
0115 {
0116     CELER_RNG_PREFIX(rand_init)(s.seed, s.subsequence, s.offset, state_);
0117     return *this;
0118 }
0119 
0120 //---------------------------------------------------------------------------//
0121 /*!
0122  * Sample a random number.
0123  */
0124 CELER_FUNCTION auto CuHipRngEngine::operator()() -> result_type
0125 {
0126     return CELER_RNG_PREFIX(rand)(state_);
0127 }
0128 
0129 //---------------------------------------------------------------------------//
0130 /*!
0131  * Specialization for CuHipRngEngine (float).
0132  */
0133 CELER_FUNCTION float
0134 GenerateCanonical<CuHipRngEngine, float>::operator()(CuHipRngEngine& rng)
0135 {
0136     return CELER_RNG_PREFIX(rand_uniform)(rng.state_);
0137 }
0138 
0139 //---------------------------------------------------------------------------//
0140 /*!
0141  * Specialization for CuHipRngEngine (double).
0142  */
0143 CELER_FUNCTION double
0144 GenerateCanonical<CuHipRngEngine, double>::operator()(CuHipRngEngine& rng)
0145 {
0146     return CELER_RNG_PREFIX(rand_uniform_double)(rng.state_);
0147 }
0148 
0149 //---------------------------------------------------------------------------//
0150 }  // namespace celeritas