| 1 | #include "sopnamsp.h"
 | 
|---|
| 2 | #include "zthread.h"
 | 
|---|
| 3 | #include "resusage.h"
 | 
|---|
| 4 | 
 | 
|---|
| 5 | #include <iostream>
 | 
|---|
| 6 | #include <vector>
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #include "tmatrix.h"
 | 
|---|
| 9 | #include "tvector.h"
 | 
|---|
| 10 | #include "matharr.h"
 | 
|---|
| 11 | #include "tarrinit.h"
 | 
|---|
| 12 | #include "stsrand.h"
 | 
|---|
| 13 | 
 | 
|---|
| 14 | #include <stdlib.h>
 | 
|---|
| 15 | #include <stdio.h>
 | 
|---|
| 16 | 
 | 
|---|
| 17 | /* -------------------------------------------------
 | 
|---|
| 18 |   Programme de test des classes de threads de SOPHYA
 | 
|---|
| 19 |   SOPHYA::ZThread SOPHYA::ZMutex ...
 | 
|---|
| 20 |   Exemples d'execution
 | 
|---|
| 21 |   csh> time zthr mtx 2 500     
 | 
|---|
| 22 |   csh> time zthr arr 2 500
 | 
|---|
| 23 |   csh> time zthr arrdl 2 500
 | 
|---|
| 24 |   csh> time zthr arrmf 2 500
 | 
|---|
| 25 |   csh> time zthr sync 4 1000
 | 
|---|
| 26 |   csh> time zthr syncp 4 1000
 | 
|---|
| 27 | */
 | 
|---|
| 28 |  
 | 
|---|
| 29 | #include <time.h>
 | 
|---|
| 30 | #include <unistd.h>
 | 
|---|
| 31 | 
 | 
|---|
| 32 | #include "timing.h"
 | 
|---|
| 33 | #include "ctimer.h"
 | 
|---|
| 34 | 
 | 
|---|
| 35 | 
 | 
|---|
| 36 | // --- Structure d'argument pour fonction d'execution dans les threads de test
 | 
|---|
| 37 | typedef struct  {
 | 
|---|
| 38 |   int_4 thid, NTh;
 | 
|---|
| 39 |   int_4 M, VSz;
 | 
|---|
| 40 | } ztarg;
 | 
|---|
| 41 | 
 | 
|---|
| 42 | // --- fonction de test simple avec boucle de sleep
 | 
|---|
| 43 | void funzt(void *arg)
 | 
|---|
| 44 | {
 | 
|---|
| 45 | time_t t0, t1;
 | 
|---|
| 46 | int i;
 | 
|---|
| 47 | 
 | 
|---|
| 48 | ztarg * za = (ztarg *)arg;
 | 
|---|
| 49 | 
 | 
|---|
| 50 | t0 = time(NULL);
 | 
|---|
| 51 | printf("+++++ funzt(ThId=%d) Entry to funzt (za.M=%d) +++++\n", za->thid, za->M);
 | 
|---|
| 52 | int imax = za->M;
 | 
|---|
| 53 | for(i=0; i<imax; i++)
 | 
|---|
| 54 |   {
 | 
|---|
| 55 |   sleep(3);
 | 
|---|
| 56 |   t1 = time(NULL)-t0;
 | 
|---|
| 57 |   printf("++funzt(ThId=%d)  Dt= %d \n", za->thid, (int)t1);
 | 
|---|
| 58 |   }
 | 
|---|
| 59 | 
 | 
|---|
| 60 | return;
 | 
|---|
| 61 | }
 | 
|---|
| 62 | 
 | 
|---|
| 63 | void randgenths(Array& mx, double sig)
 | 
|---|
| 64 | {
 | 
|---|
| 65 |   RandomGenerator rg;
 | 
|---|
| 66 |   for(sa_size_t j=0; j<mx.SizeY(); j++)   
 | 
|---|
| 67 |     for(sa_size_t i=0; i<mx.SizeX(); i++) mx(i,j) = rg.Gaussian(sig);   
 | 
|---|
| 68 | }
 | 
|---|
| 69 | 
 | 
|---|
| 70 | // --- fonction de test simple avec calcul matriciel (produit de 2 matrices double)
 | 
|---|
| 71 | void mtx_funzt(void *arg)
 | 
