[1028] | 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;
|
---|
| 16 |
|
---|
| 17 | // A thread class for doing matrix computation
|
---|
| 18 | class MtxComputer : public JTCThread
|
---|
| 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 | // Loop creating a random matrix, inverting it
|
---|
| 41 | // and checking the result
|
---|
| 42 | for(int k=0; k<nloop_; k++) {
|
---|
| 43 | try {
|
---|
| 44 | Matrix a(n,n);
|
---|
| 45 | a = Sequence(RandomSequence(RandomSequence::Gaussian, 0., 2.5));
|
---|
| 46 | Matrix inva = Inverse(a);
|
---|
| 47 | Matrix diff = id-a*inva;
|
---|
| 48 | double max = -1.e99;
|
---|
| 49 | double min = 1.e99;
|
---|
| 50 | double x;
|
---|
| 51 | int nerr = 0;
|
---|
| 52 | for(int i=0; i<n; i++)
|
---|
| 53 | for(int j=0; j<n; j++) {
|
---|
| 54 | x = diff(i,j);
|
---|
| 55 | if (x < min) min = diff(i,j);
|
---|
| 56 | if (x > max) max = diff(i,j);
|
---|
| 57 | if (fabs(x) > seuil) nerr++;
|
---|
| 58 | }
|
---|
| 59 | { // Synchronized writing to cout stream
|
---|
| 60 | JTCSynchronized sync(prtmon);
|
---|
| 61 | cout << " ------- Thread[" << id_ << "] K= "
|
---|
| 62 | << k << " NErr = " << nerr << endl;
|
---|
| 63 | cout << " Min(Diff) = " << min << " Max(Diff) = " << max << endl;
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 | catch (PException const & e) {
|
---|
| 67 | cerr << " Catched PException in Thread(" << (string)getName()
|
---|
| 68 | << ") Msg= " << e.Msg() << endl;
|
---|
| 69 | }
|
---|
| 70 | catch (JTCException const & e) {
|
---|
| 71 | cerr << " Catched JTCException in Thread(" << (string)getName()
|
---|
| 72 | << ") Msg= " << e.getMessage() << endl;
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 |
|
---|
| 78 | // ------- The main program ---------
|
---|
| 79 | int main(int narg, char* arg[])
|
---|
| 80 | {
|
---|
| 81 |
|
---|
| 82 | #define MAXNTHR 10
|
---|
| 83 | if (narg < 4) {
|
---|
| 84 | cout << " jtctarr - JThreadsC++/Sophya::TArray<T> Test \n "
|
---|
| 85 | << " ... Usage jtctarr NThreads NLoop MtxSize [-JTCss ... \n"
|
---|
| 86 | << " NThreads=1...4 NLoop=10...10^4 MtxSize=10...10^3 \n"
|
---|
| 87 | << endl;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | InitTim(); // Initializing the CPU timer
|
---|
| 91 |
|
---|
| 92 | int nthr = atoi(arg[1]);
|
---|
| 93 | if (nthr > MAXNTHR) nthr = MAXNTHR;
|
---|
| 94 | int nloop = atoi(arg[2]);
|
---|
| 95 | int size = atoi(arg[3]);
|
---|
| 96 | cout << " ::::: jtctarr - JThreadsC++/Sophya::TArray<T> Test ::::: \n"
|
---|
| 97 | << " NThreads= " << nthr << " NLoop= " << nloop << " MtxSize= "
|
---|
| 98 | << size << endl;
|
---|
| 99 |
|
---|
| 100 | try {
|
---|
| 101 | SophyaInit(); // Sophya Initialization
|
---|
| 102 | JTCInitialize iniJTC(narg, arg);
|
---|
| 103 | JTCThreadHandle t[MAXNTHR];
|
---|
[1030] | 104 | int k;
|
---|
| 105 | for(k=0; k<nthr; k++) t[k] = new MtxComputer(k, size, nloop);
|
---|
[1028] | 106 | cout << " Starting threads ... " << endl;
|
---|
[1029] | 107 | for(k=0; k<nthr; k++) t[k]->start();
|
---|
[1028] | 108 | // Waiting for threads to end
|
---|
[1029] | 109 | for(k=0; k<nthr; k++) t[k]->join();
|
---|
[1028] | 110 | }
|
---|
| 111 | catch (PThrowable const & e) {
|
---|
| 112 | cerr << " Catched PThrowable in main Msg= " << e.Msg() << endl;
|
---|
| 113 | }
|
---|
| 114 | catch (JTCException const & e) {
|
---|
| 115 | cerr << " Catched JTCException in main Msg= " << e.getMessage() << endl;
|
---|
| 116 | }
|
---|
| 117 | catch(...) {
|
---|
| 118 | cerr << " Catched ... exception in main " << endl;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | PrtTim("End of jtctarr");
|
---|
| 122 | }
|
---|