| [2615] | 1 | #include "sopnamsp.h"
 | 
|---|
| [1028] | 2 | #include "machdefs.h"
 | 
|---|
 | 3 | #include "JTC/JTC.h"
 | 
|---|
 | 4 | 
 | 
|---|
 | 5 | #include <math.h>
 | 
|---|
| [2322] | 6 | #include <iostream>
 | 
|---|
| [1028] | 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 
 | 
|---|
| [1033] | 16 | JTCMonitor prtmon;  // <JThreadC++> 
 | 
|---|
| [1028] | 17 | 
 | 
|---|
 | 18 | // A thread class for doing matrix computation 
 | 
|---|
| [1033] | 19 | class MtxComputer : public JTCThread   // <JThreadC++> 
 | 
|---|
| [1028] | 20 | {
 | 
|---|
 | 21 | public:
 | 
|---|
 | 22 |   MtxComputer(int id, int sz, int nloop);
 | 
|---|
 | 23 |   virtual void run();
 | 
|---|
 | 24 | protected:
 | 
|---|
 | 25 |   int id_;
 | 
|---|
 | 26 |   int sz_;
 | 
|---|
 | 27 |   int nloop_;
 | 
|---|
 | 28 | };
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 | MtxComputer::MtxComputer(int id, int sz, int nloop)
 | 
|---|
 | 31 | {
 | 
|---|
 | 32 |   id_ = id;  sz_ = sz;  nloop_ = nloop;  
 | 
|---|
 | 33 | }
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 | void MtxComputer::run()
 | 
|---|
 | 36 | {
 | 
|---|
 | 37 |   int n = sz_;
 | 
|---|
 | 38 |   double seuil = 1.e-6;
 | 
|---|
 | 39 |   Matrix id;
 | 
|---|
 | 40 |   id = IdentityMatrix(1.,n);
 | 
|---|
| [1033] | 41 |   double gmax = -1.e99;
 | 
|---|
 | 42 |   double gmin = 1.e99;
 | 
|---|
 | 43 |   int npb = 0;
 | 
|---|
| [1028] | 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 |         }
 | 
|---|
| [1033] | 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++> 
 | 
|---|
| [1028] | 68 |         cout << " ------- Thread[" << id_ << "] K= " 
 | 
|---|
 | 69 |              << k << "  NErr = " << nerr << endl;
 | 
|---|
 | 70 |         cout << "  Min(Diff) = " << min << " Max(Diff) = " << max << endl;
 | 
|---|
| [1033] | 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 |         }
 | 
|---|
| [1028] | 78 |       } 
 | 
|---|
 | 79 |     }
 | 
|---|
 | 80 |     catch (PException const & e) {
 | 
|---|
 | 81 |         cerr << " Catched PException in  Thread(" << (string)getName() 
 | 
|---|
 | 82 |              << ") Msg= " << e.Msg() << endl;
 | 
|---|
 | 83 |     }
 | 
|---|
| [1033] | 84 |     catch (JTCException const & e) {  // <JThreadC++>
 | 
|---|
| [1028] | 85 |         cerr << " Catched JTCException in  Thread(" << (string)getName() 
 | 
|---|
 | 86 |              << ") Msg= " << e.getMessage() << endl;
 | 
|---|
 | 87 |     }
 | 
|---|
 | 88 |   }
 | 
|---|
 | 89 | }
 | 
|---|
 | 90 | 
 | 
|---|
 | 91 | 
 | 
|---|
 | 92 | // ------- The main program ---------
 | 
|---|
 | 93 | int 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;
 | 
|---|
| [1033] | 102 |     exit(0);
 | 
|---|
| [1028] | 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
 | 
|---|
| [1033] | 117 |     JTCInitialize iniJTC(narg, arg);  // <JThreadC++>  Initialize library
 | 
|---|
 | 118 | 
 | 
|---|
 | 119 |     JTCThreadHandle t[MAXNTHR];  // <JThreadC++>  
 | 
|---|
| [1030] | 120 |     int k;
 | 
|---|
 | 121 |     for(k=0; k<nthr; k++)  t[k] = new MtxComputer(k, size, nloop);
 | 
|---|
| [1028] | 122 |     cout << " Starting threads ... " << endl;
 | 
|---|
| [1033] | 123 |     for(k=0; k<nthr; k++) t[k]->start();  // <JThreadC++> 
 | 
|---|
| [1028] | 124 |     // Waiting for threads to end
 | 
|---|
| [1033] | 125 |     for(k=0; k<nthr; k++) t[k]->join();   // <JThreadC++>
 | 
|---|
| [1028] | 126 |   }
 | 
|---|
 | 127 |   catch (PThrowable const & e) {
 | 
|---|
 | 128 |     cerr << " Catched PThrowable in main Msg= " << e.Msg() << endl;
 | 
|---|
 | 129 |   }
 | 
|---|
| [1033] | 130 |   catch (JTCException const & e) {  // <JThreadC++>
 | 
|---|
| [1028] | 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 | }
 | 
|---|