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

Last change on this file since 3752 was 2946, checked in by ansari, 19 years ago

adaptation prog pizthr.cc suite modif interface ZThread - Reza 26/4/2006

File size: 11.0 KB
Line 
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
25using 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
35class PIZTApp;
36class MtxComputer : public ZThread // <JThreadC++>
37{
38public:
39 MtxComputer(PIZTApp * app, PIScale * sc, int id, int sz, int nloop);
40 virtual ~MtxComputer();
41 virtual void run();
42protected:
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
52class CPUTimer : public PIPeriodic {
53public:
54 CPUTimer(PIZTApp * app);
55 virtual void DoPeriodic();
56protected:
57 PIZTApp * zapp;
58 int dt;
59};
60
61// ................... PIZTApp ...................
62// La classe application <JThreadC++>
63// class PIZTApp : public PIApplication , public ZThread {
64class PIZTApp : public PIApplication { // Pas oblige d'en faire un Thread
65public:
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
73protected:
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
95PIZTApp::PIZTApp(int narg, char* arg[])
96 : PIApplication(600, 400, narg, arg) , tm(this) // 200 ms timer
97{
98if (narg < 3) throw ParmError("PIZTApp::PIZTApp() narg<3 !");
99
100m[0] = new PIMenu(Menubar(),"Fichier");
101m[0]->AppendItem("Start", 10101);
102m[0]->AppendItem("Exit", 10110);
103AppendMenu(m[0]);
104m[1] = new PIMenu(Menubar(),"Thread-1");
105m[1]->AppendItem("Cancel", 10203);
106AppendMenu(m[1]);
107m[2] = new PIMenu(Menubar(),"Thread-2");
108m[2]->AppendItem("Cancel", 10303);
109AppendMenu(m[2]);
110
111cputlab = new PILabel(MainWin(), "CPUTime", 590, 30, 5, 5);
112cputlab->SetBorderWidth(1);
113
114tlab[0] = new PILabel(MainWin(), "Thr-1", 50, 30, 10, 40);
115sc[0] = new PIScale(MainWin(), "Sc-Thr-1", 510, kSDirLtoR, 220, 40, 70, 40);
116sc[0]->SetValue(0);
117tlab[1] = new PILabel(MainWin(), "Thr-2", 50, 30, 300, 40);
118sc[1] = new PIScale(MainWin(), "Sc-Thr-2", 520, kSDirLtoR, 220, 40, 360, 40);
119sc[1]->SetValue(0);
120
121// Creating the console and redirecting output
122cons = new PIConsole(MainWin(), "PIConsole", 700, 500, 80, 600, 315, 0, 85, true );
123cons->SetBinding(PIBK_fixed, PIBK_fixed, PIBK_fixed, PIBK_fixed);
124RedirectOutStream(cons);
125//RedirectErrStream(cons);
126
127// Creating matrix Computing thread
128int nloop = atoi(arg[1]);
129int size = atoi(arg[2]);
130thr[0] = new MtxComputer(this, sc[0], 0, size, nloop);
131thr[1] = new MtxComputer(this, sc[1], 1, size, nloop);
132
133cput0 = clock(); t0 = time(NULL);
134tm.Start(); // We start our cpu timer
135syn_cntrl = 0; // Normal event loop
136}
137
138PIZTApp::~PIZTApp()
139{
140cout << " Getting in PIZTApp::~PIZTApp() - " << endl;
141int k;
142 cout << " PIZTApp::~PIZTApp() - deleting m[0..2] ... " << endl;
143for(k=0; k<3; k++) delete m;
144 cout << " PIZTApp::~PIZTApp() - deleting tlab/sc[0..1] ... " << endl;
145for(k=0; k<2; k++) { delete tlab[k]; delete sc[k]; }
146 cout << " PIZTApp::~PIZTApp() - deleting cputlab ... " << endl;
147delete cputlab;
148 cout << " PIZTApp::~PIZTApp() - deleting thr[0..1] ... " << endl;
149delete thr[0];
150delete thr[1];
151 cout << " PIZTApp::~PIZTApp() - deleting cons ... " << endl;
152delete cons;
153}
154
155void PIZTApp::UpdateCPUTime()
156{
157clock_t cput;
158time_t elt;
159time_t tm; struct tm * stm;
160elt = time(&tm); stm = localtime(&tm);
161cput = clock();
162float tcal = ( (float)(cput) - (float)(cput0) ) / (float)(CLOCKS_PER_SEC);
163float etm = elt - t0;
164float percen = (elt > t0) ? (tcal*100.)/etm : 0.;
165char buff[192];
166sprintf(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
170string s = buff;
171cputlab->SetLabel(s);
172}
173
174ZMutex & PIZTApp::getMutex()
175{
176syn_cntrl++;
177return amon;
178}
179
180void PIZTApp::run()
181 // void PIZTApp::Run() Si pas thread
182{
183
184XEvent evt;
185int szx, szy, szf;
186XtAppContext * appctx = PIXtAppCtx(szx, szy, szf);
187// Pour appeler FinishCreate() des objets dans la fenetre principale
188if (mStop) { // C'est la premiere fois
189 topcont->SetSize(topcont->XSize(), topcont->YSize());
190 MBCont()->FinishCreate();
191 }
192else mStop = true; // On rerentre apres un stop
193
194cout << "DBG-PIZTApp::run/Run Starting event loop " << endl;
195try {
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}
211catch (PThrowable & e) {
212 cerr << "PIZTApp::Run() Catched Exception Msg= " << e.Msg() << endl;
213}
214catch (...) {
215 cerr << "PIZTApp::run() Catched ... exception " << endl;
216}
217
218
219}
220
221void PIZTApp::Process(PIMessage msg, PIMsgHandler* sender, void* data)
222{
223 int num;
224try {
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]->cancel(); // <ZThread>
242 break;
243 }
244}
245catch (ZThreadExc & e) { // <ZThread>
246 cerr << "PIZTApp::Process() Catched ZThreadExc Msg= " << e.Msg() << endl;
247}
248catch (...) {
249 cerr << "PIZTApp::Process() Catched ... exception " << endl;
250}
251
252}
253
254// .........................................................
255// ............. CPUTimer implementation .................
256// .........................................................
257
258CPUTimer::CPUTimer(PIZTApp * app)
259: PIPeriodic(1)
260{
261zapp = app;
262dt = -1;
263SetIntervalms(200); // 200 ms interval timer
264}
265
266void
267CPUTimer::DoPeriodic()
268{
269dt++;
270if ((dt == 0) || (dt >= 5)) { zapp->UpdateCPUTime(); dt = 0; }
271}
272
273
274// .........................................................
275// ............. MtxComputer implementation ...............
276// .........................................................
277
278// A global monitor for print synchronisation
279ZMutex prtmon; // <ZThread>
280
281MtxComputer::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
286MtxComputer::~MtxComputer()
287{
288}
289
290void 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
354static 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
365int 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
Note: See TracBrowser for help on using the repository browser.