[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"
|
---|
[3606] | 30 | #include "randr48.h"
|
---|
[3390] | 31 |
|
---|
| 32 |
|
---|
[3613] | 33 | // Choix du nom de la classe thread-safe a tester
|
---|
[3606] | 34 | #define TC_RandomGenerator ThSDR48RandGen
|
---|
| 35 |
|
---|
[3390] | 36 | //---------------------------------------------------------------------------------
|
---|
| 37 | // Definition d'une classe heritant de ZThread, pour remplir un vecteur avec
|
---|
| 38 | // des aleatoires
|
---|
| 39 | class MTRnd : public ZThread {
|
---|
| 40 | public:
|
---|
| 41 | MTRnd(TVector<r_8>& v, bool fgg=true);
|
---|
| 42 | virtual void run();
|
---|
| 43 | protected:
|
---|
| 44 | string nom_;
|
---|
| 45 | TVector<r_8> vv_;
|
---|
| 46 | bool fgg_; // true -> gaussien, false rand01()
|
---|
| 47 | };
|
---|
| 48 |
|
---|
| 49 | static int mtrandId = 0; // Pour donner un identificateur a chaque thread
|
---|
| 50 |
|
---|
| 51 | MTRnd::MTRnd(Vector& v, bool fgg)
|
---|
| 52 | : vv_(v,true), fgg_(fgg) // Partage de reference du vecteur
|
---|
| 53 | {
|
---|
| 54 | char buff[32];
|
---|
| 55 | sprintf(buff, "MTRnd-Id=%d", mtrandId);
|
---|
| 56 | mtrandId++;
|
---|
| 57 | nom_ = buff;
|
---|
| 58 | cout << " Thread MTRnd(" << nom_ << " ) Created ... " << endl;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 |
|
---|
| 62 | // Le travail fait dans chaque thread (appele par start())
|
---|
| 63 | void MTRnd::run()
|
---|
| 64 | {
|
---|
| 65 | Timer tm(nom_.c_str());
|
---|
| 66 | cout << "MTRnd::run() - Nom= " << nom_ << " vv.Size()= " << vv_.Size() << endl;
|
---|
[3606] | 67 | TC_RandomGenerator rgen;
|
---|
[3390] | 68 | if (fgg_)
|
---|
| 69 | for(sa_size_t k=0; k<vv_.Size(); k++) vv_(k) = rgen.Gaussian();
|
---|
| 70 | else
|
---|
| 71 | for(sa_size_t k=0; k<vv_.Size(); k++) vv_(k) = rgen.Flat01();
|
---|
| 72 | }
|
---|
| 73 | // ----- Fin de la definition de la classe MTRnd ----
|
---|
| 74 | //---------------------------------------------------------------------------------
|
---|
| 75 |
|
---|
| 76 | //----------------- Les fonction de test -----------------
|
---|
| 77 | //------- f0_tmtrnd()
|
---|
| 78 | int f0_tmtrnd(sa_size_t VSZ, bool fgg=true)
|
---|
| 79 | {
|
---|
| 80 | cout << "[1] f0_tmtrnd/starting, VSZ= " << VSZ << endl;
|
---|
| 81 | TVector<r_8> DATA(VSZ);
|
---|
| 82 | {
|
---|
[3615] | 83 | Timer tm("f0_tmtrnd-Gaussian/drand01");
|
---|
[3390] | 84 | if (fgg)
|
---|
[3617] | 85 | for(sa_size_t k=0; k<VSZ; k++) DATA(k) = GaussianRand(1.,0.);
|
---|
[3390] | 86 | else
|
---|
| 87 | for(sa_size_t k=0; k<VSZ; k++) DATA(k) = drand01();
|
---|
[3615] | 88 | cout << "[2] f0_tmtrnd/ End of random generation using Gaussian()/drand01() " << endl;
|
---|
[3390] | 89 | }
|
---|
| 90 | POutPersist po("data0.ppf");
|
---|
| 91 | po << DATA;
|
---|
| 92 | cout << "[3] f0_tmtrnd/ DATA saved to file data0.ppf " << endl;
|
---|
| 93 | return 0;
|
---|
| 94 | }
|
---|
| 95 | //------- f1_tmtrnd()
|
---|
| 96 | int f1_tmtrnd(sa_size_t VSZ, size_t seqsz, bool fgg=true)
|
---|
| 97 | {
|
---|
| 98 | cout << "[1] f1_tmtrnd/starting, VSZ= " << VSZ << " SeqSz=" << seqsz << endl;
|
---|
| 99 | TVector<r_8> DATA(VSZ);
|
---|
[3606] | 100 | TC_RandomGenerator rg(seqsz, (seqsz==0)?false:true);
|
---|
[3390] | 101 | unsigned short seed[3];
|
---|
[3606] | 102 | // rg.GetSeed(seed,2); // pour imprimer l'etat du generateur
|
---|
[3390] | 103 | {
|
---|
[3615] | 104 | Timer tm("f0_tmtrnd-Gaussian/drand01");
|
---|
[3390] | 105 | if (fgg)
|
---|
| 106 | for(sa_size_t k=0; k<VSZ; k++) DATA(k) = rg.Gaussian();
|
---|
| 107 | else
|
---|
| 108 | for(sa_size_t k=0; k<VSZ; k++) DATA(k) = rg.Flat01();
|
---|
| 109 | cout << "[2] f1_tmtrnd/ End of random generation using RandomGen class " << endl;
|
---|
| 110 | }
|
---|
| 111 | POutPersist po("data1.ppf");
|
---|
| 112 | po << DATA;
|
---|
| 113 | cout << "[3] f1_tmtrnd/ DATA saved to file data1.ppf " << endl;
|
---|
| 114 | POutPersist porg("rg1.ppf");
|
---|
| 115 | porg << rg;
|
---|
[3606] | 116 | // rg.GetSeed(seed); // pour imprimer l'etat du generateur
|
---|
[3390] | 117 | cout << "[4] f1_tmtrnd/ RandGen saved to file rg1.ppf " << endl;
|
---|
| 118 | return 0;
|
---|
| 119 | }
|
---|
| 120 | //------- f2_tmtrnd()
|
---|
| 121 | int f2_tmtrnd(sa_size_t VSZ, string & inppf, bool fgg=true)
|
---|
| 122 | {
|
---|
| 123 | cout << "[1] f2_tmtrnd/starting, VSZ= " << VSZ << " InPPFName=" << inppf << endl;
|
---|
| 124 | TVector<r_8> DATA(VSZ);
|
---|
[3606] | 125 | TC_RandomGenerator rg;
|
---|
[3390] | 126 | PInPersist pirg(inppf);
|
---|
| 127 | pirg >> rg;
|
---|
| 128 | cout << "[1.b] f2_tmtrnd/ RandGen read from InPPF OK " << endl;
|
---|
| 129 | unsigned short seed[3];
|
---|
[3606] | 130 | // rg.GetSeed(seed,2); // pour imprimer l'etat du generateur
|
---|
[3390] | 131 | {
|
---|
[3615] | 132 | Timer tm("f0_tmtrnd-Gaussian/drand01");
|
---|
[3390] | 133 | if (fgg)
|
---|
| 134 | for(sa_size_t k=0; k<VSZ; k++) DATA(k) = rg.Gaussian();
|
---|
| 135 | else
|
---|
| 136 | for(sa_size_t k=0; k<VSZ; k++) DATA(k) = rg.Flat01();
|
---|
| 137 | cout << "[2] f1_tmtrnd/ End of random generation using RandomGen class " << endl;
|
---|
| 138 | }
|
---|
| 139 | POutPersist po("data2.ppf");
|
---|
| 140 | po << DATA;
|
---|
| 141 | cout << "[3] f1_tmtrnd/ DATA saved to file data2.ppf " << endl;
|
---|
| 142 | POutPersist porg("rg2.ppf");
|
---|
| 143 | porg << rg;
|
---|
[3606] | 144 | // rg.GetSeed(seed,2); // pour imprimer l'etat du generateur
|
---|
[3390] | 145 | cout << "[4] f1_tmtrnd/ RandGen saved to file rg2.ppf " << endl;
|
---|
| 146 | return 0;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | //------- f3_tmtrnd()
|
---|
| 150 | int f3_tmtrnd(sa_size_t VSZ, int NTH, bool fgg=true)
|
---|
| 151 | {
|
---|
| 152 | cout << "[1] f3_tmtrnd/starting, VSZ= " << VSZ << " NTH= " << NTH << endl;
|
---|
| 153 | ResourceUsage res(ResourceUsage::RU_All);
|
---|
| 154 | vector<MTRnd *> vth;
|
---|
| 155 | TVector<r_8> DATA(VSZ);
|
---|
| 156 | sa_size_t csz = VSZ / NTH;
|
---|
| 157 | sa_size_t first, last;
|
---|
| 158 | cout << "[2] f3_tmtrnd/creating threads " << endl;
|
---|
| 159 | for(int kt=0; kt<NTH; kt++) {
|
---|
| 160 | first = kt*csz;
|
---|
| 161 | last = (kt == NTH-1) ? VSZ-1 : first+csz-1;
|
---|
| 162 | TVector<r_8> sv = DATA(Range(first, last));
|
---|
| 163 | vth.push_back(new MTRnd(sv, fgg) );
|
---|
| 164 | }
|
---|
| 165 | cout << "[3] f3_tmtrnd/starting threads " << endl;
|
---|
| 166 | for(int kt=0; kt<NTH; kt++) vth[kt]->start();
|
---|
| 167 |
|
---|
| 168 | cout << "[4] f3_tmtrnd/waiting for all threads to finish " << endl;
|
---|
| 169 | sleep(2);
|
---|
| 170 | for(int kt=0; kt<NTH; kt++) vth[kt]->join();
|
---|
| 171 |
|
---|
| 172 | cout << "[5] f3_tmtrnd/deleting thread objects " << endl;
|
---|
| 173 | for(int kt=0; kt<NTH; kt++) delete vth[kt];
|
---|
| 174 |
|
---|
| 175 | cout << "[6] f3_tmtrnd/saving DATA to file data3.ppf " << endl;
|
---|
| 176 | POutPersist po("data3.ppf");
|
---|
| 177 | po << DATA;
|
---|
| 178 | cout << res;
|
---|
| 179 | return 0;
|
---|
| 180 | }
|
---|
| 181 | //----------------- FIN des fonctions de test -----------------
|
---|
| 182 |
|
---|
| 183 | //-------------------------------------------------------------
|
---|
| 184 | //---------------------- MAIN MAIN MAIN -----------------------
|
---|
| 185 | //-------------------------------------------------------------
|
---|
| 186 |
|
---|
| 187 | int main(int narg, char *arg[])
|
---|
| 188 |
|
---|
| 189 | {
|
---|
| 190 | InitTim();
|
---|
| 191 | SophyaInit();
|
---|
| 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 | }
|
---|