[3602] | 1 | #include "machdefs.h"
|
---|
| 2 | #include <math.h>
|
---|
| 3 | #include <stdlib.h>
|
---|
[3619] | 4 | #include <string.h>
|
---|
[3602] | 5 | #include "thsafeop.h"
|
---|
| 6 | #include "fiondblock.h"
|
---|
| 7 |
|
---|
| 8 | #include "randr48.h"
|
---|
| 9 |
|
---|
| 10 | namespace SOPHYA {
|
---|
| 11 |
|
---|
[3838] | 12 | /*!
|
---|
| 13 | \class DR48RandGen
|
---|
| 14 | \ingroup BaseTools
|
---|
| 15 | \brief Implementation of the RandomGeneratorInterface class using drand48() functions
|
---|
| 16 |
|
---|
| 17 | Its PPF handler can be used to save the complete state of the class and the underlying
|
---|
| 18 | random number generator used.
|
---|
| 19 |
|
---|
| 20 | \sa SOPHYA::ObjFileIO<ThSDR48RandGen>
|
---|
| 21 |
|
---|
| 22 | */
|
---|
| 23 |
|
---|
[3739] | 24 | static bool dr48_first = true;
|
---|
| 25 | DR48RandGen::DR48RandGen()
|
---|
| 26 | {
|
---|
| 27 | if (dr48_first) {
|
---|
| 28 | Next(); dr48_first=false;
|
---|
| 29 | }
|
---|
| 30 | }
|
---|
| 31 |
|
---|
[3602] | 32 | DR48RandGen::DR48RandGen(long int seed)
|
---|
| 33 | {
|
---|
| 34 | srand48(seed);
|
---|
[3739] | 35 | dr48_first=false;
|
---|
[3602] | 36 | }
|
---|
| 37 |
|
---|
| 38 | DR48RandGen::~DR48RandGen()
|
---|
| 39 | {
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[3615] | 42 | void DR48RandGen::ShowRandom()
|
---|
| 43 | {
|
---|
| 44 | cout<<"RandomGenerator is DR48RandGen"<<endl;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[3889] | 47 | /*!
|
---|
| 48 | \brief Initialize the state of the random generator object using the integer value argument
|
---|
| 49 |
|
---|
| 50 | \warning This method changes the global random generator drand48() state
|
---|
| 51 | */
|
---|
[3602] | 52 | void DR48RandGen::SetSeed(long int seed)
|
---|
| 53 | {
|
---|
| 54 | srand48(seed);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[3889] | 57 | /*!
|
---|
| 58 | \brief Initialize the state of the random generator object using the short integer array
|
---|
| 59 |
|
---|
| 60 | \warning This method changes the global random generator drand48() state
|
---|
| 61 | */
|
---|
[3602] | 62 | void DR48RandGen::SetSeed(uint_2 seed[3])
|
---|
| 63 | {
|
---|
| 64 | seed48(seed);
|
---|
| 65 | }
|
---|
[3889] | 66 |
|
---|
| 67 | //! Return the random generator state
|
---|
[3602] | 68 | void DR48RandGen::GetSeed(uint_2 seed[3])
|
---|
| 69 | {
|
---|
| 70 | uint_2 *p, seed_dummy[3] = {0,0,0};
|
---|
| 71 | p = seed48(seed_dummy);
|
---|
| 72 | memcpy(seed,p,3*sizeof(uint_2));
|
---|
| 73 | // on re-initialise a ce qui etait avant
|
---|
| 74 | seed48(seed);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | r_8 DR48RandGen::Next()
|
---|
| 78 | {
|
---|
| 79 | return drand48();
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[3889] | 82 | /*!
|
---|
| 83 | \brief Random Initialization of the state of the random generator object using the system clock
|
---|
| 84 |
|
---|
| 85 | \warning This method changes the global random generator drand48() state
|
---|
| 86 | */
|
---|
[3602] | 87 | void DR48RandGen::AutoInit(int lp)
|
---|
| 88 | {
|
---|
| 89 | vector<uint_2> seed;
|
---|
| 90 | GenerateSeedVector(0,seed,lp);
|
---|
| 91 | uint_2 s[3] = {seed[0],seed[1],seed[2]};
|
---|
| 92 | SetSeed(s);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | //------------------------------------------------------------
|
---|
| 96 | //------------------------------------------------------------
|
---|
| 97 | //------------------------------------------------------------
|
---|
| 98 | /*!
|
---|
| 99 | \class ThSDR48RandGen
|
---|
| 100 | \ingroup BaseTools
|
---|
[3838] | 101 | \brief Thread-safe version of DR48RandGen random number generator using drand48() functions.
|
---|
[3602] | 102 |
|
---|
[3838] | 103 |
|
---|
| 104 | Several instances of this class can be used in different threads without the risk of
|
---|
| 105 | corrupting the internal state of the drand48() generator. However, in multi-thread applications,
|
---|
| 106 | there is no guarantee to obtain the same sequence of numbers in each thread.
|
---|
[3602] | 107 | Its PPF handler can be used to save the complete state of the class and the underlying
|
---|
[3838] | 108 | random number generator (drand48() which is used.
|
---|
[3602] | 109 |
|
---|
| 110 | \sa SOPHYA::ObjFileIO<ThSDR48RandGen>
|
---|
| 111 |
|
---|
[3838] | 112 | \code
|
---|
| 113 | // A.1- Create a thread safe generator based on drand48()
|
---|
| 114 | ThSDR48RandGen rg;
|
---|
| 115 | // A.2- Auto initilize its state (using the system time)
|
---|
| 116 | rg.AutoInit();
|
---|
| 117 | // A.3- compute and print a smal sequence of random numbers
|
---|
| 118 | int N = 10;
|
---|
| 119 | for(int i=0; i<N; i++)
|
---|
| 120 | cout << " I=" << i << " rand_flat01= " << rg.Flat01() << " rand.gaussian= " << rg.Gaussian() << endl;
|
---|
| 121 | // A.4- Save the generator state for subsequent use
|
---|
| 122 | POutPersist po("rg.ppf");
|
---|
| 123 | po << rg;
|
---|
| 124 | // A.5- compute and print a second sequence of random numbers
|
---|
| 125 | for(int i=0; i<N; i++)
|
---|
| 126 | cout << "++ I=" << i+N << " rand_flat01= " << rg.Flat01() << " rand.gaussian= " << rg.Gaussian() << endl;
|
---|
| 127 |
|
---|
| 128 | ... In another program :
|
---|
| 129 |
|
---|
| 130 | // B.1- Create and initialize the generator from the previously saved state
|
---|
| 131 | ThSDR48RandGen rgr;
|
---|
| 132 | PInPersist pin("rg.ppf");
|
---|
| 133 | pin >> rgr;
|
---|
| 134 | int N = 10;
|
---|
| 135 | // B.2- Compute and print a sequence of random number, should be compared to the sequance A.5
|
---|
| 136 | for(int i=0; i<N; i++)
|
---|
| 137 | cout << "-- I=" << i << " rand_flat01= " << rgr.Flat01() << " rand.gaussian= " << rgr.Gaussian() << endl;
|
---|
| 138 |
|
---|
| 139 |
|
---|
| 140 | \endcode
|
---|
[3602] | 141 | */
|
---|
| 142 |
|
---|
| 143 | // Objet statique global pour gestion de lock entre threads
|
---|
| 144 | static ThSafeOp* ths_rand = NULL;
|
---|
| 145 |
|
---|
[3838] | 146 | /*!
|
---|
| 147 | \brief Constructor with optional specification of the internal buffer size and thread-safety flag
|
---|
| 148 |
|
---|
| 149 | The behaviour of the base class DR48RandGen can be reproduced by specifying tsafe=false
|
---|
| 150 | \param n : an internal buffer of size n is created and filled through block calls to drand48()
|
---|
| 151 | \param tsafe : if false, creates a non thread-safe generator
|
---|
| 152 | */
|
---|
| 153 |
|
---|
[3602] | 154 | ThSDR48RandGen::ThSDR48RandGen(size_t n, bool tsafe)
|
---|
| 155 | {
|
---|
| 156 | if (ths_rand == NULL) ths_rand = new ThSafeOp;
|
---|
| 157 | if (tsafe) { // thread-safe
|
---|
| 158 | fg_nothrsafe = false;
|
---|
| 159 | if (n < 1) n = 1024;
|
---|
| 160 | rseq_.ReSize(n, false);
|
---|
| 161 | idx_ = n;
|
---|
| 162 | }
|
---|
| 163 | else { // NOT thread-safe
|
---|
| 164 | fg_nothrsafe = true;
|
---|
| 165 | idx_ = 1;
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | ThSDR48RandGen::ThSDR48RandGen(ThSDR48RandGen const & rg)
|
---|
| 170 | {
|
---|
| 171 | if (ths_rand == NULL) ths_rand = new ThSafeOp;
|
---|
| 172 | if (!rg.fg_nothrsafe) { // thread-safe
|
---|
| 173 | fg_nothrsafe = false;
|
---|
| 174 | rseq_.ReSize(rg.rseq_.Size(), false);
|
---|
| 175 | idx_ = rseq_.Size();
|
---|
| 176 | }
|
---|
| 177 | else { // NOT thread-safe
|
---|
| 178 | fg_nothrsafe = true;
|
---|
| 179 | idx_ = 1;
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 |
|
---|
| 184 | ThSDR48RandGen::~ThSDR48RandGen(void)
|
---|
| 185 | {
|
---|
| 186 | // rien a faire
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | void ThSDR48RandGen::SetBuffSize(size_t n)
|
---|
| 190 | // redimensionnement du buffer
|
---|
| 191 | {
|
---|
| 192 | if(fg_nothrsafe) return;
|
---|
| 193 | if (n < 1) n = 1024;
|
---|
| 194 | rseq_.ReSize(n, false);
|
---|
| 195 | idx_ = n;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 |
|
---|
[3615] | 199 | void ThSDR48RandGen::ShowRandom()
|
---|
| 200 | {
|
---|
[3618] | 201 | cout<<"RandomGenerator is ThSDR48RandGen("<<rseq_.Size()<<","<<!fg_nothrsafe<<")"<<endl;
|
---|
[3615] | 202 | }
|
---|
| 203 |
|
---|
[3889] | 204 | /*!
|
---|
| 205 | \brief Initialize the state of the random generator object using the integer value argument
|
---|
| 206 |
|
---|
| 207 | \warning This method changes the global random generator drand48() state
|
---|
| 208 | */
|
---|
[3602] | 209 | void ThSDR48RandGen::SetSeed(long int seed)
|
---|
| 210 | {
|
---|
| 211 | if (ths_rand == NULL) ths_rand = new ThSafeOp;
|
---|
| 212 | ths_rand->lock();
|
---|
| 213 | DR48RandGen::SetSeed(seed);
|
---|
[3889] | 214 | if(!fg_nothrsafe) idx_ = rseq_.Size();
|
---|
[3602] | 215 | ths_rand->unlock();
|
---|
| 216 | return;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
[3889] | 219 | /*!
|
---|
| 220 | \brief Initialize the state of the random generator object using the short integer array
|
---|
| 221 |
|
---|
| 222 | \warning This method changes the global random generator drand48() state
|
---|
| 223 | */
|
---|
[3602] | 224 | void ThSDR48RandGen::SetSeed(uint_2 seed[3])
|
---|
| 225 | {
|
---|
| 226 | if (ths_rand == NULL) ths_rand = new ThSafeOp;
|
---|
| 227 | ths_rand->lock();
|
---|
[3889] | 228 | DR48RandGen::SetSeed(seed);
|
---|
| 229 | if(!fg_nothrsafe) idx_ = rseq_.Size();
|
---|
[3602] | 230 | ths_rand->unlock();
|
---|
| 231 | }
|
---|
| 232 |
|
---|
[3889] | 233 | //! Return the random generator state
|
---|
[3602] | 234 | void ThSDR48RandGen::GetSeed(uint_2 seed[3])
|
---|
| 235 | {
|
---|
| 236 | if (ths_rand == NULL) ths_rand = new ThSafeOp;
|
---|
| 237 | ths_rand->lock();
|
---|
[3889] | 238 | DR48RandGen::GetSeed(seed);
|
---|
[3602] | 239 | ths_rand->unlock();
|
---|
| 240 | return;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 |
|
---|
[3889] | 244 | /*!
|
---|
| 245 | \brief Random Initialization of the state of the random generator object using the system clock
|
---|
[3602] | 246 |
|
---|
[3889] | 247 | \warning This method changes the global random generator drand48() state
|
---|
| 248 | */
|
---|
[3602] | 249 | void ThSDR48RandGen::AutoInit(int lp)
|
---|
| 250 | {
|
---|
| 251 | vector<uint_2> seed;
|
---|
| 252 | GenerateSeedVector(0,seed,lp);
|
---|
| 253 | uint_2 s[3] = {seed[0],seed[1],seed[2]};
|
---|
| 254 | SetSeed(s);
|
---|
| 255 | }
|
---|
| 256 |
|
---|
[3604] | 257 |
|
---|
[3602] | 258 | void ThSDR48RandGen::GenSeq(void)
|
---|
| 259 | {
|
---|
| 260 | ths_rand->lock();
|
---|
| 261 | for(size_t k=0; k<rseq_.Size(); k++) rseq_(k) = drand48();
|
---|
| 262 | ths_rand->unlock();
|
---|
| 263 | idx_ = 0;
|
---|
| 264 | }
|
---|
[3604] | 265 |
|
---|
| 266 | r_8 ThSDR48RandGen::Next()
|
---|
| 267 | {
|
---|
| 268 | if (rseq_.Size() == 0) return drand48();
|
---|
| 269 | else {
|
---|
| 270 | if(idx_==rseq_.Size()) GenSeq();
|
---|
| 271 | return(rseq_(idx_++));
|
---|
| 272 | }
|
---|
| 273 | }
|
---|
[3602] | 274 |
|
---|
[3889] | 275 | void ThSDR48RandGen::GetSeed_P(uint_2 seed[3])
|
---|
| 276 | {
|
---|
| 277 | return DR48RandGen::GetSeed(seed);
|
---|
| 278 | }
|
---|
| 279 |
|
---|
[3602] | 280 | //----------------------------------------------------------
|
---|
| 281 | // Classe pour la gestion de persistance
|
---|
| 282 | // ObjFileIO<DR48RandGen>
|
---|
| 283 | //----------------------------------------------------------
|
---|
| 284 |
|
---|
| 285 | /* --Methode-- */
|
---|
| 286 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
| 287 | void ObjFileIO<DR48RandGen>::WriteSelf(POutPersist& s) const
|
---|
| 288 | {
|
---|
| 289 | if (dobj == NULL)
|
---|
| 290 | throw NullPtrError("ObjFileIO<DR48RandGen>::WriteSelf() dobj=NULL");
|
---|
| 291 | uint_2 seed[3];
|
---|
| 292 | dobj->GetSeed(seed);
|
---|
| 293 | s.Put(seed,3);
|
---|
| 294 | return;
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | /* --Methode-- */
|
---|
| 298 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
| 299 | void ObjFileIO<DR48RandGen>::ReadSelf(PInPersist& s)
|
---|
| 300 | {
|
---|
| 301 | uint_2 seed[3];
|
---|
| 302 | s.Get(seed,3);
|
---|
| 303 | if(dobj == NULL) dobj = new DR48RandGen();
|
---|
| 304 | dobj->SetSeed(seed);
|
---|
| 305 | return;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | //----------------------------------------------------------
|
---|
| 309 | // Classe pour la gestion de persistance
|
---|
| 310 | // ObjFileIO<ThSDR48RandGen>
|
---|
| 311 | //----------------------------------------------------------
|
---|
| 312 |
|
---|
| 313 | /* --Methode-- */
|
---|
| 314 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
| 315 | void ObjFileIO<ThSDR48RandGen>::WriteSelf(POutPersist& s) const
|
---|
| 316 | {
|
---|
| 317 | if (dobj == NULL)
|
---|
| 318 | throw NullPtrError("ObjFileIO<ThSDR48RandGen>::WriteSelf() dobj=NULL");
|
---|
| 319 | ths_rand->lock(); // thread-safety
|
---|
| 320 | uint_4 itab[6];
|
---|
| 321 | //itab : [0]: version, [1,2,3] = srand48 state/seed , [4,5] = reserved for future use
|
---|
| 322 | itab[0] = 1;
|
---|
| 323 | // On recupere et on ecrit ds le PPF l'etat du generateur aleatoire
|
---|
| 324 | uint_2 seed_16v[3];
|
---|
| 325 | dobj->GetSeed_P(seed_16v);
|
---|
| 326 | for(int i=0; i<3; i++) itab[i+1] = seed_16v[i];
|
---|
| 327 | itab[4] = 0;
|
---|
| 328 | s.Put(itab, 6);
|
---|
| 329 | uint_8 sz = dobj->rseq_.Size();
|
---|
| 330 | s.Put(sz); // Taille du tableau intermediaire
|
---|
| 331 | uint_8 ix = dobj->idx_;
|
---|
| 332 | s.Put(ix); // valeur de l'index
|
---|
| 333 |
|
---|
| 334 | if (dobj->rseq_.Size() > 0) s << dobj->rseq_; // On ecrit le tableau (NDataBlock) si necessaire
|
---|
| 335 | ths_rand->unlock(); // thread-safety
|
---|
| 336 | return;
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | /* --Methode-- */
|
---|
| 340 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
| 341 | void ObjFileIO<ThSDR48RandGen>::ReadSelf(PInPersist& s)
|
---|
| 342 | {
|
---|
| 343 | uint_4 itab[6];
|
---|
| 344 | //itab : [0]: version, [1,2,3] = srand48 state/seed , [4] = reserved for future use
|
---|
| 345 | s.Get(itab, 6);
|
---|
| 346 | uint_8 sz,ix;
|
---|
| 347 | s.Get(sz); // Taille du tableau intermediaire
|
---|
| 348 | s.Get(ix); // Taille du tableau intermediaire
|
---|
| 349 |
|
---|
| 350 | if (dobj == NULL) dobj = new ThSDR48RandGen(sz, (sz>0)?true:false);
|
---|
| 351 | dobj->idx_ = ix;
|
---|
| 352 | if (sz > 0) {
|
---|
| 353 | s >> dobj->rseq_; // On lit le tableau (NDataBlock) si necessaire
|
---|
| 354 | dobj->fg_nothrsafe = false;
|
---|
| 355 | }
|
---|
| 356 | else { // Objet lu est NON thread-safe, taille_tableau rseq_ = 0
|
---|
| 357 | dobj->fg_nothrsafe = true;
|
---|
| 358 | if (dobj->rseq_.Size() > 0) dobj->rseq_.Dealloc();
|
---|
| 359 | }
|
---|
| 360 | // On initialise l'etat du generateur aleatoire avec les valeurs lues
|
---|
| 361 | uint_2 seed_16v[3];
|
---|
| 362 | //NON ? pourquoi faire GetSeed ? : dobj->GetSeed_P(seed_16v);
|
---|
| 363 | for(int i=0; i<3; i++) seed_16v[i] = itab[i+1];
|
---|
| 364 | dobj->SetSeed(seed_16v);
|
---|
| 365 | return;
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 | // ---------------------------------------------------------
|
---|
| 369 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
| 370 | #pragma define_template ObjFileIO<DR48RandGen>
|
---|
| 371 | #endif
|
---|
| 372 |
|
---|
| 373 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
| 374 | template class ObjFileIO<DR48RandGen>;
|
---|
| 375 | #endif
|
---|
| 376 |
|
---|
| 377 | // ---------------------------------------------------------
|
---|
| 378 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
| 379 | #pragma define_template ObjFileIO<ThSDR48RandGen>
|
---|
| 380 | #endif
|
---|
| 381 |
|
---|
| 382 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
| 383 | template class ObjFileIO<ThSDR48RandGen>;
|
---|
| 384 | #endif
|
---|
| 385 | // ---------------------------------------------------------
|
---|
| 386 |
|
---|
| 387 |
|
---|
| 388 | } /* namespace SOPHYA */
|
---|