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

Last change on this file since 1726 was 1343, checked in by ansari, 25 years ago

Ajout test LapackServer::SVD() ds lpk.cc - Reza 24/11/2000

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