[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 {
|
---|
[3178] | 98 | TVector<int_8> vv;
|
---|
[3177] | 99 | bool busy;
|
---|
| 100 | int stat;
|
---|
| 101 | } St_VecPool;
|
---|
[1611] | 102 |
|
---|
[3177] | 103 | // -------------------------------------------------------------------
|
---|
| 104 | // Structure de gestion de zones memoire partagee (des vecteurs) entre
|
---|
| 105 | // threads - qui doivent operer successivement sur les vecteurs
|
---|
| 106 | // -------------------------------------------------------------------
|
---|
| 107 | class MTVecPool {
|
---|
| 108 | public:
|
---|
| 109 | MTVecPool(uint_4 nth, uint_4 vsz, uint_4 nvec)
|
---|
| 110 | {
|
---|
| 111 | if (nth > 60) throw ParmError("MTVecPool::MTVecPool() nth > 60");
|
---|
| 112 | if ((nth < 1) || (vsz < 2))
|
---|
| 113 | throw ParmError("MTVecPool::MTVecPool() nth<1 OR vsz<2 ");
|
---|
| 114 | _vmx.SetSize(vsz, nvec);
|
---|
| 115 | _nth = nth;
|
---|
| 116 | _vsz = vsz;
|
---|
| 117 | cout << "-- MTVecPool(nth=" << nth << ")" << endl;
|
---|
| 118 | _vmx.Show();
|
---|
| 119 | }
|
---|
| 120 | ~MTVecPool() { }
|
---|
| 121 | // Renvoie un pointeur de vecteur pour thread tid
|
---|
[3178] | 122 | TVector<int_8>* GetVec(uint_4 tid, uint_4& idx)
|
---|
[3177] | 123 | {
|
---|
| 124 | if (tid >= _nth) ParmError("MTVecPool::getvec() tid > _nth");
|
---|
| 125 | //DBG cout << "DBG-GetVec(tid= " << tid << ")" << endl;
|
---|
| 126 | if (tid == 0) {
|
---|
| 127 | mex.lock();
|
---|
| 128 | St_VecPool stv;
|
---|
| 129 | idx = _vecs.size();
|
---|
[3178] | 130 | stv.vv = _vmx.Column(idx);
|
---|
[3177] | 131 | stv.busy = true;
|
---|
| 132 | stv.stat = 0;
|
---|
| 133 | _vecs.push_back(stv);
|
---|
| 134 | mex.unlock();
|
---|
[3178] | 135 | //DBG cout << "DBG-GetVec(tid= " << tid << ") -> Idx=" << idx << " VecSz=" << &(_vecs[idx].vv) << endl;
|
---|
| 136 | return (&(_vecs[idx].vv));
|
---|
[3177] | 137 | }
|
---|
| 138 | else {
|
---|
| 139 | mex.lock();
|
---|
| 140 | bool found = false;
|
---|
| 141 | while (!found) {
|
---|
| 142 | for(uint_4 k=0; k<_vecs.size(); k++) {
|
---|
| 143 | if ( (_vecs[k].stat == tid) && (! _vecs[k].busy) ) {
|
---|
| 144 | found = true; idx = k;
|
---|
| 145 | _vecs[k].stat = tid; _vecs[k].busy = true;
|
---|
| 146 | break;
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 | if (found) {
|
---|
| 150 | mex.unlock();
|
---|
| 151 | //DBG cout << "DBG-GetVec(tid= " << tid << ") -> nv=" << hex << rv << dec << endl;
|
---|
[3178] | 152 | return (&(_vecs[idx].vv));
|
---|
[3177] | 153 | }
|
---|
| 154 | else {
|
---|
| 155 | mex.broadcast();
|
---|
| 156 | mex.wait();
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 | }
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[3178] | 162 | // Retourne l'index du vecteur au gestionnaire, qui le marque comme disponible
|
---|
[3177] | 163 | void RetVec(uint_4 idx)
|
---|
| 164 | {
|
---|
| 165 | //DBG cout << "DBG-RetVec(idx= " << idx << ")" << endl;
|
---|
| 166 | ZSync zs(mex, 2);
|
---|
| 167 | _vecs[idx].busy = false; _vecs[idx].stat++;
|
---|
| 168 | zs.NOp();
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | // Verifie l'etat memoire de tous les vecteurs et fait des print
|
---|
| 172 | int Check()
|
---|
| 173 | {
|
---|
| 174 | cout << "MTVecPool::Check() NVec=" << _vecs.size() << " VSz="
|
---|
| 175 | << _vsz << " NThreads=" << _nth << endl;
|
---|
| 176 | int nerr = 0;
|
---|
| 177 | int_8 sum = 0;
|
---|
| 178 | int_8 p2 = 1;
|
---|
| 179 | int_8 min,max;
|
---|
| 180 | for(int i=0; i<_nth; i++) { sum += p2; p2 *= 2; }
|
---|
| 181 | for(uint_4 k=0; k<_vecs.size(); k++) {
|
---|
| 182 | if ( (_vecs[k].busy) || (_vecs[k].stat != _nth) ) {
|
---|
| 183 | cout << " Check()/Pb Busy Or Stat for k=" << k << endl;
|
---|
| 184 | nerr++;
|
---|
| 185 | }
|
---|
[3178] | 186 | _vecs[k].vv -= sum;
|
---|
| 187 | _vecs[k].vv.MinMax(min, max);
|
---|
[3177] | 188 | if ((min!=0) || (max!=0)) {
|
---|
| 189 | cout << " Check()/Pb vec[k=" << k << "] != (sum=" << sum << ")" << endl;
|
---|
| 190 | nerr++;
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 | if (nerr == 0) cout << "MTVecPool::Check() - OK (NErr=0)" << endl;
|
---|
| 194 | else cout << "MTVecPool::Check() PB NErr=" << nerr << endl;
|
---|
| 195 | return nerr;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | // ... variables membres
|
---|
| 199 | ZMutex mex;
|
---|
| 200 | uint_4 _vsz;
|
---|
| 201 | uint_4 _nth;
|
---|
| 202 | TMatrix<int_8> _vmx;
|
---|
| 203 | vector< St_VecPool> _vecs;
|
---|
| 204 | };
|
---|
| 205 |
|
---|
| 206 |
|
---|
| 207 | static MTVecPool* mtvp = NULL;
|
---|
| 208 |
|
---|
| 209 | // --- fonction de test avec synchronisation entre threads
|
---|
| 210 | void sync_funzt(void *arg)
|
---|
| 211 | {
|
---|
| 212 | ztarg * za = (ztarg *)arg;
|
---|
| 213 | cout << ">>>> sync_funzt(ThId=" << za->thid << ") - NVec/NLoop= " << za->M << endl;
|
---|
| 214 |
|
---|
| 215 | if (mtvp == NULL)
|
---|
| 216 | throw NullPtrError("sync_funzt: MTVecPool* mtvp = NULL");
|
---|
| 217 |
|
---|
| 218 | int_4 L = za->M;
|
---|
| 219 | int_8 p2 = 1;
|
---|
| 220 | uint_4 k, tid;
|
---|
| 221 | tid = za->thid;
|
---|
| 222 | for(k=0; k<tid; k++) p2 *= 2;
|
---|
| 223 |
|
---|
| 224 | char buff[128];
|
---|
| 225 | sprintf(buff, "sync_funzt(ThId=%d) StarOfLoop", za->thid);
|
---|
| 226 | PrtTim(buff);
|
---|
| 227 | uint_4 idx;
|
---|
| 228 | for(k=0; k<L; k++) {
|
---|
[3178] | 229 | *(mtvp->GetVec(tid, idx)) += p2;
|
---|
[3177] | 230 | //DBG cout << "DBG-sync_funzt(tid=" << tid << ", idx=" << idx << endl;
|
---|
| 231 | mtvp->RetVec(idx);
|
---|
| 232 | }
|
---|
| 233 | sprintf(buff, "sync_funzt(ThId=%d) EndOfLoop", za->thid);
|
---|
| 234 | PrtTim(buff);
|
---|
| 235 | return;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
[1611] | 238 | class CountLock : public ZMutex {
|
---|
| 239 | int count;
|
---|
| 240 | public:
|
---|
| 241 | CountLock() { count = 0; }
|
---|
| 242 | inline int Count() { lock(); int rc = ++count; unlock(); return(rc);
|
---|
| 243 | }
|
---|
| 244 | };
|
---|
| 245 |
|
---|
| 246 |
|
---|
[3080] | 247 | static int N = 1;
|
---|
| 248 | static int M = 5;
|
---|
[3177] | 249 | static int VSZ = 128;
|
---|
[3080] | 250 |
|
---|
[1611] | 251 | int main(int narg, char *arg[])
|
---|
| 252 |
|
---|
| 253 | {
|
---|
| 254 |
|
---|
| 255 | if (narg < 4) {
|
---|
[3177] | 256 | cout << " Usage: zthr select N LM [Sz] " << endl;
|
---|
[3080] | 257 | cout << " select= sl -> simple loop with sleep " << endl;
|
---|
| 258 | cout << " select= mtx -> matrix init and multiply mx1*mx2" << endl;
|
---|
| 259 | cout << " select= arr -> array/matrix init and operation c1*a1+c2*a2 " << endl;
|
---|
| 260 | cout << " select= clk -> Mutex lock count " << endl;
|
---|
[3177] | 261 | cout << " select= sync -> Thread synchronisation using ZMutex" << endl;
|
---|
[3080] | 262 | cout << " N= Number of threads (sl/mtx) or CountLock " << endl;
|
---|
[3177] | 263 | cout << " LM = Loop limit (sl/sync) or Matrix size (mtx) " << endl;
|
---|
| 264 | cout << " Sz = Vector size for select=sync (default=256) " << endl;
|
---|
[3080] | 265 | return(1);
|
---|
[1611] | 266 | }
|
---|
| 267 |
|
---|
[3080] | 268 | string sel = arg[1];
|
---|
[3177] | 269 | if ((sel != "sl") && (sel != "mtx") && (sel != "arr") &&
|
---|
| 270 | (sel != "sync") && (sel != "clk")) {
|
---|
[3080] | 271 | cout << "zthr/erreur argument sel (!= sl / mtx / arr / clk) " << endl;
|
---|
| 272 | return 2;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | //-- Decodage arguments
|
---|
| 276 | N = atoi(arg[2]);
|
---|
| 277 | M = atoi(arg[3]);
|
---|
[3177] | 278 | if (narg > 4) VSZ = atoi(arg[4]);
|
---|
| 279 | cout << "zthr/Info: select=" << sel << " N=" << N << " M= " << M
|
---|
| 280 | << " VSz=" << VSZ << endl;
|
---|
[3080] | 281 |
|
---|
| 282 |
|
---|
[1611] | 283 | InitTim();
|
---|
[3080] | 284 | SophyaInit();
|
---|
[1611] | 285 |
|
---|
[3080] | 286 | int rc = 0;
|
---|
| 287 | try {
|
---|
[3177] | 288 | ResourceUsage res(ResourceUsage::RU_All);
|
---|
| 289 | if ((sel == "mtx") || (sel == "arr") || (sel == "sl") || (sel == "sync") ) {
|
---|
| 290 | if (sel == "sync") mtvp = new MTVecPool(N,VSZ,M);
|
---|
[3080] | 291 | vector<ztarg *> vza;
|
---|
| 292 | vector<ZThread *> vzth;
|
---|
| 293 | for(int i=0; i<N; i++) {
|
---|
| 294 | cout << "*****zthr: Creating Thread " << i+1 << " /" << N << endl;
|
---|
| 295 | ZThread * pzt = new ZThread();
|
---|
| 296 | ztarg* zap = new ztarg;
|
---|
| 297 | vzth.push_back(pzt);
|
---|
[3177] | 298 | // ATTENTION : il faut que le thid = 0 ... N-1 (et pas 1)
|
---|
| 299 | zap->thid = i; zap->M = M;
|
---|
[3080] | 300 | vza.push_back(zap);
|
---|
| 301 | if (sel == "mtx") pzt->setAction(mtx_funzt, vza[i]);
|
---|
| 302 | else if (sel == "arr") pzt->setAction(arr_funzt, vza[i]);
|
---|
[3177] | 303 | else if (sel == "sync") pzt->setAction(sync_funzt, vza[i]);
|
---|
[3080] | 304 | else pzt->setAction(funzt, vza[i]);
|
---|
| 305 | }
|
---|
| 306 | cout << "***zthr: Starting threads ... " << endl;
|
---|
| 307 | PrtTim("***zthr/StarThr");
|
---|
| 308 | for(int i=0; i<N; i++) vzth[i]->start();
|
---|
| 309 | sleep(1);
|
---|
| 310 | cout << "***ResourceUsage before thr[i].join()" << endl;
|
---|
| 311 | cout << res;
|
---|
| 312 | cout << "***zthr Joining Threads ..." << endl;
|
---|
| 313 | for(int i=0; i<N; i++) vzth[i]->join();
|
---|
| 314 | cout << "***zthr Threads Z1 ... Z" << N << " Finished OK" << endl;
|
---|
| 315 | cout << res;
|
---|
[3177] | 316 | cout << " Resu: getDataSize() = " << res.getDataSize() << " getStackSize()="
|
---|
| 317 | << res.getStackSize() << endl;
|
---|
[3080] | 318 | for(int i=0; i<N; i++) {
|
---|
| 319 | delete vzth[i];
|
---|
| 320 | delete vza[i];
|
---|
| 321 | }
|
---|
[3177] | 322 | if (mtvp) {
|
---|
| 323 | Timer tm("MTVecPool::Check()");
|
---|
| 324 | mtvp->Check();
|
---|
| 325 | tm.Nop();
|
---|
| 326 | delete mtvp;
|
---|
| 327 | }
|
---|
[3080] | 328 | }
|
---|
| 329 | else {
|
---|
| 330 | PrtTim("BeginOfCount");
|
---|
| 331 | CountLock clk;
|
---|
| 332 | int kk;
|
---|
| 333 | for(kk=0; kk<atoi(arg[3]); kk++) {
|
---|
| 334 | clk.Count();
|
---|
| 335 | }
|
---|
| 336 | cout << " End CountLock-Test Count= " << clk.Count() << endl;
|
---|
| 337 | }
|
---|
[1611] | 338 | }
|
---|
[3080] | 339 | catch (PThrowable exc) {
|
---|
| 340 | cerr << "zthr: catched Exception " << exc.Msg() << endl;
|
---|
| 341 | rc = 77;
|
---|
| 342 | }
|
---|
| 343 | catch (...) {
|
---|
| 344 | cerr << " catched unknown (...) exception (lpk.cc) " << endl;
|
---|
| 345 | rc = 78;
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 | return(rc);
|
---|
| 349 |
|
---|
| 350 | }
|
---|
[1611] | 351 |
|
---|
| 352 |
|
---|
| 353 |
|
---|