CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
MixMaxRng.h
Go to the documentation of this file.
1//
2// -*- C++ -*-
3//
4// -----------------------------------------------------------------------
5// HEP Random
6// --- MixMaxRng ---
7// class header file
8// -----------------------------------------------------------------------
9//
10// This file interfaces the MixMax PseudoRandom Number Generator
11// proposed by:
12//
13// G.K.Savvidy and N.G.Ter-Arutyunian,
14// On the Monte Carlo simulation of physical systems,
15// J.Comput.Phys. 97, 566 (1991);
16// Preprint EPI-865-16-86, Yerevan, Jan. 1986
17// http://dx.doi.org/10.1016/0021-9991(91)90015-D
18//
19// K.Savvidy
20// "The MIXMAX random number generator"
21// Comp. Phys. Commun. (2015)
22// http://dx.doi.org/10.1016/j.cpc.2015.06.003
23//
24// K.Savvidy and G.Savvidy
25// "Spectrum and Entropy of C-systems. MIXMAX random number generator"
26// Chaos, Solitons & Fractals, Volume 91, (2016) pp. 33-38
27// http://dx.doi.org/10.1016/j.chaos.2016.05.003
28//
29// =======================================================================
30// Implementation by Konstantin Savvidy - Copyright 2004-2017
31// =======================================================================
32
33#ifndef MixMaxRng_h
34#define MixMaxRng_h 1
35
36#include <array>
37#include <cstdint>
38#include "CLHEP/Random/defs.h"
39#include "CLHEP/Random/RandomEngine.h"
40
41namespace CLHEP {
42
43/**
44 * @author K.Savvidy
45 * @ingroup random
46 */
47
48using myID_t = std::uint32_t;
49using myuint_t = unsigned long long int;
50
52
53 static const int N = 17;
54
55public:
56
57 MixMaxRng(std::istream& is);
58 MixMaxRng();
59 MixMaxRng(long seed);
60 ~MixMaxRng();
61 // Constructors and destructor.
62
63 MixMaxRng(const MixMaxRng& rng);
64 MixMaxRng& operator=(const MixMaxRng& rng);
65 // Copy constructor and assignment operator.
66
67 double flat() { return (S.counter<=(N-1)) ? generate(S.counter):iterate(); }
68 // Returns a pseudo random number between 0 and 1
69 // (excluding the zero: in (0,1] )
70 // smallest number which it will give is approximately 10^-19
71
72 void flatArray (const int size, double* vect);
73 // Fills the array "vect" of specified size with flat random values.
74
75 void setSeed(long seed, int dum=0);
76 // Sets the state of the algorithm according to seed.
77
78 void setSeeds(const long * seeds, int seedNum=0);
79 // Sets the initial state of the engine according to the array of between one and four 32-bit seeds.
80 // If the size of long is greater on the platform, only the lower 32-bits are used.
81 // Streams created from seeds differing by at least one bit somewhere are guaranteed absolutely
82 // to be independent and non-colliding for at least the next 10^100 random numbers
83
84 void saveStatus( const char filename[] = "MixMaxRngState.conf" ) const;
85 // Saves the the current engine state in the file given, by default MixMaxRngState.conf
86
87 void restoreStatus( const char filename[] = "MixMaxRngState.conf" );
88 // Reads a valid engine state from a given file, by default MixMaxRngState.conf
89 // and restores it.
90
91 void showStatus() const;
92 // Dumps the engine status on the screen.
93
94 operator double();
95 // Returns same as flat()
96 operator float();
97 // less precise flat, faster if possible
98 operator unsigned int();
99 // 32-bit flat
100
101 virtual std::ostream & put (std::ostream & os) const;
102 virtual std::istream & get (std::istream & is);
103 static std::string beginTag ( );
104 virtual std::istream & getState ( std::istream & is );
105
106 std::string name() const { return "MixMaxRng"; }
107 static std::string engineName();
108
109 std::vector<unsigned long> put () const;
110 bool get (const std::vector<unsigned long> & v);
111 bool getState (const std::vector<unsigned long> & v);
112
113private:
114
115 static constexpr long long int SPECIAL = ((N==17)? 0 : ((N==240)? 487013230256099140ULL:0) ); // etc...
116 static constexpr long long int SPECIALMUL= ((N==17)? 36: ((N==240)? 51 :53) ); // etc...
117 // Note the potential for confusion...
118 static constexpr int BITS=61;
119 static constexpr myuint_t M61=2305843009213693951ULL;
120 static constexpr double INV_M61=0.43368086899420177360298E-18;
121 static constexpr unsigned int VECTOR_STATE_SIZE = 2*N+4; // 2N+4 for MIXMAX
122
123 #define MIXMAX_MOD_MERSENNE(k) ((((k)) & M61) + (((k)) >> BITS) )
124
125 static constexpr int rng_get_N();
126 static constexpr long long int rng_get_SPECIAL();
127 static constexpr int rng_get_SPECIALMUL();
128 void seed_uniquestream( myID_t clusterID, myID_t machineID, myID_t runID, myID_t streamID );
129 void seed_spbox(myuint_t);
130 void print_state() const;
131 myuint_t precalc();
132 myuint_t get_next() ;
133 inline double get_next_float() { return get_next_float_packbits(); }
134 // Returns a random double with all 52 bits random, in the range (0,1]
135
136 MixMaxRng Branch();
137 void BranchInplace(int id);
138
139 MixMaxRng(myID_t clusterID, myID_t machineID, myID_t runID, myID_t streamID ); // Constructor with four 32-bit seeds
140 inline void seed64(myuint_t seedval) { seed_uniquestream( 0, 0, (myID_t)(seedval>>32), (myID_t)seedval ); } // seed with one 64-bit seed
141
142 double generate(int i);
143 double iterate();
144
145 double get_next_float_packbits();
146#if defined __GNUC__
147#pragma GCC diagnostic push
148#pragma GCC diagnostic ignored "-Wstrict-aliasing"
149#pragma GCC diagnostic ignored "-Wuninitialized"
150#endif
151 inline double convert1double(myuint_t u)
152 {
153 const double one = 1;
154 const myuint_t onemask = *(myuint_t*)&one;
155 myuint_t tmp = (u>>9) | onemask; // bits between 52 and 62 dont affect the result!
156 double d = *(double*)&tmp;
157 return d-1.0;
158 }
159#if defined __GNUC__
160#pragma GCC diagnostic pop
161#endif
162 myuint_t MOD_MULSPEC(myuint_t k);
163 myuint_t MULWU(myuint_t k);
164 void seed_vielbein( unsigned int i); // seeds with the i-th unit vector, i = 0..N-1, for testing only
165 myuint_t iterate_raw_vec(myuint_t* Y, myuint_t sumtotOld);
166 myuint_t apply_bigskip(myuint_t* Vout, myuint_t* Vin, myID_t clusterID, myID_t machineID, myID_t runID, myID_t streamID );
167 myuint_t modadd(myuint_t foo, myuint_t bar);
168#if defined(__x86_64__)
169 myuint_t mod128(__uint128_t s);
170 myuint_t fmodmulM61(myuint_t cum, myuint_t a, myuint_t b);
171#else // on all other platforms, including 32-bit linux, PPC and PPC64, ARM and all Windows
172 myuint_t fmodmulM61(myuint_t cum, myuint_t s, myuint_t a);
173#endif
174
175private:
176
177 struct rng_state_st
178 {
179 std::array<myuint_t, N> V;
180 myuint_t sumtot;
181 int counter;
182 };
183
184 typedef struct rng_state_st rng_state_t; // struct alias
185 rng_state_t S;
186};
187
188} // namespace CLHEP
189
190#endif
void restoreStatus(const char filename[]="MixMaxRngState.conf")
Definition: MixMaxRng.cc:125
void showStatus() const
Definition: MixMaxRng.cc:205
void flatArray(const int size, double *vect)
Definition: MixMaxRng.cc:327
void setSeed(long seed, int dum=0)
Definition: MixMaxRng.cc:215
std::string name() const
Definition: MixMaxRng.h:106
static std::string beginTag()
Definition: MixMaxRng.cc:403
void saveStatus(const char filename[]="MixMaxRngState.conf") const
Definition: MixMaxRng.cc:105
virtual std::istream & get(std::istream &is)
Definition: MixMaxRng.cc:385
MixMaxRng & operator=(const MixMaxRng &rng)
Definition: MixMaxRng.cc:88
virtual std::istream & getState(std::istream &is)
Definition: MixMaxRng.cc:408
std::vector< unsigned long > put() const
Definition: MixMaxRng.cc:368
double flat()
Definition: MixMaxRng.h:67
void setSeeds(const long *seeds, int seedNum=0)
Definition: MixMaxRng.cc:226
static std::string engineName()
Definition: MixMaxRng.cc:253
#define double(obj)
Definition: excDblThrow.cc:32
std::uint32_t myID_t
Definition: MixMaxRng.h:48
unsigned long long int myuint_t
Definition: MixMaxRng.h:49