|---|
| 72 | {
 | 
|---|
| 73 |   ztarg * za = (ztarg *)arg;
 | 
|---|
| 74 |   cout << ">>>> mtx-funzt(ThId=" << za->thid << ") - Matrix size= " << za->M << endl;
 | 
|---|
| 75 |   
 | 
|---|
| 76 |   sa_size_t m = za->M;
 | 
|---|
| 77 |   Matrix a1(m,m), a2(m,m), mxprod; 
 | 
|---|
| 78 | //  a1 = RandomSequence(RandomSequence::Gaussian, 0., 4.);
 | 
|---|
| 79 | //  a2 = RandomSequence(RandomSequence::Gaussian, 0., 3.);
 | 
|---|
| 80 |   randgenths(a1,4.);
 | 
|---|
| 81 |   randgenths(a2,3.);
 | 
|---|
| 82 |   char buff[128];
 | 
|---|
| 83 |   sprintf(buff, "mtx-funzt(ThId=%d) EndOfInit", za->thid); 
 | 
|---|
| 84 |   PrtTim(buff);
 | 
|---|
| 85 |   mxprod = a1*a2;
 | 
|---|
| 86 |   sprintf(buff, "mtx-funzt(ThId=%d) EndOfMxProd", za->thid); 
 | 
|---|
| 87 |   PrtTim(buff);
 | 
|---|
| 88 |   return;
 | 
|---|
| 89 | }
 | 
|---|
| 90 | // --- fonction de test simple avec calcul sur tableaux entier I4
 | 
|---|
| 91 | void arr_funzt(void *arg)
 | 
|---|
| 92 | {
 | 
|---|
| 93 |   ztarg * za = (ztarg *)arg;
 | 
|---|
| 94 |   cout << ">>>> arr-funzt(ThId=" << za->thid << ") - Matrix size= " << za->M << endl;
 | 
|---|
| 95 |   
 | 
|---|
| 96 |   sa_size_t m = za->M;
 | 
|---|
| 97 |   TMatrix<int_4> a1(m,m), a2(m,m), ares; 
 | 
|---|
| 98 |   a1 = RegularSequence(1.,1.);
 | 
|---|
| 99 |   a2 = RegularSequence(5.,3.);
 | 
|---|
| 100 |   char buff[128];
 | 
|---|
| 101 |   sprintf(buff, "arr-funzt(ThId=%d) EndOfInit", za->thid); 
 | 
|---|
| 102 |   PrtTim(buff);
 | 
|---|
| 103 |   //  ares = 4*a1*12*a2;  correction le 23/05/2007 - * (prod.mtx par erreur)
 | 
|---|
| 104 |   ares = (4*a1)+(12*a2);
 | 
|---|
| 105 |   sprintf(buff, "arr-funzt(ThId=%d) EndOfOper", za->thid); 
 | 
|---|
| 106 |   PrtTim(buff);
 | 
|---|
| 107 |   return;
 | 
|---|
| 108 | }
 | 
|---|
| 109 | // --- fonction de test simple avec calcul de type DL sur tableaux double 1D
 | 
|---|
| 110 | void arrdl_funzt(void *arg)
 | 
|---|
| 111 | {
 | 
|---|
| 112 |   ztarg * za = (ztarg *)arg;
 | 
|---|
| 113 |   sa_size_t vsz = za->M*za->M;
 | 
|---|
| 114 |   sa_size_t EXS = 10;
 | 
|---|
| 115 | 
 | 
|---|
| 116 |   cout << ">>>> arrdl-funzt(ThId=" << za->thid << " DataBlock_Sz=M*M " << vsz  
 | 
|---|
| 117 |        << " V2=DLO4(V1) , NOp ~= 10*10*vsz=" << 10*EXS*vsz << endl;
 | 
|---|
| 118 |   
 | 
|---|
| 119 |   TVector<r_8> v1(vsz), v2(vsz); 
 | 
|---|
| 120 |   TVector<r_8> coeff(EXS); 
 | 
|---|
| 121 | //  coeff = RandomSequence();
 | 
|---|
| 122 |   randgenths(coeff, 1.);
 | 
|---|
| 123 |   //  v1 = RegularSequence(1.,0.001);  --- ATTENTION , couteux en temps
 | 
|---|
| 124 |   //  NDataBlock<r_8> v1(vsz, false), v2(vsz, false); 
 | 
|---|
| 125 |   for(sa_size_t i=0; i<vsz; i++)   v1(i) = i*0.001; 
 | 
|---|
| 126 |   char buff[128];
 | 
|---|
| 127 |   sprintf(buff, "arrdl-funzt(ThId=%d) EndOfInit", za->thid); 
 | 
|---|
| 128 |   PrtTim(buff);
 | 
|---|
| 129 | 
 | 
|---|
| 130 |   double c2 = 0.5;
 | 
|---|
| 131 |   double c3 = 1./6.;
 | 
|---|
| 132 |   double c4 = 1./24;
 | 
|---|
| 133 |   for(sa_size_t k=0; k<EXS; k++) {
 | 
|---|
| 134 |     for(sa_size_t i=0; i<vsz; i++) {
 | 
|---|
| 135 |       register double x = v1(i)*coeff(k); 
 | 
|---|
| 136 |       v2(i) = x*(1+x*(x*c2+x*(x*c3+x*x*c4)));
 | 
|---|
| 137 |     }
 | 
|---|
| 138 |   }
 | 
|---|
| 139 |   sprintf(buff, "arrdl-funzt(ThId=%d) EndOfOper", za->thid); 
 | 
|---|
| 140 |   PrtTim(buff);
 | 
|---|
| 141 |   return;
 | 
|---|
| 142 | }
 | 
