Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-13 08:53:54

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