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

Last change on this file since 3606 was 3606, checked in by ansari, 16 years ago

Modif tmtrnd.cc pour utilisation classe ThSDR48RandGen , Reza 29/04/2009

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