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