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