source: Sophya/trunk/SophyaProg/Tests/pizthr.cc@ 1726

Last change on this file since 1726 was 1611, checked in by ansari, 24 years ago

Programmes test pour ZThread - Reza 31/7/2001

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