|---|
| 143 | // --- fonction de test simple avec calcul de type fonction mathematique sur tableaux double 1D
 | 
|---|
| 144 | void arrmf_funzt(void *arg)
 | 
|---|
| 145 | {
 | 
|---|
| 146 |   ztarg * za = (ztarg *)arg;
 | 
|---|
| 147 |   sa_size_t vsz = za->M*za->M;
 | 
|---|
| 148 | 
 | 
|---|
| 149 |   cout << ">>>> arrmf-funzt(ThId=" << za->thid << " VecSz=M*M " << vsz  
 | 
|---|
| 150 |        << " V2=Sin(V1) , NOp ~= 50*vsz=" << 50*vsz << endl;
 | 
|---|
| 151 |   
 | 
|---|
| 152 |   TVector<r_8> v1(vsz), v2(vsz); 
 | 
|---|
| 153 |   //--  v1 = RegularSequence(1.,0.001);  COUTEUX en TCPU
 | 
|---|
| 154 |   for(sa_size_t i=0; i<vsz; i++)  v1(i) = i*0.001; 
 | 
|---|
| 155 |   char buff[128];
 | 
|---|
| 156 |   sprintf(buff, "arrmf-funzt(ThId=%d) EndOfInit", za->thid);
 | 
|---|
| 157 |   PrtTim(buff);
 | 
|---|
| 158 | 
 | 
|---|
| 159 |   v2 = Sin(v1);
 | 
|---|
| 160 |   v2 += Cos(v1);
 | 
|---|
| 161 |   sprintf(buff, "arrmf-funzt(ThId=%d) EndOfOper", za->thid); 
 | 
|---|
| 162 |   PrtTim(buff);
 | 
|---|
| 163 |   return;
 | 
|---|
| 164 | }
 | 
|---|
| 165 | 
 | 
|---|
| 166 | 
 | 
|---|
| 167 | // Structure de gestion utilisee par la classe MTVecPool
 | 
|---|
| 168 | typedef struct {
 | 
|---|
| 169 |   bool busy;
 | 
|---|
| 170 |   int stat;
 | 
|---|
| 171 | } St_VecPool;
 | 
|---|
| 172 | 
 | 
|---|
| 173 | // -------------------------------------------------------------------
 | 
|---|
| 174 | // Structure de gestion de zones memoire partagee (des vecteurs) entre
 | 
|---|
| 175 | // threads - qui doivent operer successivement sur les vecteurs
 | 
|---|
| 176 | // -------------------------------------------------------------------
 | 
