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