ROOT  6.06/09
Reference Guide
mixmax.h
Go to the documentation of this file.
1 // $Id:$
2 // -*- C++ -*-
3 //
4 // -----------------------------------------------------------------------
5 // MixMax Matrix PseudoRandom Number Generator
6 // --- MixMax ---
7 // class header file
8 // -----------------------------------------------------------------------
9 //
10 //
11 // Created by Konstantin Savvidy on Sun Feb 22 2004.
12 // The code is released under
13 // GNU Lesser General Public License v3
14 //
15 // Generator described in
16 // N.Z.Akopov, G.K.Savvidy and N.G.Ter-Arutyunian, Matrix Generator of Pseudorandom Numbers,
17 // J.Comput.Phys. 97, 573 (1991);
18 // Preprint EPI-867(18)-86, Yerevan Jun.1986;
19 //
20 // and
21 //
22 // K.Savvidy
23 // The MIXMAX random number generator
24 // Comp. Phys. Commun. (2015)
25 // http://dx.doi.org/10.1016/j.cpc.2015.06.003
26 //
27 // -----------------------------------------------------------------------
28 
29 #ifndef ROOT_MIXMAX_H_
30 #define ROOT_MIXMAX_H_ 1
31 
32 //#define USE_INLINE_ASM //DP: uncomment if want to use inline asm
33 
34 #include <stdio.h>
35 #include <stdint.h>
36 
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 #ifndef _N
43 #define N 256
44 /* The currently recommended N are 3150, 1260, 508, 256, 240, 88
45  Since the algorithm is linear in N, the cost per number is almost independent of N.
46  */
47 #else
48 #define N _N
49 #endif
50 
51 #ifndef __LP64__
52 typedef uint64_t myuint;
53 //#warning but no problem, 'myuint' is 'uint64_t'
54 #else
55 typedef unsigned long long int myuint;
56 //#warning but no problem, 'myuint' is 'unsigned long long int'
57 #endif
58 
60 {
61  myuint V[N];
62  myuint sumtot;
63  int counter;
64  FILE* fh;
65 };
66 
67 typedef struct rng_state_st rng_state_t; // C struct alias
68 
69 int rng_get_N(void); // get the N programmatically, useful for checking the value for which the library was compiled
70 
71 rng_state_t *rng_alloc(); /* allocate the state */
72 int rng_free(rng_state_t* X); /* free memory occupied by the state */
73 rng_state_t *rng_copy(myuint *Y); /* init from vector, takes the vector Y,
74  returns pointer to the newly allocated and initialized state */
75 void read_state(rng_state_t* X, const char filename[] );
76 void print_state(rng_state_t* X);
77 int iterate(rng_state_t* X);
78 myuint iterate_raw_vec(myuint* Y, myuint sumtotOld);
79 
80 void set_skip_number(int n);
81 void set_first_return_element(int n);
82 int get_skip_number();
84 
85 
86 // FUNCTIONS FOR SEEDING
87 
88 typedef uint32_t myID_t;
89 
90 void seed_uniquestream(rng_state_t* X, myID_t clusterID, myID_t machineID, myID_t runID, myID_t streamID );
91 /*
92  best choice: will make a state vector from which you can get at least 10^100 numbers
93  guaranteed mathematically to be non-colliding with any other stream prepared from another set of 32bit IDs,
94  so long as it is different by at least one bit in at least one of the four IDs
95  -- useful if you are running a parallel simulation with many clusters, many CPUs each
96  */
97 
98 void seed_spbox(rng_state_t* X, myuint seed); // non-linear method, makes certified unique vectors, probability for streams to collide is < 1/10^4600
99 
100 void seed_vielbein(rng_state_t* X, unsigned int i); // seeds with the i-th unit vector, i = 0..N-1, for testing only
101 
102 
103 
104 // FUNCTIONS FOR GETTING RANDOM NUMBERS
105 
106 #ifdef __MIXMAX_C
107  myuint get_next(rng_state_t* X); // returns 64-bit int, which is between 1 and 2^61-1 inclusive
108  double get_next_float(rng_state_t* X); // returns double precision floating point number in (0,1]
109 #endif //__MIXMAX_C
110 
111 void fill_array(rng_state_t* X, unsigned int n, double *array); // fastest method: set n to a multiple of N (e.g. n=256)
112 
113 void iterate_and_fill_array(rng_state_t* X, double *array); // fills the array with N numbers
114 
115 myuint precalc(rng_state_t* X);
116 /* needed if the state has been changed by something other than iterate, but no worries, seeding functions call this for you when necessary */
117 myuint apply_bigskip(myuint* Vout, myuint* Vin, myID_t clusterID, myID_t machineID, myID_t runID, myID_t streamID );
118 // applies a skip of some number of steps calculated from the four IDs
119 void branch_inplace( rng_state_t* Xin, myID_t* ID ); // almost the same as apply_bigskip, but in-place and from a vector of IDs
120 
121 
122 #define BITS 61
123 
124 /* magic with Mersenne Numbers */
125 
126 #define M61 2305843009213693951ULL
127 
128  myuint modadd(myuint foo, myuint bar);
129  myuint modmulM61(myuint s, myuint a);
130  myuint fmodmulM61(myuint cum, myuint s, myuint a);
131 
132 #define MERSBASE M61 //xSUFF(M61)
133 #define MOD_PAYNE(k) ((((k)) & MERSBASE) + (((k)) >> BITS) ) // slightly faster than my old way, ok for addition
134 #define MOD_REM(k) ((k) % MERSBASE ) // latest Intel CPU is supposed to do this in one CPU cycle, but on my machines it seems to be 20% slower than the best tricks
135 #define MOD_MERSENNE(k) MOD_PAYNE(k)
136 
137 /// #define INV_MERSBASE (0x1p-61)
138 #define INV_MERSBASE (0.433680868994201773791060216479542685926876E-18L) // that is 1/(2^61-1)
139 
140 // the charpoly is irreducible for the combinations of N and SPECIAL and has maximal period for N=508, 256, half period for 1260, and 1/12 period for 3150
141 
142 #if (N==256)
143 #define SPECIALMUL 0
144 #define SPECIAL -1 // 487013230256099064ULL // s=487013230256099064, m=1 -- good old MIXMAX
145 #define MOD_MULSPEC(k) fmodmulM61( 0, SPECIAL , (k) );
146 
147 #elif (N==17)
148 #define SPECIALMUL 36 // m=2^37+1
149 
150 #elif (N==8)
151 #define SPECIALMUL 53 // m=2^53+1
152 
153 #elif (N==40)
154 #define SPECIALMUL 42 // m=2^42+1
155 
156 #elif (N==96)
157 #define SPECIALMUL 55 // m=2^55+1
158 
159 #elif (N==64)
160 #define SPECIALMUL 55 // m=2^55 (!!!) and m=2^37+2
161 
162 #elif (N==120)
163 #define SPECIALMUL 51 // m=2^51+1 and a SPECIAL=+1 (!!!)
164 #define SPECIAL 1
165 #define MOD_MULSPEC(k) (k);
166 
167 #else
168 #warning Not a verified N, you are on your own!
169 #define SPECIALMUL 58
170 
171 #endif // list of interesting N for modulus M61 ends here
172 
173 
174 #ifndef __MIXMAX_C // c++ can put code into header files, why cant we? (with the inline declaration, should be safe from duplicate-symbol error)
175 
176 #define get_next(X) GET_BY_MACRO(X)
177 
178 inline myuint GET_BY_MACRO(rng_state_t* X) {
179  int i;
180  i=X->counter;
181 
182  if (i<=(N-1) ){
183  X->counter++;
184  return X->V[i];
185  }else{
186  int niter = get_skip_number() +1;
187  for (int iter = 0; iter < niter; ++iter) {
188  X->sumtot = iterate_raw_vec(X->V, X->sumtot);
189  }
190  int element = get_first_return_element();
191  X->counter= element+1;
192  return X->V[element];
193  }
194 }
195 
196 #define get_next_float(X) get_next_float_BY_MACRO(X)
197 
198 
200  int64_t Z=(int64_t)get_next(X);
201 #if defined(__SSE__) && defined(USE_INLINE_ASM)
202 //#warning using SSE inline assembly for int64 -> double conversion, not really necessary in GCC-5 or better
203  double F;
204  __asm__ ("pxor %0, %0;"
205  "cvtsi2sdq %1, %0;"
206  :"=x"(F)
207  :"r"(Z)
208  );
209  return F*INV_MERSBASE;
210 #else
211  return Z*INV_MERSBASE;
212 #endif
213  }
214 
215 #endif // __MIXMAX_C
216 
217 
218 // ERROR CODES - exit() is called with these
219 #define ARRAY_INDEX_OUT_OF_BOUNDS 0xFF01
220 #define SEED_WAS_ZERO 0xFF02
221 #define ERROR_READING_STATE_FILE 0xFF03
222 #define ERROR_READING_STATE_COUNTER 0xFF04
223 #define ERROR_READING_STATE_CHECKSUM 0xFF05
224 
225 #ifdef __cplusplus
226 }
227 #endif
228 
229 //#define HOOKUP_GSL 1
230 
231 #ifdef HOOKUP_GSL // if you need to use mixmax through GSL, pass -DHOOKUP_GSL=1 to the compiler
232 
233 #include <gsl/gsl_rng.h>
234 unsigned long gsl_get_next(void *vstate);
235 double gsl_get_next_float(void *vstate);
236 void seed_for_gsl(void *vstate, unsigned long seed);
237 
238 static const gsl_rng_type mixmax_type =
239 {"MIXMAX", /* name */
240  MERSBASE, /* RAND_MAX */
241  1, /* RAND_MIN */
242  sizeof (rng_state_t),
243  &seed_for_gsl,
244  &gsl_get_next,
245  &gsl_get_next_float
246 };
247 
248 unsigned long gsl_get_next(void *vstate) {
249  rng_state_t* X= (rng_state_t*)vstate;
250  return (unsigned long)get_next(X);
251 }
252 
253 double gsl_get_next_float(void *vstate) {
254  rng_state_t* X= (rng_state_t*)vstate;
255  return ( (double)get_next(X)) * INV_MERSBASE;
256 }
257 
258 void seed_for_gsl(void *vstate, unsigned long seed){
259  rng_state_t* X= (rng_state_t*)vstate;
260  seed_spbox(X,(myuint)seed);
261 }
262 
263 const gsl_rng_type *gsl_rng_ran3 = &mixmax_type;
264 
265 
266 #endif // HOOKUP_GSL
267 
268 
269 #endif // closing ROOT_MIXMAX_H_
270 //} // namespace CLHEP
271 
int rng_free(rng_state_t *X)
Definition: mixmax.cxx:212
myuint apply_bigskip(myuint *Vout, myuint *Vin, myID_t clusterID, myID_t machineID, myID_t runID, myID_t streamID)
Definition: mixmax.cxx:421
int iterate(rng_state_t *X)
Definition: mixmax.cxx:57
void set_first_return_element(int n)
Definition: mixmax.cxx:46
void set_skip_number(int n)
Definition: mixmax.cxx:43
myuint V[N]
Definition: mixmax.h:61
myuint modmulM61(myuint s, myuint a)
myuint precalc(rng_state_t *X)
Definition: mixmax.cxx:278
static const char * filename()
void fill_array(rng_state_t *X, unsigned int n, double *array)
Definition: mixmax.cxx:141
TArc * a
Definition: textangle.C:12
void iterate_and_fill_array(rng_state_t *X, double *array)
Definition: mixmax.cxx:163
myuint iterate_raw_vec(myuint *Y, myuint sumtotOld)
Definition: mixmax.cxx:72
void seed_uniquestream(rng_state_t *X, myID_t clusterID, myID_t machineID, myID_t runID, myID_t streamID)
Definition: mixmax.cxx:411
rng_state_t * rng_alloc()
Definition: mixmax.cxx:204
#define get_next_float(X)
Definition: mixmax.h:196
myuint fmodmulM61(myuint cum, myuint s, myuint a)
Definition: mixmax.cxx:318
void seed_vielbein(rng_state_t *X, unsigned int i)
Definition: mixmax.cxx:238
void read_state(rng_state_t *X, const char filename[])
Definition: mixmax.cxx:346
std::map< std::string, std::string >::const_iterator iter
Definition: TAlienJob.cxx:54
uint64_t myuint
Definition: mixmax.h:52
#define N
Definition: mixmax.h:43
int get_first_return_element()
Definition: mixmax.cxx:53
#define F(x, y, z)
FILE * fh
Definition: mixmax.h:64
myuint modadd(myuint foo, myuint bar)
Definition: mixmax.cxx:188
uint32_t myID_t
Definition: mixmax.h:88
int counter
Definition: mixmax.h:63
void seed_spbox(rng_state_t *X, myuint seed)
Definition: mixmax.cxx:255
myuint sumtot
Definition: mixmax.h:62
#define MERSBASE
Definition: mixmax.h:132
myuint GET_BY_MACRO(rng_state_t *X)
Definition: mixmax.h:178
void print_state(rng_state_t *X)
Definition: mixmax.cxx:333
#define INV_MERSBASE
#define INV_MERSBASE (0x1p-61)
Definition: mixmax.h:138
int get_skip_number()
Definition: mixmax.cxx:50
#define get_next(X)
Definition: mixmax.h:176
struct rng_state_st rng_state_t
Definition: mixmax.h:67
rng_state_t * rng_copy(myuint *Y)
Definition: mixmax.cxx:218
void branch_inplace(rng_state_t *Xin, myID_t *ID)
Definition: mixmax.cxx:417
std::complex< float_v > Z
Definition: main.cpp:120
double get_next_float_BY_MACRO(rng_state_t *X)
Definition: mixmax.h:199
const Int_t n
Definition: legend1.C:16
int rng_get_N(void)
Definition: mixmax.cxx:290