#include "sopnamsp.h" #include "zthread.h" #include "resusage.h" #include "ctimer.h" #include "timing.h" #include #include #include #include #include #include "tvector.h" #include "fioarr.h" #include "matharr.h" #include "tarrinit.h" #include "srandgen.h" #include "stsrand.h" //-------------------------------------------------------------------------------- // Programme d'exemple multi-threads et test de la classe de generateur aleatoire // R. Ansari - C. Magneville , Novembre 2007 // (C) UPS+LAL IN2P3/CNRS (C) DAPNIA/SPP CEA - 2007 // Exemples de commandes pour test (dans l'ordre ci-dessous : // csh> tmtrnd OCG 1000000 XXX // csh> tmtrnd STG 1000000 1024 // csh> tmtrnd RDG 1000000 rg1.ppf // csh> tmtrnd MTG 3000000 4 //-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------- // Definition d'une classe heritant de ZThread, pour remplir un vecteur avec // des aleatoires class MTRnd : public ZThread { public: MTRnd(TVector& v, bool fgg=true); virtual void run(); protected: string nom_; TVector vv_; bool fgg_; // true -> gaussien, false rand01() }; static int mtrandId = 0; // Pour donner un identificateur a chaque thread MTRnd::MTRnd(Vector& v, bool fgg) : vv_(v,true), fgg_(fgg) // Partage de reference du vecteur { char buff[32]; sprintf(buff, "MTRnd-Id=%d", mtrandId); mtrandId++; nom_ = buff; cout << " Thread MTRnd(" << nom_ << " ) Created ... " << endl; } // Le travail fait dans chaque thread (appele par start()) void MTRnd::run() { Timer tm(nom_.c_str()); cout << "MTRnd::run() - Nom= " << nom_ << " vv.Size()= " << vv_.Size() << endl; RandomGenerator rgen; if (fgg_) for(sa_size_t k=0; k sv = DATA(Range(first, last)); vth.push_back(new MTRnd(sv, fgg) ); } cout << "[3] f3_tmtrnd/starting threads " << endl; for(int kt=0; ktstart(); cout << "[4] f3_tmtrnd/waiting for all threads to finish " << endl; sleep(2); for(int kt=0; ktjoin(); cout << "[5] f3_tmtrnd/deleting thread objects " << endl; for(int kt=0; kt); DObjRegister(ObjFileIO, RandomGenerator); int rc = 0; if (narg < 4) { cout << " tmtrnd/Error args - Usage: tmtrnd SEL arg1 arg2 ... " << endl; cout << " ==> SEL=OCG/OCF : tmtrnd OCG/OCF VecSize XXX (-> f0_tmtrnd())" << endl; cout << " OCG/OCF Random gen. with (old) c-functions G/F-> Gaussian/Flat " << endl; cout << " ==> SEL=STG/STF : tmtrnd STG/STF VecSize SeqSize (-> f2_tmtrnd())" << endl; cout << " STG/STF RandGen(SeqSize) SeqSz=0 -> NO-ThSafe G/F-> Gaussian/Flat " << endl; cout << " ==> SEL=RDG/RDF : tmtrnd RDG/RDF VecSize InPPFName (-> f2_tmtrnd())" << endl; cout << " RDG/RDF Read RandGen object from file InPPFName G/F-> Gaussian/Flat " << endl; cout << " ==> SEL=MTG/MTF : tmtrnd MTG/MTF VecSize NThread (-> f3_tmtrnd()) " << endl; cout << " MTG/MTF Multithread generation, MTG-> Gaussian, MTF-> Flat " << endl; return 1; } string sel = arg[1]; try { bool fgg = (sel[2]=='G')?true:false; if ((sel == "OCG")||(sel == "OCF")) { sa_size_t VSZ = atoi(arg[2]); rc = f0_tmtrnd(VSZ, fgg); } if ((sel == "STG")||(sel == "STF")) { sa_size_t VSZ = atoi(arg[2]); size_t seqsz = atoi(arg[3]); rc = f1_tmtrnd(VSZ, seqsz, fgg); } if ((sel == "RDG")||(sel == "RDF")) { sa_size_t VSZ = atoi(arg[2]); string inppf = arg[3]; rc = f2_tmtrnd(VSZ, inppf, fgg); } if ((sel == "MTG")||(sel == "MTF")) { sa_size_t VSZ = atoi(arg[2]); int NTH = atoi(arg[3]); rc = f3_tmtrnd(VSZ, NTH, fgg); } } catch (PThrowable exc) { cerr << "zthr: catched Exception " << exc.Msg() << endl; rc = 77; } catch (...) { cerr << " catched unknown (...) exception (tmtrnd.cc) " << endl; rc = 78; } cout << "----------- tmtrnd/END ------- " << endl; PrtTim("---END tmtrnd---"); return(rc); }