CLHEP 2.4.6.4
C++ Class Library for High Energy Physics
Loading...
Searching...
No Matches
RanshiEngine.cc
Go to the documentation of this file.
1//
2// -*- C++ -*-
3//
4// -----------------------------------------------------------------------
5// HEP Random
6// --- RanshiEngine ---
7// class implementation file
8// -----------------------------------------------------------------------
9//
10// This algorithm implements the random number generator as proposed by
11// "F. Gutbrod, Comp. Phys. Comm. 87 (1995) 291-306".
12//
13// =======================================================================
14// Ken Smith - Created: 9th June 1998
15// - Removed std::pow() from flat method: 21st Jul 1998
16// - Added conversion operators: 6th Aug 1998
17// J. Marraffino - Added some explicit casts to deal with
18// machines where sizeof(int) != sizeof(long) 22 Aug 1998
19// M. Fischler - Modified constructors taking seeds to not
20// depend on numEngines (same seeds should
21// produce same sequences). Default still
22// depends on numEngines. 16 Sep 1998
23// - Modified use of the various exponents of 2
24// to avoid per-instance space overhead and
25// correct the rounding procedure 16 Sep 1998
26// J. Marraffino - Remove dependence on hepString class 13 May 1999
27// M. Fischler - In restore, checkFile for file not found 03 Dec 2004
28// M. Fischler - Methods for instance save/restore 12/8/04
29// M. Fischler - split get() into tag validation and
30// getState() for anonymous restores 12/27/04
31// M. Fischler - State-saving using only ints, for portability 4/12/05
32// L. Garren - use explicit 32bit mask to avoid compiler warnings 6/6/2014
33// L. Garren - adding pragma for 32bit gcc 4.9 11/20/2014
34//
35// =======================================================================
36
37#include "CLHEP/Random/defs.h"
38#include "CLHEP/Random/RanshiEngine.h"
39#include "CLHEP/Random/engineIDulong.h"
40#include "CLHEP/Utility/atomic_int.h"
41
42#include <atomic>
43#include <string.h> // for strcmp
44#include <iostream>
45#include <string>
46#include <vector>
47
48// don't generate warnings about agressive loop optimization
49#if defined __GNUC__
50 #if __GNUC__ > 3 && __GNUC_MINOR__ > 8
51 #pragma GCC diagnostic push
52 #pragma GCC diagnostic ignored "-Waggressive-loop-optimizations"
53 #endif
54#endif
55
56namespace CLHEP {
57
58namespace {
59 // Number of instances with automatic seed selection
60 CLHEP_ATOMIC_INT_TYPE numberOfEngines(0);
61}
62
63static const int MarkerLen = 64; // Enough room to hold a begin or end marker.
64
65std::string RanshiEngine::name() const {return "RanshiEngine";}
66
69 halfBuff(0), numFlats(0)
70{
71 int numEngines = numberOfEngines++;
72 int i = 0;
73 while (i < numBuff) {
74 buffer[i] = (unsigned int)((numEngines+19780503L*(i+1))& 0xffffffff);
75 ++i;
76 }
77 theSeed = numEngines+19780503L*++i;
78 redSpin = (unsigned int)(theSeed & 0xffffffff);
79
80 for( i = 0; i < 10000; ++i) flat(); // Warm-up by running thorugh 10000 nums
81}
82
85 halfBuff(0), numFlats(0)
86{
87 is >> *this;
88}
89
92 halfBuff(0), numFlats(0)
93{
94 for (int i = 0; i < numBuff; ++i) {
95 buffer[i] = (unsigned int)seed&0xffffffff;
96 }
97 theSeed = seed;
98 redSpin = (unsigned int)(theSeed & 0xffffffff);
99 int j;
100 for (j = 0; j < numBuff*20; ++j) { // "warm-up" for engine to hit
101 flat(); // every ball on average 20X.
102 }
103}
104
105RanshiEngine::RanshiEngine(int rowIndex, int colIndex)
107 halfBuff(0), numFlats(0)
108{
109 int i = 0;
110 while( i < numBuff ) {
111 buffer[i] = (unsigned int)((rowIndex + (i+1)*(colIndex+8))&0xffffffff);
112 ++i;
113 }
114 theSeed = rowIndex;
115 redSpin = colIndex & 0xffffffff;
116 for( i = 0; i < 100; ++i) flat(); // Warm-up by running thorugh 100 nums
117}
118
120
122 unsigned int redAngle = (((numBuff/2) - 1) & redSpin) + halfBuff;
123 unsigned int blkSpin = buffer[redAngle] & 0xffffffff;
124 unsigned int boostResult = blkSpin ^ redSpin;
125
126 buffer[redAngle] = ((blkSpin << 17) | (blkSpin >> (32-17))) ^ redSpin;
127
128 redSpin = (blkSpin + numFlats++) & 0xffffffff;
129 halfBuff = numBuff/2 - halfBuff;
130
131 return ( blkSpin * twoToMinus_32() + // most significant part
132 (boostResult>>11) * twoToMinus_53() + // fill in remaining bits
133 nearlyTwoToMinus_54()); // non-zero
134}
135
136void RanshiEngine::flatArray(const int size, double* vect) {
137 for (int i = 0; i < size; ++i) {
138 vect[i] = flat();
139 }
140}
141
142void RanshiEngine::setSeed(long seed, int) {
143 *this = RanshiEngine(seed);
144}
145
146void RanshiEngine::setSeeds(const long* seeds, int) {
147 if (*seeds) {
148 int i = 0;
149 while (seeds[i] && i < numBuff) {
150 buffer[i] = (unsigned int)seeds[i];
151 ++i;
152 }
153 while (i < numBuff) {
154 buffer[i] = buffer[i-1];
155 ++i;
156 }
157 theSeed = seeds[0];
158 redSpin = (unsigned int)theSeed;
159 }
160 theSeeds = seeds;
161}
162
163void RanshiEngine::saveStatus(const char filename[]) const {
164 std::ofstream outFile(filename, std::ios::out);
165 if (!outFile.bad()) {
166 outFile << "Uvec\n";
167 std::vector<unsigned long> v = put();
168 #ifdef TRACE_IO
169 std::cout << "Result of v = put() is:\n";
170 #endif
171 for (unsigned int i=0; i<v.size(); ++i) {
172 outFile << v[i] << "\n";
173 #ifdef TRACE_IO
174 std::cout << v[i] << " ";
175 if (i%6==0) std::cout << "\n";
176 #endif
177 }
178 #ifdef TRACE_IO
179 std::cout << "\n";
180 #endif
181 }
182#ifdef REMOVED
183 if (!outFile.bad()) {
184 outFile << std::setprecision(20) << theSeed << std::endl;
185 for (int i = 0; i < numBuff; ++i) {
186 outFile << buffer[i] << " ";
187 }
188 outFile << redSpin << " " << numFlats << " " << halfBuff << std::endl;
189 }
190#endif
191}
192
193void RanshiEngine::restoreStatus(const char filename[]) {
194 std::ifstream inFile(filename, std::ios::in);
195 if (!checkFile ( inFile, filename, engineName(), "restoreStatus" )) {
196 std::cerr << " -- Engine state remains unchanged\n";
197 return;
198 }
199 if ( possibleKeywordInput ( inFile, "Uvec", theSeed ) ) {
200 std::vector<unsigned long> v;
201 unsigned long xin;
202 for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
203 inFile >> xin;
204 #ifdef TRACE_IO
205 std::cout << "ivec = " << ivec << " xin = " << xin << " ";
206 if (ivec%3 == 0) std::cout << "\n";
207 #endif
208 if (!inFile) {
209 inFile.clear(std::ios::badbit | inFile.rdstate());
210 std::cerr << "\nRanshiEngine state (vector) description improper."
211 << "\nrestoreStatus has failed."
212 << "\nInput stream is probably mispositioned now." << std::endl;
213 return;
214 }
215 v.push_back(xin);
216 }
217 getState(v);
218 return;
219 }
220
221 if (!inFile.bad()) {
222// inFile >> theSeed; removed -- encompased by possibleKeywordInput
223 for (int i = 0; i < numBuff; ++i) {
224 inFile >> buffer[i];
225 }
226 inFile >> redSpin >> numFlats >> halfBuff;
227 }
228}
229
231 std::cout << std::setprecision(20) << std::endl;
232 std::cout << "----------- Ranshi engine status ----------" << std::endl;
233 std::cout << "Initial seed = " << theSeed << std::endl;
234 std::cout << "Current red spin = " << redSpin << std::endl;
235 std::cout << "Values produced = " << numFlats << std::endl;
236 std::cout << "Side of buffer = " << (halfBuff ? "upper" : "lower")
237 << std::endl;
238 std::cout << "Current buffer = " << std::endl;
239 for (int i = 0; i < numBuff; i+=4) {
240 std::cout << std::setw(10) << std::setiosflags(std::ios::right)
241 << buffer[i] << std::setw(11) << buffer[i+1] << std::setw(11)
242 << buffer[i+2] << std::setw(11) << buffer[i+3] << std::endl;
243 }
244 std::cout << "-------------------------------------------" << std::endl;
245}
246
247RanshiEngine::operator double() {
248 return flat();
249}
250
251RanshiEngine::operator float() {
252 unsigned int redAngle = (((numBuff/2) - 1) & redSpin) + halfBuff;
253 unsigned int blkSpin = buffer[redAngle] & 0xffffffff;
254
255 buffer[redAngle] = ((blkSpin << 17) | (blkSpin >> (32-17))) ^ redSpin;
256
257 redSpin = (blkSpin + numFlats++) & 0xffffffff;
258 halfBuff = numBuff/2 - halfBuff;
259
260 return float(blkSpin * twoToMinus_32());
261}
262
263RanshiEngine::operator unsigned int() {
264 unsigned int redAngle = (((numBuff/2) - 1) & redSpin) + halfBuff;
265 unsigned int blkSpin = buffer[redAngle] & 0xffffffff;
266
267 buffer[redAngle] = ((blkSpin << 17) | (blkSpin >> (32-17))) ^ redSpin;
268
269 redSpin = (blkSpin + numFlats++) & 0xffffffff;
270 halfBuff = numBuff/2 - halfBuff;
271
272 return blkSpin;
273}
274
275std::ostream& RanshiEngine::put (std::ostream& os ) const {
276 char beginMarker[] = "RanshiEngine-begin";
277 os << beginMarker << "\nUvec\n";
278 std::vector<unsigned long> v = put();
279 for (unsigned int i=0; i<v.size(); ++i) {
280 os << v[i] << "\n";
281 }
282 return os;
283#ifdef REMOVED
284 char endMarker[] = "RanshiEngine-end";
285 long pr=os.precision(20);
286 os << " " << beginMarker << " ";
287
288 os << theSeed << "\n";
289 for (int i = 0; i < numBuff; ++i) {
290 os << buffer[i] << "\n";
291 }
292 os << redSpin << " " << numFlats << "\n" << halfBuff;
293
294 os << " " << endMarker << "\n";
295 os.precision(pr);
296 return os;
297#endif
298}
299
300std::vector<unsigned long> RanshiEngine::put () const {
301 std::vector<unsigned long> v;
302 v.push_back (engineIDulong<RanshiEngine>());
303 for (int i = 0; i < numBuff; ++i) {
304 v.push_back(static_cast<unsigned long>(buffer[i]));
305 }
306 v.push_back(static_cast<unsigned long>(redSpin));
307 v.push_back(static_cast<unsigned long>(numFlats));
308 v.push_back(static_cast<unsigned long>(halfBuff));
309 return v;
310}
311
312std::istream& RanshiEngine::get (std::istream& is) {
313 char beginMarker [MarkerLen];
314 is >> std::ws;
315 is.width(MarkerLen); // causes the next read to the char* to be <=
316 // that many bytes, INCLUDING A TERMINATION \0
317 // (Stroustrup, section 21.3.2)
318 is >> beginMarker;
319 if (strcmp(beginMarker,"RanshiEngine-begin")) {
320 is.clear(std::ios::badbit | is.rdstate());
321 std::cerr << "\nInput mispositioned or"
322 << "\nRanshiEngine state description missing or"
323 << "\nwrong engine type found." << std::endl;
324 return is;
325 }
326 return getState(is);
327}
328
329std::string RanshiEngine::beginTag ( ) {
330 return "RanshiEngine-begin";
331}
332
333std::istream& RanshiEngine::getState (std::istream& is) {
334 if ( possibleKeywordInput ( is, "Uvec", theSeed ) ) {
335 std::vector<unsigned long> v;
336 unsigned long uu;
337 for (unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
338 is >> uu;
339 if (!is) {
340 is.clear(std::ios::badbit | is.rdstate());
341 std::cerr << "\nRanshiEngine state (vector) description improper."
342 << "\ngetState() has failed."
343 << "\nInput stream is probably mispositioned now." << std::endl;
344 return is;
345 }
346 v.push_back(uu);
347 }
348 getState(v);
349 return (is);
350 }
351
352// is >> theSeed; Removed, encompassed by possibleKeywordInput()
353
354 char endMarker [MarkerLen];
355 for (int i = 0; i < numBuff; ++i) {
356 is >> buffer[i];
357 }
358 is >> redSpin >> numFlats >> halfBuff;
359 is >> std::ws;
360 is.width(MarkerLen);
361 is >> endMarker;
362 if (strcmp(endMarker,"RanshiEngine-end")) {
363 is.clear(std::ios::badbit | is.rdstate());
364 std::cerr << "\nRanshiEngine state description incomplete."
365 << "\nInput stream is probably mispositioned now." << std::endl;
366 return is;
367 }
368 return is;
369}
370
371bool RanshiEngine::get (const std::vector<unsigned long> & v) {
372 if ((v[0] & 0xffffffffUL) != engineIDulong<RanshiEngine>()) {
373 std::cerr <<
374 "\nRanshiEngine get:state vector has wrong ID word - state unchanged\n";
375 return false;
376 }
377 return getState(v);
378}
379
380bool RanshiEngine::getState (const std::vector<unsigned long> & v) {
381 if (v.size() != VECTOR_STATE_SIZE ) {
382 std::cerr <<
383 "\nRanshiEngine get:state vector has wrong length - state unchanged\n";
384 return false;
385 }
386 for (int i = 0; i < numBuff; ++i) {
387 buffer[i] = (unsigned int)v[i+1];
388 }
389 redSpin = (unsigned int)v[numBuff+1];
390 numFlats = (unsigned int)v[numBuff+2];
391 halfBuff = (unsigned int)v[numBuff+3];
392 return true;
393}
394
395} // namespace CLHEP
396
397#if defined __GNUC__
398 #if __GNUC__ > 3 && __GNUC_MINOR__ > 8
399 #pragma GCC diagnostic pop
400 #endif
401#endif
#define CLHEP_ATOMIC_INT_TYPE
Definition: atomic_int.h:25
static double twoToMinus_32()
static double twoToMinus_53()
static double nearlyTwoToMinus_54()
static bool checkFile(std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
Definition: RandomEngine.cc:49
std::string name() const
Definition: RanshiEngine.cc:65
virtual std::istream & getState(std::istream &is)
static std::string beginTag()
void flatArray(const int size, double *vect)
virtual std::istream & get(std::istream &is)
void saveStatus(const char filename[]="RanshiEngine.conf") const
std::vector< unsigned long > put() const
static std::string engineName()
Definition: RanshiEngine.h:97
void restoreStatus(const char filename[]="RanshiEngine.conf")
void setSeeds(const long *seeds, int)
void setSeed(long seed, int)
void showStatus() const
#define double(obj)
Definition: excDblThrow.cc:32
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
Definition: RandomEngine.h:168