|---|
| 177 | class MTVecPool {
 | 
|---|
| 178 | public:
 | 
|---|
| 179 |   MTVecPool(uint_4 nth, uint_4 vsz, uint_4 nvec)
 | 
|---|
| 180 |   {
 | 
|---|
| 181 |     if (nth > 60) throw ParmError("MTVecPool::MTVecPool() nth > 60");
 | 
|---|
| 182 |     if ((nth < 1) || (vsz < 2))
 | 
|---|
| 183 |         throw ParmError("MTVecPool::MTVecPool() nth<1 OR vsz<2 ");
 | 
|---|
| 184 |     _vmx.SetSize(vsz, nvec);
 | 
|---|
| 185 |     _nth = nth;
 | 
|---|
| 186 |     _vsz = vsz; 
 | 
|---|
| 187 |     TVector<int_8> xx(2);
 | 
|---|
| 188 |     for(int k=0; k<nth; k++) _vecp.push_back(xx);
 | 
|---|
| 189 |     cout << "-- MTVecPool(nth=" << nth << ")" << endl;
 | 
|---|
| 190 |     _vmx.Show();
 | 
|---|
| 191 |   }
 | 
|---|
| 192 |   ~MTVecPool()   { }
 | 
|---|
| 193 |   // Renvoie un pointeur de vecteur pour thread tid
 | 
|---|
| 194 |   TVector<int_8>*  GetVecP(uint_4 tid, uint_4& idx)
 | 
|---|
| 195 |   {
 | 
|---|
| 196 |     if (tid >= _nth) ParmError("MTVecPool::GetVecP() tid > _nth");
 | 
|---|
| 197 |     //DBG  cout << "DBG-GetVecP(tid= " << tid << ")" << endl;
 | 
|---|
| 198 |     if (tid == 0) {
 | 
|---|
| 199 |       mex.lock();
 | 
|---|
| 200 |       St_VecPool stv;
 | 
|---|
| 201 |       idx = _vecs.size();
 | 
|---|
| 202 |       _vecp[tid].Share(_vmx.Column(idx));
 | 
|---|
| 203 |       stv.busy = true;
 | 
|---|
| 204 |       stv.stat = 0;
 | 
|---|
| 205 |       _vecs.push_back(stv);
 | 
|---|
| 206 |       mex.unlock();
 | 
|---|
| 207 |       //DBG cout << "DBG-GetVecP(tid= " << tid << ") -> Idx=" << idx << " VecSz=" << &(_vecs[idx].vv) << endl;
 | 
|---|
| 208 |       return (&(_vecp[tid]));
 | 
|---|
| 209 |     }
 | 
|---|
| 210 |     else {
 | 
|---|
| 211 |       mex.lock();
 | 
|---|
| 212 |       bool found = false;
 | 
|---|
| 213 |       while (!found) {
 | 
|---|
| 214 |         for(uint_4 k=0; k<_vecs.size(); k++) {
 | 
|---|
| 215 |           if ( (_vecs[k].stat == tid) && (! _vecs[k].busy) ) {
 | 
|---|
| 216 |             found = true;  idx = k;
 | 
|---|
| 217 |             _vecs[k].stat = tid; _vecs[k].busy = true; 
 | 
|---|
| 218 |             break;
 | 
|---|
| 219 |           }
 | 
|---|
| 220 |         }
 | 
|---|
| 221 |         if (found) {
 | 
|---|
| 222 |           _vecp[tid].Share(_vmx.Column(idx));
 | 
|---|
| 223 |           mex.unlock();
 | 
|---|
| 224 |           //DBG  cout << "DBG-GetVecP(tid= " << tid << ") -> nv=" << hex << rv << dec << endl;
 | 
|---|
| 225 |           return (&(_vecp[tid]));
 | 
|---|
| 226 |         }
 | 
|---|
| 227 |         else { 
 | 
|---|
| 228 |           mex.broadcast();
 | 
|---|
| 229 |           mex.wait();
 | 
|---|
| 230 |         }
 | 
|---|
| 231 |       }
 | 
|---|
| 232 |     }
 | 
|---|
| 233 |   }
 | 
|---|
| 234 |   // Renvoie un vecteur pour thread tid
 | 
|---|
| 235 |   TVector<int_8>  GetVec(uint_4 tid, uint_4& idx)
 | 
|---|
| 236 |   {
 | 
|---|
| 237 |     if (tid >= _nth) ParmError("MTVecPool::GetVec() tid > _nth");
 | 
|---|
| 238 |     //DBG  cout << "DBG-GetVec(tid= " << tid << ")" << endl;
 | 
|---|
| 239 |     if (tid == 0) {
 | 
|---|
| 240 |       mex.lock();
 | 
|---|
| 241 |       St_VecPool stv;
 | 
|---|
| 242 |       idx = _vecs.size();
 | 
|---|
| 243 |       stv.busy = true;
 | 
|---|
| 244 |       stv.stat = 0;
 | 
|---|
| 245 |       _vecs.push_back(stv);
 | 
|---|
| 246 |       mex.unlock();
 | 
|---|
| 247 |       //DBG cout << "DBG-GetVec(tid= " << tid << ") -> Idx=" << idx << " VecSz=" << &(_vecs[idx].vv) << endl;
 | 
|---|
| 248 |       return (_vmx.Column(idx));
 | 
|---|
| 249 |     }
 | 
|---|
| 250 |     else {
 | 
|---|
| 251 |       mex.lock();
 | 
|---|
| 252 |       bool found = false;
 | 
|---|
| 253 |       while (!found) {
 | 
|---|
| 254 |         for(uint_4 k=0; k<_vecs.size(); k++) {
 | 
|---|
| 255 |           if ( (_vecs[k].stat == tid) && (! _vecs[k].busy) ) {
 | 
|---|
| 256 |             found = true;  idx = k;
 | 
|---|
| 257 |             _vecs[k].stat = tid; _vecs[k].busy = true; 
 | 
|---|
| 258 |             break;
 | 
|---|
| 259 |           }
 | 
|---|
| 260 |         }
 | 
|---|
| 261 |         if (found) {
 | 
|---|
| 262 |           mex.unlock();
 | 
|---|
| 263 |           //DBG  cout << "DBG-GetVec(tid= " << tid << ") -> nv=" << hex << rv << dec << endl;
 | 
|---|
| 264 |           return (_vmx.Column(idx));
 | 
|---|
| 265 |         }
 | 
|---|
| 266 |         else { 
 | 
|---|
| 267 |           mex.broadcast();
 | 
|---|
| 268 |           mex.wait();
 | 
|---|
| 269 |         }
 | 
|---|
| 270 |       }
 | 
|---|
| 271 |     }
 | 
|---|
| 272 |   }
 | 
|---|
| 273 | 
 | 
|---|
| 274 |   // Retourne l'index du vecteur au gestionnaire, qui le marque comme disponible
 | 
|---|
| 275 |   void RetVec(uint_4 idx)
 | 
|---|
| 276 |   {
 | 
|---|
| 277 |     //DBG cout << "DBG-RetVec(idx= " << idx << ")" << endl;
 | 
|---|
| 278 |     ZSync zs(mex, 2);
 | 
|---|
| 279 |     _vecs[idx].busy = false; _vecs[idx].stat++; 
 | 
|---|
| 280 |     zs.NOp();
 | 
|---|
| 281 |   }
 | 
|---|
| 282 | 
 | 
|---|
| 283 |   // Verifie l'etat memoire de tous les vecteurs et fait des print
 | 
|---|
| 284 |   int Check()
 | 
|---|
| 285 |   {
 | 
|---|
| 286 |     cout << "MTVecPool::Check() NVec=" << _vecs.size() << " VSz=" 
 | 
|---|
| 287 |          << _vsz << " NThreads=" << _nth << endl;
 | 
|---|
| 288 |     int nerr = 0;
 | 
|---|
| 289 |     int_8 sum = 0;
 | 
|---|
| 290 |     int_8 p2 = 1;
 | 
|---|
| 291 |     int_8 min,max;
 | 
|---|
| 292 |     for(int i=0; i<_nth; i++) { sum += p2; p2 *= 2; }
 | 
|---|
| 293 |     for(uint_4 k=0; k<_vecs.size(); k++) {
 | 
|---|
| 294 |       if ( (_vecs[k].busy) || (_vecs[k].stat != _nth) ) {
 | 
|---|
| 295 |         cout << " Check()/Pb Busy Or Stat  for k=" << k << endl;
 | 
|---|
| 296 |         nerr++;
 | 
|---|
| 297 |       }
 | 
|---|
| 298 |       _vmx.Column(k) -= sum;
 | 
|---|
| 299 |       _vmx.Column(k).MinMax(min, max);
 | 
|---|
| 300 |       if ((min!=0) || (max!=0)) {
 | 
|---|
| 301 |         cout << " Check()/Pb vec[k=" << k << "] != (sum=" << sum << ")" << endl;
 | 
|---|
| 302 |         nerr++;
 | 
|---|
| 303 |       }
 | 
|---|
| 304 |     }
 | 
|---|
| 305 |     if (nerr == 0) cout << "MTVecPool::Check()  - OK (NErr=0)" << endl;
 | 
|---|
| 306 |     else cout << "MTVecPool::Check() PB NErr=" << nerr << endl;
 | 
|---|
| 307 |     return nerr;
 | 
|---|
| 308 |   }
 | 
|---|
| 309 | 
 | 
|---|
| 310 |   // ... variables membres
 | 
|---|
| 311 |   ZMutex mex;
 | 
|---|
| 312 |   uint_4 _vsz;
 | 
|---|
| 313 |   uint_4 _nth;
 | 
|---|
| 314 |   TMatrix<int_8> _vmx;
 | 
|---|
| 315 |   vector< St_VecPool> _vecs;
 | 
|---|
| 316 |   vector< TVector<int_8> > _vecp;
 | 
|---|
| 317 | };
 | 
