Block-Structured AMR Software Framework
AMReX_Random.H
Go to the documentation of this file.
1 #ifndef AMREX_RAND_H
2 #define AMREX_RAND_H
3 #include <AMReX_Config.H>
4 
5 #include <AMReX.H>
6 #include <AMReX_GpuQualifiers.H>
8 #include <AMReX_RandomEngine.H>
9 #include <limits>
10 #include <cstdint>
11 
12 namespace amrex
13 {
21  Real Random ();
22 
24  Real Random (RandomEngine const& random_engine)
25  {
26 #if defined (__SYCL_DEVICE_ONLY__)
27  mkl::rng::device::uniform<Real> distr;
28  return mkl::rng::device::generate(distr, *random_engine.engine);
29 #else
30 #ifdef BL_USE_FLOAT
33  return 1.0f - hiprand_uniform(random_engine.rand_state); ,
34  return 1.0f - curand_uniform(random_engine.rand_state);
35  )
36  ))
37 #else
40  return 1.0 - hiprand_uniform_double(random_engine.rand_state); ,
41  return 1.0 - curand_uniform_double(random_engine.rand_state);
42  )
43  ))
44 #endif
46  amrex::ignore_unused(random_engine);
47  return Random();
48  ))
49 #endif
50  }
51 
59  Real RandomNormal (Real mean, Real stddev);
60 
62  Real RandomNormal (Real mean, Real stddev, RandomEngine const& random_engine)
63  {
64 #if defined (__SYCL_DEVICE_ONLY__)
65  mkl::rng::device::gaussian<Real> distr(mean, stddev);
66  return mkl::rng::device::generate(distr, *random_engine.engine);
67 #else
68 #ifdef BL_USE_FLOAT
71  return stddev * hiprand_normal(random_engine.rand_state) + mean; ,
72  return stddev * curand_normal(random_engine.rand_state) + mean;
73  )
74  ))
75 #else
78  return stddev * hiprand_normal_double(random_engine.rand_state) + mean; ,
79  return stddev * curand_normal_double(random_engine.rand_state) + mean;
80  )
81  ))
82 #endif
84  amrex::ignore_unused(random_engine);
85  return RandomNormal(mean, stddev);
86  ))
87 #endif
88  }
89 
99  unsigned int RandomPoisson (Real lambda);
100 
102  unsigned int RandomPoisson (Real lambda, RandomEngine const& random_engine)
103  {
104 #if defined (__SYCL_DEVICE_ONLY__)
105  mkl::rng::device::poisson<unsigned int> distr(lambda);
106  return mkl::rng::device::generate(distr, *random_engine.engine);
107 #else
110  return hiprand_poisson(random_engine.rand_state, lambda); ,
111  return curand_poisson(random_engine.rand_state, lambda);
112  )
113  ))
115  amrex::ignore_unused(random_engine);
116  return RandomPoisson(lambda);
117  ))
118 #endif
119  }
120 
128  unsigned int Random_int (unsigned int n); // [0,n-1]
129 
131  unsigned int Random_int (unsigned int n, RandomEngine const& random_engine)
132  {
133 #if defined(__SYCL_DEVICE_ONLY__)
134  mkl::rng::device::uniform<unsigned int> distr(0,n);
135  return mkl::rng::device::generate(distr, *random_engine.engine);
136 #else
138  unsigned int rand;
139  constexpr unsigned int RAND_M = 4294967295; // 2**32-1
140  do {
141  AMREX_HIP_OR_CUDA( rand = hiprand(random_engine.rand_state);,
142  rand = curand(random_engine.rand_state) );
143  } while (rand > (RAND_M - RAND_M % n));
144  return rand % n;
145  ))
147  amrex::ignore_unused(random_engine);
148  return Random_int(n);
149  ))
150 #endif
151  }
152 
160  ULong Random_long (ULong n); // [0,n-1]
161 
164  void FillRandom (Real* p, Long N);
165 
167  void FillRandomNormal (Real* p, Long N, Real mean, Real stddev);
168 
169  namespace detail {
170  inline ULong DefaultGpuSeed () {
171  return ParallelDescriptor::MyProc()*1234567ULL + 12345ULL;
172  }
173  }
174 
187  void InitRandom (ULong cpu_seed, int nprocs=ParallelDescriptor::NProcs(),
188  ULong gpu_seed = detail::DefaultGpuSeed());
189 
190  void ResetRandomSeed (ULong cpu_seed, ULong gpu_seed = detail::DefaultGpuSeed());
191 
196  void SaveRandomState (std::ostream& os);
197 
198  void RestoreRandomState (std::istream& is, int nthreads_old, int nstep_old);
199 
209  void UniqueRandomSubset (Vector<int> &uSet, int setSize, int poolSize,
210  bool printSet = false);
211 
213 }
214 
215 #endif
#define AMREX_FORCE_INLINE
Definition: AMReX_Extension.H:119
#define AMREX_HIP_OR_CUDA(a, b)
Definition: AMReX_GpuControl.H:21
#define AMREX_IF_ON_DEVICE(CODE)
Definition: AMReX_GpuQualifiers.H:56
#define AMREX_IF_ON_HOST(CODE)
Definition: AMReX_GpuQualifiers.H:58
#define AMREX_GPU_HOST_DEVICE
Definition: AMReX_GpuQualifiers.H:20
int MyProc() noexcept
return the rank number local to the current Parallel Context
Definition: AMReX_ParallelDescriptor.H:125
int NProcs() noexcept
return the number of MPI ranks local to the current Parallel Context
Definition: AMReX_ParallelDescriptor.H:243
ULong DefaultGpuSeed()
Definition: AMReX_Random.H:170
Definition: AMReX_Amr.cpp:49
void FillRandomNormal(MultiFab &mf, int scomp, int ncomp, Real mean, Real stddev)
Fill MultiFab with random numbers from normal distribution.
Definition: AMReX_MultiFabUtil.cpp:1207
void FillRandom(MultiFab &mf, int scomp, int ncomp)
Fill MultiFab with random numbers from uniform distribution.
Definition: AMReX_MultiFabUtil.cpp:1194
void InitRandom(ULong cpu_seed, int nprocs, ULong gpu_seed)
Set the seed of the random number generator.
Definition: AMReX_Random.cpp:89
unsigned int Random_int(unsigned int n)
Generates one pseudorandom unsigned integer which is uniformly distributed on [0,n-1]-interval for ea...
Definition: AMReX_Random.cpp:137
Real Random()
Generate a psuedo-random double from uniform distribution.
Definition: AMReX_Random.cpp:123
void ResetRandomSeed(ULong cpu_seed, ULong gpu_seed)
Definition: AMReX_Random.cpp:205
Real RandomNormal(Real mean, Real stddev)
Generate a psuedo-random double from a normal distribution.
Definition: AMReX_Random.cpp:116
ULong Random_long(ULong n)
Generates one pseudorandom unsigned long which is uniformly distributed on [0,n-1]-interval for each ...
Definition: AMReX_Random.cpp:144
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void ignore_unused(const Ts &...)
This shuts up the compiler about unused variables.
Definition: AMReX.H:111
void UniqueRandomSubset(Vector< int > &uSet, int setSize, int poolSize, bool printSet)
Create a unique subset of random numbers from a pool of integers in the range [0, poolSize - 1] the s...
Definition: AMReX_Random.cpp:183
unsigned int RandomPoisson(Real lambda)
Generate a psuedo-random integer from a Poisson distribution.
Definition: AMReX_Random.cpp:130
void DeallocateRandomSeedDevArray()
Definition: AMReX_Random.cpp:211
void SaveRandomState(std::ostream &os)
Save and restore random state.
Definition: AMReX_Random.cpp:152
void RestoreRandomState(std::istream &is, int nthreads_old, int nstep_old)
Definition: AMReX_Random.cpp:160
Definition: AMReX_FabArrayCommI.H:841
Definition: AMReX_RandomEngine.H:57
randState_t * rand_state
Definition: AMReX_RandomEngine.H:58