[3396] | 1 | //--------------------------------------------------------------------------------
|
---|
| 2 | // Programme de test des classes NTuple/DataTable/ ... en multi-threads (SOPHYA)
|
---|
| 3 | // R. Ansari - C. Magneville , Novembre 2007
|
---|
| 4 | // (C) UPS+LAL IN2P3/CNRS (C) DAPNIA/SPP CEA - 2007
|
---|
| 5 | //
|
---|
| 6 | // Exemples de commandes pour test (renvoie Rc=0 si OK)
|
---|
| 7 | // csh> tmtdt
|
---|
| 8 | // tmtdt SEL TotSize NThreads [NOTHS]
|
---|
| 9 | // csh> tmtdt NT 50000 2
|
---|
| 10 | // csh> tmtdt DT 50000 2
|
---|
| 11 | // csh> tmtdt SWPPF 50000 2
|
---|
| 12 | // Le fits ne semble pas etre thread-safe, uniquement avec NTH=1
|
---|
| 13 | // csh> tmtdt SWFITS 50000 1
|
---|
| 14 | //--------------------------------------------------------------------------------
|
---|
| 15 |
|
---|
| 16 | #include "sopnamsp.h"
|
---|
| 17 | #include "sspvflags.h"
|
---|
| 18 | #include "zthread.h"
|
---|
| 19 | #include "resusage.h"
|
---|
| 20 | #include "ctimer.h"
|
---|
| 21 | #include "timing.h"
|
---|
| 22 |
|
---|
| 23 | #include <stdlib.h>
|
---|
| 24 | #include <stdio.h>
|
---|
| 25 | #include <unistd.h>
|
---|
| 26 | #include <string>
|
---|
| 27 | #include <iostream>
|
---|
| 28 |
|
---|
| 29 | #include "tvector.h"
|
---|
| 30 | #include "fioarr.h"
|
---|
| 31 | #include "matharr.h"
|
---|
| 32 | #include "tarrinit.h"
|
---|
| 33 |
|
---|
| 34 | // Pour tester la gestion multi-thread de Fill NTuple/DataTable
|
---|
| 35 | #include "ntuple.h"
|
---|
| 36 | #include "datatable.h"
|
---|
| 37 | #include "swppfdtable.h"
|
---|
| 38 | #ifndef SOPHYA_NO_FitsIOServer
|
---|
| 39 | #include "fitsinoutfile.h"
|
---|
| 40 | #include "swfitsdtable.h"
|
---|
| 41 | #include "fitshdtable.h"
|
---|
| 42 | #endif
|
---|
| 43 | #include "histinit.h"
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 |
|
---|
| 47 | //---------------------------------------------------------------------------------
|
---|
| 48 | // Definition d'une classe heritant de ZThread, pour remplir ou lire NTuple/DataTable
|
---|
| 49 | // des aleatoires
|
---|
| 50 | class MTntdt : public ZThread {
|
---|
| 51 | public:
|
---|
| 52 | MTntdt(bool fgc, sa_size_t sz, sa_size_t start, sa_size_t end);
|
---|
| 53 | inline void SetNTDTable(NTuple* nt=NULL, BaseDataTable* dt=NULL)
|
---|
| 54 | { nt_ = nt; dt_ = dt; }
|
---|
| 55 | inline uint_8 NErr() { return nerr_; }
|
---|
| 56 | virtual void run();
|
---|
| 57 | protected:
|
---|
| 58 | bool fgc_; // true -> fill , false -> read
|
---|
| 59 | sa_size_t size_, start_, end_;
|
---|
| 60 | NTuple* nt_; // A remplir si != NULL
|
---|
| 61 | BaseDataTable* dt_; // A remplir si != NULL
|
---|
| 62 | string nom_;
|
---|
| 63 | int tid_;
|
---|
| 64 | uint_8 nerr_;
|
---|
| 65 | };
|
---|
| 66 |
|
---|
| 67 | static int mtrandId = 0; // Pour donner un identificateur a chaque thread
|
---|
| 68 |
|
---|
| 69 | MTntdt::MTntdt(bool fgc, sa_size_t sz, sa_size_t start, sa_size_t end)
|
---|
| 70 | : fgc_(fgc) , size_(sz), start_(start) , end_(end) , nerr_(0)
|
---|
| 71 | {
|
---|
| 72 | SetNTDTable();
|
---|
| 73 | char buff[32];
|
---|
| 74 | sprintf(buff, "MTntdt-Id=%d", mtrandId);
|
---|
| 75 | tid_ = mtrandId; mtrandId++;
|
---|
| 76 | nom_ = buff;
|
---|
| 77 | cout << " Thread MTntdt(" << nom_ << " ) Created ... " << endl;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 |
|
---|
| 81 | // Le travail fait dans chaque thread (appele par start())
|
---|
| 82 | void MTntdt::run()
|
---|
| 83 | {
|
---|
| 84 | Timer tm(nom_.c_str());
|
---|
| 85 | char buff[128];
|
---|
| 86 | cout << "MTntdt::run() - Nom= " << nom_ << " TotSize " << size_
|
---|
| 87 | << " Start=" << start_ << " End=" << end_ << endl;
|
---|
| 88 | double x,cx,sx;
|
---|
| 89 | double xnt[10];
|
---|
| 90 | uint_8 nok = 0;
|
---|
| 91 |
|
---|
| 92 |
|
---|
| 93 | if (fgc_) { // Creation
|
---|
| 94 | if (nt_) {
|
---|
| 95 | for(sa_size_t k=start_; k<=end_; k++) {
|
---|
| 96 | x = k*0.013;
|
---|
| 97 | cx = cos(x);
|
---|
| 98 | sx = sin(x);
|
---|
| 99 |
|
---|
| 100 | xnt[0] = tid_;
|
---|
| 101 | xnt[1] = k;
|
---|
| 102 | xnt[2] = x;
|
---|
| 103 | xnt[3] = cx;
|
---|
| 104 | xnt[4] = sx;
|
---|
| 105 | nt_->Fill(xnt);
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | if (dt_) {
|
---|
| 109 | DataTableRow dtr = dt_->EmptyRow();
|
---|
| 110 | for(sa_size_t k=start_; k<=end_; k++) {
|
---|
| 111 | x = k*0.013;
|
---|
| 112 | cx = cos(x);
|
---|
| 113 | sx = sin(x);
|
---|
| 114 |
|
---|
| 115 | dtr[0] = tid_;
|
---|
| 116 | dtr[1] = k;
|
---|
| 117 | dtr[2] = x;
|
---|
| 118 | dtr[3] = cx;
|
---|
| 119 | dtr[4] = sx;
|
---|
| 120 | dt_->AddRow(dtr);
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | sprintf(buff, "%s : EndOf NT/DT Creation", nom_.c_str());
|
---|
| 124 | tm.Split(buff);
|
---|
| 125 | }
|
---|
| 126 | else { // lecture - verification
|
---|
| 127 | if (nt_) {
|
---|
| 128 | for(sa_size_t k=start_; k<=end_; k++) {
|
---|
| 129 | nt_->GetVecD(k, xnt);
|
---|
| 130 | sa_size_t kk = (sa_size_t)(xnt[1]+0.01);
|
---|
| 131 | x = kk*0.013;
|
---|
| 132 | if (fabs(x-xnt[2])>1.e-9) { nerr_++; continue; }
|
---|
| 133 | cx = cos(x);
|
---|
| 134 | if (fabs(cx-xnt[3])>1.e-9) { nerr_++; continue; }
|
---|
| 135 | sx = sin(x);
|
---|
| 136 | if (fabs(sx-xnt[4])>1.e-9) { nerr_++; continue; }
|
---|
| 137 | nok++;
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | if (dt_) {
|
---|
| 141 | DataTableRow dtr = dt_->EmptyRow();
|
---|
| 142 | for(sa_size_t k=start_; k<=end_; k++) {
|
---|
| 143 | dt_->GetRow(k, dtr);
|
---|
| 144 | sa_size_t kk = (int_8)(dtr[1]);
|
---|
| 145 | x = kk*0.013;
|
---|
| 146 | xnt[2] = (r_8)(dtr[2]);
|
---|
| 147 | if (fabs(x-xnt[2])>1.e-9) { nerr_++; continue; }
|
---|
| 148 | xnt[3] = (r_8)(dtr[3]);
|
---|
| 149 | cx = cos(x);
|
---|
| 150 | if (fabs(cx-xnt[3])>1.e-9) { nerr_++; continue; }
|
---|
| 151 | xnt[4] = (r_8)(dtr[4]);
|
---|
| 152 | sx = sin(x);
|
---|
| 153 | if (fabs(sx-xnt[4])>1.e-9) { nerr_++; continue; }
|
---|
| 154 | nok++;
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 | sprintf(buff, "%s : EndOf NT/DT Check NOk= %ld NErr=%ld ", nom_.c_str(),
|
---|
| 158 | (long)nok, (long)nerr_);
|
---|
| 159 | tm.Split(buff);
|
---|
| 160 | }
|
---|
| 161 | }
|
---|
| 162 | // ----- Fin de la definition de la classe MTntdt ----
|
---|
| 163 | //---------------------------------------------------------------------------------
|
---|
| 164 |
|
---|
| 165 |
|
---|
| 166 | //------- f3_tmtdt()
|
---|
| 167 | int f3_tmtdt(sa_size_t SZ, int NTH,
|
---|
| 168 | bool fgnt=true, bool fgdt=false, bool fgswppf=false, bool fgswfits=false,
|
---|
| 169 | bool fgthsafe=true)
|
---|
| 170 | {
|
---|
| 171 | cout << "[1] f3_tmtdt/starting, SZ= " << SZ << " NTH= " << NTH << endl;
|
---|
| 172 | if (fgthsafe) cout << " ... Running NTuple/DataTable Thread-safe " << endl;
|
---|
| 173 | else cout << " ... WARNING Running NTuple/DataTable NOT Thread-safe " << endl;
|
---|
| 174 |
|
---|
| 175 | ResourceUsage res(ResourceUsage::RU_All);
|
---|
| 176 | vector<MTntdt *> vth;
|
---|
| 177 |
|
---|
| 178 | sa_size_t csz = SZ / NTH;
|
---|
| 179 | sa_size_t first, last;
|
---|
| 180 |
|
---|
| 181 | int rc = 0;
|
---|
| 182 | // Verification des flags
|
---|
| 183 | if (!fgnt && !fgdt && !fgswppf && !fgswfits) fgnt = true;
|
---|
| 184 |
|
---|
| 185 | if (fgnt) fgdt = fgswppf = fgswfits = false;
|
---|
| 186 | else if (fgdt) fgswppf = fgswfits = false;
|
---|
| 187 | else if (fgswppf) fgswfits = false;
|
---|
| 188 |
|
---|
| 189 | #ifdef SOPHYA_NO_FitsIOServer
|
---|
| 190 | if (fgswfits) {
|
---|
| 191 | cout << " SOPHYA compiled without FitsIOServer --> SwPPFDataTable " << endl;
|
---|
| 192 | fgswppf = true; fgswfits = false;
|
---|
| 193 | }
|
---|
| 194 | #endif
|
---|
| 195 | char* nomnt[5]={"tid","k","x","cx","sx"};
|
---|
| 196 | NTuple* nt = NULL;
|
---|
| 197 | BaseDataTable* dt = NULL;
|
---|
| 198 | DataTable* dtmem = NULL;
|
---|
| 199 | SwPPFDataTable* dtswppf = NULL;
|
---|
| 200 |
|
---|
| 201 | POutPersist* pond = new POutPersist("mtdt.ppf");
|
---|
| 202 |
|
---|
| 203 | if (fgnt) {
|
---|
| 204 | nt = new NTuple(5, nomnt);
|
---|
| 205 | if (fgthsafe) nt->SetThreadSafe(true);
|
---|
| 206 | }
|
---|
| 207 | else if (fgdt) {
|
---|
| 208 | dtmem = new DataTable;
|
---|
| 209 | dt = dtmem;
|
---|
| 210 | }
|
---|
| 211 | else if (fgswppf) {
|
---|
| 212 | dtswppf = new SwPPFDataTable(*pond);
|
---|
| 213 | dt = dtswppf;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | #ifndef SOPHYA_NO_FitsIOServer
|
---|
| 217 | SwFitsDataTable* dtswfits = NULL;
|
---|
| 218 | FitsInOutFile* fio = NULL;
|
---|
| 219 | if (fgswfits) {
|
---|
| 220 | // SwFitsDataTable creation
|
---|
| 221 | fio = new FitsInOutFile("!mtdt.fits",FitsInOutFile::Fits_Create);
|
---|
| 222 | dtswfits = new SwFitsDataTable(*fio, 512, true);
|
---|
| 223 | dt = dtswfits;
|
---|
| 224 | }
|
---|
| 225 | #endif
|
---|
| 226 |
|
---|
| 227 | if (dt) {
|
---|
| 228 | if (fgthsafe) dt->SetThreadSafe(true);
|
---|
| 229 |
|
---|
| 230 | dt->AddIntegerColumn("tid");
|
---|
| 231 | dt->AddLongColumn("k");
|
---|
| 232 | dt->AddDoubleColumn("x");
|
---|
| 233 | dt->AddDoubleColumn("cx");
|
---|
| 234 | dt->AddDoubleColumn("sx");
|
---|
| 235 | }
|
---|
| 236 | Timer tm("f2_tmtdt-NTupleDTable/MultiThreads");
|
---|
| 237 | cout << "[2] f3_tmtdt/creating fill threads " << endl;
|
---|
| 238 |
|
---|
| 239 | for(int kt=0; kt<NTH; kt++) {
|
---|
| 240 | first = kt*csz;
|
---|
| 241 | last = (kt == NTH-1) ? SZ-1 : first+csz-1;
|
---|
| 242 | MTntdt* throp = new MTntdt(true, SZ, first, last);
|
---|
| 243 | throp->SetNTDTable(nt, dt);
|
---|
| 244 | vth.push_back(throp);
|
---|
| 245 | }
|
---|
| 246 | cout << "[3] f3_tmtdt/starting fill threads " << endl;
|
---|
| 247 | for(int kt=0; kt<NTH; kt++) vth[kt]->start();
|
---|
| 248 | cout << "[4] f3_tmtdt/waiting for all threads to finish " << endl;
|
---|
| 249 | sleep(2);
|
---|
| 250 | for(int kt=0; kt<NTH; kt++) vth[kt]->join();
|
---|
| 251 |
|
---|
| 252 | tm.Split();
|
---|
| 253 |
|
---|
| 254 | cout << "[5] f3_tmtdt/deleting thread objects " << endl;
|
---|
| 255 | for(int kt=0; kt<NTH; kt++) delete vth[kt];
|
---|
| 256 | vth.clear();
|
---|
| 257 |
|
---|
| 258 | if (pond && (dtmem || dtswppf || nt)) {
|
---|
| 259 | cout << "[6.a] f3_tmtdt/saving ntuple / data table to PPF " << endl;
|
---|
| 260 | if (nt) {
|
---|
| 261 | (*pond) << PPFNameTag("NT") << (*nt);
|
---|
| 262 | cout << "[6.a.1] f3_tmtdt/ Ntuple NT saved to mtdt.ppf " << endl;
|
---|
| 263 | cout << (*nt) ;
|
---|
| 264 | }
|
---|
| 265 | if (dtmem || dtswppf) {
|
---|
| 266 | (*pond) << PPFNameTag("DT");
|
---|
| 267 | if (dtmem) (*pond) << (*dtmem);
|
---|
| 268 | else if (dtswppf) (*pond) << (*dtswppf);
|
---|
| 269 | cout << "[6.a.2] f3_tmtdt/ DataTable DT saved to mtdt.ppf " << endl;
|
---|
| 270 | cout << (*dt) ;
|
---|
| 271 | }
|
---|
| 272 | }
|
---|
| 273 | if (pond) { delete pond; pond = NULL; } // Closing the OutPPF file
|
---|
| 274 |
|
---|
| 275 | tm.Split();
|
---|
| 276 |
|
---|
| 277 | // Si c'est un SwPPFDataTable, on le relit sur le fichier PPF
|
---|
| 278 | PInPersist* pind = NULL;
|
---|
| 279 | if (dtswppf) {
|
---|
| 280 | delete dtswppf;
|
---|
| 281 | SwPPFDataTable* dtswppf = new SwPPFDataTable();
|
---|
| 282 | pind = new PInPersist("mtdt.ppf");
|
---|
| 283 | (*pind) >> PPFNameTag("DT") >> (*dtswppf);
|
---|
| 284 | dt = dtswppf;
|
---|
| 285 | if (fgthsafe) dt->SetThreadSafe(true);
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | // Si c'est un SwFITSDataTable, on le relit sur le fichier FITS
|
---|
| 289 | #ifndef SOPHYA_NO_FitsIOServer
|
---|
| 290 | if (dtswfits) {
|
---|
| 291 | delete dtswfits;
|
---|
| 292 | delete fio; fio = NULL;
|
---|
| 293 | // fio = new FitsInOutFile("mtdt.fits",FitsInOutFile::Fits_RO);
|
---|
| 294 | // fio->MoveAbsToHDU(2);
|
---|
| 295 | // SwFitsDataTable creation/readin in from fits file
|
---|
| 296 | string finame = "mtdt.fits";
|
---|
| 297 | dtswfits = new SwFitsDataTable(finame);
|
---|
| 298 | dt = dtswfits;
|
---|
| 299 | }
|
---|
| 300 | #endif
|
---|
| 301 |
|
---|
| 302 | tm.Split();
|
---|
| 303 | cout << "[7] f3_tmtdt/creating read/check threads " << endl;
|
---|
| 304 |
|
---|
| 305 | for(int kt=0; kt<NTH; kt++) {
|
---|
| 306 | first = kt*csz;
|
---|
| 307 | last = (kt == NTH-1) ? SZ-1 : first+csz-1;
|
---|
| 308 | MTntdt* throp = new MTntdt(false, SZ, first, last);
|
---|
| 309 | throp->SetNTDTable(nt, dt);
|
---|
| 310 | vth.push_back(throp);
|
---|
| 311 | }
|
---|
| 312 | cout << "[8] f3_tmtdt/starting read/check threads " << endl;
|
---|
| 313 | for(int kt=0; kt<NTH; kt++) vth[kt]->start();
|
---|
| 314 | cout << "[8] f3_tmtdt/waiting for all threads to finish " << endl;
|
---|
| 315 | sleep(2);
|
---|
| 316 | for(int kt=0; kt<NTH; kt++) vth[kt]->join();
|
---|
| 317 |
|
---|
| 318 | tm.Split();
|
---|
| 319 |
|
---|
| 320 | cout << "[9] f3_tmtdt/deleting thread objects " << endl;
|
---|
| 321 | for(int kt=0; kt<NTH; kt++) {
|
---|
| 322 | cout << " Thread[" << kt << "] NErr= " << vth[kt]->NErr() << endl;
|
---|
| 323 | if (vth[kt]->NErr() != 0) rc++;
|
---|
| 324 | delete vth[kt];
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | if (pind) { delete pind; pind = NULL; } // Closing the InPPF file (for SwPPfDataTable)
|
---|
| 328 | if (nt) delete nt;
|
---|
| 329 | if (dt) delete dt;
|
---|
| 330 |
|
---|
| 331 | #ifndef SOPHYA_NO_FitsIOServer
|
---|
| 332 | if (fio) delete fio;
|
---|
| 333 | #endif
|
---|
| 334 |
|
---|
| 335 | cout << res;
|
---|
| 336 | return rc;
|
---|
| 337 | }
|
---|
| 338 | //----------------- FIN des fonctions de test -----------------
|
---|
| 339 |
|
---|
| 340 | //-------------------------------------------------------------
|
---|
| 341 | //---------------------- MAIN MAIN MAIN -----------------------
|
---|
| 342 | //-------------------------------------------------------------
|
---|
| 343 |
|
---|
| 344 | int main(int narg, char *arg[])
|
---|
| 345 |
|
---|
| 346 | {
|
---|
| 347 | InitTim();
|
---|
| 348 | SophyaInit();
|
---|
| 349 |
|
---|
| 350 | int rc = 0;
|
---|
| 351 | if (narg < 4) {
|
---|
| 352 | cout << "tmtdt/Error args - Usage: tmtdt SEL TotSize NThreads [NOTHS] " << endl;
|
---|
| 353 | cout << " SEL: =NT->NTuple , =DT->DataTable, =SWPPF , =SWFITS SwPPF.SwFits DataTable" << endl;
|
---|
| 354 | cout << " TotSize: Totale NTuple/DataTable NRows, NThreads= Number of Threads" << endl;
|
---|
| 355 | cout << " if arg[4]==NOTHS -> NO call tp SetThreadSafe(true)" << endl;
|
---|
| 356 | return 1;
|
---|
| 357 | }
|
---|
| 358 | string sel = arg[1];
|
---|
| 359 | bool fgnt,fgdt,fgswppf,fgswfits;
|
---|
| 360 | fgnt = fgdt = fgswppf = fgswfits= false;
|
---|
| 361 | if (sel == "NT") fgnt = true;
|
---|
| 362 | else if (sel == "DT") fgdt = true;
|
---|
| 363 | else if (sel == "SWPPF") fgswppf = true;
|
---|
| 364 | else if (sel == "SWFITS") fgswfits = true;
|
---|
| 365 |
|
---|
| 366 | bool fgths = true;
|
---|
| 367 | if ((narg > 4) && (strcmp(arg[4],"NOTHS")==0)) fgths = false;
|
---|
| 368 |
|
---|
| 369 | sa_size_t SZ = atoi(arg[2]);
|
---|
| 370 | int NTH = atoi(arg[3]);
|
---|
| 371 |
|
---|
| 372 | try {
|
---|
| 373 | rc = f3_tmtdt(SZ, NTH, fgnt, fgdt, fgswppf, fgswfits, fgths);
|
---|
| 374 | }
|
---|
| 375 | catch (PThrowable exc) {
|
---|
| 376 | cerr << "zthr: catched Exception " << exc.Msg() << endl;
|
---|
| 377 | rc = 77;
|
---|
| 378 | }
|
---|
| 379 | catch (...) {
|
---|
| 380 | cerr << " catched unknown (...) exception (tmtdt.cc) " << endl;
|
---|
| 381 | rc = 78;
|
---|
| 382 | }
|
---|
| 383 | cout << "----------- tmtdt/END (Rc= " << rc << " ) ------------ " << endl;
|
---|
| 384 | PrtTim("---END tmtdt---");
|
---|
| 385 | return(rc);
|
---|
| 386 |
|
---|
| 387 | }
|
---|