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::ShowRandom()
|
---|
24 | {
|
---|
25 | cout<<"RandomGenerator is FMTRandGen"<<endl;
|
---|
26 | }
|
---|
27 |
|
---|
28 | void FMTRandGen::SetSeed(uint_4 seed)
|
---|
29 | {
|
---|
30 | dsfmt_init_gen_rand(&dsfmt_,seed);
|
---|
31 | }
|
---|
32 |
|
---|
33 | void FMTRandGen::SetSeed(vector<uint_4> seed)
|
---|
34 | {
|
---|
35 | if(seed.size()<=0) return;
|
---|
36 | int key_length = (int)seed.size();
|
---|
37 | uint_4* init_key = new uint_4[key_length];
|
---|
38 | for(int i=0;i<key_length;i++) init_key[i] = seed[i];
|
---|
39 |
|
---|
40 | dsfmt_init_by_array(&dsfmt_,init_key,key_length);
|
---|
41 |
|
---|
42 | delete [] init_key;
|
---|
43 | }
|
---|
44 |
|
---|
45 | r_8 FMTRandGen::Next()
|
---|
46 | {
|
---|
47 | return dsfmt_genrand_close_open(&dsfmt_);
|
---|
48 | }
|
---|
49 |
|
---|
50 | void FMTRandGen::AutoInit(int lp)
|
---|
51 | {
|
---|
52 | vector<uint_2> seed;
|
---|
53 | GenerateSeedVector(1,seed,lp);
|
---|
54 | vector<uint_4> s;
|
---|
55 | uint_4 s4;
|
---|
56 | s4 = seed[0] | (seed[1]<<16);
|
---|
57 | s.push_back(s4);
|
---|
58 | s4 = seed[2] | (seed[3]<<16);
|
---|
59 | s.push_back(s4);
|
---|
60 | SetSeed(s);
|
---|
61 | }
|
---|
62 |
|
---|
63 | //----------------------------------------------------------
|
---|
64 | // Classe pour la gestion de persistance
|
---|
65 | // ObjFileIO<FMTRandGen>
|
---|
66 | //----------------------------------------------------------
|
---|
67 |
|
---|
68 | /* --Methode-- */
|
---|
69 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
70 | void ObjFileIO<FMTRandGen>::WriteSelf(POutPersist& s) const
|
---|
71 | {
|
---|
72 | if (dobj == NULL)
|
---|
73 | throw NullPtrError("ObjFileIO<FMTRandGen>::WriteSelf() dobj=NULL");
|
---|
74 | s.Put((uint_4)DSFMT_MEXP);
|
---|
75 | s.Put(dobj->dsfmt_.idx);
|
---|
76 | for(int i=0;i<DSFMT_N+1;i++) {
|
---|
77 | for(int j=0;j<2;j++) {
|
---|
78 | uint_8 v = dobj->dsfmt_.status[i].u[j];
|
---|
79 | s.PutU8(v);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | return;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /* --Methode-- */
|
---|
86 | DECL_TEMP_SPEC /* equivalent a template <> , pour SGI-CC en particulier */
|
---|
87 | void ObjFileIO<FMTRandGen>::ReadSelf(PInPersist& s)
|
---|
88 | {
|
---|
89 | uint_4 mexp;
|
---|
90 | s.Get(mexp);
|
---|
91 | if(mexp != DSFMT_MEXP)
|
---|
92 | throw SzMismatchError("ObjFileIO<FMTRandGen>::WriteSelf() wrong DSFMT_MEXP");
|
---|
93 | if(dobj == NULL) dobj = new FMTRandGen();
|
---|
94 | s.Get(dobj->dsfmt_.idx);
|
---|
95 | for(int i=0;i<DSFMT_N+1;i++) {
|
---|
96 | for(int j=0;j<2;j++) {
|
---|
97 | uint_8 v;
|
---|
98 | s.Get(v);
|
---|
99 | dobj->dsfmt_.status[i].u[j] = v;
|
---|
100 | }
|
---|
101 | }
|
---|
102 | return;
|
---|
103 | }
|
---|
104 |
|
---|
105 | // ---------------------------------------------------------
|
---|
106 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
107 | #pragma define_template ObjFileIO<FMTRandGen>
|
---|
108 | #endif
|
---|
109 |
|
---|
110 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
111 | template class ObjFileIO<FMTRandGen>;
|
---|
112 | #endif
|
---|
113 |
|
---|
114 | } /* namespace SOPHYA */
|
---|