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/detail/CuHipRngStateInit.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/engine/CuHipRngEngine.hh"
0014 
0015 #include "../CuHipRngData.hh"
0016 
0017 namespace celeritas
0018 {
0019 namespace detail
0020 {
0021 //---------------------------------------------------------------------------//
0022 template<Ownership W, MemSpace M>
0023 struct CuHipRngInitData
0024 {
0025     StateCollection<ull_int, W, M> seeds;
0026 
0027     //// METHODS ////
0028 
0029     //! True if assigned
0030     explicit CELER_FUNCTION operator bool() const { return !seeds.empty(); }
0031 
0032     //! State size
0033     CELER_FUNCTION size_type size() const { return seeds.size(); }
0034 
0035     //! Assign from another set of data
0036     template<Ownership W2, MemSpace M2>
0037     CuHipRngInitData& operator=(CuHipRngInitData<W2, M2> const& other)
0038     {
0039         CELER_EXPECT(other);
0040         seeds = other.seeds;
0041         return *this;
0042     }
0043 };
0044 
0045 //---------------------------------------------------------------------------//
0046 /*!
0047  * Initialize the given track slot.
0048  */
0049 struct RngSeedExecutor
0050 {
0051     NativeCRef<CuHipRngParamsData> const params;
0052     NativeRef<CuHipRngStateData> const state;
0053     NativeCRef<CuHipRngInitData> const seeds;
0054 
0055     //! Initialize the given track slot
0056     inline CELER_FUNCTION void operator()(TrackSlotId tid) const
0057     {
0058         CELER_EXPECT(tid < state.size());
0059         CuHipRngInitializer init;
0060         init.seed = seeds.seeds[tid];
0061         CuHipRngEngine rng{params, state, tid};
0062         rng = init;
0063     }
0064 
0065     //! Initialize from the given thread
0066     CELER_FORCEINLINE_FUNCTION void operator()(ThreadId tid) const
0067     {
0068         return (*this)(TrackSlotId{tid.unchecked_get()});
0069     }
0070 };
0071 
0072 //---------------------------------------------------------------------------//
0073 // Initialize the RNG state on host/device
0074 void rng_state_init(DeviceCRef<CuHipRngParamsData> const& params,
0075                     DeviceRef<CuHipRngStateData> const& state,
0076                     DeviceCRef<CuHipRngInitData> const& seeds,
0077                     StreamId stream);
0078 
0079 void rng_state_init(HostCRef<CuHipRngParamsData> const& params,
0080                     HostRef<CuHipRngStateData> const& state,
0081                     HostCRef<CuHipRngInitData> const& seeds,
0082                     StreamId);
0083 
0084 #if !CELER_USE_DEVICE
0085 //---------------------------------------------------------------------------//
0086 /*!
0087  * Initialize the RNG states on device from seeds randomly generated on host.
0088  */
0089 inline void rng_state_init(DeviceCRef<CuHipRngParamsData> const&,
0090                            DeviceRef<CuHipRngStateData> const&,
0091                            DeviceCRef<CuHipRngInitData> const&,
0092                            StreamId)
0093 {
0094     CELER_ASSERT_UNREACHABLE();
0095 }
0096 #endif
0097 
0098 //---------------------------------------------------------------------------//
0099 }  // namespace detail
0100 }  // namespace celeritas