[3602] | 1 | #include <math.h>
|
---|
| 2 | #include <stdlib.h>
|
---|
| 3 |
|
---|
| 4 | #include "randfmt.h"
|
---|
| 5 |
|
---|
| 6 | namespace SOPHYA {
|
---|
| 7 |
|
---|
| 8 | //----------------------------------------------------------------
|
---|
| 9 | // Implementation d'un generateur aleatoire utilisant le code
|
---|
| 10 | // SIMD-oriented Fast Mersenne Twister (SFMT).
|
---|
| 11 | // http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
|
---|
| 12 |
|
---|
| 13 |
|
---|
| 14 | FMTRandGen::FMTRandGen(uint_4 seed)
|
---|
| 15 | {
|
---|
| 16 | dsfmt_init_gen_rand(&dsfmt_,seed);
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | FMTRandGen::~FMTRandGen()
|
---|
| 20 | {
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | void FMTRandGen::SetSeed(uint_4 seed)
|
---|
| 24 | {
|
---|
| 25 | dsfmt_init_gen_rand(&dsfmt_,seed);
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | void FMTRandGen::SetSeed(vector<uint_4> seed)
|
---|
| 29 | {
|
---|
| 30 | if(seed.size()<=0) return;
|
---|
| 31 | int key_length = (int)seed.size();
|
---|
| 32 | uint_4* init_key = new uint_4[key_length];
|
---|
| 33 | for(int i=0;i<key_length;i++) init_key[i] = seed[i];
|
---|
| 34 |
|
---|
| 35 | dsfmt_init_by_array(&dsfmt_,init_key,key_length);
|
---|
| 36 |
|
---|
| 37 | delete [] init_key;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | r_8 FMTRandGen::Next()
|
---|
| 41 | {
|
---|
| 42 | return dsfmt_genrand_close_open(&dsfmt_);
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | void FMTRandGen::AutoInit(int lp)
|
---|
| 46 | {
|
---|
| 47 | vector<uint_2> seed;
|
---|
| 48 | GenerateSeedVector(1,seed,lp);
|
---|
| 49 | vector<uint_4> s;
|
---|
| 50 | uint_4 s4;
|
---|
| 51 | s4 = seed[0] | (seed[1]<<16);
|
---|
| 52 | s.push_back(s4);
|
---|
| 53 | s4 = seed[2] | (seed[3]<<16);
|
---|
| 54 | s.push_back(s4);
|
---|
| 55 | SetSeed(s);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | //----------------------------------------------------------
|
---|
| 59 | // Classe pour la gestion de persistance
|
---|
| 60 | // ObjFileIO<FMTRandGen>
|
---|
| 61 | //----------------------------------------------------------
|
---|
| 62 |
|
---|
| 63 | /* --Methode-- */
|
---|
| 64 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
| 65 | void ObjFileIO<FMTRandGen>::WriteSelf(POutPersist& s) const
|
---|
| 66 | {
|
---|
| 67 | if (dobj == NULL)
|
---|
| 68 | throw NullPtrError("ObjFileIO<FMTRandGen>::WriteSelf() dobj=NULL");
|
---|
| 69 | s.Put((uint_4)DSFMT_MEXP);
|
---|
| 70 | s.Put(dobj->dsfmt_.idx);
|
---|
[3603] | 71 | for(int i=0;i<DSFMT_N+1;i++) {
|
---|
| 72 | for(int j=0;j<2;j++) {
|
---|
| 73 | uint_8 v = dobj->dsfmt_.status[i].u[j];
|
---|
| 74 | s.PutU8(v);
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
[3602] | 77 | return;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /* --Methode-- */
|
---|
| 81 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
| 82 | void ObjFileIO<FMTRandGen>::ReadSelf(PInPersist& s)
|
---|
| 83 | {
|
---|
| 84 | uint_4 mexp;
|
---|
| 85 | s.Get(mexp);
|
---|
| 86 | if(mexp != DSFMT_MEXP)
|
---|
| 87 | throw SzMismatchError("ObjFileIO<FMTRandGen>::WriteSelf() wrong DSFMT_MEXP");
|
---|
| 88 | if(dobj == NULL) dobj = new FMTRandGen();
|
---|
| 89 | s.Get(dobj->dsfmt_.idx);
|
---|
[3603] | 90 | for(int i=0;i<DSFMT_N+1;i++) {
|
---|
| 91 | for(int j=0;j<2;j++) {
|
---|
| 92 | uint_8 v;
|
---|
| 93 | s.Get(v);
|
---|
| 94 | dobj->dsfmt_.status[i].u[j] = v;
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
[3602] | 97 | return;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | // ---------------------------------------------------------
|
---|
| 101 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
| 102 | #pragma define_template ObjFileIO<FMTRandGen>
|
---|
| 103 | #endif
|
---|
| 104 |
|
---|
| 105 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
| 106 | template class ObjFileIO<FMTRandGen>;
|
---|
| 107 | #endif
|
---|
| 108 |
|
---|
| 109 | } /* namespace SOPHYA */
|
---|