source: Sophya/trunk/SophyaProg/Tests/zthr.cc@ 3334

Last change on this file since 3334 was 3264, checked in by ansari, 18 years ago

amelioration / optimisation des operations arrdl/arrmf dans zthr.cc , Reza 11/06/2007

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