3#include "plumbing/random.h"
13static std::mt19937_64 mersenne_twister_gen;
16static std::uniform_real_distribution<double> real_rnd_dist(0.0, 1.0);
22#if !defined(CUDA) && !defined(HIP)
24 return real_rnd_dist(mersenne_twister_gen);
31double hila::host_random() {
32 return real_rnd_dist(mersenne_twister_gen);
37static bool rng_is_initialized =
false;
50 if (hila::partitions.number() > 1)
53 return (seed + n) ^ (n << 31);
66 mersenne_twister_gen.seed(seed);
68 for (
int i = 0; i < 9000; i++)
69 mersenne_twister_gen();
88 rng_is_initialized =
true;
91 hila::error(
"lattice.initialize() must be called before hila::seed_random()");
96 if (hila::partitions.number() > 1)
104 clock_gettime(CLOCK_MONOTONIC, &tp);
106 seed = (seed << 30) ^ tp.tv_nsec;
107 hila::out0 <<
"Random seed from time: " << seed <<
'\n';
114 hila::out0 <<
"Using node random numbers, seed for node 0: " << seed << std::endl;
118#if defined(CUDA) || defined(HIP)
124 hila::out0 <<
"Not initializing GPU random numbers\n";
139 random_seed_arr = (
unsigned short(*)[3])memalloc(3 * node.sites *
sizeof(
unsigned short));
141 random_seed_arr[i][0] = (
unsigned short)(seed + site[i].index);
142 random_seed_arr[i][1] = (
unsigned short)(seed + 2 * site[i].index);
143 random_seed_arr[i][2] = (
unsigned short)(seed + 3 * site[i].index);
146 random_seed_ptr = random_seed_arr[0];
155#if !(defined(CUDA) || defined(HIP))
189 }
while (urnd == 0.0);
191 r =
sqrt(-
::log(urnd) * (2.0 * VARIANCE));
196#if !defined(CUDA) && !defined(HIP)
217 static double second;
218 static bool draw_new =
true;
244 return rng_is_initialized;
259 if (!rng_is_initialized) {
260 hila::out0 <<
"ERROR: trying to use random numbers without initializing the generator"
264#if defined(CUDA) || defined(HIP)
266 hila::out0 <<
"ERROR: GPU random number generator is not initialized and onsites()-loop is "
267 "using random numbers"
Array< n, m, T > cos(Array< n, m, T > a)
Cosine.
Array< n, m, T > sqrt(Array< n, m, T > a)
Square root.
Array< n, m, T > sin(Array< n, m, T > a)
Sine.
Array< n, m, T > log(Array< n, m, T > a)
Logarithm.
bool is_initialized() const
is the lattice initialized
Implement hila::swap for gauge fields.
void initialize_device_rng(uint64_t seed)
Initialize device random number generator on GPUs, if application run on GPU platform....
void free_device_rng()
Free GPU RNG state, does nothing on non-GPU archs.
double random()
Real valued uniform random number generator.
int myrank()
rank of this node
int number_of_nodes()
how many nodes there are
bool is_device_rng_on()
Check if the RNG on GPU is allocated and ready to use.
uint64_t shuffle_rng_seed(uint64_t seed)
Random shuffling of rng seed for MPI nodes.
double gaussrand()
Gaussian random generation routine.
std::ostream out0
This writes output only from main process (node 0)
void seed_random(uint64_t seed, bool device_rng=true)
Seed random generators with 64-bit unsigned value. On MPI shuffles the seed so that different MPI ran...
T broadcast(T &var, int rank=0)
Broadcast the value of var to all MPI ranks from rank (default=0).
bool is_rng_seeded()
Check if RNG is seeded already.
void initialize_host_rng(uint64_t seed)
Initialize host (CPU) random number generator separately, done implicitly by seed_random()
double gaussrand2(double &out2)
hila::gaussrand2 returns 2 Gaussian distributed random numbers with variance .
void check_that_rng_is_initialized()
Check if RNG is initialized, do what the name says.
void terminate(int status)