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