C++ mpi module for stochmagnet_main Package
SM_MultiStochasticFunctions.h
1 #ifndef SM_MultiStochasticFunctions_H
2 #define SM_MultiStochasticFunctions_H
3 
4 //base class header
5 #include "SM_MultiStochasticFunctionsInterface.h"
6 
7 //uniform int distribution class
8 #include "RNDB_UniformIntDistribution.h"
9 //random number generator header
10 #include "RNDB_MT19937.h"
11 
12 //stochastic function implemented headers
13 #include "SM_StochasticFunctions.h"
14 
15 //OMP header
16 //#include "OMP_Thread.h"
17 
25 template<class F>
27 
28  // ATTRIBUTES
29 
30 public:
31 
32 
33 
34 private:
37 
38  //seed int boost uniform generator
39  RNDB_UniformIntDistribution mSeedDistribution;
40  //random number generator
41  RNDB_MT19937 mSeedGenerator;
42 
43  //stochatic normal & uniform functions per thread
44  std::vector<CORE_UniquePointer<F>> mFs;
45 
46 protected:
47  // METHODS
48 
49  // CONSTRUCTORS
50 
54 
55  }
56 private:
57 
58 
59 
60 
61  // DESTRUCTORS
62 protected:
63 
67  }
68 
69 
70 
71 public:
72  //MEMORY
73 
87  virtual tMemSize getMemorySize() const override {
88  return sizeof(*this)+getContentsMemorySize();
89  }
90 
99  virtual tMemSize getContentsMemorySize() const override {
100  tMemSize mem=SuperClass::getContentsMemorySize();
101  for(const auto& f: mFs) mem+=f->getContentsMemorySize();
102  mem+=mSeedDistribution.getContentsMemorySize();
103  mem+=mSeedGenerator.getContentsMemorySize();
104  return mem;
105  }
106 public:
107  // CREATE class
111  inline static CORE_UniquePointer<SelfClass> New() {
112  CORE_UniquePointer<SelfClass> p=CORE_UniquePointer<SelfClass>(new SelfClass(),
114 
115  p->createFs();
116  //return the boost stochastic function
117  return p;
118  }
119 
120 private:
121 
122  inline void createFs() {
123  // if (F::IsThreadSafe()) {
124  // tInteger nThreads=OMP_Thread::GetThreadsNumber();
125  // mFs.resize(nThreads);
126  // for(auto& f:mFs) {
127  // f=F::New();
128  // }
129  // } else {
130  mFs.resize(1);
131  mFs[0]=F::New();
132  //}
133  }
134 
135 public:
139  virtual SM_StochasticFunctionsInterface& operator()(const tInteger& threadId) override {
140  ASSERT_IN(threadId<mFs.size());
141  return *mFs[threadId].get();
142  }
143 
147  inline F& operator[](const tInteger& threadId) {
148  ASSERT_IN(threadId<mFs.size());
149  return *mFs[threadId].get();
150  }
154  inline const F& operator[](const tInteger& threadId) const {
155  ASSERT_IN(threadId<mFs.size());
156  return *mFs[threadId].get();
157  }
158 
159  // SET & GET methods
160  // =================
161 
165  virtual tInteger getSize() const final {
166  return mFs.size();
167  }
168 
172  virtual void setSeed(const tULLInt& seed) final {
173  mSeedGenerator.setSeed(seed);
174  for(auto& f:mFs) {
175  f->setSeed(seed);
176  }
177  }
178 
181  virtual void setSeed() final {
182  mSeedGenerator.setSeed();
183  for(auto& f:mFs) {
184  f->setSeed();
185  }
186 
187  }
188 
193  virtual void randomSeed(const tInteger& coresNumber,const tInteger& coreIndex) final {
194 
195 
196  //number of threads per core
197  tInteger threadsNumber=mFs.size();
198 
199 
200  //generate the seed
201  //tInteger nSeeds=coreIndex*threadsNumber;
202 
203  //genarate 1000 random numbers
204  tULLInt seed=0;
205  for(tInteger t=0;t<1000;t++) {
206  seed=mSeedDistribution.random(mSeedGenerator);
207  }
208 
209  //thread index: coreIndex*threadsNumber
210  for(tInteger t=0;t<coreIndex*threadsNumber;t++) {
211  seed=mSeedDistribution.random(mSeedGenerator);
212  }
213 
214  //total number of threads : nCores x threadsNumber
215  for(auto& f:mFs) {
216  //jump number for the thread
217  f->setSeed(seed);
218 
219  //generate the seed for the next thread
220  seed=mSeedDistribution.random(mSeedGenerator);
221 
222  }
223 
224 
225  }
232  virtual void jump(const tInteger& coresNumber,const tInteger& coreIndex,
233  const tULLInt& jumpsNumber,const tULLInt& nRNGs) final {
234 
235 
236  //number of threads per core
237  tInteger threadsNumber=mFs.size();
238 
239  //number of random number to generate by thread
240  tULLInt nRNGs_thread=nRNGs/threadsNumber;
241 
242  //number of random number generated for threads t in [0,threadsNumber[ within coreIndex
243  tULLInt S_t=jumpsNumber;
244 
245 
246 
247  //generate the seed
248  //tInteger nSeeds=coreIndex*threadsNumber;
249 
250  //genarate 1000 random numbers
251  tULLInt seed=0;
252  for(tInteger t=0;t<1000;t++) {
253  seed=mSeedDistribution.random(mSeedGenerator);
254  }
255 
256  //thread index: coreIndex*threadsNumber
257  for(tInteger t=0;t<coreIndex*threadsNumber;t++) {
258  seed=mSeedDistribution.random(mSeedGenerator);
259  }
260 
261  //total number of threads : nCores x threadsNumber
262  for(auto& f:mFs) {
263  //jump number for the thread
264  f->jump(S_t,seed);
265 
266  //generate the seed for the next thread
267  seed=mSeedDistribution.random(mSeedGenerator);
268 
269  //jumps numbe rfor next threads
270  S_t+=nRNGs_thread;
271  }
272 
273 
274  }
275 
276 
280  virtual tString toString() const override {
281  std::stringstream ret;
282  ret<<" number of stoachastic functions per core :" <<mFs.size()<<" of type :"<<mFs[0]->toString()<<"\n";
283  return ret.str();
284  }
285 
286 
287 };
288 
289 #endif
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:113
T random(RAND_RandomNumberGenerator< T1, G, D1 > &generator)
random a number
Definition: RAND_RandomNumberDistribution.h:116
void setSeed(const tULInt &seed)
initialize the seed of the random number generator
Definition: RAND_RandomNumberGenerator.h:107
This class implements a stochastic function with a normal law implemented from the boost library.
Definition: RNDB_MT19937.h:16
virtual tMemSize getContentsMemorySize() const
return the memory size of the included associations
Definition: RNDB_MT19937.h:90
This class implements a an uniform int law in [a,b] by the boost library.
Definition: RNDB_UniformIntDistribution.h:15
virtual tMemSize getContentsMemorySize() const
return the memory size of the included associations
Definition: RNDB_UniformIntDistribution.h:90
This class describes a multi stochastic functions based on same random number generator which impleme...
Definition: SM_MultiStochasticFunctionsInterface.h:18
virtual tMemSize getContentsMemorySize() const
return nthe memory size of the included associations
Definition: SM_MultiStochasticFunctionsInterface.h:85
This class describes a multi stochastic functions based on same random number generator which impleme...
Definition: SM_MultiStochasticFunctions.h:26
F & operator[](const tInteger &threadId)
operator for getting stochatic function at thread id
Definition: SM_MultiStochasticFunctions.h:147
virtual void randomSeed(const tInteger &coresNumber, const tInteger &coreIndex) final
jump a number of virtual random number generator
Definition: SM_MultiStochasticFunctions.h:193
virtual void setSeed(const tULLInt &seed) final
void set seed
Definition: SM_MultiStochasticFunctions.h:172
virtual void jump(const tInteger &coresNumber, const tInteger &coreIndex, const tULLInt &jumpsNumber, const tULLInt &nRNGs) final
jump a number of virtual random number generator
Definition: SM_MultiStochasticFunctions.h:232
virtual tString toString() const override
return the string representation of the multi stochastic functions
Definition: SM_MultiStochasticFunctions.h:280
SM_MultiStochasticFunctions(void)
create
Definition: SM_MultiStochasticFunctions.h:53
virtual tInteger getSize() const final
get the number of stochastic functions
Definition: SM_MultiStochasticFunctions.h:165
virtual tMemSize getMemorySize() const override
return the memory size of the class and the memory size of all its attributes/associations
Definition: SM_MultiStochasticFunctions.h:87
static CORE_UniquePointer< SelfClass > New()
build a new instance of default class factory
Definition: SM_MultiStochasticFunctions.h:111
virtual tMemSize getContentsMemorySize() const override
return nthe memory size of the included associations
Definition: SM_MultiStochasticFunctions.h:99
virtual ~SM_MultiStochasticFunctions(void)
destroy
Definition: SM_MultiStochasticFunctions.h:66
virtual SM_StochasticFunctionsInterface & operator()(const tInteger &threadId) override
operator for getting stochatic function at thread id
Definition: SM_MultiStochasticFunctions.h:139
const F & operator[](const tInteger &threadId) const
operator for getting stochatic function at thread id
Definition: SM_MultiStochasticFunctions.h:154
virtual void setSeed() final
set seed
Definition: SM_MultiStochasticFunctions.h:181
This class describes a stochastic functions based on same random number generator which implement ran...
Definition: SM_StochasticFunctionsInterface.h:18