okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ad33b8074
main
1//==---- rng_utils_detail.hpp --------------------------*- C++ -*-----------==// 2// 3// Copyright (C) Intel Corporation 4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 5// See https://llvm.org/LICENSE.txt for license information. 6// 7//===----------------------------------------------------------------------===// 8 9#ifndef __DPCT_RNG_UTILS_DETAIL_HPP__ 10#define __DPCT_RNG_UTILS_DETAIL_HPP__ 11 12namespace dpct::rng::host::detail { 13static const std::string OneMKLNotSupport = 14"The oneAPI Math Kernel Library (oneMKL) Interfaces Project does not " 15"support this API." ; 16class rng_generator_base { 17public : 18/// Set the seed of host rng_generator. 19/// \param seed The engine seed. 20virtual void set_seed (const std::uint64_t seed )= 0 ; 21 22/// Set the dimensions of host rng_generator. 23/// \param dimensions The engine dimensions. 24virtual void set_dimensions (const std::uint32_t dimensions )= 0 ; 25 26/// Set the queue of host rng_generator. 27/// \param queue The engine queue. 28virtual void set_queue (sycl::queue * queue )= 0 ; 29 30/// Set the mode of host rng_generator. 31/// \param mode The engine mode. 32virtual void set_mode (const random_mode mode )= 0 ; 33 34/// Generate unsigned int random number(s) with 'uniform_bits' distribution. 35/// \param output The pointer of the first random number. 36/// \param n The number of random numbers. 37virtual inline void generate_uniform_bits (unsigned int * output , 38 std::int64_t n )= 0 ; 39 40/// Generate unsigned long long random number(s) with 'uniform_bits' 41/// distribution. 42/// \param output The pointer of the first random number. 43/// \param n The number of random numbers. 44virtual inline void generate_uniform_bits (unsigned long long * output , 45 std::int64_t n )= 0 ; 46 47/// Generate float random number(s) with 'lognormal' distribution. 48/// \param output The pointer of the first random number. 49/// \param n The number of random numbers. 50/// \param m Mean of associated normal distribution 51/// \param s Standard deviation of associated normal distribution. 52virtual inline void generate_lognormal (float * output , std::int64_t n ,float m , 53float s )= 0 ; 54 55/// Generate double random number(s) with 'lognormal' distribution. 56/// \param output The pointer of the first random number. 57/// \param n The number of random numbers. 58/// \param m Mean of associated normal distribution 59/// \param s Standard deviation of associated normal distribution. 60virtual inline void generate_lognormal (double * output , std::int64_t n , 61double m ,double s )= 0 ; 62 63/// Generate float random number(s) with 'gaussian' distribution. 64/// \param output The pointer of the first random number. 65/// \param n The number of random numbers. 66/// \param mean Mean of normal distribution 67/// \param stddev Standard deviation of normal distribution. 68virtual inline void generate_gaussian (float * output , std::int64_t n , 69float mean ,float stddev )= 0 ; 70 71/// Generate double random number(s) with 'gaussian' distribution. 72/// \param output The pointer of the first random number. 73/// \param n The number of random numbers. 74/// \param mean Mean of normal distribution 75/// \param stddev Standard deviation of normal distribution. 76virtual inline void generate_gaussian (double * output , std::int64_t n , 77double mean ,double stddev )= 0 ; 78 79/// Generate unsigned int random number(s) with 'poisson' distribution. 80/// \param output The pointer of the first random number. 81/// \param n The number of random numbers. 82/// \param lambda Lambda for the Poisson distribution. 83virtual inline void generate_poisson (unsigned int * output , std::int64_t n , 84double lambda )= 0 ; 85 86/// Generate float random number(s) with 'uniform' distribution. 87/// \param output The pointer of the first random number. 88/// \param n The number of random numbers. 89virtual inline void generate_uniform (float * output , std::int64_t n )= 0 ; 90 91/// Generate double random number(s) with 'uniform' distribution. 92/// \param output The pointer of the first random number. 93/// \param n The number of random numbers. 94virtual inline void generate_uniform (double * output , std::int64_t n )= 0 ; 95 96/// Skip ahead several random number(s). 97/// \param num_to_skip The number of random numbers to be skipped. 98virtual void skip_ahead (const std::uint64_t num_to_skip )= 0 ; 99 100/// Set the direction numbers of host rng_generator. Only Sobol engine 101/// supports this method. 102/// \param direction_numbers The engine direction numbers. 103virtual void set_direction_numbers ( 104const std::vector < std::uint32_t > & direction_numbers )= 0 ; 105 106/// Set the engine index of host rng_generator. Only MT2203 engine 107/// supports this method. 108/// \param engine_idx The engine index. 109virtual void set_engine_idx (std::uint32_t engine_idx )= 0 ; 110 111protected : 112/// Construct the host rng_generator. 113/// \param queue The queue where the generator should be executed. 114rng_generator_base (sycl::queue * queue ) :_queue (queue ) {} 115 116 sycl::queue * _queue = nullptr ; 117 std::uint64_t _seed {0 }; 118 std::uint32_t _dimensions {1 }; 119random_mode _mode {random_mode::best }; 120 std::vector < std::uint32_t > _direction_numbers ; 121 std::uint32_t _engine_idx {0 }; 122}; 123 124/// The random number generator on host. 125template < typename engine_t = oneapi::mkl::rng::philox4x32x10 > 126class rng_generator :public rng_generator_base { 127public : 128/// Constructor of rng_generator. 129/// \param q The queue where the generator should be executed. 130rng_generator (sycl::queue & q = ::dpct::cs::get_default_queue ()) 131 :rng_generator_base (& q ), 132_engine (create_engine (& q ,_seed ,_dimensions ,_mode )) {} 133 134/// Set the seed of host rng_generator. 135/// \param seed The engine seed. 136void set_seed (const std::uint64_t seed ) { 137if (seed == _seed ) 138return ; 139_seed = seed ; 140_engine = create_engine (_queue ,_seed ,_dimensions ,_mode ); 141 } 142 143/// Set the dimensions of host rng_generator. 144/// \param dimensions The engine dimensions. 145void set_dimensions (const std::uint32_t dimensions ) { 146if (dimensions == _dimensions ) 147return ; 148_dimensions = dimensions ; 149_engine = create_engine (_queue ,_seed ,_dimensions ,_mode ); 150 } 151 152/// Set the queue of host rng_generator. 153/// \param queue The engine queue. 154void set_queue (sycl::queue * queue ) { 155if (queue == _queue ) 156return ; 157_queue = queue ; 158_engine = create_engine (_queue ,_seed ,_dimensions ,_mode ); 159 } 160 161/// Set the mode of host rng_generator. 162/// \param mode The engine mode. 163void set_mode (const random_mode mode ) { 164#ifndef __INTEL_MKL__ 165throw std::runtime_error (OneMKLNotSupport ); 166#else 167if constexpr (!std::is_same_v < engine_t , oneapi::mkl::rng::mrg32k3a > ) { 168throw std::runtime_error ("Only mrg32k3a engine support this method." ); 169 } 170if (mode == _mode ) 171return ; 172_mode = mode ; 173_engine = create_engine (_queue ,_seed ,_dimensions ,_mode ); 174#endif 175 } 176 177/// Set the direction numbers of Sobol host rng_generator. 178/// \param direction_numbers The user-defined direction numbers. 179void 180set_direction_numbers (const std::vector < std::uint32_t > & direction_numbers ) { 181#ifndef __INTEL_MKL__ 182throw std::runtime_error (OneMKLNotSupport ); 183#else 184if constexpr (std::is_same_v < engine_t , oneapi::mkl::rng::sobol > ) { 185if (direction_numbers == _direction_numbers ) 186return ; 187_direction_numbers = direction_numbers ; 188_engine = 189create_engine (_queue ,_seed ,_dimensions ,_mode ,_direction_numbers ); 190 }else { 191throw std::runtime_error ("Only Sobol engine supports this method." ); 192 } 193#endif 194 } 195 196/// Set the engine index of MT2203 host rng_generator. 197/// \param engine_idx The user-defined engine index. 198void set_engine_idx (std::uint32_t engine_idx ) { 199#ifndef __INTEL_MKL__ 200throw std::runtime_error ("The oneAPI Math Kernel Library (oneMKL) " 201"Interfaces Project does not support this API." ); 202#else 203if constexpr (std::is_same_v < engine_t , oneapi::mkl::rng::mt2203 > ) { 204if (engine_idx == _engine_idx ) 205return ; 206_engine_idx = engine_idx ; 207_engine = create_engine (_queue ,_seed ,_dimensions ,_mode , std::nullopt , 208_engine_idx ); 209 }else { 210throw std::runtime_error ("Only MT2203 engine supports this method." ); 211 } 212#endif 213 } 214 215/// Generate unsigned int random number(s) with 'uniform_bits' distribution. 216/// \param output The pointer of the first random number. 217/// \param n The number of random numbers. 218inline void generate_uniform_bits (unsigned int * output , std::int64_t n ) { 219#ifndef __INTEL_MKL__ 220throw std::runtime_error (OneMKLNotSupport ); 221#else 222 static_assert(sizeof (unsigned int )== sizeof (std::uint32_t )); 223generate < oneapi::mkl::rng::uniform_bits < std::uint32_t >> ( 224 (std::uint32_t * )output ,n ); 225#endif 226 } 227 228/// Generate unsigned long long random number(s) with 'uniform_bits' 229/// distribution. 230/// \param output The pointer of the first random number. 231/// \param n The number of random numbers. 232inline void generate_uniform_bits (unsigned long long * output , 233 std::int64_t n ) { 234#ifndef __INTEL_MKL__ 235throw std::runtime_error (OneMKLNotSupport ); 236#else 237 static_assert(sizeof (unsigned long long )== sizeof (std::uint64_t )); 238generate < oneapi::mkl::rng::uniform_bits < std::uint64_t >> ( 239 (std::uint64_t * )output ,n ); 240#endif 241 } 242 243/// Generate float random number(s) with 'lognormal' distribution. 244/// \param output The pointer of the first random number. 245/// \param n The number of random numbers. 246/// \param m Mean of associated normal distribution 247/// \param s Standard deviation of associated normal distribution. 248inline void generate_lognormal (float * output , std::int64_t n ,float m , 249float s ) { 250generate < oneapi::mkl::rng::lognormal < float >> (output ,n ,m ,s ); 251 } 252 253/// Generate double random number(s) with 'lognormal' distribution. 254/// \param output The pointer of the first random number. 255/// \param n The number of random numbers. 256/// \param m Mean of associated normal distribution 257/// \param s Standard deviation of associated normal distribution. 258inline void generate_lognormal (double * output , std::int64_t n ,double m , 259double s ) { 260generate < oneapi::mkl::rng::lognormal < double >> (output ,n ,m ,s ); 261 } 262 263/// Generate float random number(s) with 'gaussian' distribution. 264/// \param output The pointer of the first random number. 265/// \param n The number of random numbers. 266/// \param mean Mean of normal distribution 267/// \param stddev Standard deviation of normal distribution. 268inline void generate_gaussian (float * output , std::int64_t n ,float mean , 269float stddev ) { 270generate < oneapi::mkl::rng::gaussian < float >> (output ,n ,mean ,stddev ); 271 } 272 273/// Generate double random number(s) with 'gaussian' distribution. 274/// \param output The pointer of the first random number. 275/// \param n The number of random numbers. 276/// \param mean Mean of normal distribution 277/// \param stddev Standard deviation of normal distribution. 278inline void generate_gaussian (double * output , std::int64_t n ,double mean , 279double stddev ) { 280generate < oneapi::mkl::rng::gaussian < double >> (output ,n ,mean ,stddev ); 281 } 282 283/// Generate unsigned int random number(s) with 'poisson' distribution. 284/// \param output The pointer of the first random number. 285/// \param n The number of random numbers. 286/// \param lambda Lambda for the Poisson distribution. 287inline void generate_poisson (unsigned int * output , std::int64_t n , 288double lambda ) { 289generate < oneapi::mkl::rng::poisson < unsigned int >> (output ,n ,lambda ); 290 } 291 292/// Generate float random number(s) with 'uniform' distribution. 293/// \param output The pointer of the first random number. 294/// \param n The number of random numbers. 295inline void generate_uniform (float * output , std::int64_t n ) { 296generate < oneapi::mkl::rng::uniform < float >> (output ,n ); 297 } 298 299/// Generate double random number(s) with 'uniform' distribution. 300/// \param output The pointer of the first random number. 301/// \param n The number of random numbers. 302inline void generate_uniform (double * output , std::int64_t n ) { 303generate < oneapi::mkl::rng::uniform < double >> (output ,n ); 304 } 305 306/// Skip ahead several random number(s). 307/// \param num_to_skip The number of random numbers to be skipped. 308void skip_ahead (const std::uint64_t num_to_skip ) { 309#ifndef __INTEL_MKL__ 310 oneapi::mkl::rng::skip_ahead (_engine ,num_to_skip ); 311#else 312if constexpr (std::is_same_v < engine_t , oneapi::mkl::rng::mt2203 > ) 313throw std::runtime_error ("no skip_ahead method of mt2203 engine." ); 314else 315 oneapi::mkl::rng::skip_ahead (_engine ,num_to_skip ); 316#endif 317 } 318 319private : 320static inline engine_t 321create_engine (sycl::queue * queue ,const std::uint64_t seed , 322const std::uint32_t dimensions ,const random_mode mode , 323 std::optional < std::vector < std::uint32_t >> direction_numbers = 324 std::nullopt , 325 std::optional < std::uint32_t > engine_idx = std::nullopt ) { 326#ifdef __INTEL_MKL__ 327if constexpr (std::is_same_v < engine_t , oneapi::mkl::rng::mrg32k3a > ) { 328// oneapi::mkl::rng::mrg32k3a_mode is only supported for GPU device. For 329// other devices, this argument will be ignored. 330if (queue -> get_device ().is_gpu ()) { 331#if __cplusplus < 202002L 332// before C++20 333switch (mode ) { 334case random_mode::best : 335return engine_t (* queue ,seed , 336 oneapi::mkl::rng::mrg32k3a_mode::custom {81920 }); 337case random_mode::legacy : 338return engine_t (* queue ,seed , 339 oneapi::mkl::rng::mrg32k3a_mode::custom {4096 }); 340case random_mode::optimal : 341return engine_t (* queue ,seed , 342 oneapi::mkl::rng::mrg32k3a_mode::optimal_v ); 343 } 344#else 345// since C++20 346#if defined(_MSC_VER )&& !defined(__clang__ ) 347#pragma message("warning: oneapi::mkl::rng::mrg32k3a_mode::custom cannot be " \ 348 "initialized with a value since C++20. Ignore the parameter " \ 349 "`mode`.") 350#else 351#warning "oneapi::mkl::rng::mrg32k3a_mode::custom cannot be initialized with " \ 352 "a value since C++20. Ignore the parameter `mode`." 353#endif 354return engine_t (* queue ,dimensions ); 355#endif 356 } 357 }else if constexpr (std::is_same_v < engine_t , oneapi::mkl::rng::mt2203 > ) { 358if (engine_idx .has_value ()) { 359return engine_t (* queue ,seed ,engine_idx .value ()); 360 } 361 }else if constexpr (std::is_same_v < engine_t , oneapi::mkl::rng::sobol > ) { 362if (direction_numbers .has_value ()) { 363return engine_t (* queue ,direction_numbers .value ()); 364 } 365return engine_t (* queue ,dimensions ); 366 } 367#endif 368return engine_t (* queue ,seed ); 369 } 370 371template < typename distr_t ,typename buffer_t ,class ...distr_params_t > 372void generate (buffer_t * output ,const std::int64_t n , 373const distr_params_t ...distr_params ) { 374auto output_buf = ::dpct::detail::get_memory < buffer_t > (output ); 375 oneapi::mkl::rng::generate (distr_t (distr_params ...),_engine ,n , 376output_buf ); 377 } 378engine_t _engine {}; 379}; 380}// namespace dpct::rng::host::detail 381 382#endif // __DPCT_RNG_UTILS_DETAIL_HPP__