|---|
| 318 | 
 | 
|---|
| 319 | 
 | 
|---|
| 320 | static MTVecPool* mtvp = NULL;
 | 
|---|
| 321 | 
 | 
|---|
| 322 | // --- fonction de test avec synchronisation entre threads en utilisant pointeur de vecteurs
 | 
|---|
| 323 | void syncp_funzt(void *arg)
 | 
|---|
| 324 | {
 | 
|---|
| 325 |   ztarg * za = (ztarg *)arg;
 | 
|---|
| 326 |   cout << ">>>> syncp_funzt(ThId=" << za->thid << ") - NVec/NLoop= " << za->M << endl;
 | 
|---|
| 327 |   
 | 
|---|
| 328 |   if (mtvp == NULL) 
 | 
|---|
| 329 |     throw NullPtrError("syncp_funzt: MTVecPool* mtvp = NULL");
 | 
|---|
| 330 | 
 | 
|---|
| 331 |   int_4 L = za->M;
 | 
|---|
| 332 |   int_4 VS = za->VSz;
 | 
|---|
| 333 |   int_8 p2 = 1;
 | 
|---|
| 334 |   uint_4 k, ii, tid;
 | 
|---|
| 335 |   tid = za->thid;
 | 
|---|
| 336 |   for(k=0; k<tid; k++) p2 *= 2;
 | 
|---|
| 337 | 
 | 
|---|
| 338 |   char buff[128];
 | 
|---|
| 339 |   sprintf(buff, "syncp_funzt(ThId=%d) StarOfLoop", za->thid); 
 | 
|---|
| 340 |   PrtTim(buff);
 | 
|---|
| 341 |   uint_4 idx;
 | 
|---|
| 342 |   for(k=0; k<L; k++) {
 | 
|---|
| 343 |     *(mtvp->GetVecP(tid, idx)) += p2;
 | 
|---|
| 344 |     //DBG cout << "DBG-syncp_funzt(tid=" << tid << ", idx=" << idx << endl;
 | 
|---|
| 345 |       mtvp->RetVec(idx);
 | 
|---|
| 346 |   }
 | 
|---|
| 347 |   sprintf(buff, "syncp_funzt(ThId=%d) EndOfLoop", za->thid); 
 | 
|---|
| 348 |   PrtTim(buff);
 | 
|---|
| 349 |   return;
 | 
|---|
| 350 | }
 | 
