| 1 | #include "sopnamsp.h" | 
|---|
| 2 | #include "machdefs.h" | 
|---|
| 3 | #include "zthread.h" | 
|---|
| 4 |  | 
|---|
| 5 | #include <math.h> | 
|---|
| 6 | #include <unistd.h> | 
|---|
| 7 | #include <iostream> | 
|---|
| 8 | #include <stdexcept> | 
|---|
| 9 |  | 
|---|
| 10 | //  Include file pour PI | 
|---|
| 11 | #include "pisysdep.h" | 
|---|
| 12 |  | 
|---|
| 13 | #include PIAPP_H | 
|---|
| 14 | #include PIMENU_H | 
|---|
| 15 | #include PISTDWDG_H | 
|---|
| 16 | #include PIWIN_H | 
|---|
| 17 | #include PIPERIODIC_H | 
|---|
| 18 | #include "picons.h" | 
|---|
| 19 |  | 
|---|
| 20 | //  Include file Sophya (TArray) | 
|---|
| 21 | #include "tarrinit.h" | 
|---|
| 22 | #include "array.h" | 
|---|
| 23 | #include "timing.h" | 
|---|
| 24 |  | 
|---|
| 25 | using namespace std; | 
|---|
| 26 |  | 
|---|
| 27 | // .......................................................... | 
|---|
| 28 | // Programme interactive de test des threads ZThread | 
|---|
| 29 | // avec les arrays de Sophya et PI | 
|---|
| 30 | //     R. Ansari  LAL-IN2P3/CNRS   02/2001 | 
|---|
| 31 | // .......................................................... | 
|---|
| 32 |  | 
|---|
| 33 | // ................... MtxComputer ................... | 
|---|
| 34 | // A thread class for doing matrix computation | 
|---|
| 35 | class PIZTApp; | 
|---|
| 36 | class MtxComputer : public ZThread   // <JThreadC++> | 
|---|
| 37 | { | 
|---|
| 38 | public: | 
|---|
| 39 | MtxComputer(PIZTApp * app, PIScale * sc, int id, int sz, int nloop); | 
|---|
| 40 | virtual ~MtxComputer(); | 
|---|
| 41 | virtual void run(); | 
|---|
| 42 | protected: | 
|---|
| 43 | int id_; | 
|---|
| 44 | int sz_; | 
|---|
| 45 | int nloop_; | 
|---|
| 46 | PIZTApp * zapp_; | 
|---|
| 47 | PIScale * sc_; | 
|---|
| 48 | }; | 
|---|
| 49 |  | 
|---|
| 50 | // ................... CPUTimer ................... | 
|---|
| 51 | // Periodic class for CPU and elapsed time counting | 
|---|
| 52 | class CPUTimer : public PIPeriodic { | 
|---|
| 53 | public: | 
|---|
| 54 | CPUTimer(PIZTApp * app); | 
|---|
| 55 | virtual void DoPeriodic(); | 
|---|
| 56 | protected: | 
|---|
| 57 | PIZTApp * zapp; | 
|---|
| 58 | int dt; | 
|---|
| 59 | }; | 
|---|
| 60 |  | 
|---|
| 61 | // ................... PIZTApp ................... | 
|---|
| 62 | // La classe application   <JThreadC++> | 
|---|
| 63 | // class PIZTApp : public PIApplication , public ZThread { | 
|---|
| 64 | class PIZTApp : public PIApplication  {   // Pas oblige d'en faire un Thread | 
|---|
| 65 | public: | 
|---|
| 66 | PIZTApp(int narg, char* arg[]); | 
|---|
| 67 | ~PIZTApp(); | 
|---|
| 68 | virtual void   Process(PIMessage msg, PIMsgHandler* sender, void* data=NULL); | 
|---|
| 69 | virtual void   UpdateCPUTime(); | 
|---|
| 70 | virtual void   run();    //  <ZTread> | 
|---|
| 71 | // virtual void Run();    Si pas thread | 
|---|
| 72 | ZMutex &   getMutex();  // <ZThread>  for synchronization | 
|---|
| 73 | protected: | 
|---|
| 74 | PIMenu * m[3]; | 
|---|
| 75 | PIScale *sc[2]; | 
|---|
| 76 | PILabel *tlab[2]; | 
|---|
| 77 | PILabel *cputlab; | 
|---|
| 78 | PIConsole* cons; | 
|---|
| 79 | // CPU and elapsed time | 
|---|
| 80 | CPUTimer tm; | 
|---|
| 81 | clock_t cput0; | 
|---|
| 82 | time_t t0; | 
|---|
| 83 | // The computing threads | 
|---|
| 84 | ZThread * thr[2];    //  <JThreadC++> | 
|---|
| 85 | // for synchronization with other threads | 
|---|
| 86 | ZMutex amon;   //  <JThreadC++> | 
|---|
| 87 | int syn_cntrl; | 
|---|
| 88 | }; | 
|---|
| 89 |  | 
|---|
| 90 |  | 
|---|
| 91 | // ......................................................... | 
|---|
| 92 | // .............   PIZTApp  implementation ................. | 
|---|
| 93 | // ......................................................... | 
|---|
| 94 |  | 
|---|
| 95 | PIZTApp::PIZTApp(int narg, char* arg[]) | 
|---|
| 96 | : PIApplication(600, 400, narg, arg) , tm(this)  // 200 ms timer | 
|---|
| 97 | { | 
|---|
| 98 | if (narg < 3) throw ParmError("PIZTApp::PIZTApp() narg<3 !"); | 
|---|
| 99 |  | 
|---|
| 100 | m[0] = new PIMenu(Menubar(),"Fichier"); | 
|---|
| 101 | m[0]->AppendItem("Start", 10101); | 
|---|
| 102 | m[0]->AppendItem("Exit", 10110); | 
|---|
| 103 | AppendMenu(m[0]); | 
|---|
| 104 | m[1] = new PIMenu(Menubar(),"Thread-1"); | 
|---|
| 105 | m[1]->AppendItem("Stop", 10203); | 
|---|
| 106 | AppendMenu(m[1]); | 
|---|
| 107 | m[2] = new PIMenu(Menubar(),"Thread-2"); | 
|---|
| 108 | m[2]->AppendItem("Stop", 10303); | 
|---|
| 109 | AppendMenu(m[2]); | 
|---|
| 110 |  | 
|---|
| 111 | cputlab = new PILabel(MainWin(), "CPUTime", 590, 30, 5, 5); | 
|---|
| 112 | cputlab->SetBorderWidth(1); | 
|---|
| 113 |  | 
|---|
| 114 | tlab[0] = new PILabel(MainWin(), "Thr-1", 50, 30, 10, 40); | 
|---|
| 115 | sc[0] = new PIScale(MainWin(), "Sc-Thr-1", 510, kSDirLtoR, 220, 40, 70, 40); | 
|---|
| 116 | sc[0]->SetValue(0); | 
|---|
| 117 | tlab[1] = new PILabel(MainWin(), "Thr-2", 50, 30, 300, 40); | 
|---|
| 118 | sc[1] = new PIScale(MainWin(), "Sc-Thr-2", 520, kSDirLtoR, 220, 40, 360, 40); | 
|---|
| 119 | sc[1]->SetValue(0); | 
|---|
| 120 |  | 
|---|
| 121 | // Creating the console and redirecting output | 
|---|
| 122 | cons = new PIConsole(MainWin(), "PIConsole", 700, 500, 80, 600, 315, 0, 85, true ); | 
|---|
| 123 | cons->SetBinding(PIBK_fixed, PIBK_fixed, PIBK_fixed, PIBK_fixed); | 
|---|
| 124 | RedirectOutStream(cons); | 
|---|
| 125 | //RedirectErrStream(cons); | 
|---|
| 126 |  | 
|---|
| 127 | // Creating matrix Computing thread | 
|---|
| 128 | int nloop = atoi(arg[1]); | 
|---|
| 129 | int size = atoi(arg[2]); | 
|---|
| 130 | thr[0] = new MtxComputer(this, sc[0], 0, size, nloop); | 
|---|
| 131 | thr[1] = new MtxComputer(this, sc[1], 1, size, nloop); | 
|---|
| 132 |  | 
|---|
| 133 | cput0 = clock();  t0 = time(NULL); | 
|---|
| 134 | tm.Start();  // We start our cpu timer | 
|---|
| 135 | syn_cntrl = 0;  // Normal event loop | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | PIZTApp::~PIZTApp() | 
|---|
| 139 | { | 
|---|
| 140 | cout << " Getting in PIZTApp::~PIZTApp() - " << endl; | 
|---|
| 141 | int k; | 
|---|
| 142 | cout << " PIZTApp::~PIZTApp() - deleting m[0..2] ... " << endl; | 
|---|
| 143 | for(k=0; k<3; k++) delete m; | 
|---|
| 144 | cout << " PIZTApp::~PIZTApp() - deleting tlab/sc[0..1] ... " << endl; | 
|---|
| 145 | for(k=0; k<2; k++) { delete tlab[k];  delete sc[k]; } | 
|---|
| 146 | cout << " PIZTApp::~PIZTApp() - deleting cputlab ... " << endl; | 
|---|
| 147 | delete cputlab; | 
|---|
| 148 | cout << " PIZTApp::~PIZTApp() - deleting thr[0..1] ... " << endl; | 
|---|
| 149 | delete thr[0]; | 
|---|
| 150 | delete thr[1]; | 
|---|
| 151 | cout << " PIZTApp::~PIZTApp() - deleting cons ... " << endl; | 
|---|
| 152 | delete cons; | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | void PIZTApp::UpdateCPUTime() | 
|---|
| 156 | { | 
|---|
| 157 | clock_t cput; | 
|---|
| 158 | time_t elt; | 
|---|
| 159 | time_t tm;  struct tm * stm; | 
|---|
| 160 | elt = time(&tm);  stm = localtime(&tm); | 
|---|
| 161 | cput = clock(); | 
|---|
| 162 | float tcal = ( (float)(cput) - (float)(cput0) ) / (float)(CLOCKS_PER_SEC); | 
|---|
| 163 | float  etm = elt - t0; | 
|---|
| 164 | float percen = (elt > t0) ?  (tcal*100.)/etm : 0.; | 
|---|
| 165 | char buff[192]; | 
|---|
| 166 | sprintf(buff, "%02d:%02d:%02d CPUTime= %g sec Elapsed= %g sec (%g %%)", | 
|---|
| 167 | stm->tm_hour, stm->tm_min, stm->tm_sec, | 
|---|
| 168 | tcal, etm, percen); | 
|---|
| 169 |  | 
|---|
| 170 | string s = buff; | 
|---|
| 171 | cputlab->SetLabel(s); | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | ZMutex &  PIZTApp::getMutex() | 
|---|
| 175 | { | 
|---|
| 176 | syn_cntrl++; | 
|---|
| 177 | return amon; | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 | void PIZTApp::run() | 
|---|
| 181 | // void PIZTApp::Run()  Si pas thread | 
|---|
| 182 | { | 
|---|
| 183 |  | 
|---|
| 184 | XEvent evt; | 
|---|
| 185 | int szx, szy, szf; | 
|---|
| 186 | XtAppContext * appctx = PIXtAppCtx(szx, szy, szf); | 
|---|
| 187 | // Pour appeler FinishCreate() des objets dans la fenetre principale | 
|---|
| 188 | if (mStop) { // C'est la premiere fois | 
|---|
| 189 | topcont->SetSize(topcont->XSize(), topcont->YSize()); | 
|---|
| 190 | MBCont()->FinishCreate(); | 
|---|
| 191 | } | 
|---|
| 192 | else mStop = true;   // On rerentre apres un stop | 
|---|
| 193 |  | 
|---|
| 194 | cout << "DBG-PIZTApp::run/Run Starting event loop " << endl; | 
|---|
| 195 | try { | 
|---|
| 196 |  | 
|---|
| 197 | while (mStop) { | 
|---|
| 198 | //    while (syn_cntrl > 0) { | 
|---|
| 199 | //      amon.wait();  //  <JThreadC++> | 
|---|
| 200 | //      syn_cntrl = 0; | 
|---|
| 201 | //    } | 
|---|
| 202 | while (XtAppPending(*appctx) != 0) { | 
|---|
| 203 | amon.lock(); //  <ZThread> | 
|---|
| 204 | XtAppNextEvent(*appctx, &evt); | 
|---|
| 205 | XtDispatchEvent(&evt); | 
|---|
| 206 | amon.unlock(); // <ZThread> | 
|---|
| 207 | } | 
|---|
| 208 | //    usleep(1000); | 
|---|
| 209 | } | 
|---|
| 210 | } | 
|---|
| 211 | catch (PThrowable  & e) { | 
|---|
| 212 | cerr << "PIZTApp::Run() Catched Exception  Msg= " << e.Msg() << endl; | 
|---|
| 213 | } | 
|---|
| 214 | catch (...) { | 
|---|
| 215 | cerr << "PIZTApp::run() Catched ... exception " << endl; | 
|---|
| 216 | } | 
|---|
| 217 |  | 
|---|
| 218 |  | 
|---|
| 219 | } | 
|---|
| 220 |  | 
|---|
| 221 | void PIZTApp::Process(PIMessage msg, PIMsgHandler* sender, void* data) | 
|---|
| 222 | { | 
|---|
| 223 | int num; | 
|---|
| 224 | try { | 
|---|
| 225 | switch(UserMsg(msg)) { | 
|---|
| 226 | case 10101 : // starting computing threads | 
|---|
| 227 | thr[0]->start();  //  <ZThread> | 
|---|
| 228 | thr[1]->start();  //  <ZThread> | 
|---|
| 229 | break; | 
|---|
| 230 | case 10110 : // stoping computing threads | 
|---|
| 231 | RedirectOutStream(NULL); | 
|---|
| 232 | RedirectErrStream(NULL); | 
|---|
| 233 | cout << " PIZTApp::Process() Waiting for threads to terminate ... " << endl; | 
|---|
| 234 | thr[0]->join();  //  <ZThread> | 
|---|
| 235 | thr[1]->join();  //  <ZThread> | 
|---|
| 236 | Stop(); | 
|---|
| 237 | break; | 
|---|
| 238 | case 10203 : | 
|---|
| 239 | case 10303 : | 
|---|
| 240 | num = (UserMsg(msg)-10203)/100; | 
|---|
| 241 | thr[num]->stop();  //  <ZThread> | 
|---|
| 242 | break; | 
|---|
| 243 | } | 
|---|
| 244 | } | 
|---|
| 245 | catch (ZThreadExc  & e) {  // <ZThread> | 
|---|
| 246 | cerr << "PIZTApp::Process() Catched ZThreadExc  Msg= " << e.Msg() << endl; | 
|---|
| 247 | } | 
|---|
| 248 | catch (...) { | 
|---|
| 249 | cerr << "PIZTApp::Process() Catched ... exception " << endl; | 
|---|
| 250 | } | 
|---|
| 251 |  | 
|---|
| 252 | } | 
|---|
| 253 |  | 
|---|
| 254 | // ......................................................... | 
|---|
| 255 | // .............   CPUTimer implementation ................. | 
|---|
| 256 | // ......................................................... | 
|---|
| 257 |  | 
|---|
| 258 | CPUTimer::CPUTimer(PIZTApp * app) | 
|---|
| 259 | : PIPeriodic(1) | 
|---|
| 260 | { | 
|---|
| 261 | zapp = app; | 
|---|
| 262 | dt = -1; | 
|---|
| 263 | SetIntervalms(200);  // 200 ms interval timer | 
|---|
| 264 | } | 
|---|
| 265 |  | 
|---|
| 266 | void | 
|---|
| 267 | CPUTimer::DoPeriodic() | 
|---|
| 268 | { | 
|---|
| 269 | dt++; | 
|---|
| 270 | if ((dt == 0) || (dt >= 5)) { zapp->UpdateCPUTime();  dt = 0; } | 
|---|
| 271 | } | 
|---|
| 272 |  | 
|---|
| 273 |  | 
|---|
| 274 | // ......................................................... | 
|---|
| 275 | // .............  MtxComputer implementation ............... | 
|---|
| 276 | // ......................................................... | 
|---|
| 277 |  | 
|---|
| 278 | // A global monitor for print synchronisation | 
|---|
| 279 | ZMutex prtmon;  // <ZThread> | 
|---|
| 280 |  | 
|---|
| 281 | MtxComputer::MtxComputer(PIZTApp * app, PIScale * sc, int id, int sz, int nloop) | 
|---|
| 282 | { | 
|---|
| 283 | id_ = id;  sz_ = sz;  nloop_ = nloop;  zapp_ = app;  sc_ = sc; | 
|---|
| 284 | } | 
|---|
| 285 |  | 
|---|
| 286 | MtxComputer::~MtxComputer() | 
|---|
| 287 | { | 
|---|
| 288 | } | 
|---|
| 289 |  | 
|---|
| 290 | void MtxComputer::run() | 
|---|
| 291 | { | 
|---|
| 292 | int n = sz_; | 
|---|
| 293 | double seuil = 1.e-6; | 
|---|
| 294 | Matrix id; | 
|---|
| 295 | id = IdentityMatrix(1.,n); | 
|---|
| 296 | double gmax = -1.e99; | 
|---|
| 297 | double gmin = 1.e99; | 
|---|
| 298 | int npb = 0; | 
|---|
| 299 | // Loop creating a random matrix, inverting it | 
|---|
| 300 | // and checking the result | 
|---|
| 301 | for(int k=0; k<nloop_; k++) { | 
|---|
| 302 | try { | 
|---|
| 303 | Matrix a(n,n); | 
|---|
| 304 | a = RandomSequence(RandomSequence::Gaussian, 0., 2.5); | 
|---|
| 305 | Matrix inva = Inverse(a); | 
|---|
| 306 | Matrix diff = id-a*inva; | 
|---|
| 307 | double max = -1.e99; | 
|---|
| 308 | double min = 1.e99; | 
|---|
| 309 | double x; | 
|---|
| 310 | int nerr = 0; | 
|---|
| 311 | for(int i=0; i<n; i++) | 
|---|
| 312 | for(int j=0; j<n; j++) { | 
|---|
| 313 | x = diff(i,j); | 
|---|
| 314 | if (x < min)  min = diff(i,j); | 
|---|
| 315 | if (x > max)  max = diff(i,j); | 
|---|
| 316 | if (fabs(x) > seuil)  nerr++; | 
|---|
| 317 | } | 
|---|
| 318 | if (min < gmin) gmin = min; | 
|---|
| 319 | if (max > gmax) gmax = max; | 
|---|
| 320 | if (nerr > 0) npb++; | 
|---|
| 321 | {  // Synchronized writing to cout stream | 
|---|
| 322 | ZSync sync(prtmon);  // <ZThread> | 
|---|
| 323 | cout << " ------- Thread[" << id_ << "] K= " | 
|---|
| 324 | << k << "  NErr = " << nerr << endl; | 
|---|
| 325 | cout << "  Min(Diff) = " << min << " Max(Diff) = " << max << endl; | 
|---|
| 326 | if (k == nloop_-1) { | 
|---|
| 327 | double frac = (double)npb*100./(double)nloop_; | 
|---|
| 328 | cout << " ...... Thread[" << id_ << "] End NPb= " << npb | 
|---|
| 329 | << " / NTot= " << nloop_ << " ( = " << frac << " %) " << endl; | 
|---|
| 330 | cout << "  GMin(Diff) = " << gmin << " GMax(Diff) = " << gmax << endl; | 
|---|
| 331 | cout << " ..................................................... " << endl; | 
|---|
| 332 | } | 
|---|
| 333 | } | 
|---|
| 334 | {  // Synchronized updating of Scale Widget | 
|---|
| 335 | ZSync sync(zapp_->getMutex());  // <ZThread> | 
|---|
| 336 | // The event loop should be stopped until the end of the block | 
|---|
| 337 | int percentage = 100*(k+1)/nloop_; | 
|---|
| 338 | sc_->SetValue(percentage); | 
|---|
| 339 | } | 
|---|
| 340 |  | 
|---|
| 341 | } | 
|---|
| 342 | catch (ZThreadExc const & e) {  // <ZThread> | 
|---|
| 343 | cerr << "MtxComputer Catched ZThreadExc in  Thread(" // << (string)getName() | 
|---|
| 344 | << ") Msg= " << e.Msg() << endl; | 
|---|
| 345 | } | 
|---|
| 346 | catch (PException const & e) { | 
|---|
| 347 | cerr << "MtxComputer Catched PException in  Thread(" // << (string)getName() | 
|---|
| 348 | << ") Msg= " << e.Msg() << endl; | 
|---|
| 349 | } | 
|---|
| 350 | } | 
|---|
| 351 | } | 
|---|
| 352 |  | 
|---|
| 353 |  | 
|---|
| 354 | static void run_piapp(void * x) | 
|---|
| 355 | { | 
|---|
| 356 | PIZTApp * t = (PIZTApp *)x; | 
|---|
| 357 | t->run(); | 
|---|
| 358 | return; | 
|---|
| 359 | } | 
|---|
| 360 |  | 
|---|
| 361 | // ......................................................... | 
|---|
| 362 | // ................... The main program .................... | 
|---|
| 363 | // ......................................................... | 
|---|
| 364 |  | 
|---|
| 365 | int main(int narg, char* arg[]) | 
|---|
| 366 | { | 
|---|
| 367 |  | 
|---|
| 368 | if (narg < 3) { | 
|---|
| 369 | cout << " pizthr - ZThread/PI/Sophya::TArray<T> Test \n " | 
|---|
| 370 | << " ... Usage pizthr NLoop MtxSize [-JTCss ... \n" | 
|---|
| 371 | << "     NLoop=10...10^4  MtxSize=10...10^3 \n" | 
|---|
| 372 | << endl; | 
|---|
| 373 | exit(0); | 
|---|
| 374 | } | 
|---|
| 375 |  | 
|---|
| 376 | InitTim();   // Initializing the CPU timer | 
|---|
| 377 |  | 
|---|
| 378 | int nloop = atoi(arg[1]); | 
|---|
| 379 | int size = atoi(arg[2]); | 
|---|
| 380 | cout << " ::::: pizthr - ZThread/PI/Sophya::TArray<T> Test ::::: \n" | 
|---|
| 381 | << " NLoop= " << nloop << " MtxSize= " << size << endl; | 
|---|
| 382 |  | 
|---|
| 383 | try { | 
|---|
| 384 | SophyaInit();  // Sophya Initialization | 
|---|
| 385 | PIZTApp * t = new PIZTApp(narg, arg);  // <ZThread> | 
|---|
| 386 | //    t->Run(); | 
|---|
| 387 | ZThread * zt = new ZThread(50000); | 
|---|
| 388 | zt->setAction(run_piapp, t); | 
|---|
| 389 | zt->start();   // <ZThread>  -   starting event loop | 
|---|
| 390 | cout << " Waiting for PIZTApp thread to end ... " << endl; | 
|---|
| 391 | zt->join();    // <ZThread>  Waiting for thread to end | 
|---|
| 392 | //  PIZTApp * t = new  PIZTApp(narg, arg); Si pas thread | 
|---|
| 393 | //  t->Run();      Si pas thread | 
|---|
| 394 | cout << " PIZTApp thread ended - deleting PIZTApplication ... " << endl; | 
|---|
| 395 | delete zt; | 
|---|
| 396 | delete t; | 
|---|
| 397 | } | 
|---|
| 398 | catch (PThrowable const & e) { | 
|---|
| 399 | cerr << " Catched PThrowable in main Msg= " << e.Msg() << endl; | 
|---|
| 400 | } | 
|---|
| 401 | catch(...) { | 
|---|
| 402 | cerr << " Catched ... exception in main " << endl; | 
|---|
| 403 | } | 
|---|
| 404 |  | 
|---|
| 405 | PrtTim("End of pizthr"); | 
|---|
| 406 | } | 
|---|
| 407 |  | 
|---|
| 408 |  | 
|---|
| 409 |  | 
|---|
| 410 |  | 
|---|