source: Sophya/trunk/SophyaProg/Tests/tmtrnd.cc@ 3390

Last change on this file since 3390 was 3390, checked in by cmv, 18 years ago

prog de tests (tmtrnd,tmtrnd3) de la classe RandomGenerator , cmv+rz 22/11/2007

File size: 7.8 KB
Line 
1#include "sopnamsp.h"
2#include "zthread.h"
3#include "resusage.h"
4#include "ctimer.h"
5#include "timing.h"
6
7#include <stdlib.h>
8#include <stdio.h>
9#include <unistd.h>
10#include <string>
11#include <iostream>
12
13#include "tvector.h"
14#include "fioarr.h"
15#include "matharr.h"
16#include "tarrinit.h"
17
18#include "srandgen.h"
19#include "stsrand.h"
20
21//--------------------------------------------------------------------------------
22// Programme d'exemple multi-threads et test de la classe de generateur aleatoire
23// R. Ansari - C. Magneville , Novembre 2007
24// (C) UPS+LAL IN2P3/CNRS (C) DAPNIA/SPP CEA - 2007
25// Exemples de commandes pour test (dans l'ordre ci-dessous :
26// csh> tmtrnd OCG 1000000 XXX
27// csh> tmtrnd STG 1000000 1024
28// csh> tmtrnd RDG 1000000 rg1.ppf
29// csh> tmtrnd MTG 3000000 4
30//--------------------------------------------------------------------------------
31
32//---------------------------------------------------------------------------------
33// Definition d'une classe heritant de ZThread, pour remplir un vecteur avec
34// des aleatoires
35class MTRnd : public ZThread {
36public:
37 MTRnd(TVector<r_8>& v, bool fgg=true);
38 virtual void run();
39protected:
40 string nom_;
41 TVector<r_8> vv_;
42 bool fgg_; // true -> gaussien, false rand01()
43};
44
45static int mtrandId = 0; // Pour donner un identificateur a chaque thread
46
47MTRnd::MTRnd(Vector& v, bool fgg)
48 : vv_(v,true), fgg_(fgg) // Partage de reference du vecteur
49{
50 char buff[32];
51 sprintf(buff, "MTRnd-Id=%d", mtrandId);
52 mtrandId++;
53 nom_ = buff;
54 cout << " Thread MTRnd(" << nom_ << " ) Created ... " << endl;
55}
56
57
58// Le travail fait dans chaque thread (appele par start())
59void MTRnd::run()
60{
61 Timer tm(nom_.c_str());
62 cout << "MTRnd::run() - Nom= " << nom_ << " vv.Size()= " << vv_.Size() << endl;
63 RandomGenerator rgen;
64 if (fgg_)
65 for(sa_size_t k=0; k<vv_.Size(); k++) vv_(k) = rgen.Gaussian();
66 else
67 for(sa_size_t k=0; k<vv_.Size(); k++) vv_(k) = rgen.Flat01();
68}
69// ----- Fin de la definition de la classe MTRnd ----
70//---------------------------------------------------------------------------------
71
72//----------------- Les fonction de test -----------------
73//------- f0_tmtrnd()
74int f0_tmtrnd(sa_size_t VSZ, bool fgg=true)
75{
76 cout << "[1] f0_tmtrnd/starting, VSZ= " << VSZ << endl;
77 TVector<r_8> DATA(VSZ);
78 {
79 Timer tm("f0_tmtrnd-GauRnd/drand01");
80 if (fgg)
81 for(sa_size_t k=0; k<VSZ; k++) DATA(k) = GauRnd(0., 1.);
82 else
83 for(sa_size_t k=0; k<VSZ; k++) DATA(k) = drand01();
84 cout << "[2] f0_tmtrnd/ End of random generation using GauRnd()/drand01() " << endl;
85 }
86 POutPersist po("data0.ppf");
87 po << DATA;
88 cout << "[3] f0_tmtrnd/ DATA saved to file data0.ppf " << endl;
89 return 0;
90}
91//------- f1_tmtrnd()
92int f1_tmtrnd(sa_size_t VSZ, size_t seqsz, bool fgg=true)
93{
94 cout << "[1] f1_tmtrnd/starting, VSZ= " << VSZ << " SeqSz=" << seqsz << endl;
95 TVector<r_8> DATA(VSZ);
96 RandomGenerator rg(seqsz, (seqsz==0)?false:true);
97 unsigned short seed[3];
98 rg.GetSeed(seed, 2); // pour imprimer l'etat du generateur
99 {
100 Timer tm("f0_tmtrnd-GauRnd/drand01");
101 if (fgg)
102 for(sa_size_t k=0; k<VSZ; k++) DATA(k) = rg.Gaussian();
103 else
104 for(sa_size_t k=0; k<VSZ; k++) DATA(k) = rg.Flat01();
105 cout << "[2] f1_tmtrnd/ End of random generation using RandomGen class " << endl;
106 }
107 POutPersist po("data1.ppf");
108 po << DATA;
109 cout << "[3] f1_tmtrnd/ DATA saved to file data1.ppf " << endl;
110 POutPersist porg("rg1.ppf");
111 porg << rg;
112 rg.GetSeed(seed, 2); // pour imprimer l'etat du generateur
113 cout << "[4] f1_tmtrnd/ RandGen saved to file rg1.ppf " << endl;
114 return 0;
115}
116//------- f2_tmtrnd()
117int f2_tmtrnd(sa_size_t VSZ, string & inppf, bool fgg=true)
118{
119 cout << "[1] f2_tmtrnd/starting, VSZ= " << VSZ << " InPPFName=" << inppf << endl;
120 TVector<r_8> DATA(VSZ);
121 RandomGenerator rg;
122 PInPersist pirg(inppf);
123 pirg >> rg;
124 cout << "[1.b] f2_tmtrnd/ RandGen read from InPPF OK " << endl;
125 unsigned short seed[3];
126 rg.GetSeed(seed, 2); // pour imprimer l'etat du generateur
127 {
128 Timer tm("f0_tmtrnd-GauRnd/drand01");
129 if (fgg)
130 for(sa_size_t k=0; k<VSZ; k++) DATA(k) = rg.Gaussian();
131 else
132 for(sa_size_t k=0; k<VSZ; k++) DATA(k) = rg.Flat01();
133 cout << "[2] f1_tmtrnd/ End of random generation using RandomGen class " << endl;
134 }
135 POutPersist po("data2.ppf");
136 po << DATA;
137 cout << "[3] f1_tmtrnd/ DATA saved to file data2.ppf " << endl;
138 POutPersist porg("rg2.ppf");
139 porg << rg;
140 rg.GetSeed(seed, 2); // pour imprimer l'etat du generateur
141 cout << "[4] f1_tmtrnd/ RandGen saved to file rg2.ppf " << endl;
142 return 0;
143}
144
145//------- f3_tmtrnd()
146int f3_tmtrnd(sa_size_t VSZ, int NTH, bool fgg=true)
147{
148 cout << "[1] f3_tmtrnd/starting, VSZ= " << VSZ << " NTH= " << NTH << endl;
149 ResourceUsage res(ResourceUsage::RU_All);
150 vector<MTRnd *> vth;
151 TVector<r_8> DATA(VSZ);
152 sa_size_t csz = VSZ / NTH;
153 sa_size_t first, last;
154 cout << "[2] f3_tmtrnd/creating threads " << endl;
155 for(int kt=0; kt<NTH; kt++) {
156 first = kt*csz;
157 last = (kt == NTH-1) ? VSZ-1 : first+csz-1;
158 TVector<r_8> sv = DATA(Range(first, last));
159 vth.push_back(new MTRnd(sv, fgg) );
160 }
161 cout << "[3] f3_tmtrnd/starting threads " << endl;
162 for(int kt=0; kt<NTH; kt++) vth[kt]->start();
163
164 cout << "[4] f3_tmtrnd/waiting for all threads to finish " << endl;
165 sleep(2);
166 for(int kt=0; kt<NTH; kt++) vth[kt]->join();
167
168 cout << "[5] f3_tmtrnd/deleting thread objects " << endl;
169 for(int kt=0; kt<NTH; kt++) delete vth[kt];
170
171 cout << "[6] f3_tmtrnd/saving DATA to file data3.ppf " << endl;
172 POutPersist po("data3.ppf");
173 po << DATA;
174 cout << res;
175 return 0;
176}
177//----------------- FIN des fonctions de test -----------------
178
179//-------------------------------------------------------------
180//---------------------- MAIN MAIN MAIN -----------------------
181//-------------------------------------------------------------
182
183int main(int narg, char *arg[])
184
185{
186 InitTim();
187 SophyaInit();
188
189 // Enregistrement des handlers PPF pour les RandomGenerator
190 PPRegister(ObjFileIO<RandomGenerator>);
191 DObjRegister(ObjFileIO<RandomGenerator>, RandomGenerator);
192
193 int rc = 0;
194 if (narg < 4) {
195 cout << " tmtrnd/Error args - Usage: tmtrnd SEL arg1 arg2 ... " << endl;
196 cout << " ==> SEL=OCG/OCF : tmtrnd OCG/OCF VecSize XXX (-> f0_tmtrnd())" << endl;
197 cout << " OCG/OCF Random gen. with (old) c-functions G/F-> Gaussian/Flat " << endl;
198 cout << " ==> SEL=STG/STF : tmtrnd STG/STF VecSize SeqSize (-> f2_tmtrnd())" << endl;
199 cout << " STG/STF RandGen(SeqSize) SeqSz=0 -> NO-ThSafe G/F-> Gaussian/Flat " << endl;
200 cout << " ==> SEL=RDG/RDF : tmtrnd RDG/RDF VecSize InPPFName (-> f2_tmtrnd())" << endl;
201 cout << " RDG/RDF Read RandGen object from file InPPFName G/F-> Gaussian/Flat " << endl;
202 cout << " ==> SEL=MTG/MTF : tmtrnd MTG/MTF VecSize NThread (-> f3_tmtrnd()) " << endl;
203 cout << " MTG/MTF Multithread generation, MTG-> Gaussian, MTF-> Flat " << endl;
204 return 1;
205 }
206 string sel = arg[1];
207
208 try {
209 bool fgg = (sel[2]=='G')?true:false;
210 if ((sel == "OCG")||(sel == "OCF")) {
211 sa_size_t VSZ = atoi(arg[2]);
212 rc = f0_tmtrnd(VSZ, fgg);
213 }
214 if ((sel == "STG")||(sel == "STF")) {
215 sa_size_t VSZ = atoi(arg[2]);
216 size_t seqsz = atoi(arg[3]);
217 rc = f1_tmtrnd(VSZ, seqsz, fgg);
218 }
219 if ((sel == "RDG")||(sel == "RDF")) {
220 sa_size_t VSZ = atoi(arg[2]);
221 string inppf = arg[3];
222 rc = f2_tmtrnd(VSZ, inppf, fgg);
223 }
224 if ((sel == "MTG")||(sel == "MTF")) {
225 sa_size_t VSZ = atoi(arg[2]);
226 int NTH = atoi(arg[3]);
227 rc = f3_tmtrnd(VSZ, NTH, fgg);
228 }
229
230 }
231 catch (PThrowable exc) {
232 cerr << "zthr: catched Exception " << exc.Msg() << endl;
233 rc = 77;
234 }
235 catch (...) {
236 cerr << " catched unknown (...) exception (tmtrnd.cc) " << endl;
237 rc = 78;
238 }
239 cout << "----------- tmtrnd/END ------- " << endl;
240 PrtTim("---END tmtrnd---");
241 return(rc);
242
243}
Note: See TracBrowser for help on using the repository browser.