source: Sophya/trunk/SophyaProg/Tests/jtcpitarr.cc@ 3399

Last change on this file since 3399 was 2615, checked in by cmv, 21 years ago

using namespace sophya enleve de machdefs.h, nouveau sopnamsp.h cmv 10/09/2004

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