STORMM Source Documentation
Loading...
Searching...
No Matches
random.h
1// -*-c++-*-
2#ifndef STORMM_RANDOM_H
3#define STORMM_RANDOM_H
4
5#include <vector>
6#include "copyright.h"
7#include "Accelerator/hybrid.h"
8#include "Constants/scaling.h"
9#include "Constants/hpc_bounds.h"
10#include "DataTypes/common_types.h"
11#include "DataTypes/stormm_vector_types.h"
12#include "Math/rounding.h"
13#include "Numerics/split_fixed_precision.h"
14#include "random_enumerators.h"
15
16namespace stormm {
17namespace random {
18
19using card::Hybrid;
20using card::HybridKind;
21using card::HybridTargetLevel;
22using constants::giga_zu;
23using data_types::isFloatingPointScalarType;
24using stmath::roundUp;
25
28constexpr int im1 = 2147483563;
29constexpr int im2 = 2147483399;
30constexpr double am = 1.0 / im1;
31constexpr int im1_minus_one = im1 - 1;
32constexpr int ia1 = 40014;
33constexpr int ia2 = 40692;
34constexpr int iq1 = 53668;
35constexpr int iq2 = 52774;
36constexpr int ir1 = 12211;
37constexpr int ir2 = 3791;
38constexpr int ntab = 32;
39constexpr int ndiv = 1 + im1_minus_one / ntab;
40constexpr double double_increment = 2.2205e-16;
41constexpr double ran2_max = 1.0 - double_increment;
43
45constexpr int default_random_seed = 827493;
46
50constexpr int default_xoroshiro128p_scrub = 25;
51constexpr int default_xoshiro256pp_scrub = 50;
53
60constexpr int max_xo_long_jumps = 1024;
61
71constexpr double rng_unit_bin_offset = 1.1102230246251565404236316680908203125e-16;
72constexpr float rng_unit_bin_offset_f = 5.960464477539062500e-08f;
74
82public:
83
87 Ran2Generator(int igseed = default_random_seed);
88
92 double uniformRandomNumber();
93
100
104 double gaussianRandomNumber();
105
111
112private:
113 int iunstable_a;
114 int iunstable_b;
115 int state_sample;
116 int state_vector[ntab + 8];
117};
118
121constexpr ullint xrs128p_jump_i = 0xdf900294d8f554a5LLU;
122constexpr ullint xrs128p_jump_ii = 0x170865df4b3201fcLLU;
123constexpr ullint xrs128p_longjump_i = 0xd2a98b26625eee7bLLU;
124constexpr ullint xrs128p_longjump_ii = 0xdddf9b1090aa7ac1LLU;
126
129constexpr ullint xrs256pp_jump_i = 0x180ec6d33cfd0abaLLU;
130constexpr ullint xrs256pp_jump_ii = 0xd5a61266f0c9392cLLU;
131constexpr ullint xrs256pp_jump_iii = 0xa9582618e03fc9aaLLU;
132constexpr ullint xrs256pp_jump_iv = 0x39abdc4529b1661cLLU;
133constexpr ullint xrs256pp_longjump_i = 0x76e15d3efefdcbbfLLU;
134constexpr ullint xrs256pp_longjump_ii = 0xc5004e441c522fb3LLU;
135constexpr ullint xrs256pp_longjump_iii = 0x77710069854ee241LLU;
136constexpr ullint xrs256pp_longjump_iv = 0x39109bb02acbe635LLU;
138
144public:
145
156 Xoroshiro128pGenerator(int igseed = default_random_seed, int niter = 25);
157 Xoroshiro128pGenerator(const ullint2 state_in);
159
163 double uniformRandomNumber();
164
170 float spUniformRandomNumber();
171
175 double gaussianRandomNumber();
176
182
184 void jump();
185
187 void longJump();
188
190 ullint2 revealState() const;
191
193 ullint revealBitString() const;
194
196 void setState(const ullint2 state_in);
197
198private:
199 ullint2 state;
200
204 ullint2 seed128(int igseed);
205
207 ullint next();
208
213 void fastForward(const ullint2 stride);
214};
215
219public:
220
230 Xoshiro256ppGenerator(int igseed = default_random_seed, int niter = 50);
231 Xoshiro256ppGenerator(const ullint4 state_in);
232
236 double uniformRandomNumber();
237
243 float spUniformRandomNumber();
244
248 double gaussianRandomNumber();
249
255
257 void jump();
258
260 void longJump();
261
263 ullint4 revealState() const;
264
266 ullint revealBitString() const;
267
269 void setState(const ullint4 state_in);
270
271private:
272 ullint4 state;
273
277 ullint4 seed256(int igseed);
278
280 ullint next();
281
286 void fastForward(const ullint4 stride);
287};
288
291template <typename T> class RandomNumberMill {
292public:
293
307 RandomNumberMill(const ullint2 state_in, size_t generators_in = 1LLU, size_t depth_in = 2LLU,
308 RandomNumberKind init_kind = RandomNumberKind::GAUSSIAN,
309 size_t bank_limit = constants::giga_zu);
310
311 RandomNumberMill(const ullint4 state_in, size_t generators_in = 1LLU, size_t depth_in = 2LLU,
312 RandomNumberKind init_kind = RandomNumberKind::GAUSSIAN,
313 size_t bank_limit = constants::giga_zu);
314
315 RandomNumberMill(size_t generators_in = 1LLU, size_t depth_in = 2LLU,
316 RandomAlgorithm style_in = RandomAlgorithm::XOSHIRO_256PP,
317 RandomNumberKind init_kind = RandomNumberKind::GAUSSIAN,
318 int igseed_in = default_random_seed, int niter = default_xoshiro256pp_scrub,
319 size_t bank_limit = constants::giga_zu);
321
323 size_t getGeneratorCount() const;
324
326 size_t getDepth() const;
327
329 size_t getRefreshStride() const;
330
335 T getBankValue(size_t generator_index, size_t layer_index) const;
336
342 void uniformRandomNumbers(size_t first_gen, size_t last_gen);
343
349 void gaussianRandomNumbers(size_t first_gen, size_t last_gen);
350
351private:
352 RandomAlgorithm style;
353 size_t generators;
354 size_t depth;
355 size_t refresh_stride;
358 Hybrid<ullint2> state_xy;
361 Hybrid<ullint2> state_zw;
364 Hybrid<T> bank;
369
373 void checkDimensions(size_t bank_limit);
374
379 void initializeBank(RandomNumberKind init_kind);
380};
381
398template <typename Tprng> double uniformRand(Tprng *rng, double scale = 1.0);
399
400template <typename Tprng> std::vector<double> uniformRand(Tprng *rng, size_t count,
401 double scale);
402
403template <typename Tprng>
404std::vector<double> uniformRand(Tprng *rng, size_t rows, size_t columns, double scale,
405 RngFillMode mode = RngFillMode::COLUMNS);
406
407template <typename Tprng, typename Tprod>
408void uniformRand(Tprng *rng, std::vector<Tprod> *xv, double scale = 1.0, double fp_scale = 1.0);
410
414template <typename Tprng> float spUniformRand(Tprng *rng, float scale = 1.0);
415
416template <typename Tprng> std::vector<float> spUniformRand(Tprng *rng, size_t count, float scale);
417
418template <typename Tprng>
419std::vector<float> spUniformRand(Tprng *rng, size_t rows, size_t columns, float scale,
420 RngFillMode mode = RngFillMode::COLUMNS);
421
422template <typename Tprng, typename Tprod>
423void spUniformRand(Tprng *rng, std::vector<Tprod> *xv, float scale = 1.0, float fp_scale = 1.0);
425
429template <typename Tprng> double guassianRand(Tprng *rng, double scale = 1.0);
430
431template <typename Tprng> std::vector<double> guassianRand(Tprng *rng, size_t count, double scale);
432
433template <typename Tprng>
434std::vector<double> guassianRand(Tprng *rng, size_t rows, size_t columns, double scale,
435 RngFillMode mode = RngFillMode::COLUMNS);
436
437template <typename Tprng, typename Tprod>
438void guassianRand(Tprng *rng, std::vector<Tprod> *xv, double fp_scale = 1.0, double scale = 1.0);
440
444template <typename Tprng> float spGuassianRand(Tprng *rng, float scale = 1.0);
445
446template <typename Tprng> std::vector<float> spGuassianRand(Tprng *rng, size_t count, float scale);
447
448template <typename Tprng>
449std::vector<float> spGuassianRand(Tprng *rng, size_t rows, size_t columns, float scale,
450 RngFillMode mode = RngFillMode::COLUMNS);
451
452template <typename Tprng, typename Tprod>
453void spGuassianRand(Tprng *rng, std::vector<Tprod> *xv, float fp_scale = 1.0, float scale = 1.0);
455
471void initXoroshiro128pArray(ullint2* state_vector, int n_generators, int igseed,
472 int scrub_cycles = default_xoroshiro128p_scrub);
473void initXoroshiro128pArray(std::vector<ullint2> *state_vector, int igseed,
474 int scrub_cycles = default_xoroshiro128p_scrub);
475void initXoroshiro128pArray(Hybrid<ullint2> *state_vector, int igseed,
476 int scrub_cycles = default_xoroshiro128p_scrub);
478
494void initXoshiro256ppArray(ullint2* state_xy, ullint2* state_zw, int n_generators, int igseed,
495 int scrub_cycles = default_xoshiro256pp_scrub);
496void initXoshiro256ppArray(std::vector<ullint2> *state_xy, std::vector<ullint2> *state_zw,
497 int igseed, int scrub_cycles = default_xoshiro256pp_scrub);
498void initXoshiro256ppArray(Hybrid<ullint2> *state_xy, Hybrid<ullint2> *state_zw, int igseed,
499 int scrub_cycles = default_xoshiro256pp_scrub);
501
525template <typename T>
526void fillRandomCache(ullint2* state_xy, ullint2* state_zw, T* cache, size_t length, size_t depth,
527 RandomAlgorithm method, RandomNumberKind product, size_t index_start,
528 size_t index_end);
529
530template <typename T>
531void fillRandomCache(std::vector<ullint2> *state_xy, std::vector<ullint2> *state_zw,
532 std::vector<T> *cache, size_t length, size_t depth, RandomAlgorithm method,
533 RandomNumberKind product, size_t index_start, size_t index_end);
534
535template <typename T>
536void fillRandomCache(Hybrid<ullint2> *state_xy, Hybrid<ullint2> *state_zw, std::vector<T> *cache,
537 size_t length, size_t depth, RandomAlgorithm method, RandomNumberKind product,
538 size_t index_start, size_t index_end);
540
563template <typename Trng, typename Tvar>
564void addRandomNoise(Trng *prng, Tvar* x, size_t length, double mult, double scale = 1.0,
565 RandomNumberKind kind = RandomNumberKind::GAUSSIAN);
566
567template <typename Trng, typename Tvar>
568void addRandomNoise(Trng *prng, std::vector<Tvar> *x, double mult, double scale = 1.0,
569 RandomNumberKind kind = RandomNumberKind::GAUSSIAN);
570
571template <typename Trng, typename Tvar>
572void addRandomNoise(Trng *prng, Hybrid<Tvar> *x, double mult, double scale = 1.0,
573 RandomNumberKind kind = RandomNumberKind::GAUSSIAN);
574
575template <typename Trng, typename Tvar>
576void addRandomNoise(Trng *prng, Tvar* x, Tvar* y, Tvar *z, size_t length, double mult,
577 double scale = 1.0, RandomNumberKind kind = RandomNumberKind::GAUSSIAN);
578
579template <typename Trng, typename Tvar>
580void addRandomNoise(Trng *prng, std::vector<Tvar> *x, std::vector<Tvar> *y, std::vector<Tvar> *z,
581 double mult, double scale = 1.0,
582 RandomNumberKind kind = RandomNumberKind::GAUSSIAN);
583
584template <typename Trng, typename Tvar>
585void addRandomNoise(Trng *prng, Hybrid<Tvar> *x, Hybrid<Tvar> *y, Hybrid<Tvar> *z, double mult,
586 double scale = 1.0, RandomNumberKind kind = RandomNumberKind::GAUSSIAN);
587
588template <typename Trng>
589void addRandomNoise(Trng *prng, llint* x, int* x_ovrf, size_t length, double mult, double scale,
590 RandomNumberKind kind = RandomNumberKind::GAUSSIAN);
591
592template <typename Trng>
593void addRandomNoise(Trng *prng, std::vector<llint> *x, std::vector<int> *x_ovrf, double mult,
594 double scale, RandomNumberKind kind = RandomNumberKind::GAUSSIAN);
595
596template <typename Trng>
597void addRandomNoise(Trng *prng, Hybrid<llint> *x, Hybrid<int> *x_ovrf, double mult, double scale,
598 RandomNumberKind kind = RandomNumberKind::GAUSSIAN);
599
600template <typename Trng>
601void addRandomNoise(Trng *prng, llint* x, int* x_ovrf, llint* y, int* y_ovrf, llint* z,
602 int* z_ovrf, size_t length, double mult, double scale,
603 RandomNumberKind kind = RandomNumberKind::GAUSSIAN);
604
605template <typename Trng>
606void addRandomNoise(Trng *prng, std::vector<llint> *x, std::vector<int> *x_ovrf,
607 std::vector<llint> *y, std::vector<int> *y_ovrf, std::vector<llint> *z,
608 std::vector<int> *z_ovrf, double mult, double scale,
609 RandomNumberKind kind = RandomNumberKind::GAUSSIAN);
610
611template <typename Trng>
612void addRandomNoise(Trng *prng, Hybrid<llint> *x, Hybrid<int> *x_ovrf, Hybrid<llint> *y,
613 Hybrid<int> *y_ovrf, Hybrid<llint> *z, Hybrid<int> *z_ovrf, double mult,
614 double scale, RandomNumberKind kind = RandomNumberKind::GAUSSIAN);
616
617} // namespace random
618} // namespace stormm
619
620#include "random.tpp"
621
622#endif
An evolution of GpuBuffer in pmemd.cuda, the Composite array has elements that are accessible from ei...
Definition hybrid.h:202
float spUniformRandomNumber()
Return a single random number distributed over a uniform distribution [0, 1), in single-precision....
Definition random.cpp:81
Ran2Generator(int igseed=default_random_seed)
Initialize the unit testing on-board pseudo-random number generator.
Definition random.cpp:17
double gaussianRandomNumber()
Return a normally distributed random number with standard deviation 1.0. This works off of the unifor...
Definition random.cpp:86
double uniformRandomNumber()
Return a single random number distributed over a uniform distribution [0, 1). This is an internal gen...
Definition random.cpp:38
float spGaussianRandomNumber()
Produce a single-precision random number on the normal distribution with a standard deviation of 1....
Definition random.cpp:93
size_t getDepth() const
Get the depth of the bank for each generator.
T getBankValue(size_t generator_index, size_t layer_index) const
Get a random number out of the bank.
RandomNumberMill(const ullint2 state_in, size_t generators_in=1LLU, size_t depth_in=2LLU, RandomNumberKind init_kind=RandomNumberKind::GAUSSIAN, size_t bank_limit=constants::giga_zu)
The constructor can work off of a simple random initialization seed integer or a specific state vecto...
void uniformRandomNumbers(size_t first_gen, size_t last_gen)
Populate a portion of this object's bank with random numbers from each of the respective generators.
size_t getGeneratorCount() const
Get the number of (forward-jumped) generators.
size_t getRefreshStride() const
Get the number of generators for which to refesh all banked values at one time.
void gaussianRandomNumbers(size_t first_gen, size_t last_gen)
Populate a portion of this object's bank with normally distributed random numbers from each of the re...
double uniformRandomNumber()
Return a single random number distributed over a uniform distribution [0, 1). This is an internal gen...
Definition random.cpp:131
double gaussianRandomNumber()
Return a normally distributed random number with standard deviation 1.0. This works off of the unifor...
Definition random.cpp:143
float spUniformRandomNumber()
Return a single random number distributed over a uniform distribution [0, 1), in single-precision....
Definition random.cpp:137
void setState(const ullint2 state_in)
Set the current state of the generator.
Definition random.cpp:179
Xoroshiro128pGenerator(int igseed=default_random_seed, int niter=25)
The constructor can start or restart the generator.
Definition random.cpp:100
ullint2 revealState() const
Reveal the current state of the generator.
Definition random.cpp:169
void longJump()
Jump forward 2^96 iterations in the sequence.
Definition random.cpp:163
float spGaussianRandomNumber()
Produce a single-precision random number on the normal distribution with a standard deviation of 1....
Definition random.cpp:150
ullint revealBitString() const
Reveal the random bit string.
Definition random.cpp:174
void jump()
Jump forward 2^64 iterations in the sequence.
Definition random.cpp:157
void longJump()
Jump forward 2^192 iterations in the sequence.
Definition random.cpp:303
ullint revealBitString() const
Reveal the random bit string.
Definition random.cpp:315
float spUniformRandomNumber()
Return a single random number distributed over a uniform distribution [0, 1), in single-precision....
Definition random.cpp:276
double gaussianRandomNumber()
Return a normally distributed random number with standard deviation 1.0. This works off of the unifor...
Definition random.cpp:282
ullint4 revealState() const
Reveal the current state of the generator.
Definition random.cpp:310
float spGaussianRandomNumber()
Produce a single-precision random number on the normal distribution with a standard deviation of 1....
Definition random.cpp:289
double uniformRandomNumber()
Return a single random number distributed over a uniform distribution [0, 1). This is an internal gen...
Definition random.cpp:270
Xoshiro256ppGenerator(int igseed=default_random_seed, int niter=50)
The constructor can start or restart the generator.
Definition random.cpp:239
void jump()
Jump forward 2^128 iterations in the sequence.
Definition random.cpp:296
void setState(const ullint4 state_in)
Set the current state of the generator.
Definition random.cpp:321