[2615] | 1 | #include "sopnamsp.h"
|
---|
[1611] | 2 | #include "zthread.h"
|
---|
[3080] | 3 | #include "resusage.h"
|
---|
[1611] | 4 |
|
---|
[2322] | 5 | #include <iostream>
|
---|
[3080] | 6 | #include <vector>
|
---|
[1611] | 7 |
|
---|
[3080] | 8 | #include "tmatrix.h"
|
---|
[3177] | 9 | #include "tvector.h"
|
---|
[3080] | 10 | #include "tarrinit.h"
|
---|
| 11 |
|
---|
[1611] | 12 | #include <stdlib.h>
|
---|
| 13 | #include <stdio.h>
|
---|
| 14 |
|
---|
[3080] | 15 | /* -------------------------------------------------
|
---|
| 16 | Programme de test des classes de threads de SOPHYA
|
---|
| 17 | SOPHYA::ZThread SOPHYA::ZMutex ...
|
---|
| 18 | Exemples d'execution
|
---|
| 19 | csh> time zthr mtx 2 500
|
---|
| 20 | csh> time zthr arr 2 500
|
---|
[3177] | 21 | csh> time zthr sync 4 1000
|
---|
[3080] | 22 | */
|
---|
| 23 |
|
---|
[1611] | 24 | #include <time.h>
|
---|
| 25 | #include <unistd.h>
|
---|
| 26 |
|
---|
| 27 | #include "timing.h"
|
---|
[3177] | 28 | #include "ctimer.h"
|
---|
[1611] | 29 |
|
---|
| 30 |
|
---|
[3080] | 31 | // --- Structure d'argument pour fonction d'execution dans les threads de test
|
---|
| 32 | typedef struct {
|
---|
[3177] | 33 | int_4 thid;
|
---|
| 34 | int_4 M;
|
---|
[3080] | 35 | } ztarg;
|
---|
| 36 |
|
---|
| 37 | // --- fonction de test simple avec boucle de sleep
|
---|
| 38 | void funzt(void *arg)
|
---|
[1611] | 39 | {
|
---|
| 40 | time_t t0, t1;
|
---|
| 41 | int i;
|
---|
| 42 |
|
---|
[3080] | 43 | ztarg * za = (ztarg *)arg;
|
---|
| 44 |
|
---|
[1611] | 45 | t0 = time(NULL);
|
---|
[3080] | 46 | printf("+++++ funzt(ThId=%d) Entry to funzt (za.M=%d) +++++\n", za->thid, za->M);
|
---|
| 47 | int imax = za->M;
|
---|
[1611] | 48 | for(i=0; i<imax; i++)
|
---|
| 49 | {
|
---|
[3080] | 50 | sleep(3);
|
---|
[1611] | 51 | t1 = time(NULL)-t0;
|
---|
[3080] | 52 | printf("++funzt(ThId=%d) Dt= %d \n", za->thid, (int)t1);
|
---|
[1611] | 53 | }
|
---|
| 54 |
|
---|
| 55 | return;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[3080] | 58 | // --- fonction de test simple avec calcul matriciel
|
---|
| 59 | void mtx_funzt(void *arg)
|
---|
[1611] | 60 | {
|
---|
[3080] | 61 | ztarg * za = (ztarg *)arg;
|
---|
| 62 | cout << ">>>> mtx-funzt(ThId=" << za->thid << ") - Matrix size= " << za->M << endl;
|
---|
| 63 |
|
---|
| 64 | sa_size_t m = za->M;
|
---|
| 65 | Matrix a1(m,m), a2(m,m), mxprod;
|
---|
| 66 | a1 = RandomSequence(RandomSequence::Gaussian, 0., 4.);
|
---|
| 67 | a2 = RandomSequence(RandomSequence::Gaussian, 0., 3.);
|
---|
| 68 | char buff[128];
|
---|
| 69 | sprintf(buff, "mtx-funzt(ThId=%d) EndOfInit", za->thid);
|
---|
| 70 | PrtTim(buff);
|
---|
| 71 | mxprod = a1*a2;
|
---|
| 72 | sprintf(buff, "mtx-funzt(ThId=%d) EndOfMxProd", za->thid);
|
---|
| 73 | PrtTim(buff);
|
---|
| 74 | return;
|
---|
| 75 | }
|
---|
| 76 | // --- fonction de test simple avec calcul matriciel
|
---|
| 77 | void arr_funzt(void *arg)
|
---|
| 78 | {
|
---|
| 79 | ztarg * za = (ztarg *)arg;
|
---|
| 80 | cout << ">>>> arr-funzt(ThId=" << za->thid << ") - Matrix size= " << za->M << endl;
|
---|
| 81 |
|
---|
| 82 | sa_size_t m = za->M;
|
---|
| 83 | TMatrix<int_4> a1(m,m), a2(m,m), ares;
|
---|
| 84 | a1 = RegularSequence(1.,1.);
|
---|
| 85 | a2 = RegularSequence(5.,3.);
|
---|
| 86 | char buff[128];
|
---|
| 87 | sprintf(buff, "arr-funzt(ThId=%d) EndOfInit", za->thid);
|
---|
| 88 | PrtTim(buff);
|
---|
| 89 | ares = 4*a1*12*a2;
|
---|
| 90 | sprintf(buff, "arr-funzt(ThId=%d) EndOfOper", za->thid);
|
---|
| 91 | PrtTim(buff);
|
---|
| 92 | return;
|
---|
| 93 | }
|
---|
[1611] | 94 |
|
---|
| 95 |
|
---|
[3177] | 96 | // Structure de gestion utilisee par la classe MTVecPool
|
---|
| 97 | typedef struct {
|
---|
| 98 | bool busy;
|
---|
| 99 | int stat;
|
---|
| 100 | } St_VecPool;
|
---|
[1611] | 101 |
|
---|
[3177] | 102 | // -------------------------------------------------------------------
|
---|
| 103 | // Structure de gestion de zones memoire partagee (des vecteurs) entre
|
---|
| 104 | // threads - qui doivent operer successivement sur les vecteurs
|
---|
| 105 | // -------------------------------------------------------------------
|
---|
| 106 | class MTVecPool {
|
---|
| 107 | public:
|
---|
| 108 | MTVecPool(uint_4 nth, uint_4 vsz, uint_4 nvec)
|
---|
| 109 | {
|
---|
| 110 | if (nth > 60) throw ParmError("MTVecPool::MTVecPool() nth > 60");
|
---|
| 111 | if ((nth < 1) || (vsz < 2))
|
---|
| 112 | throw ParmError("MTVecPool::MTVecPool() nth<1 OR vsz<2 ");
|
---|
| 113 | _vmx.SetSize(vsz, nvec);
|
---|
| 114 | _nth = nth;
|
---|
| 115 | _vsz = vsz;
|
---|
| 116 | cout << "-- MTVecPool(nth=" << nth << ")" << endl;
|
---|
| 117 | _vmx.Show();
|
---|
| 118 | }
|
---|
| 119 | ~MTVecPool() { }
|
---|
| 120 | // Renvoie un pointeur de vecteur pour thread tid
|
---|
| 121 | TVector<int_8> GetVec(uint_4 tid, uint_4& idx)
|
---|
| 122 | {
|
---|
| 123 | if (tid >= _nth) ParmError("MTVecPool::getvec() tid > _nth");
|
---|
| 124 | //DBG cout << "DBG-GetVec(tid= " << tid << ")" << endl;
|
---|
| 125 | if (tid == 0) {
|
---|
| 126 | mex.lock();
|
---|
| 127 | St_VecPool stv;
|
---|
| 128 | idx = _vecs.size();
|
---|
| 129 | stv.busy = true;
|
---|
| 130 | stv.stat = 0;
|
---|
| 131 | _vecs.push_back(stv);
|
---|
| 132 | mex.unlock();
|
---|
| 133 | //DBG cout << "DBG-GetVec(tid= " << tid << ") -> Idx=" << idx << " VecSz=" << _vmx.Column(idx).Size() << endl;
|
---|
| 134 | return (_vmx.Column(idx));
|
---|
| 135 | }
|
---|
| 136 | else {
|
---|
| 137 | mex.lock();
|
---|
| 138 | bool found = false;
|
---|
| 139 | while (!found) {
|
---|
| 140 | for(uint_4 k=0; k<_vecs.size(); k++) {
|
---|
| 141 | if ( (_vecs[k].stat == tid) && (! _vecs[k].busy) ) {
|
---|
| 142 | found = true; idx = k;
|
---|
| 143 | _vecs[k].stat = tid; _vecs[k].busy = true;
|
---|
| 144 | break;
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 | if (found) {
|
---|
| 148 | mex.unlock();
|
---|
| 149 | //DBG cout << "DBG-GetVec(tid= " << tid << ") -> nv=" << hex << rv << dec << endl;
|
---|
| 150 | return (_vmx.Column(idx));
|
---|
| 151 | }
|
---|
| 152 | else {
|
---|
| 153 | mex.broadcast();
|
---|
| 154 | mex.wait();
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | // Retourne le pointeur de vecteur au gestionnaire, qui le marque comme disponible
|
---|
| 161 | void RetVec(uint_4 idx)
|
---|
| 162 | {
|
---|
| 163 | //DBG cout << "DBG-RetVec(idx= " << idx << ")" << endl;
|
---|
| 164 | ZSync zs(mex, 2);
|
---|
| 165 | _vecs[idx].busy = false; _vecs[idx].stat++;
|
---|
| 166 | zs.NOp();
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | // Verifie l'etat memoire de tous les vecteurs et fait des print
|
---|
| 170 | int Check()
|
---|
| 171 | {
|
---|
| 172 | cout << "MTVecPool::Check() NVec=" << _vecs.size() << " VSz="
|
---|
| 173 | << _vsz << " NThreads=" << _nth << endl;
|
---|
| 174 | int nerr = 0;
|
---|
| 175 | int_8 sum = 0;
|
---|
| 176 | int_8 p2 = 1;
|
---|
| 177 | int_8 min,max;
|
---|
| 178 | for(int i=0; i<_nth; i++) { sum += p2; p2 *= 2; }
|
---|
| 179 | for(uint_4 k=0; k<_vecs.size(); k++) {
|
---|
| 180 | if ( (_vecs[k].busy) || (_vecs[k].stat != _nth) ) {
|
---|
| 181 | cout << " Check()/Pb Busy Or Stat for k=" << k << endl;
|
---|
| 182 | nerr++;
|
---|
| 183 | }
|
---|
| 184 | _vmx.Column(k) -= sum;
|
---|
| 185 | _vmx.Column(k).MinMax(min, max);
|
---|
| 186 | if ((min!=0) || (max!=0)) {
|
---|
| 187 | cout << " Check()/Pb vec[k=" << k << "] != (sum=" << sum << ")" << endl;
|
---|
| 188 | nerr++;
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 | if (nerr == 0) cout << "MTVecPool::Check() - OK (NErr=0)" << endl;
|
---|
| 192 | else cout << "MTVecPool::Check() PB NErr=" << nerr << endl;
|
---|
| 193 | return nerr;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | // ... variables membres
|
---|
| 197 | ZMutex mex;
|
---|
| 198 | uint_4 _vsz;
|
---|
| 199 | uint_4 _nth;
|
---|
| 200 | TMatrix<int_8> _vmx;
|
---|
| 201 | vector< St_VecPool> _vecs;
|
---|
| 202 | };
|
---|
| 203 |
|
---|
| 204 |
|
---|
| 205 | static MTVecPool* mtvp = NULL;
|
---|
| 206 |
|
---|
| 207 | // --- fonction de test avec synchronisation entre threads
|
---|
| 208 | void sync_funzt(void *arg)
|
---|
| 209 | {
|
---|
| 210 | ztarg * za = (ztarg *)arg;
|
---|
| 211 | cout << ">>>> sync_funzt(ThId=" << za->thid << ") - NVec/NLoop= " << za->M << endl;
|
---|
| 212 |
|
---|
| 213 | if (mtvp == NULL)
|
---|
| 214 | throw NullPtrError("sync_funzt: MTVecPool* mtvp = NULL");
|
---|
| 215 |
|
---|
| 216 | int_4 L = za->M;
|
---|
| 217 | int_8 p2 = 1;
|
---|
| 218 | uint_4 k, tid;
|
---|
| 219 | tid = za->thid;
|
---|
| 220 | for(k=0; k<tid; k++) p2 *= 2;
|
---|
| 221 |
|
---|
| 222 | char buff[128];
|
---|
| 223 | sprintf(buff, "sync_funzt(ThId=%d) StarOfLoop", za->thid);
|
---|
| 224 | PrtTim(buff);
|
---|
| 225 | uint_4 idx;
|
---|
| 226 | for(k=0; k<L; k++) {
|
---|
| 227 | mtvp->GetVec(tid, idx) += p2;
|
---|
| 228 | //DBG cout << "DBG-sync_funzt(tid=" << tid << ", idx=" << idx << endl;
|
---|
| 229 | mtvp->RetVec(idx);
|
---|
| 230 | }
|
---|
| 231 | sprintf(buff, "sync_funzt(ThId=%d) EndOfLoop", za->thid);
|
---|
| 232 | PrtTim(buff);
|
---|
| 233 | return;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[1611] | 236 | class CountLock : public ZMutex {
|
---|
| 237 | int count;
|
---|
| 238 | public:
|
---|
| 239 | CountLock() { count = 0; }
|
---|
| 240 | inline int Count() { lock(); int rc = ++count; unlock(); return(rc);
|
---|
| 241 | }
|
---|
| 242 | };
|
---|
| 243 |
|
---|
| 244 |
|
---|
[3080] | 245 | static int N = 1;
|
---|
| 246 | static int M = 5;
|
---|
[3177] | 247 | static int VSZ = 128;
|
---|
[3080] | 248 |
|
---|
[1611] | 249 | int main(int narg, char *arg[])
|
---|
| 250 |
|
---|
| 251 | {
|
---|
| 252 |
|
---|
| 253 | if (narg < 4) {
|
---|
[3177] | 254 | cout << " Usage: zthr select N LM [Sz] " << endl;
|
---|
[3080] | 255 | cout << " select= sl -> simple loop with sleep " << endl;
|
---|
| 256 | cout << " select= mtx -> matrix init and multiply mx1*mx2" << endl;
|
---|
| 257 | cout << " select= arr -> array/matrix init and operation c1*a1+c2*a2 " << endl;
|
---|
| 258 | cout << " select= clk -> Mutex lock count " << endl;
|
---|
[3177] | 259 | cout << " select= sync -> Thread synchronisation using ZMutex" << endl;
|
---|
[3080] | 260 | cout << " N= Number of threads (sl/mtx) or CountLock " << endl;
|
---|
[3177] | 261 | cout << " LM = Loop limit (sl/sync) or Matrix size (mtx) " << endl;
|
---|
| 262 | cout << " Sz = Vector size for select=sync (default=256) " << endl;
|
---|
[3080] | 263 | return(1);
|
---|
[1611] | 264 | }
|
---|
| 265 |
|
---|
[3080] | 266 | string sel = arg[1];
|
---|
[3177] | 267 | if ((sel != "sl") && (sel != "mtx") && (sel != "arr") &&
|
---|
| 268 | (sel != "sync") && (sel != "clk")) {
|
---|
[3080] | 269 | cout << "zthr/erreur argument sel (!= sl / mtx / arr / clk) " << endl;
|
---|
| 270 | return 2;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | //-- Decodage arguments
|
---|
| 274 | N = atoi(arg[2]);
|
---|
| 275 | M = atoi(arg[3]);
|
---|
[3177] | 276 | if (narg > 4) VSZ = atoi(arg[4]);
|
---|
| 277 | cout << "zthr/Info: select=" << sel << " N=" << N << " M= " << M
|
---|
| 278 | << " VSz=" << VSZ << endl;
|
---|
[3080] | 279 |
|
---|
| 280 |
|
---|
[1611] | 281 | InitTim();
|
---|
[3080] | 282 | SophyaInit();
|
---|
[1611] | 283 |
|
---|
[3080] | 284 | int rc = 0;
|
---|
| 285 | try {
|
---|
[3177] | 286 | ResourceUsage res(ResourceUsage::RU_All);
|
---|
| 287 | if ((sel == "mtx") || (sel == "arr") || (sel == "sl") || (sel == "sync") ) {
|
---|
| 288 | if (sel == "sync") mtvp = new MTVecPool(N,VSZ,M);
|
---|
[3080] | 289 | vector<ztarg *> vza;
|
---|
| 290 | vector<ZThread *> vzth;
|
---|
| 291 | for(int i=0; i<N; i++) {
|
---|
| 292 | cout << "*****zthr: Creating Thread " << i+1 << " /" << N << endl;
|
---|
| 293 | ZThread * pzt = new ZThread();
|
---|
| 294 | ztarg* zap = new ztarg;
|
---|
| 295 | vzth.push_back(pzt);
|
---|
[3177] | 296 | // ATTENTION : il faut que le thid = 0 ... N-1 (et pas 1)
|
---|
| 297 | zap->thid = i; zap->M = M;
|
---|
[3080] | 298 | vza.push_back(zap);
|
---|
| 299 | if (sel == "mtx") pzt->setAction(mtx_funzt, vza[i]);
|
---|
| 300 | else if (sel == "arr") pzt->setAction(arr_funzt, vza[i]);
|
---|
[3177] | 301 | else if (sel == "sync") pzt->setAction(sync_funzt, vza[i]);
|
---|
[3080] | 302 | else pzt->setAction(funzt, vza[i]);
|
---|
| 303 | }
|
---|
| 304 | cout << "***zthr: Starting threads ... " << endl;
|
---|
| 305 | PrtTim("***zthr/StarThr");
|
---|
| 306 | for(int i=0; i<N; i++) vzth[i]->start();
|
---|
| 307 | sleep(1);
|
---|
| 308 | cout << "***ResourceUsage before thr[i].join()" << endl;
|
---|
| 309 | cout << res;
|
---|
| 310 | cout << "***zthr Joining Threads ..." << endl;
|
---|
| 311 | for(int i=0; i<N; i++) vzth[i]->join();
|
---|
| 312 | cout << "***zthr Threads Z1 ... Z" << N << " Finished OK" << endl;
|
---|
| 313 | cout << res;
|
---|
[3177] | 314 | cout << " Resu: getDataSize() = " << res.getDataSize() << " getStackSize()="
|
---|
| 315 | << res.getStackSize() << endl;
|
---|
[3080] | 316 | for(int i=0; i<N; i++) {
|
---|
| 317 | delete vzth[i];
|
---|
| 318 | delete vza[i];
|
---|
| 319 | }
|
---|
[3177] | 320 | if (mtvp) {
|
---|
| 321 | Timer tm("MTVecPool::Check()");
|
---|
| 322 | mtvp->Check();
|
---|
| 323 | tm.Nop();
|
---|
| 324 | delete mtvp;
|
---|
| 325 | }
|
---|
[3080] | 326 | }
|
---|
| 327 | else {
|
---|
| 328 | PrtTim("BeginOfCount");
|
---|
| 329 | CountLock clk;
|
---|
| 330 | int kk;
|
---|
| 331 | for(kk=0; kk<atoi(arg[3]); kk++) {
|
---|
| 332 | clk.Count();
|
---|
| 333 | }
|
---|
| 334 | cout << " End CountLock-Test Count= " << clk.Count() << endl;
|
---|
| 335 | }
|
---|
[1611] | 336 | }
|
---|
[3080] | 337 | catch (PThrowable exc) {
|
---|
| 338 | cerr << "zthr: catched Exception " << exc.Msg() << endl;
|
---|
| 339 | rc = 77;
|
---|
| 340 | }
|
---|
| 341 | catch (...) {
|
---|
| 342 | cerr << " catched unknown (...) exception (lpk.cc) " << endl;
|
---|
| 343 | rc = 78;
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | return(rc);
|
---|
| 347 |
|
---|
| 348 | }
|
---|
[1611] | 349 |
|
---|
| 350 |
|
---|
| 351 |
|
---|