source: Sophya/trunk/SophyaProg/Tests/jtctarr.cc@ 3178

Last change on this file since 3178 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: 3.8 KB
Line 
1#include "sopnamsp.h"
2#include "machdefs.h"
3#include "JTC/JTC.h"
4
5#include <math.h>
6#include <iostream>
7
8#include "tarrinit.h"
9#include "array.h"
10#include "timing.h"
11
12// Programme de test des threads JThreadsC++
13// avec les arrays de Sophya
14
15// A global monitor for print synchronisation
16JTCMonitor prtmon; // <JThreadC++>
17
18// A thread class for doing matrix computation
19class MtxComputer : public JTCThread // <JThreadC++>
20{
21public:
22 MtxComputer(int id, int sz, int nloop);
23 virtual void run();
24protected:
25 int id_;
26 int sz_;
27 int nloop_;
28};
29
30MtxComputer::MtxComputer(int id, int sz, int nloop)
31{
32 id_ = id; sz_ = sz; nloop_ = nloop;
33}
34
35void MtxComputer::run()
36{
37 int n = sz_;
38 double seuil = 1.e-6;
39 Matrix id;
40 id = IdentityMatrix(1.,n);
41 double gmax = -1.e99;
42 double gmin = 1.e99;
43 int npb = 0;
44 // Loop creating a random matrix, inverting it
45 // and checking the result
46 for(int k=0; k<nloop_; k++) {
47 try {
48 Matrix a(n,n);
49 a = Sequence(RandomSequence(RandomSequence::Gaussian, 0., 2.5));
50 Matrix inva = Inverse(a);
51 Matrix diff = id-a*inva;
52 double max = -1.e99;
53 double min = 1.e99;
54 double x;
55 int nerr = 0;
56 for(int i=0; i<n; i++)
57 for(int j=0; j<n; j++) {
58 x = diff(i,j);
59 if (x < min) min = diff(i,j);
60 if (x > max) max = diff(i,j);
61 if (fabs(x) > seuil) nerr++;
62 }
63 if (min < gmin) gmin = min;
64 if (max > gmax) gmax = max;
65 if (nerr > 0) npb++;
66 { // Synchronized writing to cout stream
67 JTCSynchronized sync(prtmon); // <JThreadC++>
68 cout << " ------- Thread[" << id_ << "] K= "
69 << k << " NErr = " << nerr << endl;
70 cout << " Min(Diff) = " << min << " Max(Diff) = " << max << endl;
71 if (k == nloop_-1) {
72 double frac = (double)npb*100./(double)nloop_;
73 cout << " ...... Thread[" << id_ << "] End NPb= " << npb
74 << " / NTot= " << nloop_ << " ( = " << frac << " %) " << endl;
75 cout << " GMin(Diff) = " << gmin << " GMax(Diff) = " << gmax << endl;
76 cout << " ..................................................... " << endl;
77 }
78 }
79 }
80 catch (PException const & e) {
81 cerr << " Catched PException in Thread(" << (string)getName()
82 << ") Msg= " << e.Msg() << endl;
83 }
84 catch (JTCException const & e) { // <JThreadC++>
85 cerr << " Catched JTCException in Thread(" << (string)getName()
86 << ") Msg= " << e.getMessage() << endl;
87 }
88 }
89}
90
91
92// ------- The main program ---------
93int main(int narg, char* arg[])
94{
95
96#define MAXNTHR 10
97 if (narg < 4) {
98 cout << " jtctarr - JThreadsC++/Sophya::TArray<T> Test \n "
99 << " ... Usage jtctarr NThreads NLoop MtxSize [-JTCss ... \n"
100 << " NThreads=1...4 NLoop=10...10^4 MtxSize=10...10^3 \n"
101 << endl;
102 exit(0);
103 }
104
105 InitTim(); // Initializing the CPU timer
106
107 int nthr = atoi(arg[1]);
108 if (nthr > MAXNTHR) nthr = MAXNTHR;
109 int nloop = atoi(arg[2]);
110 int size = atoi(arg[3]);
111 cout << " ::::: jtctarr - JThreadsC++/Sophya::TArray<T> Test ::::: \n"
112 << " NThreads= " << nthr << " NLoop= " << nloop << " MtxSize= "
113 << size << endl;
114
115 try {
116 SophyaInit(); // Sophya Initialization
117 JTCInitialize iniJTC(narg, arg); // <JThreadC++> Initialize library
118
119 JTCThreadHandle t[MAXNTHR]; // <JThreadC++>
120 int k;
121 for(k=0; k<nthr; k++) t[k] = new MtxComputer(k, size, nloop);
122 cout << " Starting threads ... " << endl;
123 for(k=0; k<nthr; k++) t[k]->start(); // <JThreadC++>
124 // Waiting for threads to end
125 for(k=0; k<nthr; k++) t[k]->join(); // <JThreadC++>
126 }
127 catch (PThrowable const & e) {
128 cerr << " Catched PThrowable in main Msg= " << e.Msg() << endl;
129 }
130 catch (JTCException const & e) { // <JThreadC++>
131 cerr << " Catched JTCException in main Msg= " << e.getMessage() << endl;
132 }
133 catch(...) {
134 cerr << " Catched ... exception in main " << endl;
135 }
136
137 PrtTim("End of jtctarr");
138}
Note: See TracBrowser for help on using the repository browser.