|---|
| 351 | // --- fonction de test avec synchronisation entre threads
 | 
|---|
| 352 | void sync_funzt(void *arg)
 | 
|---|
| 353 | {
 | 
|---|
| 354 |   ztarg * za = (ztarg *)arg;
 | 
|---|
| 355 |   cout << ">>>> sync_funzt(ThId=" << za->thid << ") - NVec/NLoop= " << za->M << endl;
 | 
|---|
| 356 |   
 | 
|---|
| 357 |   if (mtvp == NULL) 
 | 
|---|
| 358 |     throw NullPtrError("sync_funzt: MTVecPool* mtvp = NULL");
 | 
|---|
| 359 | 
 | 
|---|
| 360 |   int_4 L = za->M;
 | 
|---|
| 361 |   int_4 VS = za->VSz;
 | 
|---|
| 362 |   int_8 p2 = 1;
 | 
|---|
| 363 |   uint_4 k, ii, tid;
 | 
|---|
| 364 |   tid = za->thid;
 | 
|---|
| 365 |   for(k=0; k<tid; k++) p2 *= 2;
 | 
|---|
| 366 | 
 | 
|---|
| 367 |   char buff[128];
 | 
|---|
| 368 |   sprintf(buff, "sync_funzt(ThId=%d) StarOfLoop", za->thid); 
 | 
|---|
| 369 |   PrtTim(buff);
 | 
|---|
| 370 |   uint_4 idx;
 | 
|---|
| 371 |   for(k=0; k<L; k++) {
 | 
|---|
| 372 |     mtvp->GetVec(tid, idx) += p2;
 | 
|---|
| 373 |     //DBG cout << "DBG-sync_funzt(tid=" << tid << ", idx=" << idx << endl;
 | 
|---|
| 374 |     mtvp->RetVec(idx);
 | 
|---|
| 375 |   }
 | 
|---|
| 376 |   sprintf(buff, "sync_funzt(ThId=%d) EndOfLoop", za->thid); 
 | 
|---|
| 377 |   PrtTim(buff);
 | 
|---|
| 378 |   return;
 | 
|---|
| 379 | }
 | 
|---|
| 380 | 
 | 
|---|
| 381 | class CountLock : public ZMutex {
 | 
|---|
| 382 |   int count;
 | 
|---|
| 383 | public:
 | 
|---|
| 384 |   CountLock() { count = 0; }
 | 
|---|
| 385 |   inline int Count() { lock(); int rc = ++count; unlock(); return(rc); 
 | 
|---|
| 386 |   }
 | 
|---|
| 387 | };
 | 
