ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
81 // FUNCTIONS FOR SEEDING
82 
83 typedef uint32_t myID_t;
84 
85 void seed_uniquestream(rng_state_t* X, myID_t clusterID, myID_t machineID, myID_t runID, myID_t streamID );
86 /*
87  best choice: will make a state vector from which you can get at least 10^100 numbers
88  guaranteed mathematically to be non-colliding with any other stream prepared from another set of 32bit IDs,
89  so long as it is different by at least one bit in at least one of the four IDs
90  -- useful if you are running a parallel simulation with many clusters, many CPUs each
91  */
92 
93 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
94 
95 void seed_vielbein(rng_state_t* X, unsigned int i); // seeds with the i-th unit vector, i = 0..N-1, for testing only
96 
97 
98 
99 // FUNCTIONS FOR GETTING RANDOM NUMBERS
100 
101 #ifdef __MIXMAX_C
102  myuint get_next(rng_state_t* X); // returns 64-bit int, which is between 1 and 2^61-1 inclusive
103  double get_next_float(rng_state_t* X); // returns double precision floating point number in (0,1]
104 #endif //__MIXMAX_C
105 
106 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)
107 
108 void iterate_and_fill_array(rng_state_t* X, double *array); // fills the array with N numbers
109 
110 myuint precalc(rng_state_t* X);
111 /* needed if the state has been changed by something other than iterate, but no worries, seeding functions call this for you when necessary */
112 myuint apply_bigskip(myuint* Vout, myuint* Vin, myID_t clusterID, myID_t machineID, myID_t runID, myID_t streamID );
113 // applies a skip of some number of steps calculated from the four IDs
114 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
115 
116 
117 #define BITS 61
118 
119 /* magic with Mersenne Numbers */
120 
121 #define M61 2305843009213693951ULL
122 
123  myuint modadd(myuint foo, myuint bar);
124  myuint modmulM61(myuint s, myuint a);
125  myuint fmodmulM61(myuint cum, myuint s, myuint a);
126 
127 #define MERSBASE M61 //xSUFF(M61)
128 #define MOD_PAYNE(k) ((((k)) & MERSBASE) + (((k)) >> BITS) ) // slightly faster than my old way, ok for addition
129 #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
130 #define MOD_MERSENNE(k) MOD_PAYNE(k)
131 
132 #define INV_MERSBASE (0x1p-61)
133 
134 
135 // 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
136 
137 #if (N==256)
138 #define SPECIALMUL 0
139 #define SPECIAL 487013230256099064ULL // s=487013230256099064, m=1 -- good old MIXMAX
140 #define MOD_MULSPEC(k) fmodmulM61( 0, SPECIAL , (k) );
141 
142 #elif (N==17)
143 #define SPECIALMUL 36 // m=2^37+1
144 
145 #elif (N==8)
146 #define SPECIALMUL 53 // m=2^53+1
147 
148 #elif (N==40)
149 #define SPECIALMUL 42 // m=2^42+1
150 
151 #elif (N==96)
152 #define SPECIALMUL 55 // m=2^55+1
153 
154 #elif (N==64)
155 #define SPECIALMUL 55 // m=2^55 (!!!) and m=2^37+2
156 
157 #elif (N==120)
158 #define SPECIALMUL 51 // m=2^51+1 and a SPECIAL=+1 (!!!)
159 #define SPECIAL 1
160 #define MOD_MULSPEC(k) (k);
161 
162 #else
163 #warning Not a verified N, you are on your own!
164 #define SPECIALMUL 58
165 
166 #endif // list of interesting N for modulus M61 ends here
167 
168 
169 #ifndef __MIXMAX_C // c++ can put code into header files, why cant we? (with the inline declaration, should be safe from duplicate-symbol error)
170 
171 #define get_next(X) GET_BY_MACRO(X)
172 
173 inline myuint GET_BY_MACRO(rng_state_t* X) {
174  int i;
175  i=X->counter;
176 
177  if (i<=(N-1) ){
178  X->counter++;
179  return X->V[i];
180  }else{
181  X->sumtot = iterate_raw_vec(X->V, X->sumtot);
182  X->counter=2;
183  return X->V[1];
184  }
185 }
186 
187 #define get_next_float(X) get_next_float_BY_MACRO(X)
188 
189 
191  int64_t Z=(int64_t)get_next(X);
192 #if defined(__SSE__) && defined(USE_INLINE_ASM)
193 //#warning using SSE inline assembly for int64 -> double conversion, not really necessary in GCC-5 or better
194  double F;
195  __asm__ ("pxor %0, %0;"
196  "cvtsi2sdq %1, %0;"
197  :"=x"(F)
198  :"r"(Z)
199  );
200  return F*INV_MERSBASE;
201 #else
202  return Z*INV_MERSBASE;
203 #endif
204  }
205 
206 #endif // __MIXMAX_C
207 
208 
209 // ERROR CODES - exit() is called with these
210 #define ARRAY_INDEX_OUT_OF_BOUNDS 0xFF01
211 #define SEED_WAS_ZERO 0xFF02
212 #define ERROR_READING_STATE_FILE 0xFF03
213 #define ERROR_READING_STATE_COUNTER 0xFF04
214 #define ERROR_READING_STATE_CHECKSUM 0xFF05
215 
216 #ifdef __cplusplus
217 }
218 #endif
219 
220 //#define HOOKUP_GSL 1
221 
222 #ifdef HOOKUP_GSL // if you need to use mixmax through GSL, pass -DHOOKUP_GSL=1 to the compiler
223 
224 #include <gsl/gsl_rng.h>
225 unsigned long gsl_get_next(void *vstate);
226 double gsl_get_next_float(void *vstate);
227 void seed_for_gsl(void *vstate, unsigned long seed);
228 
229 static const gsl_rng_type mixmax_type =
230 {"MIXMAX", /* name */
231  MERSBASE, /* RAND_MAX */
232  1, /* RAND_MIN */
233  sizeof (rng_state_t),
234  &seed_for_gsl,
235  &gsl_get_next,
236  &gsl_get_next_float
237 };
238 
239 unsigned long gsl_get_next(void *vstate) {
240  rng_state_t* X= (rng_state_t*)vstate;
241  return (unsigned long)get_next(X);
242 }
243 
244 double gsl_get_next_float(void *vstate) {
245  rng_state_t* X= (rng_state_t*)vstate;
246  return ( (double)get_next(X)) * INV_MERSBASE;
247 }
248 
249 void seed_for_gsl(void *vstate, unsigned long seed){
250  rng_state_t* X= (rng_state_t*)vstate;
251  seed_spbox(X,(myuint)seed);
252 }
253 
254 const gsl_rng_type *gsl_rng_ran3 = &mixmax_type;
255 
256 
257 #endif // HOOKUP_GSL
258 
259 
260 #endif // closing ROOT_MIXMAX_H_
261 //} // namespace CLHEP
262 
TControlBar * bar
Definition: demos.C:15
int rng_free(rng_state_t *X)
Definition: mixmax.cxx:186
myuint apply_bigskip(myuint *Vout, myuint *Vin, myID_t clusterID, myID_t machineID, myID_t runID, myID_t streamID)
Definition: mixmax.cxx:395
int iterate(rng_state_t *X)
Definition: mixmax.cxx:40
myuint V[N]
Definition: mixmax.h:61
myuint modmulM61(myuint s, myuint a)
myuint precalc(rng_state_t *X)
Definition: mixmax.cxx:252
static const char * filename()
void fill_array(rng_state_t *X, unsigned int n, double *array)
Definition: mixmax.cxx:117
TArc * a
Definition: textangle.C:12
void iterate_and_fill_array(rng_state_t *X, double *array)
Definition: mixmax.cxx:137
myuint iterate_raw_vec(myuint *Y, myuint sumtotOld)
Definition: mixmax.cxx:53
void seed_uniquestream(rng_state_t *X, myID_t clusterID, myID_t machineID, myID_t runID, myID_t streamID)
Definition: mixmax.cxx:385
rng_state_t * rng_alloc()
Definition: mixmax.cxx:178
#define get_next_float(X)
Definition: mixmax.h:187
myuint fmodmulM61(myuint cum, myuint s, myuint a)
Definition: mixmax.cxx:292
void seed_vielbein(rng_state_t *X, unsigned int i)
Definition: mixmax.cxx:212
void read_state(rng_state_t *X, const char filename[])
Definition: mixmax.cxx:320
uint64_t myuint
Definition: mixmax.h:52
#define N
Definition: mixmax.h:43
#define F(x, y, z)
FILE * fh
Definition: mixmax.h:64
myuint modadd(myuint foo, myuint bar)
Definition: mixmax.cxx:162
uint32_t myID_t
Definition: mixmax.h:83
int counter
Definition: mixmax.h:63
void seed_spbox(rng_state_t *X, myuint seed)
Definition: mixmax.cxx:229
myuint sumtot
Definition: mixmax.h:62
#define MERSBASE
Definition: mixmax.h:127
myuint GET_BY_MACRO(rng_state_t *X)
Definition: mixmax.h:173
void print_state(rng_state_t *X)
Definition: mixmax.cxx:307
#define INV_MERSBASE
Definition: mixmax.h:132
#define get_next(X)
Definition: mixmax.h:171
struct rng_state_st rng_state_t
Definition: mixmax.h:67
rng_state_t * rng_copy(myuint *Y)
Definition: mixmax.cxx:192
void branch_inplace(rng_state_t *Xin, myID_t *ID)
Definition: mixmax.cxx:391
std::complex< float_v > Z
Definition: main.cpp:120
double get_next_float_BY_MACRO(rng_state_t *X)
Definition: mixmax.h:190
const Int_t n
Definition: legend1.C:16
int rng_get_N(void)
Definition: mixmax.cxx:264