|---|
| 388 | 
 | 
|---|
| 389 | 
 | 
|---|
| 390 | static int N = 1;
 | 
|---|
| 391 | static int M = 5;
 | 
|---|
| 392 | static int VSZ = 32;
 | 
|---|
| 393 | 
 | 
|---|
| 394 | int main(int narg, char *arg[])
 | 
|---|
| 395 | 
 | 
|---|
| 396 | {
 | 
|---|
| 397 | 
 | 
|---|
| 398 |   if (narg < 4) {
 | 
|---|
| 399 |     cout << " Usage: zthr select N LM [Sz] " << endl;
 | 
|---|
| 400 |     cout << "  select= sl -> simple loop with sleep " << endl;
 | 
|---|
| 401 |     cout << "  select= mtx -> matrix<r_8> init and multiply mx1*mx2" << endl;
 | 
|---|
| 402 |     cout << "  select= arr -> array/matrix<int_4> init and operation c1*a1+c2*a2 " << endl;
 | 
|---|
| 403 |     cout << "  select= arrdl -> vectorOpe V2 ~= DLO4(V1), VSz=LM*LM " << endl;
 | 
|---|
| 404 |     cout << "  select= arrmf -> vectorOpe V2 = Sin(V1), VSz=LM*LM " << endl;
 | 
|---|
| 405 |     cout << "  select= clk -> Mutex lock count  " << endl;
 | 
|---|
| 406 |     cout << "  select= sync -> Thread synchronisation using ZMutex" << endl;
 | 
|---|
| 407 |     cout << "  select= syncp -> Thread synchronisation using ZMutex , Vector pointers" << endl;
 | 
|---|
| 408 |     cout << "  N= Number of threads (sl/mtx) or CountLock " << endl;
 | 
|---|
| 409 |     cout << "  LM = Loop limit (sl/sync) or Matrix size (mtx) " << endl;
 | 
|---|
| 410 |     cout << "  Sz = Vector size for select=sync,syncp (default=32) " << endl;
 | 
|---|
| 411 |     return(1);
 | 
|---|
| 412 |   }
 | 
|---|
| 413 | 
 | 
|---|
| 414 |   string sel = arg[1];
 | 
|---|
| 415 |   if ((sel != "sl") && (sel != "mtx") && (sel != "arr") && 
 | 
|---|
| 416 |       (sel != "arrdl") && (sel != "arrmf") && 
 | 
|---|
| 417 |       (sel != "sync") && (sel != "syncp") && (sel != "clk")) {
 | 
|---|
| 418 |     cout << "zthr/erreur argument sel (!= sl / mtx / arr / clk) " << endl;
 | 
|---|
| 419 |     return 2;
 | 
|---|
| 420 |   }
 | 
|---|
| 421 | 
 | 
|---|
| 422 |   //--  Decodage arguments
 | 
|---|
| 423 |   N = atoi(arg[2]);
 | 
|---|
| 424 |   M = atoi(arg[3]);
 | 
|---|
| 425 |   if (narg > 4) VSZ = atoi(arg[4]);
 | 
|---|
| 426 |   cout << "zthr/Info: select=" << sel << " N=" << N << " M= " << M 
 | 
|---|
| 427 |        << " VSz=" << VSZ << endl;
 | 
|---|
| 428 |  
 | 
|---|
| 429 |   
 | 
|---|
| 430 |   InitTim();
 | 
|---|
| 431 |   SophyaInit();
 | 
|---|
| 432 | 
 | 
|---|
| 433 |   int rc = 0;
 | 
|---|
| 434 |   try {
 | 
|---|
| 435 |     ResourceUsage res(ResourceUsage::RU_All);
 | 
|---|
| 436 |     if ((sel == "mtx") || (sel == "arr") || (sel == "sl") || 
 | 
|---|
| 437 |         (sel == "arrdl") || (sel == "arrmf") ||
 | 
|---|
| 438 |         (sel == "sync") || (sel == "syncp")) {
 | 
|---|
| 439 |       if ( (sel == "sync") || (sel == "syncp"))  mtvp = new MTVecPool(N,VSZ,M);
 | 
|---|
| 440 |       vector<ztarg *> vza;
 | 
|---|
| 441 |       vector<ZThread *> vzth;
 | 
|---|
| 442 |       for(int i=0; i<N; i++) {
 | 
|---|
| 443 |         cout << "*****zthr: Creating Thread " << i+1 << " /" << N << endl;
 | 
|---|
| 444 |         ZThread * pzt = new ZThread();
 | 
|---|
| 445 |         ztarg* zap = new ztarg;
 | 
|---|
| 446 |         vzth.push_back(pzt);
 | 
|---|
| 447 |         // ATTENTION : il faut que le thid = 0 ... N-1 (et pas 1)
 | 
|---|
| 448 |         zap->thid = i;  zap->M = M;
 | 
|---|
| 449 |         zap->NTh = N;   zap->VSz = VSZ;
 | 
|---|
| 450 |         vza.push_back(zap);
 | 
|---|
| 451 |         if (sel == "mtx")  pzt->setAction(mtx_funzt, vza[i]);
 | 
|---|
| 452 |         else if (sel == "arr")  pzt->setAction(arr_funzt, vza[i]);
 | 
|---|
| 453 |         else if (sel == "arrdl")  pzt->setAction(arrdl_funzt, vza[i]);
 | 
|---|
| 454 |         else if (sel == "arrmf")  pzt->setAction(arrmf_funzt, vza[i]);
 | 
|---|
| 455 |         else if (sel == "sync")  pzt->setAction(sync_funzt, vza[i]);
 | 
|---|
| 456 |         else if (sel == "syncp")  pzt->setAction(syncp_funzt, vza[i]);
 | 
|---|
| 457 |         else pzt->setAction(funzt, vza[i]);
 | 
|---|
| 458 |       }
 | 
|---|
| 459 |       cout << "***zthr: Starting threads ... " << endl;
 | 
|---|
| 460 |       PrtTim("***zthr/StarThr");
 | 
|---|
| 461 |       for(int i=0; i<N; i++) vzth[i]->start();
 | 
|---|
| 462 |       sleep(1);
 | 
|---|
| 463 |       cout << "***ResourceUsage before thr[i].join()" << endl;
 | 
|---|
| 464 |       cout << res;
 | 
|---|
| 465 |       cout << "***zthr Joining Threads ..." << endl;
 | 
|---|
| 466 |       for(int i=0; i<N; i++) vzth[i]->join();
 | 
|---|
| 467 |       cout << "***zthr Threads Z1 ... Z" << N << " Finished OK" << endl;
 | 
|---|
| 468 |       cout << " zthr/Resusage: getDataSize() = " << res.getDataSize() << "  getStackSize()=" 
 | 
|---|
| 469 |            << res.getStackSize() << endl;
 | 
|---|
| 470 |       cout << res;
 | 
|---|
| 471 |       for(int i=0; i<N; i++) {
 | 
|---|
| 472 |         delete vzth[i];
 | 
|---|
| 473 |         delete vza[i];
 | 
|---|
| 474 |       }
 | 
|---|
| 475 |       if (mtvp) {
 | 
|---|
| 476 |         Timer tm("MTVecPool::Check()");
 | 
|---|
| 477 |         mtvp->Check();
 | 
|---|
| 478 |         tm.Nop();
 | 
|---|
| 479 |         delete mtvp;
 | 
|---|
| 480 |       }
 | 
|---|
| 481 |     }
 | 
|---|
| 482 |     else {
 | 
|---|
| 483 |       PrtTim("BeginOfCount");
 | 
|---|
| 484 |       CountLock clk;
 | 
|---|
| 485 |       int kk;
 | 
|---|
| 486 |       for(kk=0; kk<atoi(arg[3]); kk++) {
 | 
|---|
| 487 |         clk.Count();
 | 
|---|
| 488 |       }
 | 
|---|
| 489 |       cout << " End CountLock-Test Count= " << clk.Count() << endl;
 | 
|---|
| 490 |     }
 | 
|---|
| 491 |   }
 | 
|---|
| 492 |   catch (PThrowable exc) {
 | 
|---|
| 493 |     cerr << "zthr: catched Exception " << exc.Msg() << endl;
 | 
|---|
| 494 |     rc = 77;
 | 
|---|
| 495 |   }  
 | 
|---|
| 496 |   catch (...) {
 | 
|---|
| 497 |     cerr << " catched unknown (...) exception (lpk.cc) " << endl; 
 | 
|---|
| 498 |     rc = 78; 
 | 
|---|
| 499 |   } 
 | 
|---|
| 500 |   
 | 
|---|
| 501 |   return(rc);
 | 
|---|
| 502 |   
 | 
|---|
| 503 | }
 | 
|---|
| 504 | 
 | 
|---|
| 505 | 
 | 
|---|
| 506 | 
 | 
|---|